From patchwork Thu Dec 22 03:51:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 35597 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp120492wrn; Wed, 21 Dec 2022 19:53:45 -0800 (PST) X-Google-Smtp-Source: AMrXdXt6ToRBbqIa/SWofE31lFgjMzNyIu36Xmq0agUX1Q5pwjUpa1QcifpQVvwEuP70sYLixg4z X-Received: by 2002:a17:902:82c8:b0:191:4389:f8fb with SMTP id u8-20020a17090282c800b001914389f8fbmr4180181plz.65.1671681225381; Wed, 21 Dec 2022 19:53:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671681225; cv=none; d=google.com; s=arc-20160816; b=LK43PvbtkAA6FvFYZZJdGFQgN8iMV+bDakBZ0vqxbe0key6LT2Cb2F73cjjiiomp/Y Ke3Xhf77aKDeZ3Jboa8d5eI0hA1/IStf2ZIbwBs4eqFYvY+/uhBG3K+QTc2sA6bFN5YR ZgBW1j83df9K0PGs8xU6VKjAVTcIvzTJbB/e8s0vyghjM4lmvyhH3K50dE0Kd3xI5iQu ypzxRHXO2WWTY7Lv3eaRzjEnz+l8u8g89fp7qLJpJdtBMSdVNxsgzhlgADQ0bo5enPDg eWBaZsyI4pNqWURcDTVl+Z5MJjzxKR3LwaK4xxO3x/2uPmeh2xWUcga6h5VjT3by7jmK /kPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zLSz8nwxMLxILKuCgwoEkMG6sxfGu8opJvrjeunG6bc=; b=Q9an5xyfD+xzOquP1RMk6P1eQfGmqB42oHNLIqgWnT06x3T+H+2Ij5xyPCDHss1/ZD XxW1bJF3+w51oXi+kQWDv9w4o5i6xis/XeIjpBFHz4Fy5suGoRsFAvke0vz7TG6AS+VT y+mEK9WP347cWKUN6jZ1BDSQr8gLYZ/5ro76QQRwhEbKX8LT7QSKBXbD4Pto8J68XEbb 6FDonUWOv4fsFhznDnW21HMWzruPXejxQiIjWTH/Y0HtZeuJXOGN4sIFxDpLHK0gcq3x tUw0Kj3IPsRGcoBMUIgxwzfUakIaAYguWjMwPaFMnOGOD87+7rtPuGBsY5Ad8HYNGw7L dkLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=ZlzUpeLp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u12-20020a17090341cc00b00188b63f078dsi19086103ple.362.2022.12.21.19.53.32; Wed, 21 Dec 2022 19:53:45 -0800 (PST) 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; dkim=pass header.i=@gnuweeb.org header.s=default header.b=ZlzUpeLp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234875AbiLVDwY (ORCPT + 99 others); Wed, 21 Dec 2022 22:52:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229561AbiLVDwU (ORCPT ); Wed, 21 Dec 2022 22:52:20 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D2D0186A6; Wed, 21 Dec 2022 19:52:19 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 0E28E81A14; Thu, 22 Dec 2022 03:52:14 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681139; bh=kULCxpE25ChDF6QfgE9AQS3bo2E7ihos1VsI9mS/Wmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZlzUpeLpn+jVdP9jtnmx4mHFn13ToH+RIy528nBPlj+pDkEW9I1aKV46OdGeur4Y2 ALPyOg/8i1qcytsNTzW6sFmZMn48TXq7qCgS/tFV6RAo3xIT3y4xvGgo+AYYTWtYgv S7XpVIXU/LODBMv1SEaBvn8eWfoG3X7knJQaUE0SynhkH3IL5i2LVj5CUXstzDfc6B ehkhaAFPuAgMD2qyoEJO25qZG7ZVg5Zko+Tku+tl1J1fij8xadukBjAQKK8jxpql3o UnYEJoBsPa2GOK3+6WXkPUq3qPsvPndaUml7jVYUapTcw6RtgIybno8AojlUJYb/Fx ACr1p4G5G4CRw== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 1/8] nolibc/sys: Implement `sigaction(2)` function Date: Thu, 22 Dec 2022 10:51:27 +0700 Message-Id: <20221222035134.3467659-2-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752884812441244689?= X-GMAIL-MSGID: =?utf-8?q?1752884812441244689?= From: Ammar Faizi This commit adds the initial implementation of nolibc `sigaction()` function. Currently, this implementation is only available on the x86-64 arch. `sigaction()` needs an architecture-dependent "signal trampoline" function that invokes the __rt_sigreturn syscall to resume the process after a signal gets handled. On Linux x86-64, the "signal trampoline" function has to be written in inline Assembly to prevent the compiler from controlling the `%rsp` (e.g., with `-fno-omit-frame-pointer`, every function has a `pushq %rbp` that makes the `%rsp` no longer point to `struct rt_sigframe`). The "signal trampoline" function is called `__arch_restore_rt` in this implementation. Signed-off-by: Ammar Faizi --- tools/include/nolibc/arch-x86_64.h | 12 +++++ tools/include/nolibc/sys.h | 80 ++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h index 0e1e9eb8545d..b6470943e836 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -212,4 +212,16 @@ __asm__ (".section .text\n" "hlt\n" // ensure it does not return ""); +void __arch_restore_rt(void); + +__asm__ ( +".section .text\n" +"__arch_restore_rt:\n\t" + "movl $0xf, %eax\n\t" // __NR_rt_sigreturn == 0xf + "syscall\n\t" // %rsp must point to a valid struct rt_sigframe. + "int3" +); + +#define __HAVE_ARCH_RESTORE_RT + #endif // _NOLIBC_ARCH_X86_64_H diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 3db1dd8c74ee..91532a2fbe2c 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -1026,6 +1026,86 @@ pid_t setsid(void) return ret; } +typedef void (*sighandler_t)(int sig); + +/* + * int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); + */ + +static __attribute__((unused)) +int sys_sigaction(int signum, const struct sigaction *act, + struct sigaction *oldact) +{ + return my_syscall4(__NR_rt_sigaction, signum, act, oldact, + sizeof(sigset_t)); +} + +static __attribute__((unused)) +int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) +{ +#ifdef __HAVE_ARCH_RESTORE_RT + struct sigaction act2 = *act; + int ret; + + /* + * On Linux x86-64, libc's sigaction() always sets the + * @act->sa_restorer when the caller passes a NULL. + * + * @act->sa_restorer is an arch-specific function used + * as a "signal trampoline". + * + * @act->sa_handler is a signal handler provided by the + * user. + * + * When the handled signal is caught, the %rip jumps to + * @act->sa_handler with user stack already set by the + * kernel as below: + * + * |--------------------| + * %rsp -> | act->sa_restorer | (return address) + * |--------------------| + * | struct rt_sigframe | (process context info) + * | | + * | | + * .................... + * + * Once this signal handler executes the "ret" instruction, + * the %rip jumps to @act->sa_restorer. The sa_restorer + * function has to invoke the __rt_sigreturn syscall with + * %rsp pointing to the `struct rt_sigframe` that the kernel + * constructed previously to resume the process. + * + * The "signal trampoline" function has to be written in + * inline Assembly to prevent the compiler from controlling + * the %rsp (e.g., with -fno-omit-frame-pointer, every + * function has a `pushq %rbp` that makes the %rsp no longer + * point to `struct rt_sigframe`). + * + * `struct rt_sigframe` contains the registers' value before + * the signal is caught. + * + */ + if (!act2.sa_restorer) { + act2.sa_flags |= SA_RESTORER; + act2.sa_restorer = __arch_restore_rt; + } + + ret = sys_sigaction(signum, &act2, oldact); + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } + return ret; +#else + /* + * TODO: Implement sa_restorer ("signal trampoline") for + * other architectures. + */ + SET_ERRNO(ENOSYS); + return -1; +#endif +} + /* * int stat(const char *path, struct stat *buf); From patchwork Thu Dec 22 03:51:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 35591 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp120296wrn; Wed, 21 Dec 2022 19:52:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXvlv1azXOckJ/Kv7j+neRyKN056on9fnKf6LpGO/I4dKnFHGWNJRJEQPbvlMoIRRsQUan4P X-Received: by 2002:a17:90b:1296:b0:219:36c6:f06d with SMTP id fw22-20020a17090b129600b0021936c6f06dmr4953442pjb.20.1671681178880; Wed, 21 Dec 2022 19:52:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671681178; cv=none; d=google.com; s=arc-20160816; b=G4VlRsOqfQLvE7tjT5e2Pe0voCgW/NrmDw8NXkPfCrPvN/Sk1ntjR5prLDh6RXcFYd SWeQHwYnO79jliQSG5ybWADPQ1vNmaABIt/E5aEJiqR6l5FXmLMAAPy7XVloffll8+Hs fYUsE7hIPL/fFYxjNgq+SsqDRfR9mUfJuZomLlYxZhBp03rF07X2bwhi7lRfn6DR2yOf 9oD7cXk/3QDTDESmddTVl2LIqhvaN5uWhYM+H104XtJnKxhCvqeLvRxvYnIJ+wQlLqbV 1mgZVQHd39vMenxkYBOWh1TiUamta4NPLpiEEYpEC329yciJgp6C/52qsEnhHUAyFBXG E0Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OSfqsxzXlXSZuRyX8dBB5uytMsrgrtBE9e2vwUeeQLQ=; b=wcnf79/LK/VkPviTarQsmcxLxYdrjNE0pyMIuF8SS0Z4wuoa1hBi5mhS9AbyP88INO qQTK5pv6xMykXlaIzKCC4oQE9XE41iUtxiD4fxVgVO2OhfYggeGu8aMKKaRFz/d1ce6A NYIYqRRarIcxhPKdK9dFkouTQdjXA17YPFWdNilkzsb1G4u0aybzm2sXztxDsSmykxg0 0lme+NO9ucCMy+QEuuUn9QKB5OLRhdL9xsS5NrUqlBI0S8+wZhiFw/poJULUnATo//Bv +91RFWTodp21LOnS6AfZNNOHjy5xl1HiyfTwKy7pxJQoZ8poF7YjJAG5OfZOigbqdj4J DyvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=Qtf2fx3C; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ot1-20020a17090b3b4100b0020da3876a77si3772775pjb.119.2022.12.21.19.52.46; Wed, 21 Dec 2022 19:52:58 -0800 (PST) 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; dkim=pass header.i=@gnuweeb.org header.s=default header.b=Qtf2fx3C; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234894AbiLVDw1 (ORCPT + 99 others); Wed, 21 Dec 2022 22:52:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234880AbiLVDwY (ORCPT ); Wed, 21 Dec 2022 22:52:24 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 641B4186A6; Wed, 21 Dec 2022 19:52:24 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id DBEC181A18; Thu, 22 Dec 2022 03:52:19 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681144; bh=VUWgZx5aCGOd+rMsLp7+4gysuZVXd6vPkv96NwcpuWs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qtf2fx3CmLBFG2dkzGK7t/5NNmia4RS1/LNt9kPbJxG/kh+CHXxF4xfS+R1JP5zjo T2ownYIeIUoiSPXCY+95CalZU5s8QHXrEylRROILdjmjt5ApwCuhmaaiTDowobCGn5 22yHxF9gdcDoYephNcVsv02j8mNYGt9BlcL253JdgFHT+dVvOSPLct2G+VfMH6n0xi 9Dthi66U2qU3ioEx/bIEIAHvQ2m1uGrJrjIXjCvgQakPxLHI/f4tLGQ0jqCNv185ma 0cNrVJS0RHFzqrfcxpBSnn44vM6GHoNVqy+fTtwGhHJvd5VXOy2i/2S3PDhu5Anb2N U9jCvUdu96y8A== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 2/8] nolibc/sys: Implement `signal(2)` function Date: Thu, 22 Dec 2022 10:51:28 +0700 Message-Id: <20221222035134.3467659-3-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752884763704372842?= X-GMAIL-MSGID: =?utf-8?q?1752884763704372842?= From: Ammar Faizi signal() function is the simpler version of sigaction(). Unlike sigaction(), which fully controls the `struct sigaction`, the caller only cares about the sa_handler when calling the signal() function. signal() internally calls sigaction(). This implementation is currently only available on the x86-64 arch. Signed-off-by: Ammar Faizi --- tools/include/nolibc/sys.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 91532a2fbe2c..ca348939eb50 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -1106,6 +1106,36 @@ int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) #endif } +/* + * sighandler_t signal(int signum, sighandler_t handler); + */ + +static __attribute__((unused)) +sighandler_t signal(int signum, sighandler_t handler) +{ +#ifdef __HAVE_ARCH_RESTORE_RT + const struct sigaction act = { + .sa_handler = handler, + .sa_flags = SA_RESTORER, + .sa_restorer = __arch_restore_rt + }; + sighandler_t old_sah; + struct sigaction old; + int ret; + + ret = sys_sigaction(signum, &act, &old); + if (ret < 0) { + SET_ERRNO(-ret); + old_sah = SIG_ERR; + } else { + old_sah = old.sa_handler; + } + return old_sah; +#else + SET_ERRNO(ENOSYS); + return SIG_ERR; +#endif +} /* * int stat(const char *path, struct stat *buf); From patchwork Thu Dec 22 03:51:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 35592 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp120340wrn; Wed, 21 Dec 2022 19:53:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXtHBYF/RIjo/cTRrR7Aqa2c1mjnBAIr0Y207r87ABkZuX6RKaedwfKEjiKOuRCv9qSWSuar X-Received: by 2002:a17:90b:2314:b0:221:71fb:388f with SMTP id mt20-20020a17090b231400b0022171fb388fmr4170996pjb.35.1671681186756; Wed, 21 Dec 2022 19:53:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671681186; cv=none; d=google.com; s=arc-20160816; b=pRyHKdyvmUM1GVMOQtCGu3XNQrYsAxCuIgm7ZCc2Q+KzX8FTv9FT8ZII+pUQfhvKYA FurFTtSUolUrfnWn1deLxBcBdTFz0CjquLusoXNupgYkl7CaRd5RGkJ8zYs/V99pBQzy rqM03irfp+e5aTXGBZJkJjZ27cbBlKe16Pm29ZRwzM3pkGi6PV59b3gxAv9fND0LhfWE e10qCac9UNq2GfALve9Tz/CjinODTXiAwgCne7NdNzASClkBkNQXfTviDWTzJXpXSgT9 inrxi3L4ylaxvrd7eLCAiBN9Q7mK4jPxTe2iGDmM6tPEcgRizQOLKU6lC+54glNd96t/ MP0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=huTqqN+ma9rw8bLQJ0gjYoAkMAHiKQr1ReiSQvPrT40=; b=ME1ATqKZ0eyycVPX03a+5hOw5nfJ0ITfBl3O0N2CwGgNPyWqqEWRhf/dTLbU9yH1BH 8j/gwZ9q/Ll0ySRXv8okgU5e2UH320qkoMHsSdjCfpAFx6PnJJQpRDZKv3LNCOcFt06F IRPUFcA0JNtGjcTgro/aLGGnVhV0gJzWOFxX/Cq1rLGnFzrkxppg3iFh95gswvfLV9R5 pSlSt4rE4Y4E+4hVUWZ8iEhEtPSLTCHEF5KTXELc4kf+B+vZEuzgrPNW0TY11vcwnyqj bAUhKBzbL/5Xq/5C9T7754lke3/tDeuGJDibvXGvUfc1Kiidv1L7EcOYEjJ7Nsk9DHXl rrLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=nad1zX65; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lw5-20020a17090b180500b00218af09d1f4si3869013pjb.37.2022.12.21.19.52.54; Wed, 21 Dec 2022 19:53:06 -0800 (PST) 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; dkim=pass header.i=@gnuweeb.org header.s=default header.b=nad1zX65; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234935AbiLVDwg (ORCPT + 99 others); Wed, 21 Dec 2022 22:52:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234911AbiLVDwb (ORCPT ); Wed, 21 Dec 2022 22:52:31 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3972E1FCFA; Wed, 21 Dec 2022 19:52:29 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id C4CD981A1C; Thu, 22 Dec 2022 03:52:24 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681149; bh=0oAsj4hR6tGtSZQUyeczrzwt8e+u+XyWFzo2fs4a44U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nad1zX656AwgsE9Fq0uKy0Jt4X/EVIrEq18oxqubn4ZO+VqLqmQ7w2HmWh1bZmEpj FiDuMJWuOszZNMEblqzx7t2B+yizrGJLNYWsMnol/ZpWqOkWsVkI9WXNRC5V+EJ0ci VKjMCb8+ZAqPEdhTFTwGCLFyhmmAuVYQmXEDQYWEPNsZUtrYYt+vs5TbZFcs995ocL t2rAUIoVjJta2v2PC+siE4BB1nJzZgoXwR645VTwm5h8gIJSWQrZyXo7NOlOzYq7J2 Agqcofl741CQUeP9uPxFb0HNV5UVRl4Qv2i0rKUCM1/n+C7JP87Vg1R9RHNPxwRPal m38dmxGmBnRaQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 3/8] nolibc/sys: Implement `getpagesize(2)` function Date: Thu, 22 Dec 2022 10:51:29 +0700 Message-Id: <20221222035134.3467659-4-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752884771770682209?= X-GMAIL-MSGID: =?utf-8?q?1752884771770682209?= From: Ammar Faizi This commit adds getpagesize() function implementation. The getpagesize() syscall doesn't always exist on the Linux syscall table. Only specific architectures have this syscall. Implementation detail: Some architectures use a fixed page size, like x86. We can hard-code the page size value on such architectures. Some other architectures may use different page sizes. For example, Linux aarch64 supports three values of page size: 4K, 16K, and 64K which are selected at kernel compilation time. The kernel stores the used page size in the auxiliary vector. The auxiliary vector can be obtained from /proc/self/auxv at AT_PAGESZ key-value-pair. /proc/self/auxv is available on all architectures. Once we obtain the page size info, cache the value in a static variable to avoid traversing the auxiliary vector again in the next getpagesize() call. The page size should never change during kernel uptime. Link: https://lwn.net/Articles/519085 Link: https://github.com/torvalds/linux/blob/v6.1/fs/binfmt_elf.c#L260 Signed-off-by: Ammar Faizi --- tools/include/nolibc/sys.h | 114 +++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index ca348939eb50..e9e3640c36e1 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "arch.h" #include "errno.h" @@ -407,6 +408,119 @@ int getdents64(int fd, struct linux_dirent64 *dirp, int count) return ret; } +/* + * The getpagesize() syscall doesn't always exist on the Linux syscall + * table. Only specific architectures have this syscall. + * + * Implementation detail: + * Some architectures use a fixed page size, like x86. We can hard-code + * the page size value on such architectures. + * + * Some other architectures may use different page sizes. For example, + * Linux aarch64 supports three values of page size: 4K, 16K, and 64K + * which are selected at kernel compilation time. The kernel stores the + * used page size in the auxiliary vector. The auxiliary vector can be + * obtained from /proc/self/auxv at AT_PAGESZ key-val-pair. + * /proc/self/auxv is available on all architectures. + * + * Once we obtain the page size info, cache the value in a static + * variable to avoid traversing the auxiliary vector again in the next + * getpagesize() call. The page size should never change during kernel + * uptime. + * + * Link: https://lwn.net/Articles/519085 + * Link: https://github.com/torvalds/linux/blob/v6.1/fs/binfmt_elf.c#L260 + * + * + * long getpagesize(void); + * + */ + +#if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) +__attribute__((unused)) +static inline long getpagesize(void) +{ + /* + * x86 family is always 4K page. Don't bother + * reading the auxiliary vector. + */ + return 4096; +} +#else +static int sys_open(const char *path, int flags, mode_t mode); +static ssize_t sys_read(int fd, void *buf, size_t count); + +/* + * This function works for all architectures. + */ +static long sys_getpagesize(void) +{ + uint64_t buf[2] = {0, 0}; + long ret; + int fd; + + + fd = sys_open("/proc/self/auxv", O_RDONLY, 0); + if (fd < 0) + return fd; + + while (1) { + ssize_t x; + + x = sys_read(fd, buf, sizeof(buf)); + if (x < 0) { + ret = x; + break; + } + + if (__builtin_expect(x == 0, 0)) { + /* + * We've reached the end of the auxiliary + * vector, but can't find the AT_PAGESZ + * entry. + */ + ret = -ENOENT; + break; + } + + /* + * buf[0] is the key. + * buf[1] is the value. + */ + if (buf[0] == AT_PAGESZ) { + ret = buf[1]; + break; + } + } + + sys_close(fd); + return ret; +} + +__attribute__((unused)) +static long getpagesize(void) +{ + static long cached; + long ret; + + /* + * No need to read the auxv for the second + * getpagesize() call. + */ + if (__builtin_expect(cached != 0, 1)) + return cached; + + ret = sys_getpagesize(); + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } else { + cached = ret; + } + return ret; +} +#endif + /* * pid_t getpgid(pid_t pid); From patchwork Thu Dec 22 03:51:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 35593 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp120353wrn; Wed, 21 Dec 2022 19:53:11 -0800 (PST) X-Google-Smtp-Source: AMrXdXu176/ygIwmO9CejGRK+T8pWa3aET0G+edbyPSATH3agEtpOPMT/ahxuaeAf49fl4yzkCfU X-Received: by 2002:a05:6a20:8f04:b0:a3:c2a2:260f with SMTP id b4-20020a056a208f0400b000a3c2a2260fmr6799847pzk.48.1671681191608; Wed, 21 Dec 2022 19:53:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671681191; cv=none; d=google.com; s=arc-20160816; b=aisLJbaBAZ8yKYrU0ZyEC13ZbUNSE6EkSjgPzODUhZVmiQ1/AvBPdC4SZ8KwLSIMX3 D2H+FZOBX5R2PqHOqA/72/NnB1m7JT2377P2F0WN/U9HJWl58aCbeT6hzm/QWG/Cs5v9 fxD6WUShn0iDVjxBS8id2xZdfDVsZfjkct6e+9g9oGpZVMOTAGdz8CzFl9CUG1ReqtFF f9NP0p3CfGJbYqdoZgXsScZaGDvBpTI3dMCsMNjs6+wnp5Tgq9pzulPO7PLVxrBuSDyO 66HowOdgAQdEJZuvha36q9+iknfpHbxe3u6b7c/MU3NTNdaYH8J/zSomiBC9GQ8b2hEn p/9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4q1W+n9bGZNVi/EyNZNJYJODIgOeRxVY7YQ67puOUHQ=; b=tkJbqGOCn2h5XBlnYKch1kD6xocEfSz3dVex4Jqf5k3aGQsIlY5vdhlNhEMXih6bqq a6iPim/Ht4i5aspY1C8vhw5ljWpVSzOTWyPIjLN68giRnoODZkhOeBTj/I/hzpI21evt L+Xst7hOnFuJNRnJwRf/q9khVAVjEO6F9adHZXzze5ztEpDImMXU+aP5zBhyhnwDggm3 JFVKY0P6AMACGBpjjKg89kHRjaXTomr7KFigp7TKq5yFRFWf7efoxF71IhG52ecj/UBZ YBLv8UwS5VGkac5OW+BrFNM1bwZatmpwDdzWBA5bq4mxGx7q+YkLKzxmAvSgEGBimqGT 58IQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=feCVYsbU; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 85-20020a630458000000b00476b6fa2963si17897256pge.599.2022.12.21.19.52.59; Wed, 21 Dec 2022 19:53:11 -0800 (PST) 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; dkim=pass header.i=@gnuweeb.org header.s=default header.b=feCVYsbU; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234911AbiLVDwj (ORCPT + 99 others); Wed, 21 Dec 2022 22:52:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234932AbiLVDwf (ORCPT ); Wed, 21 Dec 2022 22:52:35 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12AF723329; Wed, 21 Dec 2022 19:52:34 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 9D18F81A20; Thu, 22 Dec 2022 03:52:29 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681153; bh=dAu4u8hilpZPllAVwu00C5OS/7zywFlN7MRAKmmU8vY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=feCVYsbUUPipmlfjwXpY+Bispi3vMoCpuOKrfMEolGAzEiNO+8X0jywpx1358j0uD Vf46EbF7dN9cXkvscwqZwrFzkGnV9KUHcM5gUgfU4VY+VKTWJtEa7mE+8N2yvv6itn VLsJvT077b6ewF/6H+NfU8vEqorWBe2iv1NpPncxOjtwIwvDB2FVPn7Mizhi0yZHyn SM9MM0N5h5CbAl+JtmCeJQDnA6sAjMkzijfsL/VXsSWvACwAkWGYSdt61c/75sT3LQ cC/ACddnjE0EI85M/8jEJdLmhNw7lDOJnR8sgP+xHzY7y8YyPOyFZuBpL9oQ7v5cm1 sMou4sZAbNuOA== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 4/8] selftests/nolibc: Add `-Wall` and `-Wno-unsed-function` to the CFLAGS Date: Thu, 22 Dec 2022 10:51:30 +0700 Message-Id: <20221222035134.3467659-5-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752884777207956645?= X-GMAIL-MSGID: =?utf-8?q?1752884777207956645?= From: Ammar Faizi Turn on more compiler warnings to help spot issues. Skip unused function warning as it's inevitable in the nolibc test suite. While adding these flags, remove unused variables in run_stdlib() warned by the `-Wall` flag. Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/Makefile | 2 +- tools/testing/selftests/nolibc/nolibc-test.c | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile index 572fe72df68d..f6602a2c2bbd 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -66,7 +66,7 @@ else Q=@ endif -CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables +CFLAGS ?= -Os -fno-ident -fno-asynchronous-unwind-tables -Wall -Wno-unused-function LDFLAGS := -s help: diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index f14f5076fb6d..8d69f8a0f35a 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -542,9 +542,7 @@ int run_syscall(int min, int max) int run_stdlib(int min, int max) { int test; - int tmp; int ret = 0; - void *p1, *p2; for (test = min; test >= 0 && test <= max; test++) { int llen = 0; // line length From patchwork Thu Dec 22 03:51:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 35594 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp120415wrn; Wed, 21 Dec 2022 19:53:26 -0800 (PST) X-Google-Smtp-Source: AMrXdXv086lc18G3XRYxm3Ro5R2/Nana7rfIe2nJo5+5EedkLYZRe+2rBR3nWgzT9Go2hq0DuDUV X-Received: by 2002:a17:903:445:b0:191:386:2314 with SMTP id iw5-20020a170903044500b0019103862314mr4692583plb.3.1671681206318; Wed, 21 Dec 2022 19:53:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671681206; cv=none; d=google.com; s=arc-20160816; b=TsbnNDSgddWtSZtTN1cWPOf96pQXvL5s3U8sFt/QyOh2wnX4yXcxflFSP+iQpyQ3ih 3kF2Sc21FlkFSZJnOSh65WrzLHVYrFF+ZdDwKgxZcZyGKM6Gllyb77/jfHumvL89XiaX 5pGNYrDhJtC+312jZ8U0pPxzUHLcvPGPgzd+fVahGOW3JNSysTjUzYd5GFdunTjZcQNR lxzKRCjQdwhr3TdspRRDMtYS87m1R3csWqGkXCjNQLpPxpNNHwFDHbFJpEFLpMURYeRF yYhHs+OygkT+aT9MFwp8vE1jNHBpkjVkvIBVWCjzAMFmcwivbUOTHhgIu4Ac8GK1mXnh 3hAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bZo/L5G2GSEVnRkrKzgCoFiIAWLx/WBu7ciFflso+2I=; b=mc70UqhHYQpzQRKavGfYQDoSP+sMZ+vhmXtq0sR9k78KetEHomCWNRbarSNe4OQ0QX p3GCFxmgyVXO8gfie4SynyJ6MthnfETve5cpANwgIkJG9CfuNxbR5zxGYKxWYAUoGm9F aD6K9cO46YucEcxtOAvLpzVSR1fnJ2bgATKbI7+8sQ8GuSKckBfqAVXrsFYenkYv0PYE cOyJPOEkRBsfvJtJ9gmBYkjYIMFEd+J4EWI0Lj/ibpfJrIEMnJFQx7+n46lpTyKM7l/V EupBitGR3u/zIa6g0yZnXPD5smKng79b1rhYY6+t4o9XyuVwYUUg43uLd2FE2t3N1luu Aqgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b="OAr/7wyr"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h10-20020a170902704a00b00175449cf6f8si13719154plt.606.2022.12.21.19.53.13; Wed, 21 Dec 2022 19:53:26 -0800 (PST) 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; dkim=pass header.i=@gnuweeb.org header.s=default header.b="OAr/7wyr"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234965AbiLVDwp (ORCPT + 99 others); Wed, 21 Dec 2022 22:52:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234953AbiLVDwm (ORCPT ); Wed, 21 Dec 2022 22:52:42 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1444186A6; Wed, 21 Dec 2022 19:52:38 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 7729A81A0D; Thu, 22 Dec 2022 03:52:34 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681158; bh=7CHYFJ/fWvU3HmqUeRZ+VXRA4YRaz7Sx/PWqqaM5V34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OAr/7wyrio6E1/EaQ1M1rE1/PZVifyMfZI3ucHwy85zJYky5RY12Y+9aiE2RZw231 2feH9uuKuOWyZtteCOwapCVVLvuI4pfaxx2SkRugm8I2vXxfldLaoNWw+0XK7I1jR8 WGpOhumr90RujdFpnWDTg6xszQEaQaHGqRnSnAqZHgHESJJVTy609MpkLkvRcTjRaT 0WyC50LXJ7FYc1kIqOh+TvotowI7B1E/wMGdqKj5834q4SSpEfpOerJV+oL29Dx9f/ ZD1OA4+Qo5fMOmSUs9Uaq5Tkzt7AU8tzmBWfll7ppSBNLmC3pPG0vMs4oqmscV4dtN FRcqZfbRnochQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 5/8] selftests/nolibc: Add `fork(2)` selftest Date: Thu, 22 Dec 2022 10:51:31 +0700 Message-Id: <20221222035134.3467659-6-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752884792857031045?= X-GMAIL-MSGID: =?utf-8?q?1752884792857031045?= From: Ammar Faizi Ensure the fork() function can create a child process. Also, when the child exits, the parent must be able to get the child's exit code via waitpid(). Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 8d69f8a0f35a..309cabbddeec 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -442,6 +442,50 @@ int test_getdents64(const char *dir) return ret; } +/* + * Test fork(). + * Make sure the exit code can be read from the parent process. + */ +static int test_fork_and_exit(int expected_code) +{ + int status; + int code; + pid_t ret; + pid_t p; + + p = fork(); + if (p < 0) + return p; + + if (!p) + exit(expected_code); + + do { + ret = waitpid(p, &status, 0); + if (ret < 0) + return ret; + } while (!WIFEXITED(status)); + + code = WEXITSTATUS(status); + if (code != expected_code) { + printf("test_fork_and_exit(): waitpid(): Invalid exit code: %d; expected = %d\n", code, expected_code); + return -1; + } + + return 0; +} + +static int test_fork(void) +{ + int i; + + for (i = 0; i < 255; i++) { + if (test_fork_and_exit(i)) + return -1; + } + return 0; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -494,6 +538,7 @@ int run_syscall(int min, int max) CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break; + CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); 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(gettimeofday_null); EXPECT_SYSZR(1, gettimeofday(NULL, NULL)); break; From patchwork Thu Dec 22 03:51:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 35595 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp120446wrn; Wed, 21 Dec 2022 19:53:32 -0800 (PST) X-Google-Smtp-Source: AMrXdXsF0GKuGnLFD6RCy/DC1+YwMjTBm05LT5Ca0aUgGyE+2xQlHhLHGDkd2FEdCgUEL5RzJVge X-Received: by 2002:a05:6a21:3284:b0:ad:4a3e:a6e1 with SMTP id yt4-20020a056a21328400b000ad4a3ea6e1mr7411514pzb.11.1671681212207; Wed, 21 Dec 2022 19:53:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671681212; cv=none; d=google.com; s=arc-20160816; b=Jvsu9l/e41IJCtv3dJpGcrOGQUIALD5O13eZauiT2xKHyVkSAbkEPyMj4IBpdOu9bL jAY4lXzsC8hRxZ7gU3rUO1fNmT/h5TB+FtX16XT2Dxdvx1UVXqecBaXLGJM9FOtP/7h8 phhUQ03CiYjReISjP2bQPn6eKbT5XIKTZnCwh7GVj4f2N9XdbQLLQeBwmFftwUoWH7mp CuVmR7f0+6kVRjbSUBJZKyEFaYVJ8mQnTLVEVTktWAr5WjKqP0fx1WjJws3qWOox+FLh kRmxWWGMBv8KULQoPNTAcyjM1pHMvtJAlwhletFyXbyvjI78egEnbJMPTVar16zL9Bme DS9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/DXO/xkzv+oOyGzpC5LOOXQpFvH8Q3z3hxlmTcBNRSY=; b=EzhqDzRnZeeYBJw7K7FJEB7rf5vbT5bT4gU308TYatPglF6HS1tmKVM7Dx5n/6U+by jLYI7Hdj8vgGj2WqSPfyieIFd1rVwHoljK4hMsGRTs5WYV1FLutbPzNGsjJttXL1711k G8LtDMcE0FT9/ndd26M42VYIEIa9nLUs32TQf9IvKJWTIzIiLo3F4VOzI8oU2ktHt1QL +2uTBRdgMaUCJ6YQUxKRiylEU1VYWUfuVzJp6X5dwySwK5THYsZ0puz6G6inyDIFkgpe sI1sH7G5xZqP9tRK6dJnWlKMZXEPmEI7a8DXtZoldY3gbROuY8UMpesgDav8VibMsCo8 036w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b="H6nFm/1g"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d4-20020a63d704000000b004785b89b364si18600245pgg.134.2022.12.21.19.53.18; Wed, 21 Dec 2022 19:53:32 -0800 (PST) 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; dkim=pass header.i=@gnuweeb.org header.s=default header.b="H6nFm/1g"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234972AbiLVDwt (ORCPT + 99 others); Wed, 21 Dec 2022 22:52:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234961AbiLVDwo (ORCPT ); Wed, 21 Dec 2022 22:52:44 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAB8A183BB; Wed, 21 Dec 2022 19:52:43 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 51EF781A09; Thu, 22 Dec 2022 03:52:39 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681163; bh=sHkHhBasF8Ss/nuP4oociGJDOz1JciWpxGd76+s8DY0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H6nFm/1gBfuoB8yKWLxfxG6Kbbby018S+O2bZItLnMcqylkQWTclWqHDUnL30A195 v5Su7gPABOFItiU1tj6bKiN0ZykO7rVAlx+bpeI2oVmXsRKKJeFD2wTTlc6HWvysjy sGqWEa4obA7Y460y3RzvxbJ424FFB6qGrkgKQfKLGEIVrlS/sLN6R/eZy/cEtaqzgF vouk9MRZpHdE88cGt++6mgYmDFUYjfYmk3hH/c2ajLVd9WoGEs020Lm57Ppank4O89 sMYE8gZzN/4btGuacDcaXmTFBvRAfd0Hgylh0OjOqr1ZLIsx9SEXGL7ZjihlxHrlhh 1UTO71M4jHsIQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 6/8] selftests/nolibc: Add `sigaction(2)` selftest Date: Thu, 22 Dec 2022 10:51:32 +0700 Message-Id: <20221222035134.3467659-7-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752884798459584079?= X-GMAIL-MSGID: =?utf-8?q?1752884798459584079?= From: Ammar Faizi Test the sigaction() function implementation. Test steps: - Set a signal handler. - Then send a signal to itself using the kill() syscall. - The program has to survive and store the caught signal number in a volatile global variable. - Validate the volatile global variable value. - Restore the original signal handler. Only the x86-64 arch runs this test. Other architectures skip this test for now. Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 81 ++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 309cabbddeec..562766e0f63c 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -486,6 +486,86 @@ static int test_fork(void) return 0; } +static volatile int g_test_sig; + +static void test_sigaction_signal_handler(int sig) +{ + g_test_sig = sig; +} + +static int test_sigaction_sig(int sig) +{ + const struct sigaction new = { + .sa_handler = test_sigaction_signal_handler + }; + struct sigaction old; + int ret; + + /* + * Set the signal handler. + */ + ret = sigaction(sig, &new, &old); + if (ret) { + printf("test_sigaction_sig(%d): Failed to set a signal handler\n", sig); + return ret; + } + + /* + * Test the signal handler. + */ + g_test_sig = 0; + kill(getpid(), sig); + + /* + * test_sigaction_signal_handler() must set @g_test_sig to @sig. + */ + if (g_test_sig != sig) { + printf("test_sigaction_sig(%d): Invalid g_test_sig value (%d != %d)\n", sig, g_test_sig, sig); + return -1; + } + + /* + * Restore the original signal handler. + */ + ret = sigaction(sig, &old, NULL); + if (ret) { + printf("test_sigaction_sig(%d): Failed to restore the signal handler\n", sig); + return ret; + } + + return 0; +} + +static int test_sigaction(void) +{ + static const int sig_to_test[] = { + SIGINT, + SIGHUP, + SIGTERM, + SIGQUIT, + SIGSEGV + }; + size_t i; + int ret; + + for (i = 0; i < (sizeof(sig_to_test) / sizeof(sig_to_test[0])); i++) { + ret = test_sigaction_sig(sig_to_test[i]); + if (ret) + return ret; + } + + return 0; +} + +static int should_test_sigaction(void) +{ +#if defined(__x86_64__) + return 1; +#else + return 0; +#endif +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -566,6 +646,7 @@ int run_syscall(int min, int max) CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break; CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fds); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break; CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break; + CASE_TEST(sigaction); EXPECT_SYSZR(should_test_sigaction(), test_sigaction()); break; CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break; CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break; From patchwork Thu Dec 22 03:51:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 35598 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp121150wrn; Wed, 21 Dec 2022 19:56:31 -0800 (PST) X-Google-Smtp-Source: AMrXdXti+uOm83PzvrACphWIFIwVIxwKdT7xfkEZaumm1gOhPYO5mvysEb9z3bqmFf9RTX4YeV8p X-Received: by 2002:a05:6a20:4c85:b0:af:a896:850e with SMTP id fq5-20020a056a204c8500b000afa896850emr5121034pzb.58.1671681390929; Wed, 21 Dec 2022 19:56:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671681390; cv=none; d=google.com; s=arc-20160816; b=iOaS7fSeQONehgHskHFZ0hJx3sRJQr95qCZkvrtUGucrKMOvPUcc/PjXW342YT62e7 aq2VkioeZJitYYEbEKadSUaJJ+l5p7kKHXjLh01b8uNhDNkX+dPViY1a+TvQqIwdQGtO vTNKXhsf15MhLEnc+dSkqRuIPgumXDsFnO4BljLp0zcoMzGV5mLBd1MFLf5aC3ahUJuc sp52LjWaBvSt6UJr4201fT46Q7JxFCZxcX5tbzevp9ve7c0lg6obz1eYt0cFYDQ1Fu5M Tx1IWxjzlAQBB65/4m8XQgxYPGoyxMTaT+lrZm3V4BZh5GrVhPbN70bFDlLNU5/DZx2L ZY0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=I3O9PGVr2ZUJg87DSqI3G5SdZ+JzrL9jgRi6YkKS60c=; b=O/5vGQPwP85qbpRG6fzXSSx+Zg4HAhXxkO6LJW+kN7UToRjrxtIpowx4LcauVR/czZ E79tfsLnL0UQsP9lD0W/lHC8AS0HYZULfZOEYUH8rSOPMmpeFXIW3NjdHms+PVqmLKiy zTx9xVmLgfxW2N1Clftbp6io1O5NcGO+r/PanR2lHP8yCtHP9XJ8kwOghQVZjKO34o9j tEiiPMAbzgNZJE3fiHjlGXSPwjfKchIIBhbft9qsr6wiyk669bqU/oX5QUyzHVbGSD82 +8rWOgo4gyC9EpFLk7Pbf0JAtvACZAAXKi2ct7FVo91GiOhU9hH+c8rod9b4DikiBMC/ XClA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=qONtcf5V; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r19-20020a634413000000b00478d902b3e1si18507840pga.61.2022.12.21.19.56.17; Wed, 21 Dec 2022 19:56:30 -0800 (PST) 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; dkim=pass header.i=@gnuweeb.org header.s=default header.b=qONtcf5V; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234986AbiLVDwz (ORCPT + 99 others); Wed, 21 Dec 2022 22:52:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234973AbiLVDwt (ORCPT ); Wed, 21 Dec 2022 22:52:49 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98ECF1FCFA; Wed, 21 Dec 2022 19:52:48 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 2AE9F81A0E; Thu, 22 Dec 2022 03:52:43 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681168; bh=Pbtd2HHhDQ00BHSBZ/yOcX52AXNgYKhg1gIsAxcbhSk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qONtcf5VH641sO9ylZviZVjfcwZaksNMVTMBM1Pa/MzgIftb/CHu2rGiJT4S+fbYJ iddrlIvb+kOp2eEK82N0vGYGrxMHmcZStq8pSYCuG3i1EnZcyTQp1N507HS7CXNky2 OjMRQc3j0TfeSM3ServnH2owi3WXCdTfZVSi2dm3YAXMHUQk/RfcSupFwi4NAiXMqU /1AU8i80aXH58Cu3DufG4OmcIiiIVkkaXrk76Gwbhwnm9BtMCBuzFzTNVsgUiAjcAq gjy/0BM0owQXbuI8boSjgGgecyZJfpj60Qg0D9PFjU96dYdZvpTomgijhusEjsHo/B zF2Pb3mIClRsQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 7/8] selftests/nolibc: Add `signal(2)` selftest Date: Thu, 22 Dec 2022 10:51:33 +0700 Message-Id: <20221222035134.3467659-8-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752884985960343139?= X-GMAIL-MSGID: =?utf-8?q?1752884985960343139?= From: Ammar Faizi Just like the sigaction() selftest, but for signal(). Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 84 +++++++++++++++++--- 1 file changed, 72 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 562766e0f63c..a65cc6b83779 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -488,7 +488,7 @@ static int test_fork(void) static volatile int g_test_sig; -static void test_sigaction_signal_handler(int sig) +static void test_signal_handler(int sig) { g_test_sig = sig; } @@ -496,7 +496,7 @@ static void test_sigaction_signal_handler(int sig) static int test_sigaction_sig(int sig) { const struct sigaction new = { - .sa_handler = test_sigaction_signal_handler + .sa_handler = test_signal_handler }; struct sigaction old; int ret; @@ -517,7 +517,7 @@ static int test_sigaction_sig(int sig) kill(getpid(), sig); /* - * test_sigaction_signal_handler() must set @g_test_sig to @sig. + * test_signal_handler() must set @g_test_sig to @sig. */ if (g_test_sig != sig) { printf("test_sigaction_sig(%d): Invalid g_test_sig value (%d != %d)\n", sig, g_test_sig, sig); @@ -536,20 +536,74 @@ static int test_sigaction_sig(int sig) return 0; } +static int test_signal_sig(int sig) +{ + sighandler_t old; + + /* + * Set the signal handler. + */ + old = signal(sig, test_signal_handler); + if (old == SIG_ERR) { + printf("test_signal_sig(%d): Failed to set a signal handler\n", sig); + return -1; + } + + /* + * Test the signal handler. + */ + g_test_sig = 0; + kill(getpid(), sig); + + /* + * test_signal_handler() must set @g_test_sig to @sig. + */ + if (g_test_sig != sig) { + printf("test_signal_sig(%d): Invalid g_test_sig value (%d != %d)\n", sig, g_test_sig, sig); + return -1; + } + + /* + * Restore the original signal handler. + */ + old = signal(sig, old); + if (old == SIG_ERR) { + printf("test_signal_sig(%d): Failed to restore the signal handler\n", sig); + return -1; + } + + return 0; +} + +static const int g_sig_to_test[] = { + SIGINT, + SIGHUP, + SIGTERM, + SIGQUIT, + SIGSEGV +}; + static int test_sigaction(void) { - static const int sig_to_test[] = { - SIGINT, - SIGHUP, - SIGTERM, - SIGQUIT, - SIGSEGV - }; size_t i; int ret; - for (i = 0; i < (sizeof(sig_to_test) / sizeof(sig_to_test[0])); i++) { - ret = test_sigaction_sig(sig_to_test[i]); + for (i = 0; i < (sizeof(g_sig_to_test) / sizeof(g_sig_to_test[0])); i++) { + ret = test_sigaction_sig(g_sig_to_test[i]); + if (ret) + return ret; + } + + return 0; +} + +static int test_signal(void) +{ + size_t i; + int ret; + + for (i = 0; i < (sizeof(g_sig_to_test) / sizeof(g_sig_to_test[0])); i++) { + ret = test_signal_sig(g_sig_to_test[i]); if (ret) return ret; } @@ -566,6 +620,11 @@ static int should_test_sigaction(void) #endif } +static int should_test_signal(void) +{ + return should_test_sigaction(); +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -647,6 +706,7 @@ int run_syscall(int min, int max) CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fds); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break; CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break; CASE_TEST(sigaction); EXPECT_SYSZR(should_test_sigaction(), test_sigaction()); break; + CASE_TEST(signal); EXPECT_SYSZR(should_test_signal(), test_signal()); break; CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break; CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break; CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break; From patchwork Thu Dec 22 03:51:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 35599 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp127962wrn; Wed, 21 Dec 2022 20:20:55 -0800 (PST) X-Google-Smtp-Source: AMrXdXvNukzwUaS49FSpWh0ZbUbs4B75bBOkAbLoXLWrpBGbvH99BzxpqfKylJ4UsnLzT+KtXiZc X-Received: by 2002:a17:906:5048:b0:7c0:b770:df94 with SMTP id e8-20020a170906504800b007c0b770df94mr3378324ejk.63.1671682855790; Wed, 21 Dec 2022 20:20:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671682855; cv=none; d=google.com; s=arc-20160816; b=KAsi72BVfR0e+gUnvsxZmaWP+hauKwW9xS73KCkviDbjxAs5I514DxGV4ammEz9p4A 65dQKh3hbokkd8lmBXtem03Vwl/RolGxitAm7hEWpxac76NB6N1D3cYTQAnop9Z/M8g0 cJcSV7vXsm4fu87/8bs3k78i9fsvjo7yhPODB6GDBruavESgB0KF6mRIRIrghx+AC5cy NW41SjhtO6n8/xgj1CgWUGX0vPIpDykjjv95pMWwA/GJobUQ7k7+7lphh7vx+Slr3fVh lZFbkYLYBTZI0xo2NXd2A/Xle3z+/+AYPM0hIut9z4xHF7ojQFoWGB8lDolXdkiHXW4U 8qvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Fg2O7UbPcgUwRWPobbOXK51PoJFSb9ID4Zc88KnkMp8=; b=vXsD2yqqO07+S3u7koXlDd2fEXQdgfUosdzJNxu/3QLIYRKUCUDFPSDLbQIgtJsuei 5aEnS6ITFoe6+dXhjFeG9dA+4t8Vnl5V2Cvff5PBqEzeLmgWNY+0Il/XJb0zWkOJ3F70 B63T2q3VOHWDjS5pfBoM9IkYl/NJohpZJ1vZpiCzjhVP5xglrWaIXucqIbFKkh5ZkXT5 zg4wCST3/fZq1Dukj5/lp50fkryp8+y3CHop805jJ+uBBGmNxmaD8m2tSm35+a7bog53 ptLQs4q+MauoDXlKHqNzM4uqIYRdBdWW5KR4kPm2xXf5AfxZBYTaZs4T9HmnyXuITy3O VolQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=o4i7RoeK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l15-20020a056402254f00b004761159f699si16480356edb.430.2022.12.21.20.20.32; Wed, 21 Dec 2022 20:20:55 -0800 (PST) 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; dkim=pass header.i=@gnuweeb.org header.s=default header.b=o4i7RoeK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234995AbiLVDw5 (ORCPT + 99 others); Wed, 21 Dec 2022 22:52:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234968AbiLVDwy (ORCPT ); Wed, 21 Dec 2022 22:52:54 -0500 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7211918B24; Wed, 21 Dec 2022 19:52:53 -0800 (PST) Received: from localhost.localdomain (unknown [182.253.88.132]) by gnuweeb.org (Postfix) with ESMTPSA id 0652881A11; Thu, 22 Dec 2022 03:52:48 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1671681173; bh=yq8GJZ+I6uJDzwFMLP2mAHZVUUL3XN13II1COgW8aFc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o4i7RoeKrTw8/sVfcITMdNGxBcpXoHEFGyTASrZyigmDqnUB2UeeQ0L8Ydl1f1otK eTNJ+/cXNHJ5NKNy69Pz25Fl3OaL6OueWiVb8AEJwNuxazEGyxm5sWouANg2TvnyH9 DsI+/pWhbWe8vAviG2ppCY87dTzC7hnHRzKvwRI3h6YQ1HHWVMxT3t8MZ+kJT1MmV0 97BfkrClE7Q6ln+pRzoKPEYnTxUtl95jGDpMaGhYeTLfhwyhaO0m8DHsIUl5S+KmZX NyBt1/W3n8FNs27j5ONGmIIbHRrcyeXsDk507wEezPdVUbfbBbQl0bsZEhKLkUqqA1 4y2EY1xQCS4FQ== From: Ammar Faizi To: Willy Tarreau , Shuah Khan , "Paul E. McKenney" Cc: Ammar Faizi , Gilang Fachrezy , VNLX Kernel Department , Alviro Iskandar Setiawan , Kanna Scarlet , Muhammad Rizki , GNU/Weeb Mailing List , Linux Kernel Mailing List , Linux Kselftest Mailing List Subject: [RFC PATCH v1 8/8] selftests/nolibc: Add `getpagesize(2)` selftest Date: Thu, 22 Dec 2022 10:51:34 +0700 Message-Id: <20221222035134.3467659-9-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222035134.3467659-1-ammar.faizi@intel.com> References: <20221222035134.3467659-1-ammar.faizi@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752886522263665545?= X-GMAIL-MSGID: =?utf-8?q?1752886522263665545?= From: Ammar Faizi Test the getpagesize() function. Make sure it returns the correct value. Signed-off-by: Ammar Faizi --- tools/testing/selftests/nolibc/nolibc-test.c | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index a65cc6b83779..19b3ed5f2356 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -625,6 +625,36 @@ static int should_test_signal(void) return should_test_sigaction(); } +static int test_getpagesize(void) +{ + long x = getpagesize(); + int c; + + if (x < 0) + return x; + +#if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) + /* + * x86 family is always 4K page. + */ + c = (x == 4096); +#elif defined(__aarch64__) + /* + * Linux aarch64 supports three values of page size: 4K, 16K, and 64K + * which are selected at kernel compilation time. + */ + c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024)); +#else + /* + * Assuming other architectures must have at + * least 4K page. + */ + c = (x >= 4096); +#endif + + return !c; +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. */ @@ -686,6 +716,7 @@ int run_syscall(int min, int max) CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *)1), -1, EFAULT); break; CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *)1), -1, EFAULT); break; #endif + CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break; CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break; CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break; CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break;