public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top, lemmi@nerd2nerd.org
Subject: Re: [PATCH 2/2] util: Provide own version of close_range(), and no-op fallback
Date: Tue, 20 Aug 2024 10:48:06 +1000	[thread overview]
Message-ID: <ZsPnxtIkQJvsoTru@zatzit.fritz.box> (raw)
In-Reply-To: <20240819231505.1481940-3-sbrivio@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2109 bytes --]

On Tue, Aug 20, 2024 at 01:15:05AM +0200, Stefano Brivio wrote:
> musl, as of 1.2.5, and glibc < 2.34 don't ship a (trivial)
> close_range() implementation. This will probably be added to musl
> soon, by the way:
>   https://www.openwall.com/lists/musl/2024/08/01/9
> 
> Add a weakly-aliased implementation, if it's supported by the kernel.
> If it's not supported (< 5.9), use a no-op fallback. Looping over 2^31
> file descriptors calling close() on them is probably not a good idea.
> 
> Reported-by: lemmi <lemmi@nerd2nerd.org>
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  util.h | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/util.h b/util.h
> index cb4d181..a9deba5 100644
> --- a/util.h
> +++ b/util.h
> @@ -14,6 +14,9 @@
>  #include <string.h>
>  #include <signal.h>
>  #include <arpa/inet.h>
> +#include <unistd.h>
> +#include <sys/syscall.h>
> +#include <linux/close_range.h>
>  
>  #include "log.h"
>  
> @@ -160,6 +163,23 @@ struct ctx;
>  
>  /* cppcheck-suppress funcArgNamesDifferent */
>  __attribute__ ((weak)) int ffsl(long int i) { return __builtin_ffsl(i); }
> +
> +#ifdef CLOSE_RANGE_UNSHARE	/* Linux kernel >= 5.9 */
> +/* glibc < 2.34 and musl as of 1.2.5 need these */
> +#ifndef SYS_close_range
> +#define SYS_close_range		436
> +#endif
> +__attribute__ ((weak)) int close_range(unsigned int first, unsigned int last,
> +				       int flags) {
> +	return syscall(SYS_close_range, first, last, flags);
> +}
> +#else
> +/* No reasonable fallback option */
> +int close_range(unsigned int first, unsigned int last, int flags) {
> +	return 0;
> +}
> +#endif
> +
>  int sock_l4_sa(const struct ctx *c, enum epoll_type type,
>  	       const void *sa, socklen_t sl,
>  	       const char *ifname, bool v6only, uint32_t data);

-- 
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2024-08-20  0:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-19 23:15 [PATCH 0/2] Build fixes for musl, glibc < 2.34 Stefano Brivio
2024-08-19 23:15 ` [PATCH 1/2] udp_flow: Add missing unistd.h include for close() Stefano Brivio
2024-08-20  0:47   ` David Gibson
2024-08-19 23:15 ` [PATCH 2/2] util: Provide own version of close_range(), and no-op fallback Stefano Brivio
2024-08-20  0:48   ` David Gibson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZsPnxtIkQJvsoTru@zatzit.fritz.box \
    --to=david@gibson.dropbear.id.au \
    --cc=lemmi@nerd2nerd.org \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

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 IMAP folder(s).