public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/4] hurd: Implement MAP_32BIT
@ 2023-04-23 21:55 Sergey Bugaev
  2023-04-23 21:55 ` [PATCH 2/4] hurd: Don't attempt to deallocate MACH_PORT_DEAD Sergey Bugaev
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Sergey Bugaev @ 2023-04-23 21:55 UTC (permalink / raw)
  To: libc-alpha; +Cc: bug-hurd, Samuel Thibault

This is a flag that can be passed to mmap () to request that the mapping
being established should be located in the lower 2 GB area of the
address space, so only the lower 31 (not 32) bits can be set in its
address, and the address can be represented as a 32-bit integer without
truncating it.

This flag is intended to be compatible with Linux, FreeBSD, and Darwin
flags of the same name. Out of those systems, it appears Linux and
FreeBSD take MAP_32BIT to mean "map 31 bit", whereas Darwin allows the
32nd bit to be set in the address as well. The Hurd follows Linux and
FreeBSD behavior.

Unlike on those systems, on the Hurd MAP_32BIT is defined on all
supported architectures (which currently are only i386 and x86_64).

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 sysdeps/mach/hurd/bits/mman_ext.h |  1 +
 sysdeps/mach/hurd/dl-sysdep.c     |  8 +++++---
 sysdeps/mach/hurd/mmap.c          | 10 ++++++----
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/sysdeps/mach/hurd/bits/mman_ext.h b/sysdeps/mach/hurd/bits/mman_ext.h
index f022826e..bbb94743 100644
--- a/sysdeps/mach/hurd/bits/mman_ext.h
+++ b/sysdeps/mach/hurd/bits/mman_ext.h
@@ -22,4 +22,5 @@
 
 #ifdef __USE_GNU
 # define SHM_ANON	((const char *) 1)
+# define MAP_32BIT	0x1000
 #endif /* __USE_GNU  */
diff --git a/sysdeps/mach/hurd/dl-sysdep.c b/sysdeps/mach/hurd/dl-sysdep.c
index 6e167e12..d7b309e0 100644
--- a/sysdeps/mach/hurd/dl-sysdep.c
+++ b/sysdeps/mach/hurd/dl-sysdep.c
@@ -451,7 +451,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
 {
   error_t err;
   vm_prot_t vmprot;
-  vm_address_t mapaddr;
+  vm_address_t mapaddr, mask;
   mach_port_t memobj_rd, memobj_wr;
 
   vmprot = VM_PROT_NONE;
@@ -462,6 +462,8 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
   if (prot & PROT_EXEC)
     vmprot |= VM_PROT_EXECUTE;
 
+  mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0;
+
   if (flags & MAP_ANON)
     memobj_rd = MACH_PORT_NULL;
   else
@@ -476,7 +478,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
 
   mapaddr = (vm_address_t) addr;
   err = __vm_map (__mach_task_self (),
-		  &mapaddr, (vm_size_t) len, 0,
+		  &mapaddr, (vm_size_t) len, mask,
 		  !(flags & MAP_FIXED),
 		  memobj_rd,
 		  (vm_offset_t) offset,
@@ -491,7 +493,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
       if (! err)
 	err = __vm_map (__mach_task_self (),
 			&mapaddr, (vm_size_t) len,
-			0,
+			mask,
 			!(flags & MAP_FIXED),
 			memobj_rd, (vm_offset_t) offset,
 			flags & (MAP_COPY|MAP_PRIVATE),
diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c
index 20a41e36..c3cc1856 100644
--- a/sysdeps/mach/hurd/mmap.c
+++ b/sysdeps/mach/hurd/mmap.c
@@ -36,7 +36,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
   error_t err;
   vm_prot_t vmprot, max_vmprot;
   memory_object_t memobj;
-  vm_address_t mapaddr;
+  vm_address_t mapaddr, mask;
   boolean_t copy;
 
   mapaddr = (vm_address_t) addr;
@@ -55,6 +55,8 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
 
   copy = ! (flags & MAP_SHARED);
 
+  mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0;
+
   switch (flags & MAP_TYPE)
     {
     default:
@@ -134,7 +136,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
     max_vmprot = VM_PROT_ALL;
 
   err = __vm_map (__mach_task_self (),
-		  &mapaddr, (vm_size_t) len, (vm_address_t) 0,
+		  &mapaddr, (vm_size_t) len, mask,
 		  mapaddr == 0,
 		  memobj, (vm_offset_t) offset,
 		  copy, vmprot, max_vmprot,
@@ -149,7 +151,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
 	  err = __vm_deallocate (__mach_task_self (), mapaddr, len);
 	  if (! err)
 	    err = __vm_map (__mach_task_self (),
-			    &mapaddr, (vm_size_t) len, (vm_address_t) 0,
+			    &mapaddr, (vm_size_t) len, mask,
 			    0, memobj, (vm_offset_t) offset,
 			    copy, vmprot, max_vmprot,
 			    copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE);
@@ -159,7 +161,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
     {
       if (mapaddr != 0 && (err == KERN_NO_SPACE || err == KERN_INVALID_ADDRESS))
 	err = __vm_map (__mach_task_self (),
-			&mapaddr, (vm_size_t) len, (vm_address_t) 0,
+			&mapaddr, (vm_size_t) len, mask,
 			1, memobj, (vm_offset_t) offset,
 			copy, vmprot, max_vmprot,
 			copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE);
-- 
2.40.0


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

* [PATCH 2/4] hurd: Don't attempt to deallocate MACH_PORT_DEAD
  2023-04-23 21:55 [PATCH 1/4] hurd: Implement MAP_32BIT Sergey Bugaev
@ 2023-04-23 21:55 ` Sergey Bugaev
  2023-04-24 20:44   ` Samuel Thibault
  2023-04-23 21:55 ` [PATCH 3/4] hurd: Microoptimize mmap () Sergey Bugaev
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: Sergey Bugaev @ 2023-04-23 21:55 UTC (permalink / raw)
  To: libc-alpha; +Cc: bug-hurd, Samuel Thibault

