Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory comparison causes system halt
    primarykey
    data
    text
    <p>I am working on a kernel module and I need to compare two buffers to find out if they are equivalent. I am using the memcmp function defined in the Linux kernel to do so. My first buffer is like this:</p> <pre><code>cache_buffer = (unsigned char *)vmalloc(4097); cache_buffer[4096] = '/0'; </code></pre> <p>The second buffer is from a page using the page_address() function. </p> <pre><code>page = bio_page(bio); kmap(page); write_buffer = (char *)page_address(page); kunmap(page); </code></pre> <p>I have printed the contents of both buffers before hand and not only to they print correctly, but they also have the same content. So next, I do this:</p> <pre><code>result = memcmp(write_buffer, cache_buffer, 2048); // only comparing up to 2048 positions </code></pre> <p>This causes the kernel to freeze up and I cannot figure out why. I checked the implementation of memcmp and saw nothing that would cause the freeze. Can anyone suggest a cause?</p> <p>Here is the memcmp implementation:</p> <pre><code>int memcmp(const void *cs, const void *ct, size_t count) { const unsigned char *su1, *su2; int res = 0; for (su1 = cs, su2 = ct; 0 &lt; count; ++su1, ++su2, count--) if ((res = *su1 - *su2) != 0) break; return res; } </code></pre> <p>EDIT: The function causing the freeze is memcmp. When I commented it out, everything worked. Also, when I did I memcmp as follows</p> <pre><code>memcmp(write_buffer, write_buffer, 2048); //comparing two write_buffers </code></pre> <p>Everything worked as well. Only when I throw the cache_buffer into the mix is when I get the error. Also, above is a simplification of my actual code. Here is the entire function:</p> <pre><code>static int compare_data(sector_t location, struct bio * bio, struct cache_c * dmc) { struct dm_io_region where; unsigned long bits; int segno; struct bio_vec * bvec; struct page * page; unsigned char * cache_data; char * temp_data; char * write_data; int result, length, i; cache_data = (unsigned char *)vmalloc((dmc-&gt;block_size * 512) + 1); where.bdev = dmc-&gt;cache_dev-&gt;bdev; where.count = dmc-&gt;block_size; where.sector = location &lt;&lt; dmc-&gt;block_shift; printk(KERN_DEBUG "place: %llu\n", where.sector); dm_io_sync_vm(1, &amp;where, READ, cache_data, &amp;bits, dmc); length = 0; bio_for_each_segment(bvec, bio, segno) { if(segno == 0) { page = bio_page(bio); kmap(page); write_data = (char *)page_address(page); //kunmap(page); length += bvec-&gt;bv_len; } else { page = bio_page(bio); kmap(page); temp_data = strcat(write_data, (char *)page_address(page)); //kunmap(page); write_data = temp_data; length += bvec-&gt;bv_len; } } printk(KERN_INFO "length: %u\n", length); cache_data[dmc-&gt;block_size * 512] = '\0'; for(i = 0; i &lt; 2048; i++) { printk("%c", write_data[i]); } printk("\n"); for(i = 0; i &lt; 2048; i++) { printk("%c", cache_data[i]); } printk("\n"); result = memcmp(write_data, cache_data, length); return result; } </code></pre> <p>EDIT #2: Sorry guys. The problem was not memcmp. It was the result of memcmp. When ever it returned a positive or negative number, the function that called my function would play with some pointers, one of which was uninitialized. I don't know why I didn't realize it before. Thanks for trying to help though!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload