]> www.infradead.org Git - users/hch/misc.git/commitdiff
/dev/zero: try to align PMD_SIZE for private mapping
authorZhang Qilong <zhangqilong3@huawei.com>
Thu, 31 Jul 2025 12:23:05 +0000 (20:23 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 13 Sep 2025 23:54:42 +0000 (16:54 -0700)
Attempt to map aligned to huge page size for private mapping which could
achieve performance gains, the mprot_tw4m in libMicro average execution
time on arm64:

  - Test case:        mprot_tw4m
  - Before the patch:   22 us
  - After the patch:    17 us

If THP config is not set, we fall back to system page size mappings.

Link: https://lkml.kernel.org/r/20250731122305.2669090-1-zhangqilong3@huawei.com
Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Tested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Acked-by: David Hildenbrand <david@redhat.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Nanyong Sun <sunnanyong@huawei.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
drivers/char/mem.c

index 48839958b0b15e99482c8b7eef703c1f8530bdd1..34b815901b205be4bf3b740bec669fcf85d889b4 100644 (file)
@@ -512,11 +512,18 @@ static int mmap_zero(struct file *file, struct vm_area_struct *vma)
        return 0;
 }
 
+#ifndef CONFIG_MMU
+static unsigned long get_unmapped_area_zero(struct file *file,
+                               unsigned long addr, unsigned long len,
+                               unsigned long pgoff, unsigned long flags)
+{
+       return -ENOSYS;
+}
+#else
 static unsigned long get_unmapped_area_zero(struct file *file,
                                unsigned long addr, unsigned long len,
                                unsigned long pgoff, unsigned long flags)
 {
-#ifdef CONFIG_MMU
        if (flags & MAP_SHARED) {
                /*
                 * mmap_zero() will call shmem_zero_setup() to create a file,
@@ -527,12 +534,18 @@ static unsigned long get_unmapped_area_zero(struct file *file,
                return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
        }
 
-       /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
-       return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags);
+       /*
+        * Otherwise flags & MAP_PRIVATE: with no shmem object beneath it,
+        * attempt to map aligned to huge page size if possible, otherwise we
+        * fall back to system page size mappings.
+        */
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+       return thp_get_unmapped_area(file, addr, len, pgoff, flags);
 #else
-       return -ENOSYS;
+       return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags);
 #endif
 }
+#endif /* CONFIG_MMU */
 
 static ssize_t write_full(struct file *file, const char __user *buf,
                          size_t count, loff_t *ppos)