...in some more places.

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 sysdeps/mach/hurd/dl-sysdep.c | 2 +-
 sysdeps/mach/hurd/mmap.c      | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/sysdeps/mach/hurd/dl-sysdep.c b/sysdeps/mach/hurd/dl-sysdep.c
index d7b309e0..25a12774 100644
--- a/sysdeps/mach/hurd/dl-sysdep.c
+++ b/sysdeps/mach/hurd/dl-sysdep.c
@@ -472,7 +472,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
       err = __io_map ((mach_port_t) fd, &memobj_rd, &memobj_wr);
       if (err)
 	return __hurd_fail (err), MAP_FAILED;
-      if (memobj_wr != MACH_PORT_NULL)
+      if (MACH_PORT_VALID (memobj_wr))
 	__mach_port_deallocate (__mach_task_self (), memobj_wr);
     }
 
diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c
index c3cc1856..790eb238 100644
--- a/sysdeps/mach/hurd/mmap.c
+++ b/sysdeps/mach/hurd/mmap.c
@@ -91,7 +91,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
             if (wobj == robj)
               max_vmprot |= VM_PROT_WRITE;
 	    memobj = robj;
-	    if (wobj != MACH_PORT_NULL)
+	    if (MACH_PORT_VALID (wobj))
 	      __mach_port_deallocate (__mach_task_self (), wobj);
 	    break;
 	  case PROT_WRITE:
@@ -99,7 +99,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
             if (robj == wobj)
               max_vmprot |= VM_PROT_READ|VM_PROT_EXECUTE;
 	    memobj = wobj;
-	    if (robj != MACH_PORT_NULL)
+	    if (MACH_PORT_VALID (robj))
 	      __mach_port_deallocate (__mach_task_self (), robj);
 	    break;
 	  case PROT_READ|PROT_WRITE:
@@ -167,7 +167,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
 			copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE);
     }
 
-  if (memobj != MACH_PORT_NULL)
+  if (MACH_PORT_VALID (memobj))
     __mach_port_deallocate (__mach_task_self (), memobj);
 
   if (err == KERN_PROTECTION_FAILURE)
-- 
2.40.0


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

* [PATCH 3/4] hurd: Microoptimize mmap ()
  2023-04-23 21:55 [PATCH 1/4] hurd: Implement MAP_32BIT Sergey Bugaev
  2023-04-23 21:55 ` [PATCH 2/4] hurd: Don't attempt to deallocate MACH_PORT_DEAD Sergey Bugaev
@ 2023-04-23 21:55 ` Sergey Bugaev
  2023-04-24 20:46   ` Samuel Thibault
  2023-04-23 21:55 ` [RFC PATCH 4/4] hurd: Implement prefer_map_32bit_exec tunable Sergey Bugaev
  2023-04-24 20:42 ` [PATCH 1/4] hurd: Implement MAP_32BIT Samuel Thibault
  3 siblings, 1 reply; 10+ messages in thread
From: Sergey Bugaev @ 2023-04-23 21:55 UTC (permalink / raw)
  To: libc-alpha; +Cc: bug-hurd, Samuel Thibault

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 sysdeps/mach/hurd/mmap.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c
index 790eb238..d570be24 100644
--- a/sysdeps/mach/hurd/mmap.c
+++ b/sysdeps/mach/hurd/mmap.c
@@ -42,7 +42,8 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
   mapaddr = (vm_address_t) addr;
 
   /* ADDR and OFFSET must be page-aligned.  */
-  if ((mapaddr & (__vm_page_size - 1)) || (offset & (__vm_page_size - 1)))
+  if (__glibc_unlikely ((mapaddr & (__vm_page_size - 1))
+      || (offset & (__vm_page_size - 1))))
     return (void *) (long int) __hurd_fail (EINVAL);
 
   vmprot = VM_PROT_NONE;
@@ -73,7 +74,8 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
 	mach_port_t robj, wobj;
 	if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj)))
 	  {
-	    if (err == MIG_BAD_ID || err == EOPNOTSUPP || err == ENOSYS)
+	    if (__glibc_unlikely (err == MIG_BAD_ID || err == EOPNOTSUPP
+		|| err == ENOSYS))
 	      err = ENODEV;	/* File descriptor doesn't support mmap.  */
 	    return (void *) (long int) __hurd_dfail (fd, err);
 	  }
@@ -173,7 +175,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
   if (err == KERN_PROTECTION_FAILURE)
     err = EACCES;
 
-  if (err)
+  if (__glibc_unlikely (err))
     return (void *) (long int) __hurd_fail (err);
 
   return (void *) mapaddr;
-- 
2.40.0


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

* [RFC PATCH 4/4] hurd: Implement prefer_map_32bit_exec tunable
  2023-04-23 21:55 [PATCH 1/4] hurd: Implement MAP_32BIT Sergey Bugaev
  2023-04-23 21:55 ` [PATCH 2/4] hurd: Don't attempt to deallocate MACH_PORT_DEAD Sergey Bugaev
  2023-04-23 21:55 ` [PATCH 3/4] hurd: Microoptimize mmap () Sergey Bugaev
@ 2023-04-23 21:55 ` Sergey Bugaev
  2023-04-24 20:48   ` Samuel Thibault
  2023-04-24 20:42 ` [PATCH 1/4] hurd: Implement MAP_32BIT Samuel Thibault
  3 siblings, 1 reply; 10+ messages in thread
