* [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping
@ 2025-07-30 9:19 Zhang Qilong
2025-07-30 9:33 ` David Hildenbrand
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Zhang Qilong @ 2025-07-30 9:19 UTC (permalink / raw)
To: arnd, gregkh
Cc: linux-kernel, linux-mm, lorenzo.stoakes, david, wangkefeng.wang,
sunnanyong, Zhang Qilong
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
Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>
---
v2:
- Add comments on code suggested by Lorenzo
- Use IS_ENABLED to check THP config
drivers/char/mem.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 48839958b0b1..c27cc89bd02d 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -525,11 +525,18 @@ static unsigned long get_unmapped_area_zero(struct file *file,
* so as not to confuse shmem with our handle on "/dev/zero".
*/
return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
}
- /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
+ /*
+ * 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.
+ */
+ if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
+ return thp_get_unmapped_area(file, addr, len, pgoff, flags);
+
return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags);
#else
return -ENOSYS;
#endif
}
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping
2025-07-30 9:19 [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping Zhang Qilong
@ 2025-07-30 9:33 ` David Hildenbrand
2025-07-30 12:49 ` Matthew Wilcox
2025-07-30 13:04 ` Lorenzo Stoakes
2 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand @ 2025-07-30 9:33 UTC (permalink / raw)
To: Zhang Qilong, arnd, gregkh
Cc: linux-kernel, linux-mm, lorenzo.stoakes, wangkefeng.wang,
sunnanyong
On 30.07.25 11:19, Zhang Qilong wrote:
> 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
>
> Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>
> ---
> v2:
> - Add comments on code suggested by Lorenzo
> - Use IS_ENABLED to check THP config
>
> drivers/char/mem.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/char/mem.c b/drivers/char/mem.c
> index 48839958b0b1..c27cc89bd02d 100644
> --- a/drivers/char/mem.c
> +++ b/drivers/char/mem.c
> @@ -525,11 +525,18 @@ static unsigned long get_unmapped_area_zero(struct file *file,
> * so as not to confuse shmem with our handle on "/dev/zero".
> */
> return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
> }
>
> - /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
> + /*
> + * 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.
> + */
> + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
> + return thp_get_unmapped_area(file, addr, len, pgoff, flags);
> +
> return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags);
> #else
> return -ENOSYS;
> #endif
> }
Acked-by: David Hildenbrand <david@redhat.com>
--
Cheers,
David / dhildenb
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping
2025-07-30 9:19 [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping Zhang Qilong
2025-07-30 9:33 ` David Hildenbrand
@ 2025-07-30 12:49 ` Matthew Wilcox
2025-07-30 12:59 ` Lorenzo Stoakes
2025-07-30 13:04 ` Lorenzo Stoakes
2 siblings, 1 reply; 5+ messages in thread
From: Matthew Wilcox @ 2025-07-30 12:49 UTC (permalink / raw)
To: Zhang Qilong
Cc: arnd, gregkh, linux-kernel, linux-mm, lorenzo.stoakes, david,
wangkefeng.wang, sunnanyong
On Wed, Jul 30, 2025 at 05:19:05PM +0800, Zhang Qilong wrote:
> + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
> + return thp_get_unmapped_area(file, addr, len, pgoff, flags);
> +
> return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags);
I'm sure the build bots will give us a compiler error.
If CONFIG_TRANSPARENT_HUGEPAGE is not enabled, we get:
include/linux/huge_mm.h:#define thp_get_unmapped_area NULL
and we chose that so that various filesystems can unconditionally set
their .get_unmapped_area method to it.
Which means the cpp will turn this into:
if (0)
return NULL(file, addr, len, pgoff, flags);
and the compiler will say:
error: implicit declaration of function ‘NULL’ [-Wimplicit-function-declaration]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping
2025-07-30 12:49 ` Matthew Wilcox
@ 2025-07-30 12:59 ` Lorenzo Stoakes
0 siblings, 0 replies; 5+ messages in thread
From: Lorenzo Stoakes @ 2025-07-30 12:59 UTC (permalink / raw)
To: Matthew Wilcox
Cc: Zhang Qilong, arnd, gregkh, linux-kernel, linux-mm, david,
wangkefeng.wang, sunnanyong
On Wed, Jul 30, 2025 at 01:49:34PM +0100, Matthew Wilcox wrote:
> On Wed, Jul 30, 2025 at 05:19:05PM +0800, Zhang Qilong wrote:
> > + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
> > + return thp_get_unmapped_area(file, addr, len, pgoff, flags);
> > +
> > return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags);
>
> I'm sure the build bots will give us a compiler error.
> If CONFIG_TRANSPARENT_HUGEPAGE is not enabled, we get:
> include/linux/huge_mm.h:#define thp_get_unmapped_area NULL
>
> and we chose that so that various filesystems can unconditionally set
> their .get_unmapped_area method to it.
>
> Which means the cpp will turn this into:
>
> if (0)
> return NULL(file, addr, len, pgoff, flags);
>
> and the compiler will say:
>
> error: implicit declaration of function ‘NULL’ [-Wimplicit-function-declaration]
>
Yeah I did ask explicitly for #ifdef here :)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping
2025-07-30 9:19 [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping Zhang Qilong
2025-07-30 9:33 ` David Hildenbrand
2025-07-30 12:49 ` Matthew Wilcox
@ 2025-07-30 13:04 ` Lorenzo Stoakes
2 siblings, 0 replies; 5+ messages in thread
From: Lorenzo Stoakes @ 2025-07-30 13:04 UTC (permalink / raw)
To: Zhang Qilong
Cc: arnd, gregkh, linux-kernel, linux-mm, david, wangkefeng.wang,
sunnanyong
On Wed, Jul 30, 2025 at 05:19:05PM +0800, Zhang Qilong wrote:
> 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
>
> Signed-off-by: Zhang Qilong <zhangqilong3@huawei.com>
> ---
> v2:
> - Add comments on code suggested by Lorenzo
> - Use IS_ENABLED to check THP config
>
> drivers/char/mem.c | 9 ++++++++-
> 1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/char/mem.c b/drivers/char/mem.c
> index 48839958b0b1..c27cc89bd02d 100644
> --- a/drivers/char/mem.c
> +++ b/drivers/char/mem.c
> @@ -525,11 +525,18 @@ static unsigned long get_unmapped_area_zero(struct file *file,
> * so as not to confuse shmem with our handle on "/dev/zero".
> */
> return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
> }
>
> - /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
> + /*
> + * 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.
> + */
Nit, but put a space after this as it aplies to both blocks.
> + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
> + return thp_get_unmapped_area(file, addr, len, pgoff, flags);
As mentioned by Matthew, and as requested originally, please make this an #ifdef
/ #else.
You might then want to factor out the #ifdef CONFIG_MMU (gross) at a higher
level.
I'm sick of us bending over backwards for museum piece (or should be) nommu but
anyway.
Something like
#ifdef 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
...
It's ugly but unfortunately I think necessary.
> +
> return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags);
> #else
> return -ENOSYS;
> #endif
> }
> --
> 2.43.0
>
Thanks, Lorenzo
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-07-30 13:05 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-30 9:19 [PATCH resend v2] /dev/zero: try to align PMD_SIZE for private mapping Zhang Qilong
2025-07-30 9:33 ` David Hildenbrand
2025-07-30 12:49 ` Matthew Wilcox
2025-07-30 12:59 ` Lorenzo Stoakes
2025-07-30 13:04 ` Lorenzo Stoakes
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).