The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/3] tools/nolibc: add support for ftruncate()
@ 2026-05-21 17:31 Thomas Weißschuh
  2026-05-21 17:31 ` [PATCH 1/3] selftests/nolibc: enable CONFIG_TMPFS for sparc32 Thomas Weißschuh
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Thomas Weißschuh @ 2026-05-21 17:31 UTC (permalink / raw)
  To: Willy Tarreau
  Cc: David Laight, linux-kernel, Daniel Palmer, Jordan Richards,
	Thomas Weißschuh

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Daniel Palmer (1):
      tools/nolibc: add a helper to split a 64-bit argument into 32-bit halves

Thomas Weißschuh (2):
      selftests/nolibc: enable CONFIG_TMPFS for sparc32
      tools/nolibc: add ftruncate()

 tools/include/nolibc/arch-arm.h                | 10 +++++
 tools/include/nolibc/arch-mips.h               | 12 ++++++
 tools/include/nolibc/arch-powerpc.h            | 12 ++++++
 tools/include/nolibc/sys.h                     | 11 ++++++
 tools/include/nolibc/unistd.h                  | 24 ++++++++++++
 tools/testing/selftests/nolibc/Makefile.nolibc |  1 +
 tools/testing/selftests/nolibc/nolibc-test.c   | 52 ++++++++++++++++++++++++++
 7 files changed, 122 insertions(+)
---
base-commit: 136ca91411b0b637e862eb7b1cce2a56853edd17
change-id: 20260318-nolibc-ftruncate-063a0ee7e2bf

Best regards,
--  
Thomas Weißschuh <linux@weissschuh.net>


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

* [PATCH 1/3] selftests/nolibc: enable CONFIG_TMPFS for sparc32
  2026-05-21 17:31 [PATCH 0/3] tools/nolibc: add support for ftruncate() Thomas Weißschuh
@ 2026-05-21 17:31 ` Thomas Weißschuh
  2026-05-21 17:31 ` [PATCH 2/3] tools/nolibc: add a helper to split a 64-bit argument into 32-bit halves Thomas Weißschuh
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Thomas Weißschuh @ 2026-05-21 17:31 UTC (permalink / raw)
  To: Willy Tarreau
  Cc: David Laight, linux-kernel, Daniel Palmer, Jordan Richards,
	Thomas Weißschuh

An upcoming selftest will use memfd_create() which require tmpfs.

Enable that.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/testing/selftests/nolibc/Makefile.nolibc | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/testing/selftests/nolibc/Makefile.nolibc b/tools/testing/selftests/nolibc/Makefile.nolibc
index c6dcd54078f2..06f881e2e90c 100644
--- a/tools/testing/selftests/nolibc/Makefile.nolibc
+++ b/tools/testing/selftests/nolibc/Makefile.nolibc
@@ -109,6 +109,7 @@ DEFCONFIG            = $(or $(DEFCONFIG_$(XARCH)),defconfig)
 EXTRACONFIG_x32       = -e CONFIG_X86_X32_ABI
 EXTRACONFIG_arm       = -e CONFIG_NAMESPACES
 EXTRACONFIG_armthumb  = -e CONFIG_NAMESPACES
+EXTRACONFIG_sparc32   = -e CONFIG_TMPFS
 EXTRACONFIG_m68k      = -e CONFIG_BLK_DEV_INITRD
 EXTRACONFIG_sh4       = -e CONFIG_BLK_DEV_INITRD -e CONFIG_CMDLINE_FROM_BOOTLOADER
 EXTRACONFIG           = $(EXTRACONFIG_$(XARCH))

-- 
2.54.0


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

* [PATCH 2/3] tools/nolibc: add a helper to split a 64-bit argument into 32-bit halves
  2026-05-21 17:31 [PATCH 0/3] tools/nolibc: add support for ftruncate() Thomas Weißschuh
  2026-05-21 17:31 ` [PATCH 1/3] selftests/nolibc: enable CONFIG_TMPFS for sparc32 Thomas Weißschuh
@ 2026-05-21 17:31 ` Thomas Weißschuh
  2026-05-21 17:31 ` [PATCH 3/3] tools/nolibc: add ftruncate() Thomas Weißschuh
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Thomas Weißschuh @ 2026-05-21 17:31 UTC (permalink / raw)
  To: Willy Tarreau
  Cc: David Laight, linux-kernel, Daniel Palmer, Jordan Richards,
	Thomas Weißschuh

From: Daniel Palmer <daniel@thingy.jp>

On 32-bit architectures some system calls require a single 64-bit
argument to be passed as two 32-bit halves.

Add a helper to easily split such arguments. This works on little and
bit endian.

Signed-off-by: Daniel Palmer <daniel@thingy.jp>
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

---
Lifted from https://lore.kernel.org/lkml/20260507090353.356764-2-daniel@thingy.jp/
---
 tools/include/nolibc/sys.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 33f9c970ae57..548f94d96ed2 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -70,6 +70,17 @@ static __inline__ int __nolibc_enosys(const char *syscall, ...)
 }
 #endif
 
+
+/*
+ * Helper for 32-bit machines where a 64-bit syscall arg needs to be split into
+ * two 32-bit parts while making sure the order of the low/high parts are correct
+ * for the endianness:
+ * __NOLIBC_LLARGPART(x, 0), __NOLIBC_LLARGPART(x, 1)
+ */
+#define __NOLIBC_LLARGPART(_arg, _part) \
+	(((union { long long ll; long l[2]; }) { .ll = _arg }).l[_part])
+
+
 /* Functions in this file only describe syscalls. They're declared static so
  * that the compiler usually decides to inline them while still being allowed
  * to pass a pointer to one of their instances. Each syscall exists in two

-- 
2.54.0


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

* [PATCH 3/3] tools/nolibc: add ftruncate()
  2026-05-21 17:31 [PATCH 0/3] tools/nolibc: add support for ftruncate() Thomas Weißschuh
  2026-05-21 17:31 ` [PATCH 1/3] selftests/nolibc: enable CONFIG_TMPFS for sparc32 Thomas Weißschuh
  2026-05-21 17:31 ` [PATCH 2/3] tools/nolibc: add a helper to split a 64-bit argument into 32-bit halves Thomas Weißschuh
@ 2026-05-21 17:31 ` Thomas Weißschuh
  2026-05-22  3:41 ` [PATCH 0/3] tools/nolibc: add support for ftruncate() Willy Tarreau
  2026-05-22  8:48 ` Daniel Palmer
  4 siblings, 0 replies; 8+ messages in thread
From: Thomas Weißschuh @ 2026-05-21 17:31 UTC (permalink / raw)
  To: Willy Tarreau
  Cc: David Laight, linux-kernel, Daniel Palmer, Jordan Richards,
	Thomas Weißschuh

On architectures with 32-bit longs, call the compat syscall
__NR_ftruncate64. As off_t is 64-bit it must be split into 2 registers.
Unlike llseek() which passes the high and low parts in explicitly named
arguments, the order here is endian independent.

Some architectures (arm, mips, ppc) require this pair of registers to
be aligned to an even register, so add custom _sys_ftruncate64()
wrappers for those.

A test case for ftruncate is added which validates negative length or
invalid fd return the appropriate error, and checks the length is
correct on success.

Co-developed-by: Jordan Richards <jordanrichards@google.com>
Signed-off-by: Jordan Richards <jordanrichards@google.com>
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

---
Lifted from https://lore.kernel.org/lkml/20260303010039.2969125-1-jordanrichards@google.com/
---
 tools/include/nolibc/arch-arm.h              | 10 ++++++
 tools/include/nolibc/arch-mips.h             | 12 +++++++
 tools/include/nolibc/arch-powerpc.h          | 12 +++++++
 tools/include/nolibc/unistd.h                | 24 +++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c | 52 ++++++++++++++++++++++++++++
 5 files changed, 110 insertions(+)

diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-arm.h
index 72a2b28170e2..8681922e05ca 100644
--- a/tools/include/nolibc/arch-arm.h
+++ b/tools/include/nolibc/arch-arm.h
@@ -7,8 +7,11 @@
 #ifndef _NOLIBC_ARCH_ARM_H
 #define _NOLIBC_ARCH_ARM_H
 
+#include <linux/unistd.h>
+
 #include "compiler.h"
 #include "crt.h"
+#include "std.h"
 
 /* Syscalls for ARM in ARM or Thumb modes :
  *   - registers are 32-bit
@@ -196,4 +199,11 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __nolibc_no_stack_prote
 }
 #endif /* NOLIBC_NO_RUNTIME */
 
+static __attribute__((unused))
+int _sys_ftruncate64(int fd, uint32_t length0, uint32_t length1)
+{
+	return __nolibc_syscall4(__NR_ftruncate64, fd, 0, length0, length1);
+}
+#define _sys_ftruncate64 _sys_ftruncate64
+
 #endif /* _NOLIBC_ARCH_ARM_H */
diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h
index 1400653c76c1..26ad413cec62 100644
--- a/tools/include/nolibc/arch-mips.h
+++ b/tools/include/nolibc/arch-mips.h
@@ -7,8 +7,11 @@
 #ifndef _NOLIBC_ARCH_MIPS_H
 #define _NOLIBC_ARCH_MIPS_H
 
+#include <linux/unistd.h>
+
 #include "compiler.h"
 #include "crt.h"
+#include "std.h"
 
 #if !defined(_ABIO32) && !defined(_ABIN32) && !defined(_ABI64)
 #error Unsupported MIPS ABI
@@ -282,4 +285,13 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __nolibc_no_stack_prote
 }
 #endif /* NOLIBC_NO_RUNTIME */
 
+#if defined(_ABIO32)
+static __attribute__((unused))
+int _sys_ftruncate64(int fd, uint32_t length0, uint32_t length1)
+{
+	return __nolibc_syscall4(__NR_ftruncate64, fd, 0, length0, length1);
+}
+#define _sys_ftruncate64 _sys_ftruncate64
+#endif
+
 #endif /* _NOLIBC_ARCH_MIPS_H */
diff --git a/tools/include/nolibc/arch-powerpc.h b/tools/include/nolibc/arch-powerpc.h
index 111cda70f2cc..a1ab91d55384 100644
--- a/tools/include/nolibc/arch-powerpc.h
+++ b/tools/include/nolibc/arch-powerpc.h
@@ -7,8 +7,11 @@
 #ifndef _NOLIBC_ARCH_POWERPC_H
 #define _NOLIBC_ARCH_POWERPC_H
 
+#include <linux/unistd.h>
+
 #include "compiler.h"
 #include "crt.h"
+#include "std.h"
 
 /* Syscalls for PowerPC :
  *   - stack is 16-byte aligned
@@ -218,4 +221,13 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __nolibc_no_stack_prote
 }
 #endif /* NOLIBC_NO_RUNTIME */
 
+#if !defined(__powerpc64__)
+static __attribute__((unused))
+int _sys_ftruncate64(int fd, uint32_t length0, uint32_t length1)
+{
+	return __nolibc_syscall4(__NR_ftruncate64, fd, 0, length0, length1);
+}
+#define _sys_ftruncate64 _sys_ftruncate64
+#endif
+
 #endif /* _NOLIBC_ARCH_POWERPC_H */
diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h
index 5882a6862066..79599ceef45d 100644
--- a/tools/include/nolibc/unistd.h
+++ b/tools/include/nolibc/unistd.h
@@ -48,6 +48,30 @@ int access(const char *path, int amode)
 	return faccessat(AT_FDCWD, path, amode, 0);
 }
 
+#if !defined(_sys_ftruncate64) && defined(__NR_ftruncate64)
+static __attribute__((unused))
+int _sys_ftruncate64(int fd, uint32_t length0, uint32_t length1)
+{
+	return __nolibc_syscall3(__NR_ftruncate64, fd, length0, length1);
+}
+#define _sys_ftruncate64 _sys_ftruncate64
+#endif
+
+static __attribute__((unused))
+int _sys_ftruncate(int fd, off_t length)
+{
+#if defined(_sys_ftruncate64)
+	return _sys_ftruncate64(fd, __NOLIBC_LLARGPART(length, 0), __NOLIBC_LLARGPART(length, 1));
+#else
+	return __nolibc_syscall2(__NR_ftruncate, fd, length);
+#endif
+}
+
+static __attribute__((unused))
+int ftruncate(int fd, off_t length)
+{
+	return __sysret(_sys_ftruncate(fd, length));
+}
 
 static __attribute__((unused))
 int msleep(unsigned int msecs)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index c3867cc570c6..7c3b711c125c 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1017,6 +1017,57 @@ int test_fork(enum fork_type type)
 	}
 }
 