From: Sergey Bugaev @ 2023-04-23 21:55 UTC (permalink / raw)
  To: libc-alpha; +Cc: bug-hurd, Samuel Thibault, H . J . Lu

This makes the prefer_map_32bit_exec tunable no longer Linux-specific.

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 sysdeps/mach/hurd/dl-sysdep.c                 |  5 ++++
 sysdeps/mach/hurd/mmap.c                      |  6 +++++
 sysdeps/unix/sysv/linux/x86_64/64/Makefile    | 23 -------------------
 sysdeps/x86_64/64/Makefile                    | 22 ++++++++++++++++++
 .../linux => }/x86_64/64/dl-tunables.list     |  0
 .../linux => }/x86_64/64/tst-map-32bit-1a.c   |  0
 .../linux => }/x86_64/64/tst-map-32bit-1b.c   |  0
 .../linux => }/x86_64/64/tst-map-32bit-mod.c  |  0
 8 files changed, 33 insertions(+), 23 deletions(-)
 create mode 100644 sysdeps/x86_64/64/Makefile
 rename sysdeps/{unix/sysv/linux => }/x86_64/64/dl-tunables.list (100%)
 rename sysdeps/{unix/sysv/linux => }/x86_64/64/tst-map-32bit-1a.c (100%)
 rename sysdeps/{unix/sysv/linux => }/x86_64/64/tst-map-32bit-1b.c (100%)
 rename sysdeps/{unix/sysv/linux => }/x86_64/64/tst-map-32bit-mod.c (100%)

diff --git a/sysdeps/mach/hurd/dl-sysdep.c b/sysdeps/mach/hurd/dl-sysdep.c
index 25a12774..79ebb0ce 100644
--- a/sysdeps/mach/hurd/dl-sysdep.c
+++ b/sysdeps/mach/hurd/dl-sysdep.c
@@ -462,6 +462,11 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
   if (prot & PROT_EXEC)
     vmprot |= VM_PROT_EXECUTE;
 
+#ifdef __LP64__
+  if ((addr == NULL) && (prot & PROT_EXEC)
+      && HAS_ARCH_FEATURE (Prefer_MAP_32BIT_EXEC))
+    flags |= MAP_32BIT;
+#endif
   mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0;
 
   if (flags & MAP_ANON)
diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c
index d570be24..c4ffbba3 100644
--- a/sysdeps/mach/hurd/mmap.c
+++ b/sysdeps/mach/hurd/mmap.c
@@ -18,6 +18,7 @@
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <errno.h>
+#include <ldsodefs.h>
 #include <hurd.h>
 #include <hurd/fd.h>
 
@@ -56,6 +57,11 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
 
   copy = ! (flags & MAP_SHARED);
 
+#ifdef __LP64__
+  if ((addr == NULL) && (prot & PROT_EXEC)
+      && HAS_ARCH_FEATURE (Prefer_MAP_32BIT_EXEC))
+    flags |= MAP_32BIT;
+#endif
   mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0;
 
   switch (flags & MAP_TYPE)
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/Makefile b/sysdeps/unix/sysv/linux/x86_64/64/Makefile
index 1bf7d528..a7b6dc5a 100644
--- a/sysdeps/unix/sysv/linux/x86_64/64/Makefile
+++ b/sysdeps/unix/sysv/linux/x86_64/64/Makefile
@@ -1,25 +1,2 @@
 # The default ABI is 64.
 default-abi := 64
