Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

fs/ecryptfs: Replace kmap() with kmap_local_page()

kmap() has been deprecated in favor of kmap_local_page().

Therefore, replace kmap() with kmap_local_page() in fs/ecryptfs.

There are two main problems with kmap(): (1) It comes with an overhead as
the mapping space is restricted and protected by a global lock for
synchronization and (2) it also requires global TLB invalidation when the
kmap’s pool wraps and it might block when the mapping space is fully
utilized until a slot becomes available.

With kmap_local_page() the mappings are per thread, CPU local, can take
page faults, and can be called from any context (including interrupts).
It is faster than kmap() in kernels with HIGHMEM enabled. The tasks can
be preempted and, when they are scheduled to run again, the kernel
virtual addresses are restored and still valid.

Obviously, thread locality implies that the kernel virtual addresses
returned by kmap_local_page() are only valid in the context of the
callers (i.e., they cannot be handed to other threads).

The use of kmap_local_page() in fs/ecryptfs does not break the
above-mentioned assumption, so it is allowed and preferred.

Tested in a QEMU/KVM x86_32 VM, 6GB RAM, booting a kernel with
HIGHMEM64GB enabled.

Suggested-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Ira Weiny <ira.weiny@intel.com>
Signed-off-by: "Fabio M. De Francesco" <fmdefrancesco@gmail.com>
Message-Id: <20230426172223.8896-2-fmdefrancesco@gmail.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>

authored by

Fabio M. De Francesco and committed by
Christian Brauner
8b70deb8 06c2afb8

+8 -8
+4 -4
fs/ecryptfs/crypto.c
··· 441 441 } 442 442 443 443 lower_offset = lower_offset_for_page(crypt_stat, page); 444 - enc_extent_virt = kmap(enc_extent_page); 444 + enc_extent_virt = kmap_local_page(enc_extent_page); 445 445 rc = ecryptfs_write_lower(ecryptfs_inode, enc_extent_virt, lower_offset, 446 446 PAGE_SIZE); 447 - kunmap(enc_extent_page); 447 + kunmap_local(enc_extent_virt); 448 448 if (rc < 0) { 449 449 ecryptfs_printk(KERN_ERR, 450 450 "Error attempting to write lower page; rc = [%d]\n", ··· 490 490 BUG_ON(!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)); 491 491 492 492 lower_offset = lower_offset_for_page(crypt_stat, page); 493 - page_virt = kmap(page); 493 + page_virt = kmap_local_page(page); 494 494 rc = ecryptfs_read_lower(page_virt, lower_offset, PAGE_SIZE, 495 495 ecryptfs_inode); 496 - kunmap(page); 496 + kunmap_local(page_virt); 497 497 if (rc < 0) { 498 498 ecryptfs_printk(KERN_ERR, 499 499 "Error attempting to read lower page; rc = [%d]\n",
+4 -4
fs/ecryptfs/read_write.c
··· 64 64 65 65 offset = ((((loff_t)page_for_lower->index) << PAGE_SHIFT) 66 66 + offset_in_page); 67 - virt = kmap(page_for_lower); 67 + virt = kmap_local_page(page_for_lower); 68 68 rc = ecryptfs_write_lower(ecryptfs_inode, virt, offset, size); 69 69 if (rc > 0) 70 70 rc = 0; 71 - kunmap(page_for_lower); 71 + kunmap_local(virt); 72 72 return rc; 73 73 } 74 74 ··· 253 253 int rc; 254 254 255 255 offset = ((((loff_t)page_index) << PAGE_SHIFT) + offset_in_page); 256 - virt = kmap(page_for_ecryptfs); 256 + virt = kmap_local_page(page_for_ecryptfs); 257 257 rc = ecryptfs_read_lower(virt, offset, size, ecryptfs_inode); 258 258 if (rc > 0) 259 259 rc = 0; 260 - kunmap(page_for_ecryptfs); 260 + kunmap_local(virt); 261 261 flush_dcache_page(page_for_ecryptfs); 262 262 return rc; 263 263 }