linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] btrfs: fix alginment of VMA for memory mapped files on THP
@ 2022-08-02 20:32 alexlzhu
  2022-08-03 13:40 ` David Sterba
  0 siblings, 1 reply; 2+ messages in thread
From: alexlzhu @ 2022-08-02 20:32 UTC (permalink / raw)
  To: kernel-team, linux-mm, clm, josef, dsterba, linux-btrfs, linux-kernel
  Cc: Alexander Zhu

From: Alexander Zhu <alexlzhu@fb.com>

With CONFIG_READ_ONLY_THP_FOR_FS, the Linux kernel supports using THPs for
read-only mmapped files, such as shared libraries. However, the kernel
makes no attempt to actually align those mappings on 2MB boundaries,
which makes it impossible to use those THPs most of the time. This issue
applies to general file mapping THP as well as existing setups using
CONFIG_READ_ONLY_THP_FOR_FS. This is easily fixed by using
thp_get_unmapped_area for the unmapped_area function in btrfs, which
is what ext2, ext4, fuse, and xfs all use.

Initially btrfs had been left out in Commit 8c07fc452ac0 ("btrfs: fix
alginment of VMA for memory mapped files on THP") as btrfs does not support
DAX. However, Commit 1854bc6e2420 ("mm/readahead: Align file mappings
for non-DAX") removed the DAX requirement. We should now be able to call
thp_get_unmapped_area() for btrfs.

The problem can be seen in /proc/PID/smaps where THPeligible is set to 0
on mappings to eligible shared object files as shown below.

Before this patch:

7fc6a7e18000-7fc6a80cc000 r-xp 00000000 00:1e 199856
/usr/lib64/libcrypto.so.1.1.1k
Size:               2768 kB
THPeligible:    0
VmFlags: rd ex mr mw me

With this patch the library is mapped at a 2MB aligned address:

fbdfe200000-7fbdfe4b4000 r-xp 00000000 00:1e 199856
/usr/lib64/libcrypto.so.1.1.1k
Size:               2768 kB
THPeligible:    1
VmFlags: rd ex mr mw me

This fixes the alignment of VMAs for any mmap of a file that has the
rd and ex permissions and size >= 2MB. The VMA alignment and
THPeligible field for anonymous memory is handled separately and
is thus not effected by this change.

Signed-off-by: Alexander Zhu <alexlzhu@fb.com>

Changes in v3:
-Fixed subject to be btrfs instead of mm
-Fixed Signed-off-by to use full name
-Added to description explaining why thp_get_unmapped_area can be
used for btrfs

Changes in v2:
-Changed from x86_64 diff to btrfs diff using thp_get_unmapped_area
---
 fs/btrfs/file.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 9dfde1af8a64..2423040db167 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -3808,6 +3808,7 @@ const struct file_operations btrfs_file_operations = {
 	.mmap		= btrfs_file_mmap,
 	.open		= btrfs_file_open,
 	.release	= btrfs_release_file,
+	.get_unmapped_area = thp_get_unmapped_area,
 	.fsync		= btrfs_sync_file,
 	.fallocate	= btrfs_fallocate,
 	.unlocked_ioctl	= btrfs_ioctl,
-- 
2.30.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] btrfs: fix alginment of VMA for memory mapped files on THP
  2022-08-02 20:32 [PATCH v3] btrfs: fix alginment of VMA for memory mapped files on THP alexlzhu
@ 2022-08-03 13:40 ` David Sterba
  0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2022-08-03 13:40 UTC (permalink / raw)
  To: alexlzhu
  Cc: kernel-team, linux-mm, clm, josef, dsterba, linux-btrfs, linux-kernel

On Tue, Aug 02, 2022 at 01:32:46PM -0700, alexlzhu@fb.com wrote:
> From: Alexander Zhu <alexlzhu@fb.com>
> 
> With CONFIG_READ_ONLY_THP_FOR_FS, the Linux kernel supports using THPs for
> read-only mmapped files, such as shared libraries. However, the kernel
> makes no attempt to actually align those mappings on 2MB boundaries,
> which makes it impossible to use those THPs most of the time. This issue
> applies to general file mapping THP as well as existing setups using
> CONFIG_READ_ONLY_THP_FOR_FS. This is easily fixed by using
> thp_get_unmapped_area for the unmapped_area function in btrfs, which
> is what ext2, ext4, fuse, and xfs all use.
> 
> Initially btrfs had been left out in Commit 8c07fc452ac0 ("btrfs: fix
> alginment of VMA for memory mapped files on THP") as btrfs does not support
> DAX. However, Commit 1854bc6e2420 ("mm/readahead: Align file mappings
> for non-DAX") removed the DAX requirement. We should now be able to call
> thp_get_unmapped_area() for btrfs.
> 
> The problem can be seen in /proc/PID/smaps where THPeligible is set to 0
> on mappings to eligible shared object files as shown below.
> 
> Before this patch:
> 
> 7fc6a7e18000-7fc6a80cc000 r-xp 00000000 00:1e 199856
> /usr/lib64/libcrypto.so.1.1.1k
> Size:               2768 kB
> THPeligible:    0
> VmFlags: rd ex mr mw me
> 
> With this patch the library is mapped at a 2MB aligned address:
> 
> fbdfe200000-7fbdfe4b4000 r-xp 00000000 00:1e 199856
> /usr/lib64/libcrypto.so.1.1.1k
> Size:               2768 kB
> THPeligible:    1
> VmFlags: rd ex mr mw me
> 
> This fixes the alignment of VMAs for any mmap of a file that has the
> rd and ex permissions and size >= 2MB. The VMA alignment and
> THPeligible field for anonymous memory is handled separately and
> is thus not effected by this change.
> 
> Signed-off-by: Alexander Zhu <alexlzhu@fb.com>

Added to misc-next, thanks.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-08-03 13:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-02 20:32 [PATCH v3] btrfs: fix alginment of VMA for memory mapped files on THP alexlzhu
2022-08-03 13:40 ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).