-
-ifeq ($(subdir),elf)
-
-tests-map-32bit = \
-  tst-map-32bit-1a \
-  tst-map-32bit-1b \
-# tests-map-32bit
-tst-map-32bit-1a-no-pie = yes
-tst-map-32bit-1b-no-pie = yes
-tests += $(tests-map-32bit)
-
-modules-map-32bit = \
-  tst-map-32bit-mod \
-# modules-map-32bit
-modules-names += $(modules-map-32bit)
-
-$(objpfx)tst-map-32bit-mod.so: $(libsupport)
-tst-map-32bit-1a-ENV = LD_PREFER_MAP_32BIT_EXEC=1
-$(objpfx)tst-map-32bit-1a: $(objpfx)tst-map-32bit-mod.so
-tst-map-32bit-1b-ENV = GLIBC_TUNABLES=glibc.cpu.prefer_map_32bit_exec=1
-$(objpfx)tst-map-32bit-1b: $(objpfx)tst-map-32bit-mod.so
-
-endif
diff --git a/sysdeps/x86_64/64/Makefile b/sysdeps/x86_64/64/Makefile
new file mode 100644
index 00000000..73fcfe0b
--- /dev/null
+++ b/sysdeps/x86_64/64/Makefile
@@ -0,0 +1,22 @@
+ifeq ($(subdir),elf)
+
+tests-map-32bit = \
+  tst-map-32bit-1a \
+  tst-map-32bit-1b \
+# tests-map-32bit
+tst-map-32bit-1a-no-pie = yes
+tst-map-32bit-1b-no-pie = yes
+tests += $(tests-map-32bit)
+
+modules-map-32bit = \
+  tst-map-32bit-mod \
+# modules-map-32bit
+modules-names += $(modules-map-32bit)
+
+$(objpfx)tst-map-32bit-mod.so: $(libsupport)
+tst-map-32bit-1a-ENV = LD_PREFER_MAP_32BIT_EXEC=1
+$(objpfx)tst-map-32bit-1a: $(objpfx)tst-map-32bit-mod.so
+tst-map-32bit-1b-ENV = GLIBC_TUNABLES=glibc.cpu.prefer_map_32bit_exec=1
+$(objpfx)tst-map-32bit-1b: $(objpfx)tst-map-32bit-mod.so
+
+endif
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list b/sysdeps/x86_64/64/dl-tunables.list
similarity index 100%
rename from sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list
rename to sysdeps/x86_64/64/dl-tunables.list
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c b/sysdeps/x86_64/64/tst-map-32bit-1a.c
similarity index 100%
rename from sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c
rename to sysdeps/x86_64/64/tst-map-32bit-1a.c
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c b/sysdeps/x86_64/64/tst-map-32bit-1b.c
similarity index 100%
rename from sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c
rename to sysdeps/x86_64/64/tst-map-32bit-1b.c
diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c b/sysdeps/x86_64/64/tst-map-32bit-mod.c
similarity index 100%
rename from sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c
rename to sysdeps/x86_64/64/tst-map-32bit-mod.c
-- 
2.40.0


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

* Re: [PATCH 1/4] hurd: Implement MAP_32BIT
  2023-04-23 21:55 [PATCH 1/4] hurd: Implement MAP_32BIT Sergey Bugaev
                   ` (2 preceding siblings ...)
  2023-04-23 21:55 ` [RFC PATCH 4/4] hurd: Implement prefer_map_32bit_exec tunable Sergey Bugaev
