From nobody Fri Dec 19 07:55:57 2025 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58CFF1CEADB for ; Wed, 22 Jan 2025 22:57:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737586623; cv=none; b=AMdBGIv4npdR9aMLP+PpYy/sIe0hMTO6GWqBpdPBtlmIJi81ADYCC0zLk3gzA0zkMCGLI/hIhvdJ5U1oWPvn0bqyRXDNx4k1swqxQwcXV02entAxUSjvwO+zaG1bUClOZ5HhwXlVRFkGsMweaE4DeB8EADnQ/QLS8/QYKpOPAXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737586623; c=relaxed/simple; bh=Z1tS2IEfH8NjFHjnsgjlU412ZTPZo9IgZRJ3cE9Ze5k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Bf0FlsG+0WKW04QL1MHEjPVktxG79o/uT9h1btWtFxnnNBB30557FM05td04AW5+SyJju91FvUlfsv2yD5CIRmIKJzeFe6dpObcNQuRgOQrxL5ZsXhNerhsiRw+2nIiNkiR0vyk/9DfS6tfctjgrtNFm+eUdNl/Qs7t9bT02450= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=H1qzrA1J; arc=none smtp.client-ip=209.85.216.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="H1qzrA1J" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2ef8c012913so457534a91.3 for ; Wed, 22 Jan 2025 14:57:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1737586621; x=1738191421; darn=vger.kernel.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=HCJijnh3eucUGl1hsJuof30/Y1t6y46+I+a/ErqCn/w=; b=H1qzrA1JQGztuVINZKIcM83YK+67aatVoO+sJ0samuynCRk1cwzUoLtm0xQLLX1lDq 18GO6MPNulpaw34s9F3+3GCGiRy4rh9u/YNFC5k3yj8vKbObQcGJBp3GqctTFN0bxY2b Ft8Ls5VfplAXpnW1FxX2CEsSSRd7UjRNnnVfpC/Xo68mTyNqQ78fGif2zNwkeDWIeY+Q W6ZThC1XCefjv52Jf5f4m8fX5IS+mkqL+QzEZHlTQdgad9UqaQZuWDTVz8c4s6wD1swO yG+KICTbdG7fjx6ywpqz41lmXY+GA8bMbOmUDTTD1NNOrIH0g8e8oLyzdC9+bKPKCn5z 7Sew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737586621; x=1738191421; 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=HCJijnh3eucUGl1hsJuof30/Y1t6y46+I+a/ErqCn/w=; b=sN+POr5qy6Eh6rN1bu1F3Y596fE6kjwngu18hTXtqjA/KLm3eBxA25LQDdAgyYg54z ZFVMKS+7uJiVIw70bQxeskxYRpJGyQxsAVP/XV6GrT6CtI+Pvw5GyKvrF1te74aYUXig hSClkheZV/WpNEEvcbUeQUfYO7drcstLZcS24pdrDzkZfw4XiUx/AJtxF3dMEDL/nsdt UtdX2Qo1qzNMoDIm5ITcXXxntYOOyvQ25axgNv3Ah4bD64ih/Dz99Ya/c0ykxCS85tWk fAR1ye8AD3UMenEj3usbR1sRtDbFlcTcoTc2DK3KxyieHuTX5/Q1Nm8D9MwWS31hyKuF xpZw== X-Forwarded-Encrypted: i=1; AJvYcCXCfj1/Ytn6tsauEfXNT1YRsoQjvZQtL+UKX2ep834yC5TlEseR3gty1fPC8c84XIu6FIm2RM7TEgHsUtI=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7LWwUNEYWOfy/cpDd/4MOJgga9d6GHuvfyfti9YP+GigM/+PS 6S7vJ/iDyQtAM/hMUKVx46GKLvpJfRc9mWP8Bps1kSu4QRRatG9NafynW09Pyrs= X-Gm-Gg: ASbGncuQjelyPKMjrCrkozfSkp8XYjGxHRO9zpJsmntBaw2e0R8oZA+Zy73dsYOUZrV LX9aHWYNJAV6wQe9Oa89FZCIAA102QKiWAZwvr9M7WkhGxgNQC5ESOPQCZ/AWbV5Q45rpQe2Un9 zp5y+lQZ3CnYXpTw19gNcCXVox2FFuWNCJzg3cnXFLBxcnaYVC0MnkfE6+jt8kWyx1Oqqp+6tO0 2jKbVERCvrQC8cB+V1hBQx+9DcXMmDwFXWo10WyQaUjoFnolaIRkchg8YFuO0XjZR2mFgMFt13T 1P/Uwu382g== X-Google-Smtp-Source: AGHT+IHS1l32r3sB5S5xxvBnREu6zfuZQuMS6eKu4LrFATLcIt6SklxYU1mGoLAC7M2MGJdnMK+c1Q== X-Received: by 2002:a17:90b:1f88:b0:2ee:db8a:2a01 with SMTP id 98e67ed59e1d1-2f782d5d838mr33980210a91.30.1737586620724; Wed, 22 Jan 2025 14:57:00 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f7e6aaf3b4sm2301375a91.25.2025.01.22.14.56.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2025 14:56:59 -0800 (PST) From: Charlie Jenkins Date: Wed, 22 Jan 2025 14:56:23 -0800 Subject: [PATCH 4/4] entry: Inline syscall_exit_to_user_mode() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250122-riscv_optimize_entry-v1-4-4ee95559cfd0@rivosinc.com> References: <20250122-riscv_optimize_entry-v1-0-4ee95559cfd0@rivosinc.com> In-Reply-To: <20250122-riscv_optimize_entry-v1-0-4ee95559cfd0@rivosinc.com> To: Paul Walmsley , Palmer Dabbelt , Huacai Chen , WANG Xuerui , Thomas Gleixner , Peter Zijlstra , Andy Lutomirski , Alexandre Ghiti Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, Charlie Jenkins X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=5140; i=charlie@rivosinc.com; h=from:subject:message-id; bh=Z1tS2IEfH8NjFHjnsgjlU412ZTPZo9IgZRJ3cE9Ze5k=; b=owGbwMvMwCXWx5hUnlvL8Y3xtFoSQ/rE8i1Xrbq3yFk9XtolEHc+YPPKS4zHdLyPbp1l/T3K8 ibjJu3nHaUsDGJcDLJiiiw81xqYW+/olx0VLZsAM4eVCWQIAxenAExEo4mR4cTqzcKbngRY/JjW 1KZ9LnmP/3vOqTsu/eEzCLYLaWh9uJbhr5xYRZ/aZPPgR5fkV/YHnnWcJf2O39WubLv+9xcTFs2 W4QIA X-Developer-Key: i=charlie@rivosinc.com; a=openpgp; fpr=7D834FF11B1D8387E61C776FFB10D1F27D6B1354 Architectures using the generic entry code can be optimized by having syscall_exit_to_user_mode inlined. Signed-off-by: Charlie Jenkins --- include/linux/entry-common.h | 43 ++++++++++++++++++++++++++++++++++++-- kernel/entry/common.c | 49 +---------------------------------------= ---- 2 files changed, 42 insertions(+), 50 deletions(-) diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h index fc61d0205c97084acc89c8e45e088946f5e6d9b2..a46861ffd6858fadf4014c387e8= f2f216a879c25 100644 --- a/include/linux/entry-common.h +++ b/include/linux/entry-common.h @@ -14,6 +14,7 @@ #include =20 #include +#include =20 /* * Define dummy _TIF work flags if not defined by the architecture or for @@ -366,6 +367,15 @@ static __always_inline void exit_to_user_mode(void) lockdep_hardirqs_on(CALLER_ADDR0); } =20 +/** + * syscall_exit_work - Handle work before returning to user mode + * @regs: Pointer to current pt_regs + * @work: Current thread syscall work + * + * Do one-time syscall specific work. + */ +void syscall_exit_work(struct pt_regs *regs, unsigned long work); + /** * syscall_exit_to_user_mode_work - Handle work before returning to user m= ode * @regs: Pointer to currents pt_regs @@ -379,7 +389,30 @@ static __always_inline void exit_to_user_mode(void) * make the final state transitions. Interrupts must stay disabled between * return from this function and the invocation of exit_to_user_mode(). */ -void syscall_exit_to_user_mode_work(struct pt_regs *regs); +static __always_inline void syscall_exit_to_user_mode_work(struct pt_regs = *regs) +{ + unsigned long work =3D READ_ONCE(current_thread_info()->syscall_work); + unsigned long nr =3D syscall_get_nr(current, regs); + + CT_WARN_ON(ct_state() !=3D CONTEXT_KERNEL); + + if (IS_ENABLED(CONFIG_PROVE_LOCKING)) { + if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr)) + local_irq_enable(); + } + + rseq_syscall(regs); + + /* + * Do one-time syscall specific work. If these work items are + * enabled, we want to run them exactly once per syscall exit with + * interrupts enabled. + */ + if (unlikely(work & SYSCALL_WORK_EXIT)) + syscall_exit_work(regs, work); + local_irq_disable_exit_to_user(); + exit_to_user_mode_prepare(regs); +} =20 /** * syscall_exit_to_user_mode - Handle work before returning to user mode @@ -410,7 +443,13 @@ void syscall_exit_to_user_mode_work(struct pt_regs *re= gs); * exit_to_user_mode(). This function is preferred unless there is a * compelling architectural reason to use the separate functions. */ -void syscall_exit_to_user_mode(struct pt_regs *regs); +static __always_inline void syscall_exit_to_user_mode(struct pt_regs *regs) +{ + instrumentation_begin(); + syscall_exit_to_user_mode_work(regs); + instrumentation_end(); + exit_to_user_mode(); +} =20 /** * irqentry_enter_from_user_mode - Establish state before invoking the irq= handler diff --git a/kernel/entry/common.c b/kernel/entry/common.c index e33691d5adf7aab4af54cf2bf8e5ef5bd6ad1424..f55e421fb196dd5f9d4e34dd85a= e096c774cf879 100644 --- a/kernel/entry/common.c +++ b/kernel/entry/common.c @@ -146,7 +146,7 @@ static inline bool report_single_step(unsigned long wor= k) return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP; } =20 -static void syscall_exit_work(struct pt_regs *regs, unsigned long work) +void syscall_exit_work(struct pt_regs *regs, unsigned long work) { bool step; =20 @@ -173,53 +173,6 @@ static void syscall_exit_work(struct pt_regs *regs, un= signed long work) ptrace_report_syscall_exit(regs, step); } =20 -/* - * Syscall specific exit to user mode preparation. Runs with interrupts - * enabled. - */ -static void syscall_exit_to_user_mode_prepare(struct pt_regs *regs) -{ - unsigned long work =3D READ_ONCE(current_thread_info()->syscall_work); - unsigned long nr =3D syscall_get_nr(current, regs); - - CT_WARN_ON(ct_state() !=3D CT_STATE_KERNEL); - - if (IS_ENABLED(CONFIG_PROVE_LOCKING)) { - if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr)) - local_irq_enable(); - } - - rseq_syscall(regs); - - /* - * Do one-time syscall specific work. If these work items are - * enabled, we want to run them exactly once per syscall exit with - * interrupts enabled. - */ - if (unlikely(work & SYSCALL_WORK_EXIT)) - syscall_exit_work(regs, work); -} - -static __always_inline void __syscall_exit_to_user_mode_work(struct pt_reg= s *regs) -{ - syscall_exit_to_user_mode_prepare(regs); - local_irq_disable_exit_to_user(); - exit_to_user_mode_prepare(regs); -} - -void syscall_exit_to_user_mode_work(struct pt_regs *regs) -{ - __syscall_exit_to_user_mode_work(regs); -} - -__visible noinstr void syscall_exit_to_user_mode(struct pt_regs *regs) -{ - instrumentation_begin(); - __syscall_exit_to_user_mode_work(regs); - instrumentation_end(); - exit_to_user_mode(); -} - noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs) { enter_from_user_mode(regs); --=20 2.43.0