eCryptfs: use page_alloc not kmalloc to get a page of memory

With SLUB debugging turned on in 2.6.26, I was getting memory corruption
when testing eCryptfs. The root cause turned out to be that eCryptfs was
doing kmalloc(PAGE_CACHE_SIZE); virt_to_page() and treating that as a nice
page-aligned chunk of memory. But at least with SLUB debugging on, this
is not always true, and the page we get from virt_to_page does not
necessarily match the PAGE_CACHE_SIZE worth of memory we got from kmalloc.

My simple testcase was 2 loops doing "rm -f fileX; cp /tmp/fileX ." for 2
different multi-megabyte files. With this change I no longer see the
corruption.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Acked-by: Michael Halcrow <mhalcrow@us.ibm.com>
Acked-by: Rik van Riel <riel@redhat.com>
Cc: <stable@kernel.org> [2.6.25.x, 2.6.26.x]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Eric Sandeen and committed by Linus Torvalds 7fcba054 25947d5a

+18 -12
+18 -12
fs/ecryptfs/crypto.c
··· 475 475 { 476 476 struct inode *ecryptfs_inode; 477 477 struct ecryptfs_crypt_stat *crypt_stat; 478 - char *enc_extent_virt = NULL; 479 - struct page *enc_extent_page; 478 + char *enc_extent_virt; 479 + struct page *enc_extent_page = NULL; 480 480 loff_t extent_offset; 481 481 int rc = 0; 482 482 ··· 492 492 page->index); 493 493 goto out; 494 494 } 495 - enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER); 496 - if (!enc_extent_virt) { 495 + enc_extent_page = alloc_page(GFP_USER); 496 + if (!enc_extent_page) { 497 497 rc = -ENOMEM; 498 498 ecryptfs_printk(KERN_ERR, "Error allocating memory for " 499 499 "encrypted extent\n"); 500 500 goto out; 501 501 } 502 - enc_extent_page = virt_to_page(enc_extent_virt); 502 + enc_extent_virt = kmap(enc_extent_page); 503 503 for (extent_offset = 0; 504 504 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); 505 505 extent_offset++) { ··· 527 527 } 528 528 } 529 529 out: 530 - kfree(enc_extent_virt); 530 + if (enc_extent_page) { 531 + kunmap(enc_extent_page); 532 + __free_page(enc_extent_page); 533 + } 531 534 return rc; 532 535 } 533 536 ··· 612 609 { 613 610 struct inode *ecryptfs_inode; 614 611 struct ecryptfs_crypt_stat *crypt_stat; 615 - char *enc_extent_virt = NULL; 616 - struct page *enc_extent_page; 612 + char *enc_extent_virt; 613 + struct page *enc_extent_page = NULL; 617 614 unsigned long extent_offset; 618 615 int rc = 0; 619 616 ··· 630 627 page->index); 631 628 goto out; 632 629 } 633 - enc_extent_virt = kmalloc(PAGE_CACHE_SIZE, GFP_USER); 634 - if (!enc_extent_virt) { 630 + enc_extent_page = alloc_page(GFP_USER); 631 + if (!enc_extent_page) { 635 632 rc = -ENOMEM; 636 633 ecryptfs_printk(KERN_ERR, "Error allocating memory for " 637 634 "encrypted extent\n"); 638 635 goto out; 639 636 } 640 - enc_extent_page = virt_to_page(enc_extent_virt); 637 + enc_extent_virt = kmap(enc_extent_page); 641 638 for (extent_offset = 0; 642 639 extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); 643 640 extent_offset++) { ··· 665 662 } 666 663 } 667 664 out: 668 - kfree(enc_extent_virt); 665 + if (enc_extent_page) { 666 + kunmap(enc_extent_page); 667 + __free_page(enc_extent_page); 668 + } 669 669 return rc; 670 670 } 671 671