From patchwork Thu Oct 10 11:33:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 13829992 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 1EAA1CF11D2 for ; Thu, 10 Oct 2024 11:41:37 +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=TDl9iLJ9IcIQCUiDPdY7G7jOst94Sq4L5J+um8W/x4k=; b=odaegtXsQMx4tfc7X4PGzpVzFT 1OL/ZB+mSvYAdVsNch2F6J3THcc5gmqJkF2kCzhwTl993pT7GIPz3Vv8L/c/3fQZKL7z2QU2TNSoz jmtMaWuIYQM72BtOoKNqw2RwP9rwRhCXk8jubSPCX12E86lf1O7oM8SO3+MGJpRjHwSUCJKiQbG8l JRFOmGoEtBZPUP1XJ2qTyWyHu0MEjOIXFEC2bwP5Qfz4lU4GR3JaAjbKtUmcsFn4BssTmbAOGd3nf feOhdTC6b+cukvuto3gI+lq/5oR+7lnyYQghV8Ep22sayI/y/vLPE7gRpeCWpK8ZYGcRWFJJIGa8F c5L2cJkw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1syrXs-0000000CaDT-2S44; Thu, 10 Oct 2024 11:41:28 +0000 Received: from mail-lj1-x230.google.com ([2a00:1450:4864:20::230]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1syrQW-0000000CYAb-17C9 for linux-arm-kernel@lists.infradead.org; Thu, 10 Oct 2024 11:33:56 +0000 Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2fac6b3c220so12352021fa.2 for ; Thu, 10 Oct 2024 04:33:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1728560030; x=1729164830; 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=TDl9iLJ9IcIQCUiDPdY7G7jOst94Sq4L5J+um8W/x4k=; b=t31gOHv7FscNVsIsBzC7KYHWTJJwU7B3sxPafNjxUhBx+72Vs9lbCTLK1ug5WE4RL7 HSgov+hpQW+nV8ZRKExQrVjWPD7CsKie0KbBtt9IBP/VU5MU0zcGGXNJO3BxSqovCdJw 4o6JudFvH5WyJTw6ZKVoomutH2/eH76R96NY2i3vo1kmRlPgItz05xErlNbeYqEZmhFD U3kDJJeeH1P3awMDn1DfNAOMJX/dn0Po6625rM5BNze+clFkpdSFclY/UwxFI+J83afD 9RGFnmXo8vGJO+kOqWnUkuO+O1T7a82B+wNAxY4Y3Iy8obHHyZCVBg7nsTSfWuLKhfgU Dy/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728560030; x=1729164830; 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=TDl9iLJ9IcIQCUiDPdY7G7jOst94Sq4L5J+um8W/x4k=; b=WW6n1IZnkNqrHAPb+BEQx2lWI9Fu61basNW8hPk8wyahRIGtYyh1Qts23IxSh+6UXA tl2RG29r/gvymXG7FXzv7QhUAU0tCcw50oCiVnHUZUAD7ZGwOrWBNGVPIFvXeR+qduXw E+17cOxZAfYuxXAVNbFf3cC3+n7r3wRKW22ZSQWURfYQNNQdxMm3eUuHubgecrVSW3P/ LS2C6UxK4A5g7K7xkKK+xYXJtit1EckEpBTVc9vaCoNlXXwM1CbqXxg30qXj7ybj3rT+ 4IgcIXtZn0KIrDbNrlC3Ed9VIaO6q6UbXaNSz7HhUpI7i6HNXRSpoNBnSmVPcoqd7PbZ eRRQ== X-Gm-Message-State: AOJu0Yzdz8y9O0fWEAGawEfdmK6EG1HYnm9svgau7ke3ReJ8PYmDtGo0 GzC6dKat6zvr+zY4GJxRpCo3N5yXA8MC3Qt769Gi7sxxIqpKmIJlGAgSkXjuWmU= X-Google-Smtp-Source: AGHT+IFYXNUhaWmpiLq2q8xgds0Kykd3WYlCfTU5AddMN69mncHRj5KixgWaFupTNP5HEIlSrViaww== X-Received: by 2002:a2e:b8cb:0:b0:2f3:f1ee:2256 with SMTP id 38308e7fff4ca-2fb18807a65mr56092621fa.44.1728560030103; Thu, 10 Oct 2024 04:33:50 -0700 (PDT) Received: from lino.lan ([85.235.12.238]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a99a7f25406sm77619066b.69.2024.10.10.04.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2024 04:33:48 -0700 (PDT) From: Linus Walleij Date: Thu, 10 Oct 2024 13:33:42 +0200 Subject: [PATCH RFC 04/28] ARM: process: Rewrite ret_from_fork i C MIME-Version: 1.0 Message-Id: <20241010-arm-generic-entry-v1-4-b94f451d087b@linaro.org> References: <20241010-arm-generic-entry-v1-0-b94f451d087b@linaro.org> In-Reply-To: <20241010-arm-generic-entry-v1-0-b94f451d087b@linaro.org> To: 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.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241010_043352_418396_09539486 X-CRM114-Status: GOOD ( 19.10 ) 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 Move the direct assembly call to schedule_tail() and the call starting a kernel thread into a C version of ret_from_fork() in process.c and rename the old ret_from_fork to ret_from_fork_asm following the pattern in x86. Leave a comment on what we will do when we get to switching to generic entry with this as a base. Signed-off-by: Linus Walleij --- arch/arm/include/asm/switch_to.h | 4 ++++ arch/arm/kernel/entry-common.S | 22 +++++++++++++++------- arch/arm/kernel/process.c | 24 ++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/arch/arm/include/asm/switch_to.h b/arch/arm/include/asm/switch_to.h index 9372348516ce..18d084aff5a4 100644 --- a/arch/arm/include/asm/switch_to.h +++ b/arch/arm/include/asm/switch_to.h @@ -17,6 +17,10 @@ #define __complete_pending_tlbi() #endif +asmlinkage void ret_from_fork_asm(void); +__visible void ret_from_fork(struct task_struct *prev, struct pt_regs *regs, + int (*fn)(void *), void *fn_arg); + /* * switch_to(prev, next) should switch from task `prev' to `next' * `prev' will never be the same as `next'. schedule() itself diff --git a/arch/arm/kernel/entry-common.S b/arch/arm/kernel/entry-common.S index 2f93c2f6d5b8..df6961a1006b 100644 --- a/arch/arm/kernel/entry-common.S +++ b/arch/arm/kernel/entry-common.S @@ -5,6 +5,7 @@ * Copyright (C) 2000 Russell King */ +#include #include #include #include @@ -128,15 +129,22 @@ ENDPROC(ret_to_user) /* * This is how we return from a fork. */ -ENTRY(ret_from_fork) - bl schedule_tail - cmp r5, #0 - movne r0, r4 - badrne lr, 1f - retne r5 +SYM_TYPED_FUNC_START(ret_from_fork_asm) + /* + * ret_from_fork() expects: + * r0: prev + * r1: regs + * r2: fn() for kernel process or NULL + * r3: fn_arg for kernel process or N/A + */ + mov r1, sp + mov r2, r5 + mov r3, r4 + bl ret_from_fork + mov r0, #0 1: get_thread_info tsk b ret_to_user -ENDPROC(ret_from_fork) +SYM_FUNC_END(ret_from_fork_asm) /*============================================================================= * SWI handler diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c index e16ed102960c..81c1c3f98834 100644 --- a/arch/arm/kernel/process.c +++ b/arch/arm/kernel/process.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -230,7 +231,26 @@ void flush_thread(void) thread_notify(THREAD_NOTIFY_FLUSH, thread); } -asmlinkage void ret_from_fork(void) __asm__("ret_from_fork"); +/* The assembly stub sets up the parameters and calls ret_from_fork() */ +asmlinkage void ret_from_fork_asm(void) __asm__("ret_from_fork_asm"); + +__visible void ret_from_fork(struct task_struct *prev, struct pt_regs *regs, + int (*fn)(void *), void *fn_arg) +{ + schedule_tail(prev); + + /* Is this a kernel thread? */ + if (unlikely(fn)) { + fn(fn_arg); + /* + * A kernel thread is allowed to return here after successfully + * calling kernel_execve(). Exit to userspace to complete the + * execve() syscall. + */ + } + + /* syscall_exit_to_user_mode(regs); here once we switch to generic entry */ +} int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) { @@ -263,7 +283,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) thread->cpu_context.r5 = (unsigned long)args->fn; childregs->ARM_cpsr = SVC_MODE; } - thread->cpu_context.pc = (unsigned long)ret_from_fork; + thread->cpu_context.pc = (unsigned long)ret_from_fork_asm; thread->cpu_context.sp = (unsigned long)childregs; clear_ptrace_hw_breakpoint(p);