@ 2023-04-24 20:42 ` Samuel Thibault
  3 siblings, 0 replies; 10+ messages in thread
From: Samuel Thibault @ 2023-04-24 20:42 UTC (permalink / raw)
  To: Sergey Bugaev; +Cc: libc-alpha, bug-hurd

Applied, thanks!

Sergey Bugaev, le lun. 24 avril 2023 00:55:23 +0300, a ecrit:
> This is a flag that can be passed to mmap () to request that the mapping
> being established should be located in the lower 2 GB area of the
> address space, so only the lower 31 (not 32) bits can be set in its
> address, and the address can be represented as a 32-bit integer without
> truncating it.
> 
> This flag is intended to be compatible with Linux, FreeBSD, and Darwin
> flags of the same name. Out of those systems, it appears Linux and
> FreeBSD take MAP_32BIT to mean "map 31 bit", whereas Darwin allows the
> 32nd bit to be set in the address as well. The Hurd follows Linux and
> FreeBSD behavior.
> 
> Unlike on those systems, on the Hurd MAP_32BIT is defined on all
> supported architectures (which currently are only i386 and x86_64).
> 
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
> ---
>  sysdeps/mach/hurd/bits/mman_ext.h |  1 +
>  sysdeps/mach/hurd/dl-sysdep.c     |  8 +++++---
>  sysdeps/mach/hurd/mmap.c          | 10 ++++++----
>  3 files changed, 12 insertions(+), 7 deletions(-)
> 
> diff --git a/sysdeps/mach/hurd/bits/mman_ext.h b/sysdeps/mach/hurd/bits/mman_ext.h
> index f022826e..bbb94743 100644
> --- a/sysdeps/mach/hurd/bits/mman_ext.h
> +++ b/sysdeps/mach/hurd/bits/mman_ext.h
> @@ -22,4 +22,5 @@
>  
>  #ifdef __USE_GNU
>  # define SHM_ANON	((const char *) 1)
> +# define MAP_32BIT	0x1000
>  #endif /* __USE_GNU  */
> diff --git a/sysdeps/mach/hurd/dl-sysdep.c b/sysdeps/mach/hurd/dl-sysdep.c
> index 6e167e12..d7b309e0 100644
> --- a/sysdeps/mach/hurd/dl-sysdep.c
> +++ b/sysdeps/mach/hurd/dl-sysdep.c
> @@ -451,7 +451,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>  {
>    error_t err;
>    vm_prot_t vmprot;
> -  vm_address_t mapaddr;
> +  vm_address_t mapaddr, mask;
>    mach_port_t memobj_rd, memobj_wr;
>  
>    vmprot = VM_PROT_NONE;
> @@ -462,6 +462,8 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>    if (prot & PROT_EXEC)
>      vmprot |= VM_PROT_EXECUTE;
>  
> +  mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0;
> +
>    if (flags & MAP_ANON)
>      memobj_rd = MACH_PORT_NULL;
>    else
> @@ -476,7 +478,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>  
>    mapaddr = (vm_address_t) addr;
>    err = __vm_map (__mach_task_self (),
> -		  &mapaddr, (vm_size_t) len, 0,
> +		  &mapaddr, (vm_size_t) len, mask,
>  		  !(flags & MAP_FIXED),
>  		  memobj_rd,
>  		  (vm_offset_t) offset,
> @@ -491,7 +493,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>        if (! err)
>  	err = __vm_map (__mach_task_self (),
>  			&mapaddr, (vm_size_t) len,
> -			0,
> +			mask,
>  			!(flags & MAP_FIXED),
>  			memobj_rd, (vm_offset_t) offset,
>  			flags & (MAP_COPY|MAP_PRIVATE),
> diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c
> index 20a41e36..c3cc1856 100644
> --- a/sysdeps/mach/hurd/mmap.c
> +++ b/sysdeps/mach/hurd/mmap.c
> @@ -36,7 +36,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>    error_t err;
>    vm_prot_t vmprot, max_vmprot;
>    memory_object_t memobj;
> -  vm_address_t mapaddr;
> +  vm_address_t mapaddr, mask;
>    boolean_t copy;
>  
>    mapaddr = (vm_address_t) addr;
> @@ -55,6 +55,8 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>  
>    copy = ! (flags & MAP_SHARED);
>  
> +  mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0;
> +
>    switch (flags & MAP_TYPE)
>      {
>      default:
> @@ -134,7 +136,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>      max_vmprot = VM_PROT_ALL;
>  
>    err = __vm_map (__mach_task_self (),
> -		  &mapaddr, (vm_size_t) len, (vm_address_t) 0,
> +		  &mapaddr, (vm_size_t) len, mask,
>  		  mapaddr == 0,
>  		  memobj, (vm_offset_t) offset,
>  		  copy, vmprot, max_vmprot,
> @@ -149,7 +151,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>  	  err = __vm_deallocate (__mach_task_self (), mapaddr, len);
>  	  if (! err)
>  	    err = __vm_map (__mach_task_self (),
> -			    &mapaddr, (vm_size_t) len, (vm_address_t) 0,
> +			    &mapaddr, (vm_size_t) len, mask,
>  			    0, memobj, (vm_offset_t) offset,
>  			    copy, vmprot, max_vmprot,
>  			    copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE);
> @@ -159,7 +161,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>      {
>        if (mapaddr != 0 && (err == KERN_NO_SPACE || err == KERN_INVALID_ADDRESS))
>  	err = __vm_map (__mach_task_self (),
> -			&mapaddr, (vm_size_t) len, (vm_address_t) 0,
> +			&mapaddr, (vm_size_t) len, mask,
>  			1, memobj, (vm_offset_t) offset,
>  			copy, vmprot, max_vmprot,
>  			copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE);
> -- 
> 2.40.0
> 

-- 
Samuel
---
Pour une évaluation indépendante, transparente et rigoureuse !
Je soutiens la Commission d'Évaluation de l'Inria.

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

* Re: [PATCH 2/4] hurd: Don't attempt to deallocate MACH_PORT_DEAD
  2023-04-23 21:55 ` [PATCH 2/4] hurd: Don't attempt to deallocate MACH_PORT_DEAD Sergey Bugaev
