From patchwork Tue Jan 7 09:41:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 13928591 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8AB23E7719A for ; Tue, 7 Jan 2025 09:53:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=OKWM4cPPHTUloQ8W+d7SfJ+6Vh0jRzzpCF+pKqqjEZA=; b=nrH1oMYFxTSknC5bQ7oy/lan3b D8KrUQgaKz+L0a3G69CRifAM4PezPE3hChlDDTrP/gsgu6yEi4qe699T/5IERA1IR8cyaVHB1UzGU iWYf2VV0tVRFmlRpvPTHeZmVMr3+XGxzEhQ4GpEgc63jQf/RN1yzJ08PmZSDQLJdIMho8WhXiU6HD 3pE8jPicoe1rcvPzpIzFXuhMP6RamdQboKwqIPHahYZAKeo1k9CU4SA9XP7ySCT+BFdZdd2czBhY8 aZYF0l0cFGv+GtUH/wrttmAWjh1tgY2dl2avZLbtAwSgA5wq0GeSg99TIdAuiq8x+Q+3+n6NNOBkn kaSJcY/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tV6HE-00000004Dq1-0lDj; Tue, 07 Jan 2025 09:53:32 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tV65k-00000004AOy-3StI for linux-arm-kernel@lists.infradead.org; Tue, 07 Jan 2025 09:41:41 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-aab6fa3e20eso2519865666b.2 for ; Tue, 07 Jan 2025 01:41:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1736242899; x=1736847699; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=OKWM4cPPHTUloQ8W+d7SfJ+6Vh0jRzzpCF+pKqqjEZA=; b=eTvkL9WM0hYZY0bEzKtvyIDHUINAImubQJfXvEgU5Iy1OB6+8dW+1LL7Fon1+bDVvg AicNP39Aq9+qedNGUxYbKFvma+oQCDD4uzLddtHZBOmdxqsx+D25rqIHXB14QwHK7q3T 9cB8HywVLtzPPZTz9AbctatQQhIHWV1LfeII4+ugeMSwjd2MSRRMkFf8CyRf6CgIoKLq 6UVLDZvdiWZu4QxQXcWUO6WBN2upey2sBE6EjwDva6NQhlcjiAqviu4SOh3cbXd0vbS6 7CcHLsf+62nk11ahGlsfrTcrVpI6PoT1Gm9NdWrfF4m/T+QbzJZ26mVINLltjkiviZ8K b+Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736242899; x=1736847699; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OKWM4cPPHTUloQ8W+d7SfJ+6Vh0jRzzpCF+pKqqjEZA=; b=NpcWyW9kXVXRpYzL1Z+gsbSMotKJRLieduycs1nUEG2L0NIKaDnBBFsDAx6lXm/mxh Z6TfDcAyeDAd5VL4ge5usfwYGpf36M2QaWSfKouuQKIydreRDWlGbGQI+1AAd7O+Anfm 8ffC004E12I6tlyOUknzDyyDyLwLkrvPtumScXbqqR2nMjvfd+LNyTw3Lk6bqT2OXRBB 6OKGL89t62W5xktF+OWwHLtqG6RJlaAdyJiPyiQfww9Ji+UlGjioT6mbbiLUG3/3ONgj v2ZYelCe98st+UtsDqLRxHJIIDtHWYcy11QGZi2w7cwPewU1NYqlw2cgPzLDKiZSTv+u BBMQ== X-Gm-Message-State: AOJu0YwSuHHGJ16ea1TqxOiwFjmEd8yOmIqVdrU+5O0sCf1pRCEbCWFM P/ghQ9UUzMERxyuWScJxh6HOEgMr4jrQt9DuL9hSoIoI8EXOKK6PU6b9EjAOv7c= X-Gm-Gg: ASbGncuQ67xrD/xr6lCX/anWTLWNEbUI+wv4aavKvO8LBHGOtzXy6tvJYo6weIWGRns /IGtKdhUAhJfPWlaukVVEyhlCO8/TlwVp8z2Q2cB+dmru9A5oWoFs9XXwqq3NwOQaoxWL2YFbFo 5Cf/brcGrIMV/K7ZZXe49yomuU2814FZPnfL6d8YVOu/jS1AcOKtcfaT6LtFKq865xpHKzsWJHb 5SK9FXttPlo7rqASe+kw065eRKpaupMD3/qejVwBZlt+0Ys+E4npwa9xdlcp1mDSfQ= X-Google-Smtp-Source: AGHT+IGORu1sJ9gjYMgOVIE8V4cB1mpEs+RP8HyomPhOZJkI2q6RHOVgjiyQlXfFj0xR8o3+R65swg== X-Received: by 2002:a17:906:c106:b0:aa6:7cae:db98 with SMTP id a640c23a62f3a-aac2d447333mr5019449766b.10.1736242899387; Tue, 07 Jan 2025 01:41:39 -0800 (PST) Received: from [192.168.1.140] ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aac0f012229sm2356901166b.133.2025.01.07.01.41.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jan 2025 01:41:38 -0800 (PST) From: Linus Walleij Date: Tue, 07 Jan 2025 10:41:26 +0100 Subject: [PATCH RFC v3 10/30] ARM: entry: move all tracing invocation to C MIME-Version: 1.0 Message-Id: <20250107-arm-generic-entry-v3-10-4e5f3c15db2d@linaro.org> References: <20250107-arm-generic-entry-v3-0-4e5f3c15db2d@linaro.org> In-Reply-To: <20250107-arm-generic-entry-v3-0-4e5f3c15db2d@linaro.org> To: Dmitry Vyukov , Oleg Nesterov , Russell King , Kees Cook , Andy Lutomirski , Will Drewry , Frederic Weisbecker , "Paul E. McKenney" , Jinjie Ruan , Arnd Bergmann , Ard Biesheuvel , Al Viro Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Linus Walleij X-Mailer: b4 0.14.2 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250107_014140_868881_31EC8407 X-CRM114-Status: GOOD ( 21.17 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The traced invocation of syscall is rewritten in C, moving over also the call to syscall_trace_exit() to C. To do this we cannot have invoke_syscall_trace_asm() be a leaf call, which is the mechanism we hithereto relied on to make sure the stack is pulled back to the state we were at before we called out to the code written in C. Push the registers potentially used by C on the stack, and make a copy of the two stack-pushed syscall arguments on the top of the stack before invoking the syscall and then drop these copied arguments and pop back the registers before returning from invoke_syscall_trace_asm(). As we are now calling from and returning to C, we no longer need to pass the regs pointer around so drop it. Signed-off-by: Linus Walleij --- arch/arm/include/asm/syscall.h | 2 +- arch/arm/kernel/entry-common.S | 32 +++++++++++++++----------------- arch/arm/kernel/ptrace.c | 2 +- arch/arm/kernel/syscall.c | 30 +++++++++++++++++++++--------- 4 files changed, 38 insertions(+), 28 deletions(-) diff --git a/arch/arm/include/asm/syscall.h b/arch/arm/include/asm/syscall.h index 66067d165ba3fbbe0f840a89ae396eb1a311bacc..cb0073c4151bf70a82d15e17a95b6b6f48b245d6 100644 --- a/arch/arm/include/asm/syscall.h +++ b/arch/arm/include/asm/syscall.h @@ -20,7 +20,7 @@ extern const unsigned long sys_call_table[]; int invoke_syscall(void *table, struct pt_regs *regs, int scno, void *retp); -int invoke_syscall_trace(void *table, struct pt_regs *regs, void *retp); +void invoke_syscall_trace(void *table, struct pt_regs *regs); static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs) diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S index 8baab7f97f59c434396f30b08ddd3029c5f9c0e5..dbc947d301ec11bb13007cb4bb161c035ede5c10 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -296,22 +296,11 @@ ENDPROC(vector_swi) __sys_trace: add r1, sp, #S_R0 + S_OFF @ pointer to regs mov r0, tbl - badr r2, __sys_trace_return bl invoke_syscall_trace - cmp r0, #-1 - bne __sys_trace_return - add sp, sp, #S_OFF @ restore stack - -__sys_trace_return_nosave: - enable_irq_notrace - mov r0, sp - bl syscall_trace_exit + add sp, sp, #S_OFF @ restore stack pointer b ret_to_user -__sys_trace_return: - str r0, [sp, #S_R0 + S_OFF]! @ save returned r0 - mov r0, sp - bl syscall_trace_exit +__sys_trace_return_nosave: b ret_to_user .macro syscall_table_start, sym @@ -437,18 +426,27 @@ SYM_FUNC_END(invoke_syscall_asm) * r0: syscall table * r1: regs * r2: syscall number - * r3: pointer to return function */ SYM_TYPED_FUNC_START(invoke_syscall_trace_asm) #ifdef CONFIG_CPU_SPECTRE csdb #endif + /* Save registers because we are being called from C */ + push {r4 - r10, lr} mov tbl, r0 + /* Make space to copy the two syscall stack arguments */ + sub sp, sp, #S_OFF mov scno, r2 - mov lr, r3 @ return address - ldmia r1, {r0 - r6} @ reload r0-r6 - stmia sp, {r4, r5} @ update stack arguments + badr lr, __invoke_syscall_trace_ret @ return right here + ldmia r1, {r0 - r6} @ reload r0-r6 from regs + stmia sp, {r4, r5} @ copy stack arguments ldr pc, [tbl, scno, lsl #2] @ call sys_* routine +__invoke_syscall_trace_ret: + /* Drop the copied stack arguments */ + add sp, sp, #S_OFF + pop {r4 - r10, lr} + ARM( mov pc, lr ) + THUMB( bx lr ) SYM_FUNC_END(invoke_syscall_trace_asm) #ifdef CONFIG_OABI_COMPAT diff --git a/arch/arm/kernel/ptrace.c b/arch/arm/kernel/ptrace.c index 07b0daf47441f1f76a8af416acc74fa5ed770403..ac7b98ae47249b4a00fc1cb871bb2d309bfd8c88 100644 --- a/arch/arm/kernel/ptrace.c +++ b/arch/arm/kernel/ptrace.c @@ -888,7 +888,7 @@ asmlinkage int syscall_trace_enter(struct pt_regs *regs) return scno; } -asmlinkage void syscall_trace_exit(struct pt_regs *regs) +void syscall_trace_exit(struct pt_regs *regs) { /* * Audit the syscall before anything else, as a debugger may diff --git a/arch/arm/kernel/syscall.c b/arch/arm/kernel/syscall.c index 377e16c8c53c8e809e3f8b157a889ef115d5c043..e90f04f35b3485968a1e9046cf0889f56cca92e5 100644 --- a/arch/arm/kernel/syscall.c +++ b/arch/arm/kernel/syscall.c @@ -18,23 +18,35 @@ __visible int invoke_syscall(void *table, struct pt_regs *regs, int scno, void * return sys_ni_syscall(); } -int invoke_syscall_trace_asm(void *table, struct pt_regs *regs, int scno, void *retp); +int invoke_syscall_trace_asm(void *table, struct pt_regs *regs, int scno); __ADDRESSABLE(invoke_syscall_trace_asm); -__visible int invoke_syscall_trace(void *table, struct pt_regs *regs, void *retp) +__visible void invoke_syscall_trace(void *table, struct pt_regs *regs) { int scno; + int ret; scno = syscall_trace_enter(regs); if (scno == -1) - return -1; + goto trace_exit_nosave; - if (scno < NR_syscalls) - /* Doing this with return makes sure the stack gets pop:ed */ - return invoke_syscall_trace_asm(table, regs, scno, retp); + if (scno < NR_syscalls) { + ret = invoke_syscall_trace_asm(table, regs, scno); + goto trace_exit_save; + } - if (scno >= __ARM_NR_BASE) - return arm_syscall(scno, regs); + if (scno >= __ARM_NR_BASE) { + ret = arm_syscall(scno, regs); + goto trace_exit_save; + } - return sys_ni_syscall(); + ret = sys_ni_syscall(); + +trace_exit_save: + /* Save return value from syscall */ + regs->ARM_r0 = ret; + +trace_exit_nosave: + local_irq_enable(); + syscall_trace_exit(regs); }