Message ID | 2f5c3338898da65210ad3f62d7b7773a96f6d251.1685387484.git.falcon@tinylab.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1737827vqr; Mon, 29 May 2023 13:00:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6hgYgCljPyy/ZsiybWrxVtj3FbPGkpeYbOAA9Sxq9eSeFkpnJhkzjmv6Jb0BbYJlEYNqEQ X-Received: by 2002:a17:902:e548:b0:1b0:fed:5cbe with SMTP id n8-20020a170902e54800b001b00fed5cbemr290175plf.30.1685390417132; Mon, 29 May 2023 13:00:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685390417; cv=none; d=google.com; s=arc-20160816; b=QumhfMgjMN7s4jJ7n3QxVlY2FGh4Ws1VfC0jyy0Alhi1TPpdGP/SHId04EuVDccBaD N2IkTucqb/mxGr1L6fCnohOCboiIbfzYnxUlmOwE4ggwur47ns5ydy3kvqJpMnO7GS26 nFK4/JD7HQcg9Da0d4vjD6spV/tKOgJdj8tEHTFeZyfUVnZ/fg6nY8UMUF03DVTHENQH bO4WsDGWjzQ3XS3K4lFTKzbfGTPgZwyZfhWECCg52oCQ1uxVB5CSKBzsiGADPXWRjI1Y 6P1OvyIPhuvoc/3f8/ZxqmgTz9mDAOh6oWjNmJvTjy3ZhJrGdNsnE1OGIDMvY/OdGZ5z 8IYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=sx0EbduvWhbezU6TciToFvF89MJJ/Tzy3kJoGGSAEAI=; b=fZrVhdVj8EN7w7hEShF+i0a/x2Ud5WsPedO65QcgTAG+5cqU4YfN7x7RT6GI68fQBd a2G5xzPEZ5UAXRL679WehafRRs7HQuehKN1Aei4R8Vgx3rDGSPfQqOnJl6LVme0/DSre 0MgohlGcHD9exKF6DpmxIEDFc8FvT4f/SDbYS1OgEVu9Vb2pPuYSYEJDr7XLaFj1LlXk bObZRoAVI6bRSYj8qowOETKBEPME6NyaX4Qd0HUalVPcP6uRHYtMhj7zCRPFiQZmRgzR fZ3V2mWYJwIN/okGuRMeZCWFNzESkBXOy0ji66/Z+C1xrzip3HZzro6OR6J07n2Zpm/V 2FDA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y13-20020a17090322cd00b001aadd1e0384si9955320plg.192.2023.05.29.13.00.04; Mon, 29 May 2023 13:00:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229802AbjE2TzJ (ORCPT <rfc822;callmefire3@gmail.com> + 99 others); Mon, 29 May 2023 15:55:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229621AbjE2TzH (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 29 May 2023 15:55:07 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.221.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 615CCF4; Mon, 29 May 2023 12:55:03 -0700 (PDT) X-QQ-mid: bizesmtp74t1685390094tpc5r986 Received: from linux-lab-host.localdomain ( [119.123.130.80]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 30 May 2023 03:54:53 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: uGhnJwy6xZISo/AEbiO2R7D9Bkp1qAC1x25dTn245rn0AypFebeWwalodNjhE EL0Hxl2tTtRKwyjA9hWn6rJEMWZwJ13M7sTzYmfFM1VAENq33hSqeuCTSPxUjmRP8In5z0j jE3PPH0La2QBf3gtfL4qavbfPRbU9RB/PQuJibgW2/x+jjWS9xtIWSHbLtt65NMZEKdLaN8 dkZK4Ia/D7JyiFc+a1Kz4je5B5SnuVU37OtBIhZa6tgmgd8wfgtSuev0V/BKN+5P6bH/hRU 5o3Ae2l3mwfoTcHHys8UBybwBk03+OCAL3pFbxALn8Bt+8SyD8qmmeKLiKKhfao3WsglZUh Asti4FqiHE3VbrkVNKSBjtA3A/Az2xVvvEmvGiv9bla+QGTOev9aF8pmMHpuQ== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 5796279202276383086 From: Zhangjin Wu <falcon@tinylab.org> To: w@1wt.eu Cc: arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH v2 07/13] tools/nolibc: sys_lseek: add pure 64bit lseek Date: Tue, 30 May 2023 03:54:23 +0800 Message-Id: <2f5c3338898da65210ad3f62d7b7773a96f6d251.1685387484.git.falcon@tinylab.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <cover.1685387484.git.falcon@tinylab.org> References: <cover.1685387484.git.falcon@tinylab.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767259941901444979?= X-GMAIL-MSGID: =?utf-8?q?1767259941901444979?= |
Series |
nolibc: add part2 of support for rv32
|
|
Commit Message
Zhangjin Wu
May 29, 2023, 7:54 p.m. UTC
use sys_llseek instead of sys_lseek to add 64bit seek even in 32bit platforms. This code is based on sysdeps/unix/sysv/linux/lseek.c of glibc and src/unistd/lseek.c of musl. Signed-off-by: Zhangjin Wu <falcon@tinylab.org> Signed-off-by: Willy Tarreau <w@1wt.eu> --- tools/include/nolibc/sys.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
Comments
On Mon, May 29, 2023, at 21:54, Zhangjin Wu wrote: > use sys_llseek instead of sys_lseek to add 64bit seek even in 32bit > platforms. > > This code is based on sysdeps/unix/sysv/linux/lseek.c of glibc and > src/unistd/lseek.c of musl. > > Signed-off-by: Zhangjin Wu <falcon@tinylab.org> > Signed-off-by: Willy Tarreau <w@1wt.eu> > --- > tools/include/nolibc/sys.h | 10 ++++++++++ > 1 file changed, 10 insertions(+) > > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h > index 98cfa2f6d021..d0720af84b6d 100644 > --- a/tools/include/nolibc/sys.h > +++ b/tools/include/nolibc/sys.h > @@ -672,7 +672,17 @@ int link(const char *old, const char *new) > static __attribute__((unused)) > off_t sys_lseek(int fd, off_t offset, int whence) > { > +#if defined(__NR_llseek) || defined(__NR__llseek) > +#ifndef __NR__llseek > +#define __NR__llseek __NR_llseek > +#endif > + off_t result; > + return my_syscall5(__NR__llseek, fd, offset >> 32, offset, &result, > whence) ?: result; > +#elif defined(__NR_lseek) > return my_syscall3(__NR_lseek, fd, offset, whence); > +#else > +#error None of __NR_lseek, __NR_llseek nor __NR__llseek defined, > cannot implement sys_lseek() > +#endif > } This is not technically wrong, but I think a different approach would be clearer: Instead of having a sys_lseek() that works differently depending on the macros, why not define the low-level helpers to match the kernel arguments like static inline __attribute__((unused)) __kernel_loff_t sys_lseek(int fd, __kernel_loff_t offset, int whence) { #ifdef __NR__llseek __kernel_loff_t result; return my_syscall5(__NR__llseek, fd, offset >> 32, offset, &result, whence) ?: result; #else #endif } static inline __attribute__((unused)) __kernel_off_t sys_lseek(int fd, __kernel_off_t offset, int whence) { #ifdef __NR_lseek return my_syscall3(__NR_lseek, fd, offset, whence); #else return -ENOSYS; #endif } And then do the selection inside of the actual lseek, something like static __attribute__((unused)) off_t lseek(int fd, off_t offset, int whence) { off_t ret = -ENOSYS; if (BITS_PER_LONG == 32) ret = sys_llseek(fd, offset, whence); if (ret == -ENOSYS) ret = sys_lseek(fd, offset, whence); if (ret < 0) { SET_ERRNO(-ret); ret = -1; } return ret; } For the loff_t selection, there is no real need to handle the fallback, so this could just be an if()/else to select 32-bit or 64-bit, but for the time_t ones the fallback is required for pre-5.6 kernels. Arnd
Hi, Arnd, Willy > On Mon, May 29, 2023, at 21:54, Zhangjin Wu wrote: > > use sys_llseek instead of sys_lseek to add 64bit seek even in 32bit > > platforms. > > > > This code is based on sysdeps/unix/sysv/linux/lseek.c of glibc and > > src/unistd/lseek.c of musl. > > > > Signed-off-by: Zhangjin Wu <falcon@tinylab.org> > > Signed-off-by: Willy Tarreau <w@1wt.eu> > > --- > > tools/include/nolibc/sys.h | 10 ++++++++++ > > 1 file changed, 10 insertions(+) > > > > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h > > index 98cfa2f6d021..d0720af84b6d 100644 > > --- a/tools/include/nolibc/sys.h > > +++ b/tools/include/nolibc/sys.h > > @@ -672,7 +672,17 @@ int link(const char *old, const char *new) > > static __attribute__((unused)) > > off_t sys_lseek(int fd, off_t offset, int whence) > > { > > +#if defined(__NR_llseek) || defined(__NR__llseek) > > +#ifndef __NR__llseek > > +#define __NR__llseek __NR_llseek > > +#endif > > + off_t result; > > + return my_syscall5(__NR__llseek, fd, offset >> 32, offset, &result, > > whence) ?: result; > > +#elif defined(__NR_lseek) > > return my_syscall3(__NR_lseek, fd, offset, whence); > > +#else > > +#error None of __NR_lseek, __NR_llseek nor __NR__llseek defined, > > cannot implement sys_lseek() > > +#endif > > } > > This is not technically wrong, but I think a different approach > would be clearer: Instead of having a sys_lseek() that works > differently depending on the macros, why not define the low-level > helpers to match the kernel arguments like > > static inline __attribute__((unused)) > __kernel_loff_t sys_lseek(int fd, __kernel_loff_t offset, int whence) > { > #ifdef __NR__llseek > __kernel_loff_t result; > return my_syscall5(__NR__llseek, fd, offset >> 32, offset, &result, whence) ?: result; > #else > > #endif > } > > static inline __attribute__((unused)) > __kernel_off_t sys_lseek(int fd, __kernel_off_t offset, int whence) > { > #ifdef __NR_lseek > return my_syscall3(__NR_lseek, fd, offset, whence); > #else > return -ENOSYS; > #endif > } > > And then do the selection inside of the actual lseek, > something like > > static __attribute__((unused)) > off_t lseek(int fd, off_t offset, int whence) > { > off_t ret = -ENOSYS; > > if (BITS_PER_LONG == 32) > ret = sys_llseek(fd, offset, whence); > > if (ret == -ENOSYS) > ret = sys_lseek(fd, offset, whence); > > if (ret < 0) { > SET_ERRNO(-ret); > ret = -1; > } > return ret; > > } Yes, It is clearer, thanks. will learn carefully about the kernel types. > > For the loff_t selection, there is no real need to handle the > fallback, so this could just be an if()/else to select 32-bit > or 64-bit, but for the time_t ones the fallback is required > for pre-5.6 kernels. > Ok, will test it on the pre-5.6 versions too. Hi, Willy, what's your suggestion about the oldest kernel versions we plan to support? ;-) Best regards, Zhangjin > Arnd
Hi Zhangjin, Arnd, On Tue, May 30, 2023 at 09:54:33PM +0800, Zhangjin Wu wrote: > > And then do the selection inside of the actual lseek, > > something like > > > > static __attribute__((unused)) > > off_t lseek(int fd, off_t offset, int whence) > > { > > off_t ret = -ENOSYS; > > > > if (BITS_PER_LONG == 32) > > ret = sys_llseek(fd, offset, whence); > > > > if (ret == -ENOSYS) > > ret = sys_lseek(fd, offset, whence); > > > > if (ret < 0) { > > SET_ERRNO(-ret); > > ret = -1; > > } > > return ret; > > > > } > > Yes, It is clearer, thanks. will learn carefully about the kernel types. I, too, like Arnd's proposal here. I tend to use a similar approach in other projects when possible. Often the limit is the types definition, which is necessary to define even empty static inline functions. The only thing is that due to the reliance on -ENOSYS above, the compiler cannot fully optimize the code away, particularly when both syscalls are defined, which may result in the compiler emitting the code for both calls on 32-bit platforms. But the idea is there anyway, and it may possibly just need a few adjustments based on BITS_PER_LONG after checking the emitted code. > > For the loff_t selection, there is no real need to handle the > > fallback, so this could just be an if()/else to select 32-bit > > or 64-bit, but for the time_t ones the fallback is required > > for pre-5.6 kernels. > > > > Ok, will test it on the pre-5.6 versions too. > > Hi, Willy, what's your suggestion about the oldest kernel versions we plan to > support? ;-) Like I said last time, since the code is included in the kernel, we expect userland developers to use this one to build their code, even if it's meant to work on older kernels. At the very least I want that supported kernels continue to work, and then as long as it does not require particular efforts, it's nice to continue to work on older ones (think LTS distros, late upgraders of legacy systems etc). Thanks, Willy
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 98cfa2f6d021..d0720af84b6d 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -672,7 +672,17 @@ int link(const char *old, const char *new) static __attribute__((unused)) off_t sys_lseek(int fd, off_t offset, int whence) { +#if defined(__NR_llseek) || defined(__NR__llseek) +#ifndef __NR__llseek +#define __NR__llseek __NR_llseek +#endif + off_t result; + return my_syscall5(__NR__llseek, fd, offset >> 32, offset, &result, whence) ?: result; +#elif defined(__NR_lseek) return my_syscall3(__NR_lseek, fd, offset, whence); +#else +#error None of __NR_lseek, __NR_llseek nor __NR__llseek defined, cannot implement sys_lseek() +#endif } static __attribute__((unused))