@ 2023-04-24 20:44   ` Samuel Thibault
  0 siblings, 0 replies; 10+ messages in thread
From: Samuel Thibault @ 2023-04-24 20:44 UTC (permalink / raw)
  To: Sergey Bugaev; +Cc: libc-alpha, bug-hurd

Applied, thanks!

Sergey Bugaev, le lun. 24 avril 2023 00:55:24 +0300, a ecrit:
> ...in some more places.
> 
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
> ---
>  sysdeps/mach/hurd/dl-sysdep.c | 2 +-
>  sysdeps/mach/hurd/mmap.c      | 6 +++---
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/sysdeps/mach/hurd/dl-sysdep.c b/sysdeps/mach/hurd/dl-sysdep.c
> index d7b309e0..25a12774 100644
> --- a/sysdeps/mach/hurd/dl-sysdep.c
> +++ b/sysdeps/mach/hurd/dl-sysdep.c
> @@ -472,7 +472,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>        err = __io_map ((mach_port_t) fd, &memobj_rd, &memobj_wr);
>        if (err)
>  	return __hurd_fail (err), MAP_FAILED;
> -      if (memobj_wr != MACH_PORT_NULL)
> +      if (MACH_PORT_VALID (memobj_wr))
>  	__mach_port_deallocate (__mach_task_self (), memobj_wr);
>      }
>  
> diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c
> index c3cc1856..790eb238 100644
> --- a/sysdeps/mach/hurd/mmap.c
> +++ b/sysdeps/mach/hurd/mmap.c
> @@ -91,7 +91,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>              if (wobj == robj)
>                max_vmprot |= VM_PROT_WRITE;
>  	    memobj = robj;
> -	    if (wobj != MACH_PORT_NULL)
> +	    if (MACH_PORT_VALID (wobj))
>  	      __mach_port_deallocate (__mach_task_self (), wobj);
>  	    break;
>  	  case PROT_WRITE:
> @@ -99,7 +99,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>              if (robj == wobj)
>                max_vmprot |= VM_PROT_READ|VM_PROT_EXECUTE;
>  	    memobj = wobj;
> -	    if (robj != MACH_PORT_NULL)
> +	    if (MACH_PORT_VALID (robj))
>  	      __mach_port_deallocate (__mach_task_self (), robj);
>  	    break;
>  	  case PROT_READ|PROT_WRITE:
> @@ -167,7 +167,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>  			copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE);
>      }
>  
> -  if (memobj != MACH_PORT_NULL)
> +  if (MACH_PORT_VALID (memobj))
>      __mach_port_deallocate (__mach_task_self (), memobj);
>  
>    if (err == KERN_PROTECTION_FAILURE)
> -- 
> 2.40.0
> 

-- 
Samuel
---
Pour une évaluation indépendante, transparente et rigoureuse !
Je soutiens la Commission d'Évaluation de l'Inria.

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

* Re: [PATCH 3/4] hurd: Microoptimize mmap ()
  2023-04-23 21:55 ` [PATCH 3/4] hurd: Microoptimize mmap () Sergey Bugaev
@ 2023-04-24 20:46   ` Samuel Thibault
  2023-04-24 21:09     ` Sergey Bugaev
  0 siblings, 1 reply; 10+ messages in thread
From: Samuel Thibault @ 2023-04-24 20:46 UTC (permalink / raw)
  To: Sergey Bugaev; +Cc: libc-alpha, bug-hurd

Hello,

Is it really worth making the code a bit obscure? The mapping RPC will
be way more expensive than branch misprediction...

Samuel

Sergey Bugaev, le lun. 24 avril 2023 00:55:25 +0300, a ecrit:
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
> ---
>  sysdeps/mach/hurd/mmap.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c
> index 790eb238..d570be24 100644
> --- a/sysdeps/mach/hurd/mmap.c
> +++ b/sysdeps/mach/hurd/mmap.c
> @@ -42,7 +42,8 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>    mapaddr = (vm_address_t) addr;
>  
>    /* ADDR and OFFSET must be page-aligned.  */
> -  if ((mapaddr & (__vm_page_size - 1)) || (offset & (__vm_page_size - 1)))
> +  if (__glibc_unlikely ((mapaddr & (__vm_page_size - 1))
> +      || (offset & (__vm_page_size - 1))))
>      return (void *) (long int) __hurd_fail (EINVAL);
>  
>    vmprot = VM_PROT_NONE;
> @@ -73,7 +74,8 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>  	mach_port_t robj, wobj;
>  	if (err = HURD_DPORT_USE (fd, __io_map (port, &robj, &wobj)))
>  	  {
> -	    if (err == MIG_BAD_ID || err == EOPNOTSUPP || err == ENOSYS)
> +	    if (__glibc_unlikely (err == MIG_BAD_ID || err == EOPNOTSUPP
> +		|| err == ENOSYS))
>  	      err = ENODEV;	/* File descriptor doesn't support mmap.  */
>  	    return (void *) (long int) __hurd_dfail (fd, err);
>  	  }
> @@ -173,7 +175,7 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>    if (err == KERN_PROTECTION_FAILURE)
>      err = EACCES;
>  
> -  if (err)
> +  if (__glibc_unlikely (err))
>      return (void *) (long int) __hurd_fail (err);
>  
>    return (void *) mapaddr;
> -- 
> 2.40.0

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

* Re: [RFC PATCH 4/4] hurd: Implement prefer_map_32bit_exec tunable
  2023-04-23 21:55 ` [RFC PATCH 4/4] hurd: Implement prefer_map_32bit_exec tunable Sergey Bugaev
@ 2023-04-24 20:48   ` Samuel Thibault
  0 siblings, 0 replies; 10+ messages in thread
From: Samuel Thibault @ 2023-04-24 20:48 UTC (permalink / raw)
  To: Sergey Bugaev; +Cc: libc-alpha, bug-hurd, H . J . Lu

Applied, thanks!

