Skip to contentSkip to conversation

linux: Add linux statx(fd, NULL, AT_EMPTY_PATH) support

PATCH Superseded Miao Wang 1 participant 1 message Aug 27, 2024

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Miao Wang

author
#0 lore raw

PATCH v3 1/1 linux: Add linux statx(fd, NULL, AT_EMPTY_PATH) support

Linux supports passing NULL instead of an empty string as the second parameter when AT_EMPTY_PATH is set in the flags, starting from 6.11, which brings a performance gain since it is much more efficient to detect a NULL parameter than to detect an empty string in the kernel. We utilize this feature if statx is used for fstat, when glibc is compiled to target kernel versions afterwards, and dynamically probe the kernel support of it, when targeting previous versions.

---
Kernel 6.11 adds support for passing NULL to statx(fd, NULL, AT_EMPTY_PATH), which improves the performance. This series utilize this feature when statx is used to implement fstat, on some 32-bit platforms and on loongarch with targeting kernel version below 6.10.6.
---
Changes in v3:
- Fixed build error and failure to set errno in fxstat64.
- Utilize tri-state supported flag to eliminate possible data read barrier instructions after whether it is supported is determined.
- Link to v2: https://sourceware.org/pipermail/libc-alpha/2024-August/159336.html

Changes in v2:
- Separate this patch out from the series.
- Put the added __statx_empty_path() into internal-stat.h.
- Minor fixes according as suggested by Ruoyao.
- Link to v1: https://sourceware.org/pipermail/libc-alpha/2024-August/159333.html

---
 sysdeps/unix/sysv/linux/fstatat64.c       |  8 +++++--
 sysdeps/unix/sysv/linux/fxstat64.c        | 12 +++++------
 sysdeps/unix/sysv/linux/internal-stat.h   | 35 +++++++++++++++++++++++++++++++
 sysdeps/unix/sysv/linux/kernel-features.h |  5 +++++
 4 files changed, 52 insertions(+), 8 deletions(-)
sysdeps/unix/sysv/linux/fstatat64.c +6 -2
index da496177c9..b67a5f4469 100644
@@ -47,8 +47,12 @@ fstatat64_time64_statx (int fd, const char *file, struct __stat64_t64 *buf,
   /* 32-bit kABI with default 64-bit time_t, e.g. arc, riscv32.   Also
      64-bit time_t support is done through statx syscall.  */
   struct statx tmp;
-  int r = INTERNAL_SYSCALL_CALL (statx, fd, file, AT_NO_AUTOMOUNT | flag,
-				 STATX_BASIC_STATS, &tmp);
+  flag |= AT_NO_AUTOMOUNT;
+  int r;
+  if ((flag & AT_EMPTY_PATH) && (file == NULL || *file == '\0'))
+    r = __statx_empty_path (fd, flag, &tmp);
+  else
+    r = INTERNAL_SYSCALL_CALL (statx, fd, file, flag, STATX_BASIC_STATS, &tmp);
   if (r != 0)
     return r;
 
sysdeps/unix/sysv/linux/fxstat64.c +6 -6
index 230374cb22..bbe52de36d 100644
@@ -20,7 +20,7 @@
 #include <sys/stat.h>
 #undef __fxstat
 #include <fcntl.h>
-#include <kernel_stat.h>
+#include <internal-stat.h>
 #include <sysdep.h>
 #include <xstatconv.h>
 #include <statx_cp.h>
@@ -53,11 +53,11 @@ ___fxstat64 (int vers, int fd, struct stat64 *buf)
 # else
   /* New 32-bit kABIs with only 64-bit time_t support, e.g. arc, riscv32.  */
   struct statx tmp;
-  int r = INLINE_SYSCALL_CALL (statx, fd, "", AT_EMPTY_PATH,
-			       STATX_BASIC_STATS, &tmp);
-  if (r == 0)
-    __cp_stat64_statx (buf, &tmp);
-  return r;
+  int r = __statx_empty_path (fd, 0, &tmp);
+  if (INTERNAL_SYSCALL_ERROR_P (r))
+    return INLINE_SYSCALL_ERROR_RETURN_VALUE (-r);
+  __cp_stat64_statx (buf, &tmp);
+  return 0;
 # endif
 #else
   /* All kABIs with non-LFS support, e.g. arm, csky, i386, hppa, m68k,
sysdeps/unix/sysv/linux/internal-stat.h +35 -0
index 9334059765..36920b4d11 100644
@@ -29,3 +29,38 @@
 #else
 # define FSTATAT_USE_STATX 0
 #endif
+
+#if FSTATAT_USE_STATX || XSTAT_IS_XSTAT64
+
+/* buf MUST be a valid buffer, or the feature detection may not work properly */
+
+static inline int
+__statx_empty_path (int fd, int flag, struct statx *buf)
+{
+  flag |= AT_EMPTY_PATH;
+#ifdef __ASSUME_STATX_NULL_PATH
+  return INTERNAL_SYSCALL_CALL (statx, fd, NULL, flag, STATX_BASIC_STATS, buf);
+#else
+  static int statx_null_path_supported = -1;
+  int r;
+  int supported = statx_null_path_supported;
+  if (__glibc_unlikely (supported == -1))
+    supported = atomic_load_relaxed (&statx_null_path_supported);
+  if (supported != 0)
+    {
+      r = INTERNAL_SYSCALL_CALL (statx, fd, NULL, flag, STATX_BASIC_STATS, buf);
+      if (__glibc_likely (supported == 1))
+	return r;
+      if (r != -EFAULT)
+	{
+	  if (r == 0)
+	    atomic_store_relaxed (&statx_null_path_supported, 1);
+	  return r;
+	}
+      atomic_store_relaxed (&statx_null_path_supported, 0);
+    }
+  return INTERNAL_SYSCALL_CALL (statx, fd, "", flag, STATX_BASIC_STATS, buf);
+#endif
+}
+
+#endif
sysdeps/unix/sysv/linux/kernel-features.h +5 -0
index a25cf07e9f..78aaf43a82 100644
@@ -257,4 +257,9 @@
 # define __ASSUME_FCHMODAT2 0
 #endif
 
+/* statx(fd, NULL, AT_EMPTY_PATH) was introduced in Linux 6.11. */
+#if __LINUX_KERNEL_VERSION >= 0x060b00
+# define __ASSUME_STATX_NULL_PATH 1
+#endif
+
 #endif /* kernel-features.h */

---
base-commit: 2eee835eca960c9d4119279804214b7a1ed5d156
change-id: 20240821-statx-null-path-531c0775bba4

Best regards,

Signature · 1 line
Miao Wang <[email protected]>