[RFC] linux: add definitions for hugetlb page size encodings

Message ID 20240614175946.1369390-1-cmllamas@google.com
State Changes Requested
Headers
Series [RFC] linux: add definitions for hugetlb page size encodings |

Checks

Context Check Description
redhat-pt-bot/TryBot-32bit success Build for i686
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed

Commit Message

Carlos Llamas June 14, 2024, 5:59 p.m. UTC
  A desired hugetlb page size can be encoded in the flags parameter of
system calls such as mmap() and shmget(). The Linux UAPI headers have
included explicit definitions for these encodings since v4.14.

This patch adds these definitions that are used along with MAP_HUGETLB
and SHM_HUGETLB flags as specified in the corresponding man pages. This
relieves programs from having to duplicate and/or compute the encodings
manually.

Signed-off-by: Carlos Llamas <cmllamas@google.com>
---
 sysdeps/unix/sysv/linux/bits/mman-linux.h | 21 ++++++++++++++++++++-
 sysdeps/unix/sysv/linux/bits/shm.h        | 22 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)
  

Comments

Florian Weimer June 17, 2024, 7:06 a.m. UTC | #1
* Carlos Llamas:

> A desired hugetlb page size can be encoded in the flags parameter of
> system calls such as mmap() and shmget(). The Linux UAPI headers have
> included explicit definitions for these encodings since v4.14.
>
> This patch adds these definitions that are used along with MAP_HUGETLB
> and SHM_HUGETLB flags as specified in the corresponding man pages. This
> relieves programs from having to duplicate and/or compute the encodings
> manually.

I think doing this is reasonable.  Please remove the MAP_UGE_* filter in
sysdeps/unix/sysv/linux/tst-mman-consts.py, too.
> +/* When MAP_HUGETLB is set, bits [26:31] encode the log2 of the huge page size.
> +   The following definitions are associated with this huge page size encoding.
> +   It is responsibility of the application to know which sizes are supported on
> +   the running system.  See mmap(2) man page for details. */

GNU style requires and extra space here: "details.  */”

Also applies to <bits/shm.h>.

Thanks,
Florian
  
Carlos Llamas June 18, 2024, 1:04 a.m. UTC | #2
On Mon, Jun 17, 2024 at 09:06:40AM +0200, Florian Weimer wrote:
> * Carlos Llamas:
> 
> > A desired hugetlb page size can be encoded in the flags parameter of
> > system calls such as mmap() and shmget(). The Linux UAPI headers have
> > included explicit definitions for these encodings since v4.14.
> >
> > This patch adds these definitions that are used along with MAP_HUGETLB
> > and SHM_HUGETLB flags as specified in the corresponding man pages. This
> > relieves programs from having to duplicate and/or compute the encodings
> > manually.
> 
> I think doing this is reasonable.  Please remove the MAP_UGE_* filter in
> sysdeps/unix/sysv/linux/tst-mman-consts.py, too.

Added to v2.

> > +/* When MAP_HUGETLB is set, bits [26:31] encode the log2 of the huge page size.
> > +   The following definitions are associated with this huge page size encoding.
> > +   It is responsibility of the application to know which sizes are supported on
> > +   the running system.  See mmap(2) man page for details. */
> 
> GNU style requires and extra space here: "details.  */”
> 
> Also applies to <bits/shm.h>.

Also added to v2.

Thanks,
Carlos Llamas
  

Patch

diff --git a/sysdeps/unix/sysv/linux/bits/mman-linux.h b/sysdeps/unix/sysv/linux/bits/mman-linux.h
index 5c3d43b0f2..cb99f609d0 100644
--- a/sysdeps/unix/sysv/linux/bits/mman-linux.h
+++ b/sysdeps/unix/sysv/linux/bits/mman-linux.h
@@ -54,10 +54,29 @@ 
 # define MAP_ANONYMOUS	0x20		/* Don't use a file.  */
 #endif
 #define MAP_ANON	MAP_ANONYMOUS
-/* When MAP_HUGETLB is set bits [26:31] encode the log2 of the huge page size.  */
+
+/* When MAP_HUGETLB is set, bits [26:31] encode the log2 of the huge page size.
+   The following definitions are associated with this huge page size encoding.
+   It is responsibility of the application to know which sizes are supported on
+   the running system.  See mmap(2) man page for details. */
+
 #define MAP_HUGE_SHIFT	26
 #define MAP_HUGE_MASK	0x3f
 
+#define MAP_HUGE_16KB	(14 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_64KB	(16 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_512KB	(19 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_1MB	(20 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_2MB	(21 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_8MB	(23 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_16MB	(24 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_32MB	(25 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_256MB	(28 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_512MB	(29 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_1GB	(30 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_2GB	(31 << MAP_HUGE_SHIFT)
+#define MAP_HUGE_16GB	(34U << MAP_HUGE_SHIFT)
+
 /* Flags to `msync'.  */
 #define MS_ASYNC	1		/* Sync memory asynchronously.  */
 #define MS_SYNC		4		/* Synchronous memory sync.  */
diff --git a/sysdeps/unix/sysv/linux/bits/shm.h b/sysdeps/unix/sysv/linux/bits/shm.h
index 95f7863913..16f3285467 100644
--- a/sysdeps/unix/sysv/linux/bits/shm.h
+++ b/sysdeps/unix/sysv/linux/bits/shm.h
@@ -58,6 +58,28 @@  typedef __syscall_ulong_t shmatt_t;
 # define SHM_HUGETLB	04000	/* segment is mapped via hugetlb */
 # define SHM_NORESERVE	010000	/* don't check for reservations */
 
+/* When SHM_HUGETLB is set, bits [26:31] encode the log2 of the huge page size.
+   The following definitions are associated with this huge page size encoding.
+   It is responsibility of the application to know which sizes are supported on
+   the running system.  See shmget(2) man page for details. */
+
+#define SHM_HUGE_SHIFT	26
+#define SHM_HUGE_MASK	0x3f
+
+#define SHM_HUGE_16KB	(14 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_64KB	(16 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_512KB	(19 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_1MB	(20 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_2MB	(21 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_8MB	(23 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_16MB	(24 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_32MB	(25 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_256MB	(28 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_512MB	(29 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_1GB	(30 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_2GB	(31 << SHM_HUGE_SHIFT)
+#define SHM_HUGE_16GB	(34U << SHM_HUGE_SHIFT)
+
 struct	shminfo
   {
     __syscall_ulong_t shmmax;