From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD6E627A916 for ; Fri, 1 May 2026 08:18:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777623518; cv=none; b=Rw9TUeZcZGPk3HNlowBDPhRwfkrO+fu8RgHSThViaKE2t0DeYBOqYYNxGBX6BCevkvovHqiVcLAtvikd+pQDihiN6ANUu8G2Y3eJiH8MxlDFZCVEDJuoy9/jIyoRE6U54BcYOQrFdxNBHAzzAHbdxnWmgwhwrFhnZrYtRqh6FHA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777623518; c=relaxed/simple; bh=0VtjBMu8lr0BjP7/tgs/4kdA7bkXSDSgU1zOVh6GYqY=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fxlmOVxPK18Y8doUShq5w5tdZDXz9FFDc16sk/TE9MS/yjr8UhJY8z8ZWf0Qab0WxUGDSn1jWUKmw0xPYNC0APUL8zPhNUsSuPg8B8Pwpmd7hE+Xqpkc3Ieqh0OczyxeFlr90BmnaSyMusfsJNeNy08TA/4xZzDVafCs2LbuO3c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FgN18anQ; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FgN18anQ" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-44a044cb827so781027f8f.0 for ; Fri, 01 May 2026 01:18:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777623515; x=1778228315; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=xACPUCIO7tgRw7l9BL/XQNa/yvzC6v2aLXOzjPTEF1U=; b=FgN18anQohJ4/QG80AGpZOw2IPyCbBpsJ4sYaXPevGGinZDhbAsvQ7B+NC9xrkCkki HIax+36UgWHM71rqla5OJwNUbUNj78GzdmpO8NnxdKN3ju++YRSM6lKiqKHPJIFTQZY9 M8FISYXpkG4eWVJR8KFIhWZQ/j6fcZAb2BYiqITzBUhosfNo+3zMlCB4m3CRuRINA5Ba BKfpPYIVxvOxdTvgn8EoSFEOGsenYMFndzMj41ueUVVM+KnE94+vd2tSBG8nEhRdZJjj JK4FRXP61Sce5yOnAcwDtKyOJ5+k5rHx4+wHW71dLT2BEGFu3TYqanvi8m6I4D+kbOBD b/Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777623515; x=1778228315; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xACPUCIO7tgRw7l9BL/XQNa/yvzC6v2aLXOzjPTEF1U=; b=PzqnudQmD45gQuzqhVHsoItL+ZINpgzewtT6olGuCf+fq6zL67ZSmDu6yXurKi7X/8 uoUSxaG2MlR6eQ7MWxYZ6i0SMaYwKSCe+vPxNBmUQnPV3quECzgNKqQjq8adne8jINDx idC6l2hUVHUzvSlVvYbEJw/oL9pqhuH6tIYWsNBktkC3+QdO0NzLIJ94IQxICUb7LbYV /Cn9BvdqYNkE2tFO06WomlkJESpLnAn962ZjTfYhxcByDkUuYxCTndzj05uOx2X93c1J /DSsPamL8W0OoDb/WqdXlPIeFJVfiNmLTJe71LK8SaLkIntGutrRlG/vrmOPKyzbrBp0 6/pA== X-Forwarded-Encrypted: i=1; AFNElJ83U7LT6sYmNI60jtdxZdAqDM7dNOZeIYPYfOjKj7vBj1W5hUjwitbSyHsbrY7+yntemVbjSMVq+1xYazw=@vger.kernel.org X-Gm-Message-State: AOJu0YzhC8JSisa52coa0xKwb8gd/5+2teYxm+nZxeCcFdeZ95JE+K5L +onyIO2KihyeAT9Th4nJe32fvWeS/0cyEtpDa1hzU1+ksgZ8V0GnPtYrt1CXtBSD X-Gm-Gg: AeBDieue2C4pDrVC8dressdWIHXRi8h7zYqBmQSH7222H23kSQ+27GfE0hbMjIRo0t6 vQ0+Jczn0k6ren+jJ/Pj+HwGkTSGxaXl0DxlwOAshCimnyDYNMtzsfbjbPokt1hWCwR++hx3q/l n4TUXU8BQOHMOx8/Eym++mi177vu+kTbytelDE+rreqrwV9Xr00vS1opMUbgSULxAfsTQqGNmvN hwjc26aZyc1+sYumAP5VGSL97mfm9lJQoGau0S7S82sQ3lhBA/iOh/XyLBwd1xHmOotmmNxeP7Q IGV56Vx6fWShQliXB4h7Am5wsS9nwjzPJCuqskRyDqOeZ7jI0vko7iYpjU841Td/UUhg8cTecFH 5T+7WUM1Uh1J8Z3YYextKXbk6+QOgO+0QMKSnsCZMhBHIkPy4DwlgVpwBWcx2a6MzngNFRWTsvx mfP+qV5hO7fUCOpnIU6OiCO2tVTqjOiECuf32H8y2JjQrW9blwHCA7Aw8asB+Huw/iCoGBor4q1 k4= X-Received: by 2002:a05:6000:1445:b0:43d:309b:9c4f with SMTP id ffacd0b85a97d-4493cb42dfbmr11342244f8f.6.1777623514825; Fri, 01 May 2026 01:18:34 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-44a8ea7cf97sm3660200f8f.6.2026.05.01.01.18.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 May 2026 01:18:34 -0700 (PDT) Date: Fri, 1 May 2026 09:18:31 +0100 From: David Laight To: Daniel Palmer Cc: w@1wt.eu, linux@weissschuh.net, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 1/2] tools/nolibc: fcntl: Add fallocate() Message-ID: <20260501091831.7abc39f1@pumpkin> In-Reply-To: <20260430164125.1106350-2-daniel@thingy.jp> References: <20260430164125.1106350-1-daniel@thingy.jp> <20260430164125.1106350-2-daniel@thingy.jp> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 1 May 2026 01:41:24 +0900 Daniel Palmer wrote: > Add fallocate(). > > Some special care is needed to put the offset and size > into the syscall parameters for 32bit machines, x32, > and mipsn32. > > For x32 we can just check if the kernel long size is the > same as off_t and use the same path as x86_64. > > For mipsn32 we override the generic version and provide > one that does the right thing. > > Signed-off-by: Daniel Palmer > --- > tools/include/nolibc/arch-mips.h | 11 +++++++++++ > tools/include/nolibc/fcntl.h | 33 ++++++++++++++++++++++++++++++++ > tools/include/nolibc/sys.h | 8 ++++++++ > 3 files changed, 52 insertions(+) > > diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h > index 1400653c76c1..e4e42f2bcaf4 100644 > --- a/tools/include/nolibc/arch-mips.h > +++ b/tools/include/nolibc/arch-mips.h > @@ -6,6 +6,7 @@ > > #ifndef _NOLIBC_ARCH_MIPS_H > #define _NOLIBC_ARCH_MIPS_H > +#include > > #include "compiler.h" > #include "crt.h" > @@ -256,6 +257,16 @@ > _arg4 ? -_num : _num; \ > }) > > +/* The generic version of this will split offset and size for _ABIN32, > + * override it and do the right thing here. > + */ > +static __attribute__((unused)) > +int _sys_fallocate(int fd, int mode, off_t offset, off_t size) > +{ > + return __nolibc_syscall4(__NR_fallocate, fd, mode, offset, size); > +} > +#define _sys_fallocate _sys_fallocate > + > #endif /* _ABIO32 */ > > #ifndef NOLIBC_NO_RUNTIME > diff --git a/tools/include/nolibc/fcntl.h b/tools/include/nolibc/fcntl.h > index 014910a8e928..dbc99188a49e 100644 > --- a/tools/include/nolibc/fcntl.h > +++ b/tools/include/nolibc/fcntl.h > @@ -14,6 +14,9 @@ > #include "types.h" > #include "sys.h" > > +/* For fallocate() modes */ > +#include > + > /* > * int openat(int dirfd, const char *path, int flags[, mode_t mode]); > */ > @@ -80,4 +83,34 @@ int creat(const char *path, mode_t mode) > return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode); > } > > +/* > + * int fallocate(int fd, int mode, off_t offset, off_t size); > + */ > + > +#if !defined(_sys_fallocate) > +static __attribute__((unused)) > +int _sys_fallocate(int fd, int mode, off_t offset, off_t size) > +{ > + /* > + * For 32 bit machines __kernel_long_t will be 4, off_t will be 8 > + * and we need to split offset and size, for 64 machines we can use > + * the values as-is. > + */ > + const bool offsetsz_two_args = sizeof(__kernel_long_t) != sizeof(off_t); I don't think you care about the size of off_t. Were it to be 4 the code would be badly wrong. > + > + if (offsetsz_two_args) > + return __nolibc_syscall6(__NR_fallocate, fd, mode, > + __NOLIBC_LLARGPART(offset, 0), __NOLIBC_LLARGPART(offset, 1), > + __NOLIBC_LLARGPART(size, 0), __NOLIBC_LLARGPART(size, 1)); > + else > + return __nolibc_syscall4(__NR_fallocate, fd, mode, offset, size); > +} The above might be more readable as: if (sizeof(__kernel_long_t) == 8) /* 64 bit, values fit in single arguments */ return __nolibc_syscall4(__NR_fallocate, fd, mode, offset, size); /* 32 bit, values need splitting, order depends on endianness */ /* This test for endianness doesn't rely on any pre-processor defines */ if (({union {int x; char c;} u; u.x = 1; u.c;})) /* Little endian */ return __nolibc_syscall6(__NR_fallocate, fd, mode, offset, offset >> 32, size, size >> 32); /* Big endian */ return __nolibc_syscall6(__NR_fallocate, fd, mode, offset >> 32, offset, size >> 32, size); Although you might want to change/factor out the endianness test. -- David > +#endif > + > +static __attribute__((unused)) > +int fallocate(int fd, int mode, off_t offset, off_t size) > +{ > + return __sysret(_sys_fallocate(fd, mode, offset, size)); > +} > + > #endif /* _NOLIBC_FCNTL_H */ > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h > index 33f9c970ae57..b7136a3a7d6a 100644 > --- a/tools/include/nolibc/sys.h > +++ b/tools/include/nolibc/sys.h > @@ -29,6 +29,14 @@ > #include "stdarg.h" > #include "types.h" > > +/* > + * Helper for 32bit machines where a 64bit syscall arg needs to be split into > + * two 32bit parts while making sure the order of the low/high parts are correct > + * for the endian: > + * __NOLIBC_LLARGPART(x, 0), __NOLIBC_LLARGPART(x, 1) > + */ > +#define __NOLIBC_LLARGPART(_arg, _part) \ > + (((union { long long ll; long l[2]; }) { .ll = _arg }).l[_part]) > > /* Syscall return helper: takes the syscall value in argument and checks for an > * error in it. This may only be used with signed returns (int or long), but