Sergey Bugaev, le lun. 24 avril 2023 00:55:26 +0300, a ecrit:
> This makes the prefer_map_32bit_exec tunable no longer Linux-specific.
> 
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
> ---
>  sysdeps/mach/hurd/dl-sysdep.c                 |  5 ++++
>  sysdeps/mach/hurd/mmap.c                      |  6 +++++
>  sysdeps/unix/sysv/linux/x86_64/64/Makefile    | 23 -------------------
>  sysdeps/x86_64/64/Makefile                    | 22 ++++++++++++++++++
>  .../linux => }/x86_64/64/dl-tunables.list     |  0
>  .../linux => }/x86_64/64/tst-map-32bit-1a.c   |  0
>  .../linux => }/x86_64/64/tst-map-32bit-1b.c   |  0
>  .../linux => }/x86_64/64/tst-map-32bit-mod.c  |  0
>  8 files changed, 33 insertions(+), 23 deletions(-)
>  create mode 100644 sysdeps/x86_64/64/Makefile
>  rename sysdeps/{unix/sysv/linux => }/x86_64/64/dl-tunables.list (100%)
>  rename sysdeps/{unix/sysv/linux => }/x86_64/64/tst-map-32bit-1a.c (100%)
>  rename sysdeps/{unix/sysv/linux => }/x86_64/64/tst-map-32bit-1b.c (100%)
>  rename sysdeps/{unix/sysv/linux => }/x86_64/64/tst-map-32bit-mod.c (100%)
> 
> diff --git a/sysdeps/mach/hurd/dl-sysdep.c b/sysdeps/mach/hurd/dl-sysdep.c
> index 25a12774..79ebb0ce 100644
> --- a/sysdeps/mach/hurd/dl-sysdep.c
> +++ b/sysdeps/mach/hurd/dl-sysdep.c
> @@ -462,6 +462,11 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>    if (prot & PROT_EXEC)
>      vmprot |= VM_PROT_EXECUTE;
>  
> +#ifdef __LP64__
> +  if ((addr == NULL) && (prot & PROT_EXEC)
> +      && HAS_ARCH_FEATURE (Prefer_MAP_32BIT_EXEC))
> +    flags |= MAP_32BIT;
> +#endif
>    mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0;
>  
>    if (flags & MAP_ANON)
> diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c
> index d570be24..c4ffbba3 100644
> --- a/sysdeps/mach/hurd/mmap.c
> +++ b/sysdeps/mach/hurd/mmap.c
> @@ -18,6 +18,7 @@
>  #include <sys/types.h>
>  #include <sys/mman.h>
>  #include <errno.h>
> +#include <ldsodefs.h>
>  #include <hurd.h>
>  #include <hurd/fd.h>
>  
> @@ -56,6 +57,11 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
>  
>    copy = ! (flags & MAP_SHARED);
>  
> +#ifdef __LP64__
> +  if ((addr == NULL) && (prot & PROT_EXEC)
> +      && HAS_ARCH_FEATURE (Prefer_MAP_32BIT_EXEC))
> +    flags |= MAP_32BIT;
> +#endif
>    mask = (flags & MAP_32BIT) ? ~(vm_address_t) 0x7FFFFFFF : 0;
>  
>    switch (flags & MAP_TYPE)
> diff --git a/sysdeps/unix/sysv/linux/x86_64/64/Makefile b/sysdeps/unix/sysv/linux/x86_64/64/Makefile
> index 1bf7d528..a7b6dc5a 100644
> --- a/sysdeps/unix/sysv/linux/x86_64/64/Makefile
> +++ b/sysdeps/unix/sysv/linux/x86_64/64/Makefile
> @@ -1,25 +1,2 @@
>  # The default ABI is 64.
>  default-abi := 64
> -
> -ifeq ($(subdir),elf)
> -
> -tests-map-32bit = \
> -  tst-map-32bit-1a \
> -  tst-map-32bit-1b \
> -# tests-map-32bit
> -tst-map-32bit-1a-no-pie = yes
> -tst-map-32bit-1b-no-pie = yes
> -tests += $(tests-map-32bit)
> -
> -modules-map-32bit = \
> -  tst-map-32bit-mod \
> -# modules-map-32bit
> -modules-names += $(modules-map-32bit)
> -
> -$(objpfx)tst-map-32bit-mod.so: $(libsupport)
> -tst-map-32bit-1a-ENV = LD_PREFER_MAP_32BIT_EXEC=1
> -$(objpfx)tst-map-32bit-1a: $(objpfx)tst-map-32bit-mod.so
> -tst-map-32bit-1b-ENV = GLIBC_TUNABLES=glibc.cpu.prefer_map_32bit_exec=1
> -$(objpfx)tst-map-32bit-1b: $(objpfx)tst-map-32bit-mod.so
> -
> -endif
> diff --git a/sysdeps/x86_64/64/Makefile b/sysdeps/x86_64/64/Makefile
> new file mode 100644
> index 00000000..73fcfe0b
> --- /dev/null
> +++ b/sysdeps/x86_64/64/Makefile
> @@ -0,0 +1,22 @@
> +ifeq ($(subdir),elf)
> +
> +tests-map-32bit = \
> +  tst-map-32bit-1a \
> +  tst-map-32bit-1b \
> +# tests-map-32bit
> +tst-map-32bit-1a-no-pie = yes
> +tst-map-32bit-1b-no-pie = yes
> +tests += $(tests-map-32bit)
> +
> +modules-map-32bit = \
> +  tst-map-32bit-mod \
> +# modules-map-32bit
> +modules-names += $(modules-map-32bit)
> +
> +$(objpfx)tst-map-32bit-mod.so: $(libsupport)
> +tst-map-32bit-1a-ENV = LD_PREFER_MAP_32BIT_EXEC=1
> +$(objpfx)tst-map-32bit-1a: $(objpfx)tst-map-32bit-mod.so
> +tst-map-32bit-1b-ENV = GLIBC_TUNABLES=glibc.cpu.prefer_map_32bit_exec=1
> +$(objpfx)tst-map-32bit-1b: $(objpfx)tst-map-32bit-mod.so
> +
> +endif
> diff --git a/sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list b/sysdeps/x86_64/64/dl-tunables.list
> similarity index 100%
> rename from sysdeps/unix/sysv/linux/x86_64/64/dl-tunables.list
> rename to sysdeps/x86_64/64/dl-tunables.list
> diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c b/sysdeps/x86_64/64/tst-map-32bit-1a.c
> similarity index 100%
> rename from sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1a.c
> rename to sysdeps/x86_64/64/tst-map-32bit-1a.c
> diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c b/sysdeps/x86_64/64/tst-map-32bit-1b.c
> similarity index 100%
> rename from sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-1b.c
> rename to sysdeps/x86_64/64/tst-map-32bit-1b.c
> diff --git a/sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c b/sysdeps/x86_64/64/tst-map-32bit-mod.c
> similarity index 100%
> rename from sysdeps/unix/sysv/linux/x86_64/64/tst-map-32bit-mod.c
> rename to sysdeps/x86_64/64/tst-map-32bit-mod.c
> -- 
> 2.40.0
> 

