From patchwork Thu Mar 6 00:43:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 14003678 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 C3361C28B22 for ; Thu, 6 Mar 2025 00:44:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :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=K574FHPasfcBoR7F0SLDoTWHHesWTUHmk6Tjlypguyo=; b=3F6n8QVJinoXsW qOSRzRXqz4lS+klPiASOkcJDvuwvVc7JlUydMO/wsaXMvfAxmLkGt+AfmScEensqvHk3r5jCvMuB+ sdgReOjxny3utOb51p7gdZGBUsLiwMgG+wh5MOasGCerXG4cdyOO/1b1+YOOq3w+/liEvTeF3sgtm QO3vSxtS2HhBCM4yMsQYUyFuC96oed8y4vBR8Y4JQoINVm+sDja4qgF1alRecmyRQkBpL4koXdZbv ixfercTVuMRFp6pUqDuA4gFRUMN0P7iyeRLWQB88ofhae/LPdnUk2wI+iSx1gNom5s5zQzYvfuP5a ne+HAHUX1n21ZZupveBw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tpzLR-00000009ijt-2pmg; Thu, 06 Mar 2025 00:44:13 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tpzLO-00000009ih5-3pTg for linux-riscv@lists.infradead.org; Thu, 06 Mar 2025 00:44:12 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2241053582dso653145ad.1 for ; Wed, 05 Mar 2025 16:44:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741221850; x=1741826650; 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=a+zSCWqqyKc551VynXTcxotuXwo5gv9XFD/e4PnRFpw=; b=iJtvKeWXMSiB8HimgMwx19j8/3zt38hc37MEFlCr8pSMTGzXE8ppl+4/sBtqprBc9X PMugahVgPPmstkmuA0O6jcs9uE8U8vQO7Dv4S1x7Xyl1bc4gpGwnekMULmcmNqyMJjEx LabugXfZifYjAR+CMOy1C7e0wl6QfkcVAXl2wybyL8opOX2sWcJCu0LDEn6rZ/7pgS3N EYcj4cyMc42xpcmurXyReFqiwWkivu8DOCvx4fybI98r/hIsTwpCxsz+bc3jUm1ehVkG qoZqY1Ckd32LaB7MfrohXQz5S3bkNCUXsSg0PbGv/OY64+smPY8C676MSfl2jD9qwzWF ycOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741221850; x=1741826650; 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=a+zSCWqqyKc551VynXTcxotuXwo5gv9XFD/e4PnRFpw=; b=LAejGh0pX7/Y/xmNE2B64HRZozqOyMA2Wh4k8Zti/pEursvvkdazTdsk7WvXK/A044 6WmmPcxVYobWoA2d6lFf950POEwXQCUDBs4UAuX37Xxh25AaE+wUlVfQJyeTIsR9d4QE 5F6UbBqDzYnX4xOB+zzOWgZ6W2/QFX8AJApBU7OxRZby31kytoyymISphz01ViNVcOqr 1ukzVfUoZBEn6uubhzfXnd5qA/vY1h8/40IYZKo481nTdkPPN39fwbyNNWSREE533yll hyOPHbMJpapejBaAuPxsiO/vaULqy5BpoTt4XcU8l/bRpRJtSrLrQ03HJlKaE7xa1KKn pCAQ== X-Gm-Message-State: AOJu0Yymjct9z9spCpjpjEsa26Zx2PfmRP4b+hFMgvQQSc4KCIgzxj7T xLFHSHlrorCgHwtZ027iEu+dGsfm4EZjWN+vm5qcUgqTIMXzb6BZkByzEXNfv4ba/GG4gU/GILV d X-Gm-Gg: ASbGncuAvwIDFdueWt7UrpKeHtpb603TZEg0QQh45q/C5WwdXXQwx+jylirJ0SlBxzx LAzskwqTCIEXa5zpg4t7xnPiBBfCoe+11oSOA0ZK3NdnMPNxte9XaEwbCYfTwBJe+CjtKCkorLI nBhxHhhVvd5l5G3g3+Xmnu43AHgXYhf9qdHAeOLViUzjnT2ZlwPyJH7Fevx6uUKydFrGhyXSXvq 8zA9mnL0GViJ58eElD1ePB6l6YWjcEFVPkQuT3x5D0vpUGTlsjt5HTWHg6X/A0Tkd5VZJ5FKbd1 4nJsg8xYIRvl29eMXTbx2vNiIgZOC/4qYKWBEtZY4zMeguh+C1PD6EDjs2oo X-Google-Smtp-Source: AGHT+IGYFA+K5CP6Cy9THeZNXREi+YFcFuwtTfzr0w6OgFVQJjQCMGiweUstJDPBRDAGvCr9mJ4IwQ== X-Received: by 2002:a17:903:2305:b0:224:de2:7fd6 with SMTP id d9443c01a7336-2240de28335mr9026765ad.25.1741221850434; Wed, 05 Mar 2025 16:44:10 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22410a7f7efsm347085ad.132.2025.03.05.16.44.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Mar 2025 16:44:09 -0800 (PST) From: Charlie Jenkins Date: Wed, 05 Mar 2025 16:43:46 -0800 Subject: [PATCH v5 3/4] LoongArch: entry: Migrate ret_from_fork() to C MIME-Version: 1.0 Message-Id: <20250305-riscv_optimize_entry-v5-3-6507b5dff3ce@rivosinc.com> References: <20250305-riscv_optimize_entry-v5-0-6507b5dff3ce@rivosinc.com> In-Reply-To: <20250305-riscv_optimize_entry-v5-0-6507b5dff3ce@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=5547; i=charlie@rivosinc.com; h=from:subject:message-id; bh=ji6jPR39ZKDby+guv8CCAiBLyKbpGz9Kynms9BH9fJU=; b=owGbwMvMwCXWx5hUnlvL8Y3xtFoSQ/qJ95ePTzBvvzPfOaKeK9gkZ6GPsf20tfcCnKo13u4KZ LworCLQUcrCIMbFICumyMJzrYG59Y5+2VHRsgkwc1iZQIYwcHEKwETSrjEyXF0Ud+RJZLCFOkfG I+4SH97LUxlbNP/rrNyTuuCCQeD5Gob/abeMBayufysUjl2cYcByfGvR7nPRXyd+OcZXxRJx4OZ 2HgA= X-Developer-Key: i=charlie@rivosinc.com; a=openpgp; fpr=7D834FF11B1D8387E61C776FFB10D1F27D6B1354 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250305_164410_956074_1609AD48 X-CRM114-Status: GOOD ( 10.56 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org LoongArch is the only architecture that calls syscall_exit_to_user_mode() from asm. Move the call into C so that this function can be inlined across all architectures. Signed-off-by: Charlie Jenkins --- arch/loongarch/include/asm/asm-prototypes.h | 8 +++++++ arch/loongarch/kernel/entry.S | 22 +++++++++---------- arch/loongarch/kernel/process.c | 33 +++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/arch/loongarch/include/asm/asm-prototypes.h b/arch/loongarch/include/asm/asm-prototypes.h index 51f224bcfc654228ae423e9a066b25b35102a5b9..704066b4f7368be15be960fadbcd6c2574bbf6c0 100644 --- a/arch/loongarch/include/asm/asm-prototypes.h +++ b/arch/loongarch/include/asm/asm-prototypes.h @@ -12,3 +12,11 @@ __int128_t __ashlti3(__int128_t a, int b); __int128_t __ashrti3(__int128_t a, int b); __int128_t __lshrti3(__int128_t a, int b); #endif + +asmlinkage void noinstr __no_stack_protector ret_from_fork(struct task_struct *prev, + struct pt_regs *regs); + +asmlinkage void noinstr __no_stack_protector ret_from_kernel_thread(struct task_struct *prev, + struct pt_regs *regs, + int (*fn)(void *), + void *fn_arg); diff --git a/arch/loongarch/kernel/entry.S b/arch/loongarch/kernel/entry.S index 48e7e34e355e83eae8165957ba2eac05a8bf17df..2abc29e573810e000f2fef4646ddca0dbb80eabe 100644 --- a/arch/loongarch/kernel/entry.S +++ b/arch/loongarch/kernel/entry.S @@ -77,24 +77,22 @@ SYM_CODE_START(handle_syscall) SYM_CODE_END(handle_syscall) _ASM_NOKPROBE(handle_syscall) -SYM_CODE_START(ret_from_fork) +SYM_CODE_START(ret_from_fork_asm) UNWIND_HINT_REGS - bl schedule_tail # a0 = struct task_struct *prev - move a0, sp - bl syscall_exit_to_user_mode + move a1, sp + bl ret_from_fork RESTORE_STATIC RESTORE_SOME RESTORE_SP_AND_RET -SYM_CODE_END(ret_from_fork) +SYM_CODE_END(ret_from_fork_asm) -SYM_CODE_START(ret_from_kernel_thread) +SYM_CODE_START(ret_from_kernel_thread_asm) UNWIND_HINT_REGS - bl schedule_tail # a0 = struct task_struct *prev - move a0, s1 - jirl ra, s0, 0 - move a0, sp - bl syscall_exit_to_user_mode + move a1, sp + move a2, s0 + move a3, s1 + bl ret_from_kernel_thread RESTORE_STATIC RESTORE_SOME RESTORE_SP_AND_RET -SYM_CODE_END(ret_from_kernel_thread) +SYM_CODE_END(ret_from_kernel_thread_asm) diff --git a/arch/loongarch/kernel/process.c b/arch/loongarch/kernel/process.c index 6e58f65455c7ca3eae2e88ed852c8655a6701e5c..98bc60d7c550fcc0225e8452f81a7d6cd7888015 100644 --- a/arch/loongarch/kernel/process.c +++ b/arch/loongarch/kernel/process.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -33,6 +34,7 @@ #include #include +#include #include #include #include @@ -47,6 +49,7 @@ #include #include #include +#include #include #include @@ -63,8 +66,9 @@ EXPORT_SYMBOL(__stack_chk_guard); unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE; EXPORT_SYMBOL(boot_option_idle_override); -asmlinkage void ret_from_fork(void); -asmlinkage void ret_from_kernel_thread(void); +asmlinkage void restore_and_ret(void); +asmlinkage void ret_from_fork_asm(void); +asmlinkage void ret_from_kernel_thread_asm(void); void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp) { @@ -138,6 +142,23 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) return 0; } +asmlinkage void noinstr __no_stack_protector ret_from_fork(struct task_struct *prev, + struct pt_regs *regs) +{ + schedule_tail(prev); + syscall_exit_to_user_mode(regs); +} + +asmlinkage void noinstr __no_stack_protector ret_from_kernel_thread(struct task_struct *prev, + struct pt_regs *regs, + int (*fn)(void *), + void *fn_arg) +{ + schedule_tail(prev); + fn(fn_arg); + syscall_exit_to_user_mode(regs); +} + /* * Copy architecture-specific thread state */ @@ -165,8 +186,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.reg03 = childksp; p->thread.reg23 = (unsigned long)args->fn; p->thread.reg24 = (unsigned long)args->fn_arg; - p->thread.reg01 = (unsigned long)ret_from_kernel_thread; - p->thread.sched_ra = (unsigned long)ret_from_kernel_thread; + p->thread.reg01 = (unsigned long)ret_from_kernel_thread_asm; + p->thread.sched_ra = (unsigned long)ret_from_kernel_thread_asm; memset(childregs, 0, sizeof(struct pt_regs)); childregs->csr_euen = p->thread.csr_euen; childregs->csr_crmd = p->thread.csr_crmd; @@ -182,8 +203,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) childregs->regs[3] = usp; p->thread.reg03 = (unsigned long) childregs; - p->thread.reg01 = (unsigned long) ret_from_fork; - p->thread.sched_ra = (unsigned long) ret_from_fork; + p->thread.reg01 = (unsigned long) ret_from_fork_asm; + p->thread.sched_ra = (unsigned long) ret_from_fork_asm; /* * New tasks lose permission to use the fpu. This accelerates context