From patchwork Tue Aug 1 15:39:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yuan Tan X-Patchwork-Id: 13336951 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74125C00528 for ; Tue, 1 Aug 2023 15:40:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234184AbjHAPkV (ORCPT ); Tue, 1 Aug 2023 11:40:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234180AbjHAPkU (ORCPT ); Tue, 1 Aug 2023 11:40:20 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7ADE0213D; Tue, 1 Aug 2023 08:40:04 -0700 (PDT) X-QQ-mid: bizesmtp86t1690904397t1qsics3 Received: from dslab-main2-ubuntu.tail147f4.ts ( [202.201.15.117]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 01 Aug 2023 23:39:56 +0800 (CST) X-QQ-SSF: 01200000000000705000000A0000000 X-QQ-FEAT: D2GZf6M6C/i2/7WsKIkl9OkzBxEGFzGXMgnT/XtLNUHlu8FLNK6N6zWdnhOnZ Cyj6BQpBjNGWAQMvloYpeyl1qZ+/5/86HEX4bQZvgzMkKHTgVp1OzsJ+K4uJBapkNkz92nD PEVSAYyVc8VeZRTfzitgRKEojjcuSZJUuUCj+igoeIC+EozWMCZKq10fgS2Rw5KiNK9y871 VAcPD616zUrh5yfeXg07s8/JqM044XCZpVUw1qav27wNIq9lcNdKpkMSqH7lSYANegVfade AJoae5ww/HpE0RQMvyih9j3nIcn2MymGsOkAkcPE2Uo+pCwZj3GYOFiUl+61GG9lMXpA/RF TtG8TyWJ2uQbFyTi7wbvakMb+WRjLxtuXxImFIc0owKMJP+dHCoBMMTQGmLSQ== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 7644089916909828399 From: Yuan Tan To: w@1wt.eu, thomas@t-8ch.de Cc: falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Yuan Tan Subject: [PATCH v3 1/2] tools/nolibc: add pipe() and pipe2() support Date: Tue, 1 Aug 2023 23:39:54 +0800 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz5a-1 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org According to manual page [1], posix spec [2] and source code like arch/mips/kernel/syscall.c, for historic reasons, the sys_pipe() syscall on some architectures has an unusual calling convention. It returns results in two registers which means there is no need for it to do verify the validity of a userspace pointer argument. Historically that used to be expensive in Linux. These days the performance advantage is negligible. Nolibc doesn't support the unusual calling convention above, luckily Linux provides a generic sys_pipe2() with an additional flags argument from 2.6.27. If flags is 0, then pipe2() is the same as pipe(). So here we use sys_pipe2() to implement the pipe(). pipe2() is also provided to allow users to use flags argument on demand. [1]: https://man7.org/linux/man-pages/man2/pipe.2.html [2]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html Suggested-by: Zhangjin Wu Link: https://lore.kernel.org/all/20230729100401.GA4577@1wt.eu/ Signed-off-by: Yuan Tan --- tools/include/nolibc/sys.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 8bfe7db20b80..56f63eb48a1b 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -752,6 +752,30 @@ int open(const char *path, int flags, ...) } +/* + * int pipe2(int pipefd[2], int flags); + * int pipe(int pipefd[2]); + */ + +static __attribute__((unused)) +int sys_pipe2(int pipefd[2], int flags) +{ + return my_syscall2(__NR_pipe2, pipefd, flags); +} + +static __attribute__((unused)) +int pipe2(int pipefd[2], int flags) +{ + return __sysret(sys_pipe2(pipefd, flags)); +} + +static __attribute__((unused)) +int pipe(int pipefd[2]) +{ + return pipe2(pipefd, 0); +} + + /* * int prctl(int option, unsigned long arg2, unsigned long arg3, * unsigned long arg4, unsigned long arg5); From patchwork Tue Aug 1 15:40:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yuan Tan X-Patchwork-Id: 13336952 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DADC7C001DF for ; Tue, 1 Aug 2023 15:40:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231994AbjHAPko (ORCPT ); Tue, 1 Aug 2023 11:40:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232257AbjHAPkm (ORCPT ); Tue, 1 Aug 2023 11:40:42 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.65.254]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C058C18B; Tue, 1 Aug 2023 08:40:32 -0700 (PDT) X-QQ-mid: bizesmtp66t1690904426thfpaxec Received: from dslab-main2-ubuntu.tail147f4.ts ( [202.201.15.117]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 01 Aug 2023 23:40:25 +0800 (CST) X-QQ-SSF: 01200000000000705000000A0000000 X-QQ-FEAT: kN2ypXZVqgwk/qBTNkd/iwlu662yQb25L197c9fZh4f02d1PhBEH9w+ZHw7Rb DlkUdXFYyspKmDSuSJXiIMbzbjgNRqkjqijFmGNyFtwdsRFrG3oMJnQ3J/4XtKc7ShV2PFG ek1MKLByFcQbrt2zsxytHpsxf5xa1Mh6V9Qi9uAsdEe+9DjTp8LxeiGbEN4fHAUQsvn4rAh NNFDpDG8gVDnEmmNOadR+qCTMNf5yOmt9dZ4LJTR27ApvzJ4uD+M32A451Sft4YN76apPBu vixRcPOdbDHBHVCwLXMuPi04EZOue08fwYz28/EdyooUDGcLljWKfoPUqMlumGAeFHTt8yv QhhuSvEqMsfTJo1HmVLY8qxjQEBjBlQ1WS9Hhyh/iL4x6rNwzyB6ty1JVK3MQ== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 5494693937254520093 From: Yuan Tan To: w@1wt.eu, thomas@t-8ch.de Cc: falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Yuan Tan Subject: [PATCH v3 2/2] selftests/nolibc: add testcase for pipe Date: Tue, 1 Aug 2023 23:40:23 +0800 Message-Id: <508ed252b8f60494d70cd211debbb297ee916325.1690903601.git.tanyuan@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz5a-1 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add a test case of pipe that sends and receives message in a single process. Suggested-by: Thomas Weißschuh Suggested-by: Willy Tarreau Link: https://lore.kernel.org/all/c5de2d13-3752-4e1b-90d9-f58cca99c702@t-8ch.de/ Signed-off-by: Yuan Tan --- tools/testing/selftests/nolibc/nolibc-test.c | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 03b1d30f5507..e5667fa3cf0a 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -767,6 +767,27 @@ int test_mmap_munmap(void) return ret; } +static int test_pipe(void) +{ + const char *const msg = "hello, nolibc"; + int pipefd[2]; + char buf[32]; + ssize_t len; + + if (pipe(pipefd) == -1) + return 1; + + write(pipefd[1], msg, strlen(msg)); + close(pipefd[1]); + len = read(pipefd[0], buf, sizeof(buf)); + close(pipefd[0]); + + if (len != strlen(msg)) + return 1; + + return !!memcmp(buf, msg, len); +} + /* Run syscall tests between IDs and . * Return 0 on success, non-zero on failure. @@ -851,6 +872,7 @@ int run_syscall(int min, int max) CASE_TEST(mmap_munmap_good); EXPECT_SYSZR(1, test_mmap_munmap()); break; CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break; CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break; + CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break; CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break; CASE_TEST(poll_stdout); EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break; CASE_TEST(poll_fault); EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;