+int test_ftruncate(void)
+{
+	struct stat stat_buf;
+	int ret, fd;
+
+	ret = ftruncate(-1, 0);
+	if (ret != -1 || errno != EBADF) {
+		errno = EINVAL;
+		return __LINE__;
+	}
+
+	fd = memfd_create(__func__, 0);
+	if (fd == -1)
+		return __LINE__;
+
+	/*
+	 * This also tests that the high 32-bit half is passed through correctly.
+	 * If it gets lost, the kernel will see a positive number and not fail.
+	 */
+	ret = ftruncate(fd, -1);
+	if (!(ret == -1 && errno == EINVAL)) {
+		if (ret == 0)
+			errno = EINVAL;
+		ret = __LINE__;
+		goto end;
+	}
+
+	ret = ftruncate(fd, 42);
+	if (ret != 0) {
+		ret = __LINE__;
+		goto end;
+	}
+
+	ret = fstat(fd, &stat_buf);
+	if (ret != 0) {
+		ret = __LINE__;
+		goto end;
+	}
+
+	if (stat_buf.st_size != 42) {
+		errno = EINVAL;
+		ret = __LINE__;
+		goto end;
+	}
+
+end:
+	close(fd);
+
+	return ret;
+}
+
 int test_stat_timestamps(void)
 {
 	struct stat st;
@@ -1539,6 +1590,7 @@ int run_syscall(int min, int max)
 		CASE_TEST(file_stream);       EXPECT_SYSZR(1, test_file_stream()); break;
 		CASE_TEST(file_stream_wsr);   EXPECT_SYSZR(1, test_file_stream_wsr()); break;
 		CASE_TEST(fork);              EXPECT_SYSZR(1, test_fork(FORK_STANDARD)); break;
+		CASE_TEST(ftruncate);         EXPECT_SYSZR(1, test_ftruncate()); break;
 		CASE_TEST(getdents64_root);   EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
 		CASE_TEST(getdents64_null);   EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
 		CASE_TEST(directories);       EXPECT_SYSZR(is_nolibc && proc, test_dirent()); break;

-- 
2.54.0


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

* Re: [PATCH 0/3] tools/nolibc: add support for ftruncate()
  2026-05-21 17:31 [PATCH 0/3] tools/nolibc: add support for ftruncate() Thomas Weißschuh
                   ` (2 preceding siblings ...)
  2026-05-21 17:31 ` [PATCH 3/3] tools/nolibc: add ftruncate() Thomas Weißschuh
@ 2026-05-22  3:41 ` Willy Tarreau
  2026-05-22  8:48 ` Daniel Palmer
  4 siblings, 0 replies; 8+ messages in thread
From: Willy Tarreau @ 2026-05-22  3:41 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: David Laight, linux-kernel, Daniel Palmer, Jordan Richards

Hi Thomas!

On Thu, May 21, 2026 at 07:31:01PM +0200, Thomas Weißschuh wrote:
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Looks good, thanks for putting all this work together.

Acked-by: Willy Tarreau <w@1wt.eu>

willy

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

* Re: [PATCH 0/3] tools/nolibc: add support for ftruncate()
  2026-05-21 17:31 [PATCH 0/3] tools/nolibc: add support for ftruncate() Thomas Weißschuh
                   ` (3 preceding siblings ...)
  2026-05-22  3:41 ` [PATCH 0/3] tools/nolibc: add support for ftruncate() Willy Tarreau
@ 2026-05-22  8:48 ` Daniel Palmer
  2026-05-22 14:41   ` Thomas Weißschuh
  4 siblings, 1 reply; 8+ messages in thread
From: Daniel Palmer @ 2026-05-22  8:48 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Willy Tarreau, David Laight, linux-kernel, Jordan Richards

Hi Thomas,

On Fri, 22 May 2026 at 02:31, Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> Daniel Palmer (1):
>       tools/nolibc: add a helper to split a 64-bit argument into 32-bit halves
>
> Thomas Weißschuh (2):
>       selftests/nolibc: enable CONFIG_TMPFS for sparc32
>       tools/nolibc: add ftruncate()
>
>  tools/include/nolibc/arch-arm.h                | 10 +++++
>  tools/include/nolibc/arch-mips.h               | 12 ++++++
>  tools/include/nolibc/arch-powerpc.h            | 12 ++++++
>  tools/include/nolibc/sys.h                     | 11 ++++++
>  tools/include/nolibc/unistd.h                  | 24 ++++++++++++
>  tools/testing/selftests/nolibc/Makefile.nolibc |  1 +
>  tools/testing/selftests/nolibc/nolibc-test.c   | 52 ++++++++++++++++++++++++++
>  7 files changed, 122 insertions(+)
> ---
> base-commit: 136ca91411b0b637e862eb7b1cce2a56853edd17
> change-id: 20260318-nolibc-ftruncate-063a0ee7e2bf
>
> Best regards,
> --
> Thomas Weißschuh <linux@weissschuh.net>
>

This looks good to me. I'll rebase my fallocate() stuff on this.

FWIW:

Acked-by: Daniel Palmer <daniel@thingy.jp>

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

* Re: [PATCH 0/3] tools/nolibc: add support for ftruncate()
  2026-05-22  8:48 ` Daniel Palmer
@ 2026-05-22 14:41   ` Thomas Weißschuh
  2026-05-22 22:12     ` Daniel Palmer
  0 siblings, 1 reply; 8+ messages in thread
From: Thomas Weißschuh @ 2026-05-22 14:41 UTC (permalink / raw)
  To: Daniel Palmer; +Cc: Willy Tarreau, David Laight, linux-kernel, Jordan Richards

On 2026-05-22 17:48:49+0900, Daniel Palmer wrote:
> On Fri, 22 May 2026 at 02:31, Thomas Weißschuh <linux@weissschuh.net> wrote:
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > ---
> > Daniel Palmer (1):
> >       tools/nolibc: add a helper to split a 64-bit argument into 32-bit halves
> >
> > Thomas Weißschuh (2):
> >       selftests/nolibc: enable CONFIG_TMPFS for sparc32
> >       tools/nolibc: add ftruncate()
> >
> >  tools/include/nolibc/arch-arm.h                | 10 +++++
> >  tools/include/nolibc/arch-mips.h               | 12 ++++++
> >  tools/include/nolibc/arch-powerpc.h            | 12 ++++++
> >  tools/include/nolibc/sys.h                     | 11 ++++++
> >  tools/include/nolibc/unistd.h                  | 24 ++++++++++++
> >  tools/testing/selftests/nolibc/Makefile.nolibc |  1 +
> >  tools/testing/selftests/nolibc/nolibc-test.c   | 52 ++++++++++++++++++++++++++
> >  7 files changed, 122 insertions(+)
> > ---
> > base-commit: 136ca91411b0b637e862eb7b1cce2a56853edd17
> > change-id: 20260318-nolibc-ftruncate-063a0ee7e2bf
> >
> > Best regards,
> > --
> > Thomas Weißschuh <linux@weissschuh.net>
> >
> 
> This looks good to me. I'll rebase my fallocate() stuff on this.
> 
> FWIW:
> 
> Acked-by: Daniel Palmer <daniel@thingy.jp>

Thanks!

Acked-by is for maintainers to use on changes within their subsystems.
I'll make this a Reviewed-by instead. Yell at me if you object.


Thomas

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

* Re: [PATCH 0/3] tools/nolibc: add support for ftruncate()
  2026-05-22 14:41   ` Thomas Weißschuh
@ 2026-05-22 22:12     ` Daniel Palmer
  0 siblings, 0 replies; 8+ messages in thread
From: Daniel Palmer @ 2026-05-22 22:12 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Willy Tarreau, David Laight, linux-kernel, Jordan Richards

Hi Thomas,

On Fri, 22 May 2026 at 23:41, Thomas Weißschuh <linux@weissschuh.net> wrote:

> > Acked-by: Daniel Palmer <daniel@thingy.jp>
>
> Thanks!
>
> Acked-by is for maintainers to use on changes within their subsystems.
> I'll make this a Reviewed-by instead. Yell at me if you object.

Today I learned. :) I thought Acked-by was like a less strong Reviewed-by.
Anyhow I looked through, Ran the tests again with my fallocate()
rebased on top and it works:

Reviewed-by: Daniel Palmer <daniel@thingy.jp>

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

end of thread, other threads:[~2026-05-22 22:12 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-21 17:31 [PATCH 0/3] tools/nolibc: add support for ftruncate() Thomas Weißschuh
2026-05-21 17:31 ` [PATCH 1/3] selftests/nolibc: enable CONFIG_TMPFS for sparc32 Thomas Weißschuh
2026-05-21 17:31 ` [PATCH 2/3] tools/nolibc: add a helper to split a 64-bit argument into 32-bit halves Thomas Weißschuh
2026-05-21 17:31 ` [PATCH 3/3] tools/nolibc: add ftruncate() Thomas Weißschuh
2026-05-22  3:41 ` [PATCH 0/3] tools/nolibc: add support for ftruncate() Willy Tarreau
2026-05-22  8:48 ` Daniel Palmer
2026-05-22 14:41   ` Thomas Weißschuh
2026-05-22 22:12     ` Daniel Palmer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox