Understanding kmap/kunmap

Kai Meyer kai at gnukai.com
Thu Dec 1 12:49:26 EST 2011


I want to be able to copy data into a struct bio *, so I use 
bio_for_each_segment to loop through each bvec, like so:

void some_function(struct bio *bio, char *some_data) {
     struct bio_vec *bvec;
     int i;
     unsigned int bio_so_far = 0;
     bio_for_each_segment(bvec, bio, i) {
         char *bio_buffer = __bio_kmap_atomic(bio, i, KM_USER0);
         memcpy(bio_buffer, some_data + bio_so_far, bvec->bv_len);
         __bio_kunmap_atomic(bio, KM_USER0);
         bio_so_far += bvec->bv_len;
     }
}

There's lots more to the function, but this is basically the distilled 
version with out any extra stuff.

What I'm finding is that when the bio has multiple bvecs that share the 
same page, only the first bvec's data actually gets copied back up to 
user-space, the rest is garbage or null (meaning, what was there 
already). For instance, I see a lot of bios from vfat that are 4096 
bytes long but are comprised of 8 bvecs that are 512 bytes long that all 
have an offset to the same page.

I've tried doing just one kmap_atomic on the page by keeping track of 
what the last page I kmap'ed was, but that didn't fix the problem either.

Any documentation or high level explanation of kmap/kunmap or other 
ideas to try are welcome.

-Kai Meyer



More information about the Kernelnewbies mailing list