diff mbox series

[3/4] hurd: Microoptimize mmap ()

Message ID 20230423215526.346009-3-bugaevc@gmail.com
State New
Headers show
Series [1/4] hurd: Implement MAP_32BIT | expand

Commit Message

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

Comments

Samuel Thibault April 24, 2023, 8:46 p.m. UTC | #1
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
Sergey Bugaev April 24, 2023, 9:09 p.m. UTC | #2
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
Samuel Thibault April 24, 2023, 9:25 p.m. UTC | #3
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
diff mbox series

Patch

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;