public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH v2 0/2] Build fixes for musl, glibc < 2.34
@ 2024-08-20 21:58 Stefano Brivio
  2024-08-20 21:58 ` [PATCH v2 1/2] udp_flow: Add missing unistd.h include for close() Stefano Brivio
  2024-08-20 21:58 ` [PATCH v2 2/2] util: Provide own version of close_range(), and no-op fallback Stefano Brivio
  0 siblings, 2 replies; 4+ messages in thread
From: Stefano Brivio @ 2024-08-20 21:58 UTC (permalink / raw)
  To: passt-dev; +Cc: David Gibson, lemmi

v2: Take care of cppcheck warning in 2/2

Stefano Brivio (2):
  udp_flow: Add missing unistd.h include for close()
  util: Provide own version of close_range(), and no-op fallback

 udp_flow.c |  1 +
 util.h     | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)

-- 
2.43.0


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

* [PATCH v2 1/2] udp_flow: Add missing unistd.h include for close()
  2024-08-20 21:58 [PATCH v2 0/2] Build fixes for musl, glibc < 2.34 Stefano Brivio
@ 2024-08-20 21:58 ` Stefano Brivio
  2024-08-20 21:58 ` [PATCH v2 2/2] util: Provide own version of close_range(), and no-op fallback Stefano Brivio
  1 sibling, 0 replies; 4+ messages in thread
From: Stefano Brivio @ 2024-08-20 21:58 UTC (permalink / raw)
  To: passt-dev; +Cc: David Gibson, lemmi

For some reason, this is reported only with musl, and older glibc
versions (2.31, at least).

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
 udp_flow.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/udp_flow.c b/udp_flow.c
index b1133c0..1ff59a9 100644
--- a/udp_flow.c
+++ b/udp_flow.c
@@ -8,6 +8,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/uio.h>
+#include <unistd.h>
 
 #include "util.h"
 #include "passt.h"
-- 
@@ -8,6 +8,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/uio.h>
+#include <unistd.h>
 
 #include "util.h"
 #include "passt.h"
-- 
2.43.0


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

* [PATCH v2 2/2] util: Provide own version of close_range(), and no-op fallback
  2024-08-20 21:58 [PATCH v2 0/2] Build fixes for musl, glibc < 2.34 Stefano Brivio
  2024-08-20 21:58 ` [PATCH v2 1/2] udp_flow: Add missing unistd.h include for close() Stefano Brivio
@ 2024-08-20 21:58 ` Stefano Brivio
  2024-08-21  1:38   ` David Gibson
  1 sibling, 1 reply; 4+ messages in thread
From: Stefano Brivio @ 2024-08-20 21:58 UTC (permalink / raw)
  To: passt-dev; +Cc: David Gibson, lemmi

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>
---
 util.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/util.h b/util.h
index cb4d181..9c95dcd 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,25 @@ 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))
+/* cppcheck-suppress funcArgNamesDifferent */
+int close_range(unsigned int first, unsigned int last, int flags) {
+	return syscall(SYS_close_range, first, last, flags);
+}
+#else
+/* No reasonable fallback option */
+/* cppcheck-suppress funcArgNamesDifferent */
+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);
-- 
@@ -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,25 @@ 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))
+/* cppcheck-suppress funcArgNamesDifferent */
+int close_range(unsigned int first, unsigned int last, int flags) {
+	return syscall(SYS_close_range, first, last, flags);
+}
+#else
+/* No reasonable fallback option */
+/* cppcheck-suppress funcArgNamesDifferent */
+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);
-- 
2.43.0


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

* Re: [PATCH v2 2/2] util: Provide own version of close_range(), and no-op fallback
  2024-08-20 21:58 ` [PATCH v2 2/2] util: Provide own version of close_range(), and no-op fallback Stefano Brivio
@ 2024-08-21  1:38   ` David Gibson
  0 siblings, 0 replies; 4+ messages in thread
From: David Gibson @ 2024-08-21  1:38 UTC (permalink / raw)
  To: Stefano Brivio; +Cc: passt-dev, lemmi

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

On Tue, Aug 20, 2024 at 11:58:53PM +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 | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/util.h b/util.h
> index cb4d181..9c95dcd 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,25 @@ 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))
> +/* cppcheck-suppress funcArgNamesDifferent */
> +int close_range(unsigned int first, unsigned int last, int flags) {
> +	return syscall(SYS_close_range, first, last, flags);
> +}
> +#else
> +/* No reasonable fallback option */
> +/* cppcheck-suppress funcArgNamesDifferent */
> +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 --]

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

end of thread, other threads:[~2024-08-21  2:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-20 21:58 [PATCH v2 0/2] Build fixes for musl, glibc < 2.34 Stefano Brivio
2024-08-20 21:58 ` [PATCH v2 1/2] udp_flow: Add missing unistd.h include for close() Stefano Brivio
2024-08-20 21:58 ` [PATCH v2 2/2] util: Provide own version of close_range(), and no-op fallback Stefano Brivio
2024-08-21  1:38   ` David Gibson

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).