From patchwork Wed Jan 15 11:13:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Celeste Liu X-Patchwork-Id: 13940313 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BBA1C02183 for ; Wed, 15 Jan 2025 11:14:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 020B36B0085; Wed, 15 Jan 2025 06:14:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F131F6B008C; Wed, 15 Jan 2025 06:14:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8DD9280001; Wed, 15 Jan 2025 06:14:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B85EA6B0085 for ; Wed, 15 Jan 2025 06:14:19 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7BEB4AF274 for ; Wed, 15 Jan 2025 11:14:19 +0000 (UTC) X-FDA: 83009427438.29.D2A9209 Received: from flow-a3-smtp.messagingengine.com (flow-a3-smtp.messagingengine.com [103.168.172.138]) by imf04.hostedemail.com (Postfix) with ESMTP id 6F7BC40012 for ; Wed, 15 Jan 2025 11:14:17 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b=bMtaM99w; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="v wKAEsD"; dmarc=pass (policy=quarantine) header.from=coelacanthus.name; spf=pass (imf04.hostedemail.com: domain of uwu@coelacanthus.name designates 103.168.172.138 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736939657; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WfiGYnphGqvudtD6RXFjAbGTohKPfJnsfNrRsHzlEL4=; b=7ae7TmjPPU0siu57xTbI8MBjmRMoSORjHkYjNZ69sg5jLrypfeoCBkL/2CmdlCYQIFU1jM iemYLNjt6dZY/9yFJxmWC1zggri2fJCPkCnfBzdZqEq95FMDkNpJoaKqVlt1ItbCJxqeVE mwWl9YxZZQsYtqW9gq+gwiG5sdkMLG8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736939657; a=rsa-sha256; cv=none; b=lX1RSZ1AjLjRyLvcJVze5vpQky1blvEcYfnVCN/DrZgNVU91CnkfppsTs1Her71uOTTfIv 9ghE/OGz1Ev0ZDgB/2j55sMGCe/yYW/HZvEDvhMNl5Bnc4jvNTWp4b1sCXIpJkhHEP8432 z56i6jKcX8MG2mjiVE5w0yaMjp+BB7w= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b=bMtaM99w; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="v wKAEsD"; dmarc=pass (policy=quarantine) header.from=coelacanthus.name; spf=pass (imf04.hostedemail.com: domain of uwu@coelacanthus.name designates 103.168.172.138 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailflow.phl.internal (Postfix) with ESMTP id EB8BD200F84; Wed, 15 Jan 2025 06:14:16 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Wed, 15 Jan 2025 06:14:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= coelacanthus.name; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1736939656; x=1736946856; bh=WfiGYnphGq vudtD6RXFjAbGTohKPfJnsfNrRsHzlEL4=; b=bMtaM99wg2t0sGsWBBr9DRK+d4 6ElpWK4uVOU6I3S3fC5DrwS6XS5Bop4pBipUtYzdgo4tTPDBqilV9gQjblb7pgKW ZThERb5+upl9p5jRj57trl5UY5G6X2fdNebgjKdTBbk3bj83Kam9brdokLXr8JZ8 sm/eCiITI9F33g6DwKwtci7MDQ3jW5GpP8Vq0mtdZYzxCQqwass2ShGCvnRPmesg XbYrGe0uVK1I6L/cI3kppZskPKPTdMOvWypAUCdu3h7I+FB5NF/w884oue9CZ61/ 7q8cgJu5xGBsC4KeuTZaxkZCi6zyvvQ20ysPThLy0CZU5UqFks8YOFrG2z0Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1736939656; x= 1736946856; bh=WfiGYnphGqvudtD6RXFjAbGTohKPfJnsfNrRsHzlEL4=; b=v wKAEsDy3E1Ykbo7zHQnkulDZ0dw9Uq6wZLJvdF2P1jPNEiKelP0sTZHWecUpSK4E bZStqGKG+BWi+Jjfx5jbblUqXArW/EGqqg14euV5qQYyzba7tFTFLGOSeQb/hO2w BhJ/RVMEp3kiuEfKLJNM3uARulwsZNbAZ7MMiV+QizNSIKk76aOoNQ5bxToWdwJe 5o41MBSDvENrjJidvtEzU66HcKY8fRr5if0H+hCPb+OwmayemsQU48Q5BuaJM2wR ju6vialqhZEfzvRe44PPV3Y3senx75wSOtZz2WJsQh2SJV8sZWdQfOAjzh4hBWXP mi+rHqlywRwL0ECEM7ydg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrudehlecutefuodetggdotefrodftvfcurf hrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffrtefo kffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsuc dlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthekredtredtjeenucfh rhhomhepvegvlhgvshhtvgcunfhiuhcuoehufihusegtohgvlhgrtggrnhhthhhushdrnh grmhgvqeenucggtffrrghtthgvrhhnpeeuiefgudffleffgeeiffeffeefieelveejgfeh hfejffeutedvffeiudevtdelgeenucffohhmrghinhepkhgvrhhnvghlrdhorhhgnecuve hluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepuhifuhestgho vghlrggtrghnthhhuhhsrdhnrghmvgdpnhgspghrtghpthhtohepvdejpdhmohguvgepsh hmthhpohhuthdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghr nhgvlhdrohhrghdprhgtphhtthhopehlughvsehsthhrrggtvgdrihhopdhrtghpthhtoh eptghhrghrlhhivgesrhhivhhoshhinhgtrdgtohhmpdhrtghpthhtohepsghjohhrnhes khgvrhhnvghlrdhorhhgpdhrtghpthhtohepshhhuhgrhheskhgvrhhnvghlrdhorhhgpd hrtghpthhtoheprhgvseifiehriidrnhgvthdprhgtphhtthhopegvsghivgguvghrmhes gihmihhsshhiohhnrdgtohhmpdhrtghpthhtohepiihhohhuqhhurghnsehishgtrghsrd grtgdrtghnpdhrtghpthhtoheplhhinhhugidqkhhsvghlfhhtvghsthesvhhgvghrrdhk vghrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: i95c648bc:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 15 Jan 2025 06:14:14 -0500 (EST) From: Celeste Liu Date: Wed, 15 Jan 2025 19:13:27 +0800 Subject: [PATCH v6 1/3] riscv/ptrace: add new regset to access original a0 register MIME-Version: 1.0 Message-Id: <20250115-riscv-new-regset-v6-1-59bfddd33525@coelacanthus.name> References: <20250115-riscv-new-regset-v6-0-59bfddd33525@coelacanthus.name> In-Reply-To: <20250115-riscv-new-regset-v6-0-59bfddd33525@coelacanthus.name> To: Oleg Nesterov , Paul Walmsley , Palmer Dabbelt , Eric Biederman , Kees Cook , Shuah Khan , Albert Ou Cc: Alexandre Ghiti , "Dmitry V. Levin" , Andrea Bolognani , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Thomas Gleixner , Ron Economos , Charlie Jenkins , Andrew Jones , Quan Zhou , Felix Yan , Ruizhe Pan , Guo Ren , Yao Zi , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Celeste Liu , stable@vger.kernel.org, =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3197; i=uwu@coelacanthus.name; h=from:subject:message-id; bh=Bd0I0lIMKMC4KxEOjUIKWSGVerkNEic9IL2ALFZ+R48=; b=owJ4nJvAy8zAJeafov85RWVtBeNptSSG9PYZ5VutbCad5Tn5ndOR38uJYV7Dv+6vnvq+FfPTO Y/NcE6L4e4oZWEQ42KQFVNkySth+cl56Wz33o7tXTBzWJlAhjBwcQrAROaGMTI8d991KlKo4aFX I19XSsrdpJofD4xDWCJfvDU3FFGb/7qL4X8Ux5rc0wu8ddemZqSnL2Bb/XjS3wA172m/l4Umzts xwZIDAL8wRz0= X-Developer-Key: i=uwu@coelacanthus.name; a=openpgp; fpr=892EBC7DC392DFF9C9C03F1D15F4180E73787863 X-Stat-Signature: fcsr3yhofc8ohdyx3iozsxqtycrj5x5b X-Rspamd-Queue-Id: 6F7BC40012 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736939657-838907 X-HE-Meta: U2FsdGVkX18lochEnMO9qNnhMeI8GcGPLuR4W4Yx2W+iVUa6JeThG2I6kASIhr8HVXTD2pg+K4uJxpudTj7IJmiSJSm6M2oagGYXOAykPEJAyK0+Rey+2ORISmL8dxBwpN8rXebZpHuuHZHnwGbN9aYlunGrjAn5pZJxruUBafA1PT2SM3bkpPkrOZEqncUXuyyi4/SIAWlzZwp+aUIesJ5eXX8e4WJ1ewIv8FPbTCKy+a4l7eB+viOjuvIXvNiF0ampQ/ynrn6CfhvtaLXu6hdftb/rjixrYrLAnj8ScXv77IbMsYKQlQiT2mId62tvQXU1sGgeX8Q14/dKKLYu+MhQIu+L9h+VAAlnkWAgfmjlyy/dnYKFCu1snRUukgrwgIGwS51CZ0f8bTuvTyO4bRtTA8urlkrEqzY7M+wPrP/MOmbcUqH8D3K9nmUi3YEpDgs6Kp31xeEiO0mnaSFqsPgqCvYwonNd0N7VPemT4SaLv23es7lzOirOkHqrMnGM1Yy0J6sNjz0ej8TMhmQgujw3O+Z80uK7TrHCmCunn+Bf2dDS1WFYYGPunSLSiFg0UyumNKrbQA72+QlHdUAVJFRvwKuN4yHoW3R6ngZsn/97blpdVgkUuJEENJ8NrXDBd6AyL17Nw3U69vssljmU/748KW3eNyIbIzExXeeHW24jBMZ7GztqB4IbUM9JplfkazhxelsEjJOslA7rmNSa+0VjWEvnv3/rskJ7R/+VX2G+8fqyTBsJRdJymABAsb1LOzJFiRzFQ7N5/AFckFME2eZrvxZIZDtMQ2QbbG7A8Iu6vjzqJoQoTwaHlo3/NKawWDtV6JJTxghgY8dXBH+l1J7w4IU/tPca3fJMj+ov/Vs5kZGUi5nictbCEHU1ZUPim+ZgPJZbpRp7g0E7R8FoqNtL8z4r4fpgXn83a73hePDQSewgy6i78ubccuBa2vmubIUTshCS/5SQmDyXGq8 vMZ4tbMI s1fXDVCOGJJmvHxnAbpA/wCpWtlC82NHibsVcsM0uXUZlskSR1aLQxmgWImBGrk+XyVbe3ZF64lYuPonmXlEFZriSzn0tWcFARUFIfjTCARGD14ccQoXL12cXvl7y6r9L22xSu9rz7pWoqcv9KyHVwvBZnh7xtTXE8JmdMgkGawBrO1pS6RvhsUn76xZaGVyHiQs1hng8CUzABO7/GFso/x6cXB97RYy0LbCox/+dj1jbOt2nwMSz0ucDcTrV691HdKrmaaNTHHwpfjuDAZXC4AX1iHXCs5SRddqAGyYbKIbHWOwy+4qoQy+nS2XCz4LPNAKdVV+ngiXFaH/z0MQILdOT3PtJpb7O0PNf4m+cN7iZYiYUWZhFKS8tt13Dj+7/0hJQuFBW6ZRoZJ/sWi0HOdnEyCso38h1FHsjRvFCewNcodu2XfIJ5kIKelY08nMm+wnWpJlfnXF7XcImhRA59HLotMOpuuaca7VzPW1na/4c9McxswfOdJXPHBycX/Bl0krIo1I8egLyIU1Dlx7357EZq/Qbqc9mTYuyMctCwbzcUt7wubeQHIXmIdfU3udO00rAdgX5I68ttDjKLa/28qAMrfjQuBJSDnTC6O1lf9vqGnNaoKrax866wYJgErs2Ztl9Ni2Rn4t28GM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The orig_a0 is missing in struct user_regs_struct of riscv, and there is no way to add it without breaking UAPI. (See Link tag below) Like NT_ARM_SYSTEM_CALL do, we add a new regset name NT_RISCV_ORIG_A0 to access original a0 register from userspace via ptrace API. Fixes: e2c0cdfba7f6 ("RISC-V: User-facing API") Link: https://lore.kernel.org/all/59505464-c84a-403d-972f-d4b2055eeaac@gmail.com/ Cc: stable@vger.kernel.org Reviewed-by: Björn Töpel Signed-off-by: Celeste Liu --- arch/riscv/kernel/ptrace.c | 32 ++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 2 files changed, 33 insertions(+) diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index ea67e9fb7a583683b922fe2c017ea61f3bc848db..ef9ab74c8575a5c440155973b1c625c06a867c97 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -31,6 +31,7 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_SUPM REGSET_TAGGED_ADDR_CTRL, #endif + REGSET_ORIG_A0, }; static int riscv_gpr_get(struct task_struct *target, @@ -184,6 +185,29 @@ static int tagged_addr_ctrl_set(struct task_struct *target, } #endif +static int riscv_orig_a0_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + return membuf_store(&to, task_pt_regs(target)->orig_a0); +} + +static int riscv_orig_a0_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + unsigned long orig_a0 = task_pt_regs(target)->orig_a0; + int ret; + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &orig_a0, 0, -1); + if (ret) + return ret; + + task_pt_regs(target)->orig_a0 = orig_a0; + return 0; +} + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -224,6 +248,14 @@ static const struct user_regset riscv_user_regset[] = { .set = tagged_addr_ctrl_set, }, #endif + [REGSET_ORIG_A0] = { + .core_note_type = NT_RISCV_ORIG_A0, + .n = 1, + .size = sizeof(elf_greg_t), + .align = sizeof(elf_greg_t), + .regset_get = riscv_orig_a0_get, + .set = riscv_orig_a0_set, + }, }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index b44069d29cecc0f9de90ee66bfffd2137f4275a8..390060229601631da2fb27030d9fa2142e676c14 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -452,6 +452,7 @@ typedef struct elf64_shdr { #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ #define NT_RISCV_TAGGED_ADDR_CTRL 0x902 /* RISC-V tagged address control (prctl()) */ +#define NT_RISCV_ORIG_A0 0x903 /* RISC-V original a0 register */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ From patchwork Wed Jan 15 11:13:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Celeste Liu X-Patchwork-Id: 13940314 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBEC1C02183 for ; Wed, 15 Jan 2025 11:14:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 291E96B008A; Wed, 15 Jan 2025 06:14:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21C9B6B008C; Wed, 15 Jan 2025 06:14:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 046106B0092; Wed, 15 Jan 2025 06:14:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D81A56B008A for ; Wed, 15 Jan 2025 06:14:23 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5393812123B for ; Wed, 15 Jan 2025 11:14:23 +0000 (UTC) X-FDA: 83009427606.15.F02D55D Received: from flow-a3-smtp.messagingengine.com (flow-a3-smtp.messagingengine.com [103.168.172.138]) by imf04.hostedemail.com (Postfix) with ESMTP id 30F8340005 for ; Wed, 15 Jan 2025 11:14:21 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b=oRHyd1GL; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="i LhZEEs"; spf=pass (imf04.hostedemail.com: domain of uwu@coelacanthus.name designates 103.168.172.138 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name; dmarc=pass (policy=quarantine) header.from=coelacanthus.name ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736939661; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cefyME3vtsv5sxq1SOu8UxjoNujymAivw3tNwb5ahHc=; b=1AS3xdrYeUe1XAyfE8WASWaTGmAjetoDSAu1tZ2mOy/4Q84KcF7zcAgE54o9JTK4eliRS3 ybdsx68/v3zBo9ePhHFMuQwzNkzDeFyelZOE7n2um4ftCLYUE+QIRQ45eCog6ak1KjksfP 4HqdwJUEus20U/824QagOjC9SW9dxiI= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b=oRHyd1GL; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="i LhZEEs"; spf=pass (imf04.hostedemail.com: domain of uwu@coelacanthus.name designates 103.168.172.138 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name; dmarc=pass (policy=quarantine) header.from=coelacanthus.name ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736939661; a=rsa-sha256; cv=none; b=CVARV+Ud6rCPV6wr57f3tD3RH5FE+VlJ2VcLdaAxcTfZBQCSAyoOBB7XsIznxtQE85i/Wo I7kDrRLmEuttWPE/5fTubp/pH6tdfTpMaeJQ1JC02LH73OpynX4bYRyEHV68cg4GnJtNlM SRVFNdeiCBJ7GKkPx8rRqho6SrKaVaM= Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailflow.phl.internal (Postfix) with ESMTP id 92B7F200FE6; Wed, 15 Jan 2025 06:14:20 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Wed, 15 Jan 2025 06:14:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= coelacanthus.name; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1736939660; x=1736946860; bh=cefyME3vts v5sxq1SOu8UxjoNujymAivw3tNwb5ahHc=; b=oRHyd1GLCtmaP88DZ05pDtICaK OXw8OwHKrV8RGJieDIm5D9l6iNnei/l4hT99f4K8RrtFscZE6ICqqnnLU+ZfQ3MU uruFIOmMbeQN0voNVhU+se82GeGFwSzrV8lY+yHw/KdaQCTIrl9uTyqP1r+OwLPu xXcpeWGpLHTZ23FFOUKh/Xy0aAj18F793ZXbThyN0dxM2NOZloQ7P4gxeMXMgCbn siNJlN3Aq0SbrlqJJNZgsa1Qq0E1u/VG0+pnUT2MRdYtHE8rlvxwbewJXdbA1B4R +Tbpbj79cJg8NRFyqaTWdirdQqnDQHlYVHkepAorS9CVeJvPngCgxZSljYLg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1736939660; x= 1736946860; bh=cefyME3vtsv5sxq1SOu8UxjoNujymAivw3tNwb5ahHc=; b=i LhZEEsE7v4/MIZ3/8d3eXsH7iCiNgXMU3vV3irR8cGwgPliFyOSWdZKRyITcV5ZX eX8lLYDKBG2uB5SqnUsCdlNrRGrYoWCnFZqy8PJeQ8d3ECqs1sxktl9oSgbF3/N2 kDWKqpFRkzqWl945myxuK6xbmVhNYChPxQpmOo8PCU/VPrGE24ruw0hsPNd6xVkB pTwhSFPAQte67aj4lyF9Fp5RW67iYRNYXMNnXUxnjbC/5boKWoEnA2omvs3D9uf1 Jtc5zrNPWAXSiF/YgiAFRPiwkKZ7b4wzCRQWozO+WOxFv4RTgogUj2aMS7n9+jUp 0UtHD1+TMLJgS4kJXiOfg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrudehlecutefuodetggdotefrodftvfcurf hrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffrtefo kffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsuc dlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthejredtredtjeenucfh rhhomhepvegvlhgvshhtvgcunfhiuhcuoehufihusegtohgvlhgrtggrnhhthhhushdrnh grmhgvqeenucggtffrrghtthgvrhhnpeeiteejtdevjeffgfehkeegfeelkeekvdehtdeg leevhfetheejheejiedtjeegteenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehufihusegtohgvlhgrtggrnhhthhhushdrnhgrmhgvpdhnsggp rhgtphhtthhopedvhedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplhhinhhugi dqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhguvhes shhtrhgrtggvrdhiohdprhgtphhtthhopegthhgrrhhlihgvsehrihhvohhsihhntgdrtg homhdprhgtphhtthhopegsjhhorhhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehs hhhurghhsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehrvgesfieirhiirdhnvghtpd hrtghpthhtohepvggsihgvuggvrhhmseigmhhishhsihhonhdrtghomhdprhgtphhtthho peiihhhouhhquhgrnhesihhstggrshdrrggtrdgtnhdprhgtphhtthhopehlihhnuhigqd hkshgvlhhfthgvshhtsehvghgvrhdrkhgvrhhnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i95c648bc:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 15 Jan 2025 06:14:19 -0500 (EST) From: Celeste Liu Date: Wed, 15 Jan 2025 19:13:28 +0800 Subject: [PATCH v6 2/3] tools: copy include/linux/stddef.h to tools/include MIME-Version: 1.0 Message-Id: <20250115-riscv-new-regset-v6-2-59bfddd33525@coelacanthus.name> References: <20250115-riscv-new-regset-v6-0-59bfddd33525@coelacanthus.name> In-Reply-To: <20250115-riscv-new-regset-v6-0-59bfddd33525@coelacanthus.name> To: Oleg Nesterov , Paul Walmsley , Palmer Dabbelt , Eric Biederman , Kees Cook , Shuah Khan , Albert Ou Cc: Alexandre Ghiti , "Dmitry V. Levin" , Andrea Bolognani , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Thomas Gleixner , Ron Economos , Charlie Jenkins , Andrew Jones , Quan Zhou , Felix Yan , Ruizhe Pan , Guo Ren , Yao Zi , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Celeste Liu X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=4577; i=uwu@coelacanthus.name; h=from:subject:message-id; bh=WV0NqvqtTK1mWkN78Oo6GA0cv0m8xWUUF/b/ZrqzqEo=; b=owJ4nJvAy8zAJeafov85RWVtBeNptSSG9PYZFdHJnxQvWgTnrrt4QWqTwV/zbd1fOHm+8piqL H7LlzfPIKCjlIVBjItBVkyRJa+E5SfnpbPdezu2d8HMYWUCGcLAxSkAE6ncz8iw/93V6o+mZ8wn SyYeMDt4u+nxizae6PPGbtsbv/s84zRlZPif1tGg3LzHjPPH7vwT/EXzzTbECkmeEP4343DKkhA j9i9cANkLSSY= X-Developer-Key: i=uwu@coelacanthus.name; a=openpgp; fpr=892EBC7DC392DFF9C9C03F1D15F4180E73787863 X-Rspamd-Queue-Id: 30F8340005 X-Stat-Signature: 1c8xse131gz51tzwcb1biet3wuhx9ub5 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1736939661-642185 X-HE-Meta: U2FsdGVkX19YMKSK5CE52pJMrTYSrnwr91pEtl3aTgR2P09omjhr/uw4LQmwM7lSsefnRyarfbjuka+7DXy++c4EnDusPTnoQ7X/FxtdTOH5a+7q0meVBqLrfqNvYSNUaX1M2e9sqnWs8fVZHuzN6z0gjB8a2nMik8hwIRv5nNkwzFVA7AIM/2ztTv027kHaJUH0yPrFfhQuqT+fsWTjPyJpaz7tdTIqVCshWmtKLwhBsAd6l8oLnjKZl3axk722NQkoHm2cnt8HcV5Bb6k4QHQnQNyCiWoRSfVyK6z4vmjexvk2Ea+ztOUd5vfeNCIwupH5DuxCu6SFy4v0noh10X/DV7I7GRxnF2AiqY1jCu3oe2NWVvnUgJzRCyzkInHJ27c3ldLqwJr8VdPrtFYiS+ftspLNci5bkB1VnaX8CDkm82ByStTekuiSjWH4yraTuCl6njZsD1Z3h5R6x0aSU72k6rw7T724Gqjgge40eotbJqbBacSi1PJTjnS42DbqTIz7Vo5/OHyfQMO1dgBGOkPcQ5N9AHcH2eJH8dB1sW1NAkF26tEli76ltlvBUXxhW5nptW0ELkCkgvtgw8V7d6IGs7aFfwsXUuB4NwrxmtPgmCLVFWsg2ro8dFt66GARrGrv9gRpjNkbCazOCU11VHtaWlBuNivAet4Of+94ZvpA2UDyC7PxcRrh2zmALJnb/rXBHufKp6iCQSnZ6cYftjRzl4FAlw6mBOPSUjhPyEMOCnqtn2+CycJ+jicV8/PJqeaxR9tmS8l6vd3hbL7bEtm0mO8XlLl69B2M3sgDbWXpiqJUdrm0E9f6hhKRcHHlHGUzWqGMHYXWcfwDHCirlqsJ64zUhISk3cPADxux1DHXTRIL2RpwU8iK6l6GFM7NgHIpFMdtPUv5sMVmhl6Mpph++Tg28YohB1Uq2uS+WPEe/WCJOAVUnwQ/abcVWKp4o5RKEo2z93bwRuE4PC+ /+o38h5A pEi1a1+dJkKmtlcCFjxd2fkHT7EL5S1jQGqOz2rcNUbfqId0SncTX23jiy+ezas/js3zQr0JLC2nlO7jK7T+mor+XCsteD+xcezA5HeOCOPJVhAyA7soiv1xwlHNCnxptUVrbrSbOq4Pe7qLkLkqKDHkmz45uRaIIssMuqi/TCYxXQkiw0lQXAs//gG67j67o7w6Z3vUu84nDO33D7wT2pAhF9ECQLwMoeacUF1AAUDVE7wbMAw0t8bXwKL23OYGPxB+T8pei530HQEPCbvKZt9Ky1QHgbTP2CD5vdjo6h1JwkXqk89vOuvt8DB6kw2HFLLGk52Mj3X+o9wI/LaLV3GJ46R6rNFBWwiaKh4jlicbbZOAFfFn2Zu7gzhdpNaCDHNmL/iYATdYLO7ksKiLsWHnbsA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Some macro defined in stddef.h are useful and have been used in many code in selftests. Copy them to tools/include so developers needn't create their copy in every files. Remove some definitions like NULL and true/false to be suitable to non-kernel environment. Signed-off-by: Celeste Liu Reviewed-by: Andrew Jones --- tools/include/linux/stddef.h | 85 +++++++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/stddef.h | 6 +-- 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/tools/include/linux/stddef.h b/tools/include/linux/stddef.h new file mode 100644 index 0000000000000000000000000000000000000000..55f3964d9a3d9f9f9345a75248eec027c56faef9 --- /dev/null +++ b/tools/include/linux/stddef.h @@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_STDDEF_H +#define _LINUX_STDDEF_H + +#include + +/** + * sizeof_field() - Report the size of a struct field in bytes + * + * @TYPE: The structure containing the field of interest + * @MEMBER: The field to return the size of + */ +#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) + +/** + * offsetofend() - Report the offset of a struct field within the struct + * + * @TYPE: The type of the structure + * @MEMBER: The member within the structure to get the end offset of + */ +#define offsetofend(TYPE, MEMBER) \ + (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) + +/** + * struct_group() - Wrap a set of declarations in a mirrored struct + * + * @NAME: The identifier name of the mirrored sub-struct + * @MEMBERS: The member declarations for the mirrored structs + * + * Used to create an anonymous union of two structs with identical + * layout and size: one anonymous and one named. The former can be + * used normally without sub-struct naming, and the latter can be + * used to reason about the start, end, and size of the group of + * struct members. + */ +#define struct_group(NAME, MEMBERS...) \ + __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS) + +/** + * struct_group_attr() - Create a struct_group() with trailing attributes + * + * @NAME: The identifier name of the mirrored sub-struct + * @ATTRS: Any struct attributes to apply + * @MEMBERS: The member declarations for the mirrored structs + * + * Used to create an anonymous union of two structs with identical + * layout and size: one anonymous and one named. The former can be + * used normally without sub-struct naming, and the latter can be + * used to reason about the start, end, and size of the group of + * struct members. Includes structure attributes argument. + */ +#define struct_group_attr(NAME, ATTRS, MEMBERS...) \ + __struct_group(/* no tag */, NAME, ATTRS, MEMBERS) + +/** + * struct_group_tagged() - Create a struct_group with a reusable tag + * + * @TAG: The tag name for the named sub-struct + * @NAME: The identifier name of the mirrored sub-struct + * @MEMBERS: The member declarations for the mirrored structs + * + * Used to create an anonymous union of two structs with identical + * layout and size: one anonymous and one named. The former can be + * used normally without sub-struct naming, and the latter can be + * used to reason about the start, end, and size of the group of + * struct members. Includes struct tag argument for the named copy, + * so the specified layout can be reused later. + */ +#define struct_group_tagged(TAG, NAME, MEMBERS...) \ + __struct_group(TAG, NAME, /* no attrs */, MEMBERS) + +/** + * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union + * + * @TYPE: The type of each flexible array element + * @NAME: The name of the flexible array member + * + * In order to have a flexible array member in a union or alone in a + * struct, it needs to be wrapped in an anonymous struct with at least 1 + * named member, but that member can be empty. + */ +#define DECLARE_FLEX_ARRAY(TYPE, NAME) \ + __DECLARE_FLEX_ARRAY(TYPE, NAME) + +#endif diff --git a/tools/include/uapi/linux/stddef.h b/tools/include/uapi/linux/stddef.h index bb6ea517efb51177a7983fadad9b590b12b786e5..f2548fd95f6e1d8cb218d52918bb81a3317d10b1 100644 --- a/tools/include/uapi/linux/stddef.h +++ b/tools/include/uapi/linux/stddef.h @@ -1,8 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ -#ifndef _LINUX_STDDEF_H -#define _LINUX_STDDEF_H - - +#ifndef _UAPI_LINUX_STDDEF_H +#define _UAPI_LINUX_STDDEF_H #ifndef __always_inline #define __always_inline __inline__ From patchwork Wed Jan 15 11:13:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Celeste Liu X-Patchwork-Id: 13940315 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C72D0C02183 for ; Wed, 15 Jan 2025 11:14:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A2926B008C; Wed, 15 Jan 2025 06:14:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 54FD1280002; Wed, 15 Jan 2025 06:14:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A20C280001; Wed, 15 Jan 2025 06:14:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 18A746B008C for ; Wed, 15 Jan 2025 06:14:28 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C291512093C for ; Wed, 15 Jan 2025 11:14:27 +0000 (UTC) X-FDA: 83009427774.02.3ED737D Received: from flow-a3-smtp.messagingengine.com (flow-a3-smtp.messagingengine.com [103.168.172.138]) by imf03.hostedemail.com (Postfix) with ESMTP id AD56020008 for ; Wed, 15 Jan 2025 11:14:25 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b="F/A5egxh"; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="U XYZP7u"; dmarc=pass (policy=quarantine) header.from=coelacanthus.name; spf=pass (imf03.hostedemail.com: domain of uwu@coelacanthus.name designates 103.168.172.138 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736939665; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4xCU5bJlFw3YObn7QvVHHfcB2ZV7lt3TH8aNCek14IU=; b=K3aDCsQayZLV4TUPJFkK52dgFtHvska6RnKoiNcG1HZnXGI681u/0COdDBxna3vS2UYo4S HvR96PTQw+RpVRDJpzGSr5Z1eeLfoaVSd5AXjeUnEkoJrjbyBrx9fqnJFCd/5yRK1iq0gP fbBHHlQOlTMSMeElIKi2GAuQ9g/Z1g0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736939665; a=rsa-sha256; cv=none; b=jBSMltIcE/tb4yMR0uixHFRcG+d6h3VRvc8Uqfh/2s0reBQAAVQ/H44IvLnUXiLeun7Sbv kXukN5XIWbBI/vIpskSUvzeZ3INi7BVnjPaKUZoxqS06498tXY7kv+yMc+O0cibUVDTSbo kdt+TigQ4UHtv8wLRzmM1PxlMLLrYc0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=coelacanthus.name header.s=fm3 header.b="F/A5egxh"; dkim=pass header.d=messagingengine.com header.s=fm2 header.b="U XYZP7u"; dmarc=pass (policy=quarantine) header.from=coelacanthus.name; spf=pass (imf03.hostedemail.com: domain of uwu@coelacanthus.name designates 103.168.172.138 as permitted sender) smtp.mailfrom=uwu@coelacanthus.name Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailflow.phl.internal (Postfix) with ESMTP id 238A0200B08; Wed, 15 Jan 2025 06:14:25 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Wed, 15 Jan 2025 06:14:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= coelacanthus.name; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1736939665; x=1736946865; bh=4xCU5bJlFw 3YObn7QvVHHfcB2ZV7lt3TH8aNCek14IU=; b=F/A5egxhBf5H3gcxEqys0v01xj uVyinEXETnAltCxxWZU85mI8e/whH+DU31DMgdN9kBPjcUoIUOGkEX8qScIyYMr1 o597TwbWRDnmhKbE17LnPZ1G6h69VLo35mF4F4qJc1jbEKfaNx0rulED7YXGhvkx Mx2/8lysPtvN3AAE/BHbvO1DtPAXtLWMTCDHsvFy2SOutkJqhsnXzv/OdViq8Ef6 WhMc3z5vhzXv4ZXBM6TKHbBmGTAT23NJndqYjN4R/ZwgErdK6b2+/5miKQUTQqjp M/2ePhRfhTRGiJQuny59l1mcDLXLudxDVxgoB1Nn1QHUu0fwya/RjUBUDG7w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1736939665; x= 1736946865; bh=4xCU5bJlFw3YObn7QvVHHfcB2ZV7lt3TH8aNCek14IU=; b=U XYZP7upwfhpYO7b3Bh0t3mFHFa7oQGx7lAlPGvJotwmleotu9axflSo31Y9Vwe4g jJDZvLbDji/myJ8bL/YqrK68ypkYLDD9D39im6DqtXjM++sL73cKmG/Q/emt7FPj uxgl2TtjqfFQTX0D2R0TCP4IrwjoAl+8Kk67MkF204xb1OAzz5J72e/0KY+/tI+5 zMfNn4ZV/eUPj59TFnOwpAtBsACpuePm2dKirPh4Ti9HxXGJezQ9x5MXP0SgYVeg kWZX4Em6bQD2IG4I91wPOVzsdXZx1PBbMtdX6GwoDNhKehe2mN5eqau4mGX2V6iY 52hFjwpJN6sUeUg/R+eSw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrudehlecutefuodetggdotefrodftvfcurf hrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffrtefo kffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsuc dlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthekredtredtjeenucfh rhhomhepvegvlhgvshhtvgcunfhiuhcuoehufihusegtohgvlhgrtggrnhhthhhushdrnh grmhgvqeenucggtffrrghtthgvrhhnpeeiudegfffhtdeuuddufefhfefhheejjefhueek hefffeeugfeuveetieeuvddtfeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehufihusegtohgvlhgrtggrnhhthhhushdrnhgrmhgvpdhnsggp rhgtphhtthhopedviedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplhhinhhugi dqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhguvhes shhtrhgrtggvrdhiohdprhgtphhtthhopegthhgrrhhlihgvsehrihhvohhsihhntgdrtg homhdprhgtphhtthhopegsjhhorhhnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehs hhhurghhsehkvghrnhgvlhdrohhrghdprhgtphhtthhopehrvgesfieirhiirdhnvghtpd hrtghpthhtohepvggsihgvuggvrhhmseigmhhishhsihhonhdrtghomhdprhgtphhtthho peiihhhouhhquhgrnhesihhstggrshdrrggtrdgtnhdprhgtphhtthhopehlihhnuhigqd hkshgvlhhfthgvshhtsehvghgvrhdrkhgvrhhnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i95c648bc:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 15 Jan 2025 06:14:23 -0500 (EST) From: Celeste Liu Date: Wed, 15 Jan 2025 19:13:29 +0800 Subject: [PATCH v6 3/3] riscv: selftests: Add a ptrace test to verify a0 and orig_a0 access MIME-Version: 1.0 Message-Id: <20250115-riscv-new-regset-v6-3-59bfddd33525@coelacanthus.name> References: <20250115-riscv-new-regset-v6-0-59bfddd33525@coelacanthus.name> In-Reply-To: <20250115-riscv-new-regset-v6-0-59bfddd33525@coelacanthus.name> To: Oleg Nesterov , Paul Walmsley , Palmer Dabbelt , Eric Biederman , Kees Cook , Shuah Khan , Albert Ou Cc: Alexandre Ghiti , "Dmitry V. Levin" , Andrea Bolognani , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Thomas Gleixner , Ron Economos , Charlie Jenkins , Andrew Jones , Quan Zhou , Felix Yan , Ruizhe Pan , Guo Ren , Yao Zi , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Celeste Liu , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=7629; i=uwu@coelacanthus.name; h=from:subject:message-id; bh=Esi60fF9RlKXqJX1LQ4xzxGmVZGZ6ydahIA2pcSWvhk=; b=owJ4nJvAy8zAJeafov85RWVtBeNptSSG9PYZlZrxmZzfjuzXVKn/fO+kRYvi076gR63iJmvSN N7W8G/6ta+jlIVBjItBVkyRJa+E5SfnpbPdezu2d8HMYWUCGcLAxSkAE2mZzsiw+3jzJa4sGe2F bwRa+Sa1zQhjKtudc51ratgxmQnM2SrZjAx7y1cGS02MvfnwCWfmuzl7Zl5glPNXPx3obrbG08x apoMfAL2mRho= X-Developer-Key: i=uwu@coelacanthus.name; a=openpgp; fpr=892EBC7DC392DFF9C9C03F1D15F4180E73787863 X-Stat-Signature: su5c6e93kditgbpdzinnnehqiboxotbm X-Rspamd-Queue-Id: AD56020008 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736939665-4560 X-HE-Meta: U2FsdGVkX1+S+0wcOGwgzzlQy+JMz3VZpjbqh0SlRkY21TxOFm44kJYoGU238RQTW5mC+KGiliz3yEJw8yMJqaSdSXhXmbW0Ai/cC8tZ+alxNIDdBn/J1rSHzpv/m4gG1yicFkrQnrhq/CiVG8VK4ad2SBRZJtreP/7Dhgu9O9uQB/Ec56JNqNeO/qWZMotvPR8JdIGWitfr8ZGno4BqRf8wXj1c546GfWNxvjKuDFwtillqdDzBlm2mySWFs0op2/wqF5q+eZhFBePu7jgMricmBlQcBXu85rumZhzKgz9TtloVACLDNUVaoJyJX1PaHzdIOoooQrxuZ8r0PIAIzn6W4sAjfgS2/bUUbDyoLr71ww/BIzPNo6G5V4OsDDQ5zlv11wMyQ0ZbRjDN4Z3n364KZdB4MOXMpKJEw2deNRNQ6mCQRuXstL+/AknJmR9YbIvzl6epynRiKANe6PE5/x2KDo/+W8TZd6YIFypsG4ICmgo/L1aVqeLrhXLdZZnqZ1BppNk56JJXGbhpwPp6swSVHI6uYS9DT2+wnHVg0KT9yGsEs87NzUFmbxlLZlio1PbjM6z06w8fs9xfySHYXu7sZQpJ7qjNm2cvfxG5ITDpwZ+7n7qzqwPep2RGfOcQkaHFfMx0U+IbOAga8ropb+NpanFeYTfGmu2N0O5Rx3GIkjK62fS6tGloa0qzs0wF2S1v6SyC9/Swim1tWbJw1lRKNNuTkp0tyq9QRxnpL9Xpj+RJjAwwCmFPxiiOzEhjYr8fwnoc6v/ussN4/6pWDdCyXpODJJPPg5/Sj0Q0W3rG6UL7xtBVQP4pjCiaLBbGV45krAhHWJR2AcDr7fkfbtxgPSjlFZxIAHuYBgMCjooMraEfjOpIz0ZrFfNPo/FI3YSPYtOLHk0BdQTwaPAqVlEvtWUoysJjSe6jSo1ZEMXdkf0XLdWMmZgi4js2gfhf130JhrHZgaF8eygSf3c mherOoBI yzbs+Qe5r1sai3WBD4shFgA+wlpLHRYK/lReNoFfsHomO2Sl+9GX50TFaG7ePQ27tLNclVIyFWso58YsO0V8/xOBiMFHN2XwlxXhrT/MOUif6RLdP2DhC5qwrR0xRogx+7mQiNYlJA0J+u9sAFLG6wBWI4w9c459aZqMrQawgwT0s27Ln8naFtcoMa2nU6XBCYfC3k+IrwT69gvfrOfHRoni8Olra5UwgpXyAcrJFAlZ0lJ6zczkz7+kZxRBl8yT4/OUKZ9rRcsY3Qk60Kgv+WsRlqIsM1EQzwk8bpPzKw/FM/+deMbSEIognMQbQ1DteRhuivta6vL/wme+Wy+pFB6p/542FFQBqQCmWtrRUj4g5xoO3+QrZ1hO4/rYh14FrQggmx4JaKjZ9zcQo03DTZv5MW1+8dEpAd1KXrLFENpLOQqy2F86xTGrvHGJYmxlyWa4MWPiwnh67xeml2ufFAVdjiH6xNCM13Kh4ucr8YxEaBRO24XJgK3wWfPg1AaUuwNzGSC1zYeau7+JSPpGADsDQ45y/wOusb8F1MpyW4uQ4D4yLIaL6/E/PdVGF8vh4x0qs X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This test checks that orig_a0 and a0 can be modified and accessed independently. Co-developed-by: Quan Zhou Signed-off-by: Quan Zhou Co-developed-by: Charlie Jenkins Signed-off-by: Charlie Jenkins Reviewed-by: Björn Töpel Signed-off-by: Celeste Liu Reviewed-by: Andrew Jones --- tools/testing/selftests/riscv/abi/.gitignore | 1 + tools/testing/selftests/riscv/abi/Makefile | 6 +- tools/testing/selftests/riscv/abi/ptrace.c | 193 +++++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/riscv/abi/.gitignore b/tools/testing/selftests/riscv/abi/.gitignore index b38358f91c4d2240ae64892871d9ca98bda1ae58..378c605919a3b3d58eec2701eb7af430cfe315d6 100644 --- a/tools/testing/selftests/riscv/abi/.gitignore +++ b/tools/testing/selftests/riscv/abi/.gitignore @@ -1 +1,2 @@ pointer_masking +ptrace diff --git a/tools/testing/selftests/riscv/abi/Makefile b/tools/testing/selftests/riscv/abi/Makefile index ed82ff9c664e7eb3f760cbab81fb957ff72579c5..359a082c88a401883fb3776b35e4dacf69beaaaa 100644 --- a/tools/testing/selftests/riscv/abi/Makefile +++ b/tools/testing/selftests/riscv/abi/Makefile @@ -1,10 +1,14 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS += -I$(top_srcdir)/tools/include +CFLAGS += $(KHDR_INCLUDES) -TEST_GEN_PROGS := pointer_masking +TEST_GEN_PROGS := pointer_masking ptrace include ../../lib.mk $(OUTPUT)/pointer_masking: pointer_masking.c $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ + +$(OUTPUT)/ptrace: ptrace.c + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/abi/ptrace.c b/tools/testing/selftests/riscv/abi/ptrace.c new file mode 100644 index 0000000000000000000000000000000000000000..cbd60462021165ca2bcc38be24b8423c96047b93 --- /dev/null +++ b/tools/testing/selftests/riscv/abi/ptrace.c @@ -0,0 +1,193 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest_harness.h" + +#define ORIG_A0_MODIFY 0x01 +#define A0_MODIFY 0x02 +#define A0_OLD 0xbadbeefbeeff +#define A0_NEW 0xffeebfeebdab + + +struct a0_regs { + __s64 orig_a0; + __u64 a0; +}; + +#define perr_and_exit(fmt, ...) \ + ({ \ + char buf[256]; \ + snprintf(buf, sizeof(buf), "%s:%d:" fmt ": %m\n", \ + __func__, __LINE__, ##__VA_ARGS__); \ + ksft_exit_fail_perror(buf); \ + }) + +static void ptrace_test(int opt, struct a0_regs result[]) +{ + int status; + long rc; + pid_t pid; + struct user_regs_struct regs; + struct iovec iov = { + .iov_base = ®s, + .iov_len = sizeof(regs), + }; + + unsigned long orig_a0; + struct iovec a0_iov = { + .iov_base = &orig_a0, + .iov_len = sizeof(orig_a0), + }; + struct ptrace_syscall_info syscall_info = { + .op = 0xff, + }; + const unsigned int expected_sci_entry_size = + offsetofend(struct ptrace_syscall_info, entry.args); + const unsigned int expected_sci_exit_size = + offsetofend(struct ptrace_syscall_info, exit.is_error); + + pid = fork(); + if (pid == 0) { + /* Mark oneself being traced */ + long val = ptrace(PTRACE_TRACEME, 0, 0, 0); + + if (val < 0) + perr_and_exit("failed to request for tracer to trace me: %ld", val); + + kill(getpid(), SIGSTOP); + + /* Perform chdir syscall that will be intercepted */ + syscall(__NR_chdir, A0_OLD); + + exit(0); + } + + if (pid < 0) + ksft_exit_fail_perror("failed to fork"); + + for (int i = 0; i < 3; i++) { + if (waitpid(pid, &status, 0) != pid) + perr_and_exit("failed to wait for the tracee %d", pid); + if (WIFSTOPPED(status)) { + switch (WSTOPSIG(status)) { + case SIGSTOP: + if (ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESYSGOOD) < 0) + perr_and_exit("failed to set PTRACE_O_TRACESYSGOOD"); + break; + case SIGTRAP|0x80: + /* Modify twice so GET_SYSCALL_INFO get modified a0 and orig_a0 */ + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to get tracee registers"); + if (ptrace(PTRACE_GETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov)) + perr_and_exit("failed to get tracee registers"); + + /* Modify a0/orig_a0 for the syscall */ + switch (opt) { + case A0_MODIFY: + regs.a0 = A0_NEW; + break; + case ORIG_A0_MODIFY: + orig_a0 = A0_NEW; + break; + } + + if (ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to set tracee registers"); + if (ptrace(PTRACE_SETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov)) + perr_and_exit("failed to set tracee registers"); + switch (i) { + case 1: + /* Stop at the beginning of syscall */ + rc = ptrace(PTRACE_GET_SYSCALL_INFO, pid, + sizeof(syscall_info), &syscall_info); + if (rc < 0) + perr_and_exit("failed to get syscall info of entry"); + if (rc < expected_sci_entry_size + || syscall_info.op != PTRACE_SYSCALL_INFO_ENTRY) + perr_and_exit("stop position of entry mismatched"); + result[0].orig_a0 = syscall_info.entry.args[0]; + break; + + case 2: + /* Stop at the end of syscall */ + rc = ptrace(PTRACE_GET_SYSCALL_INFO, pid, + sizeof(syscall_info), &syscall_info); + if (rc < 0) + perr_and_exit("failed to get syscall info of entry"); + if (rc < expected_sci_exit_size + || syscall_info.op != PTRACE_SYSCALL_INFO_EXIT) + perr_and_exit("stop position of exit mismatched"); + result[0].a0 = syscall_info.exit.rval; + + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to get tracee registers"); + result[1].a0 = regs.a0; + if (ptrace(PTRACE_GETREGSET, pid, NT_RISCV_ORIG_A0, + &a0_iov)) + perr_and_exit("failed to get tracee registers"); + result[1].orig_a0 = orig_a0; + } + } + if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) + perr_and_exit("failed to resume tracee"); + } + } + + /* Resume the tracee */ + ptrace(PTRACE_CONT, pid, 0, 0); + if (waitpid(pid, &status, 0) != pid) + perr_and_exit("failed to wait for the tracee"); + +} + +TEST(ptrace_access_a0) +{ + struct a0_regs result[2]; + + ptrace_test(A0_MODIFY, result); + + /* Verify PTRACE_SETREGSET */ + /* The modification of a0 cannot affect the first argument of the syscall */ + EXPECT_EQ(A0_OLD, result[0].orig_a0); + EXPECT_EQ(A0_NEW, result[0].a0); + + /* Verify PTRACE_GETREGSET */ + EXPECT_EQ(result[1].orig_a0, result[0].orig_a0); + EXPECT_EQ(result[1].a0, result[0].a0); +} + +TEST(ptrace_access_orig_a0) +{ + struct a0_regs result[2]; + + ptrace_test(ORIG_A0_MODIFY, result); + + /* Verify PTRACE_SETREGSET */ + /* Only modify orig_a0 to change the first argument of the syscall */ + EXPECT_EQ(A0_NEW, result[0].orig_a0); + /* a0 will not be affected */ + EXPECT_NE(A0_NEW, result[0].a0); + + /* Verify PTRACE_GETREGSET */ + EXPECT_EQ(result[1].orig_a0, result[0].orig_a0); + EXPECT_EQ(result[1].a0, result[0].a0); +} + +TEST_HARNESS_MAIN