-- 
Samuel
---
Pour une évaluation indépendante, transparente et rigoureuse !
Je soutiens la Commission d'Évaluation de l'Inria.

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

* Re: [PATCH 3/4] hurd: Microoptimize mmap ()
  2023-04-24 20:46   ` Samuel Thibault
@ 2023-04-24 21:09     ` Sergey Bugaev
  2023-04-24 21:25       ` Samuel Thibault
  0 siblings, 1 reply; 10+ messages in thread
From: Sergey Bugaev @ 2023-04-24 21:09 UTC (permalink / raw)
  To: Samuel Thibault; +Cc: libc-alpha, bug-hurd

On Mon, Apr 24, 2023 at 11:47 PM Samuel Thibault
<samuel.thibault@gnu.org> wrote:
> Is it really worth making the code a bit obscure?

No, not really.

What happened here was I looked at what my mask computation compiled
to, to verify it does the right thing on both x86_64 and i686, and
then I saw how the error branches are compiled, and next thing you
know there are new __glibc_unlikely's in my tree :)

What I should rather look into is marking __hurd_fail and friends with
__attribute__((cold)); that would take care of all the error branches
everywhere automatically without having to mark things up. But I did a
quick grep and found nothing using __attribute__((cold)) yet, so I
don't know what the right way of using it would be (and maybe it's not
being used intentionally?). I'm thinking it should probably go into
misc/sys/cdefs.h as __COLD (or __attribute_cold?). Something like
this:

#if __glibc_has_attribute (cold)
#define __COLD __attribute__ ((cold))
#else
#define __COLD
#endif

What do you think?

Sergey

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

* Re: [PATCH 3/4] hurd: Microoptimize mmap ()
  2023-04-24 21:09     ` Sergey Bugaev
@ 2023-04-24 21:25       ` Samuel Thibault
  0 siblings, 0 replies; 10+ messages in thread
From: Samuel Thibault @ 2023-04-24 21:25 UTC (permalink / raw)
  To: Sergey Bugaev; +Cc: libc-alpha, bug-hurd

Sergey Bugaev, le mar. 25 avril 2023 00:09:58 +0300, a ecrit:
> What I should rather look into is marking __hurd_fail and friends with
> __attribute__((cold)); that would take care of all the error branches
> everywhere automatically without having to mark things up.

Yes, that'd probably be great :)

> But I did a quick grep and found nothing using __attribute__((cold))
> yet, so I don't know what the right way of using it would be
> (and maybe it's not being used intentionally?).

It's probably that just nobody thought about adding it.

> I'm thinking it should probably go into misc/sys/cdefs.h as __COLD (or
> __attribute_cold?). Something like this:
> 
> #if __glibc_has_attribute (cold)
> #define __COLD __attribute__ ((cold))
> #else
> #define __COLD
> #endif
> 
> What do you think?

Yes! Though you can even make it

#if __GNUC_PREREQ (4,3) || __glibc_has_attribute (__cold__)

Samuel

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

end of thread, other threads:[~2023-04-24 21:25 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-23 21:55 [PATCH 1/4] hurd: Implement MAP_32BIT Sergey Bugaev
2023-04-23 21:55 ` [PATCH 2/4] hurd: Don't attempt to deallocate MACH_PORT_DEAD Sergey Bugaev
2023-04-24 20:44   ` Samuel Thibault
2023-04-23 21:55 ` [PATCH 3/4] hurd: Microoptimize mmap () Sergey Bugaev
2023-04-24 20:46   ` Samuel Thibault
2023-04-24 21:09     ` Sergey Bugaev
2023-04-24 21:25       ` Samuel Thibault
2023-04-23 21:55 ` [RFC PATCH 4/4] hurd: Implement prefer_map_32bit_exec tunable Sergey Bugaev
2023-04-24 20:48   ` Samuel Thibault
2023-04-24 20:42 ` [PATCH 1/4] hurd: Implement MAP_32BIT Samuel Thibault

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 read-only IMAP folder(s) and NNTP newsgroup(s).