From nobody Sun Feb 8 19:48:50 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 052531BC20 for ; Thu, 6 Mar 2025 00:44:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741221849; cv=none; b=EIkbNL7WyEkyS6n+pkqLYx76/tqFKz87RqXZ4wxQgjiY9rAWuWvpqPjKVOLljzIrVqJsf8qqUkpNxuFGYmC5cGjdioPVPufhjRHEVKGiGAU0QVU6eYOURoXz1FkJ83yLCEE/QIVtbhpFXDHkQu4TQJD2S9QxOCsLTmOduGDER4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741221849; c=relaxed/simple; bh=gaq/v9YuAdYAHAPq6G2li6XLwEhlQb1rcc0R86FQKSc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CJr7AQqHcbX/EZK2DSuikHDeysIjHphA+pRGDM4ernmA1VOCABgW7I6NJb9Y/8Lz+j1Wu3lUfxDyIy269u9LgOV9lBlJorWlNXKXFruRW8widZi+sUnk0QhISwfNecp97rRvKEqoqijDcIZ3dpC4van2Kj8aANiE8I4YBbkuG/4= 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=05jPVfS9; arc=none smtp.client-ip=209.85.214.179 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="05jPVfS9" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2234e5347e2so996195ad.1 for ; Wed, 05 Mar 2025 16:44:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741221847; x=1741826647; 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=R3jS1rl0NyPiPtBrV6gbvaKvWtHD4LTe2LvNMOIdkhU=; b=05jPVfS9KwqNHxZl2G5BIfH9D1Ub+FXc2sDPcMpZ8vHIG512yFWcxhtnJYhu6eh1ke geyahNNays0DQob808OPwSl1kU4c0t8GcSiQpv0FPfCw/wKkuvzl4lRYCne1VWfrJEyF CBygSWCgvUBuaiQbP0vXFeTpQAYc6GXGCk2xt/ev9Avl++ua6UN52oznbuTKgIhicV5w wOSuIAkBCImrkOoyIzTFtpWG7AhgAuYQ9N1njN4XI3JquB8WDg67ylg17rB0mhIJZg6T xpqhUbFu4Fi3swxO7PXZqDVfVAmGfFeN3Sx9q1ZUjMI5fV6KzNsM2ElmIDAO5GcmgQzb 1NFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741221847; x=1741826647; 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=R3jS1rl0NyPiPtBrV6gbvaKvWtHD4LTe2LvNMOIdkhU=; b=IvYO0+tl0GQkUc2qhoa01XCieiK2ife3EJZQG4xlJi+uAFYLv3G/a3zAu3ecbU29+V gWRyO3FN5NdlmlMs2AVGekAqgms4ZmYts6Ax4+H5kMDxkFU3QmRXGD0lY8Dh4tUxBd/r gcN6ztXjy9yz7Rj1M4ArlflJJSEEvzZcPn1n3Mmj/2+NX1KNA9Z4C+/YOI5hy8cXzWg9 k4yKGKcvFPTd4ZGsIMeYLGR4KBkymL23racp5e1cfLcgFLoMYVdWx/JF1G5eu/NLE4/6 bLE7nwhRzjsaC9FrG3G8flryrbPB0mObSvliV6sTlHvo4iJeJzN0DHdoANNlRd5PDF+U 6c0w== X-Forwarded-Encrypted: i=1; AJvYcCXD3RSz2DYKgAcI2F5qK1X4XsYxhELQcO9NcfbLvMuOMb05V5n+oTRVmgeQ6fQdvVgXaMvjVg4u7NDiK4w=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2rfsGLAav8LxUiV7KLXRJCWLrdGg9UIzjhBgUJJNzy1kuuUsh rdIKBWSyPrEPDPMIx4bCErH/TV3fD/iMGHUcajP0ktHjnNjhNnwrDsx6XKfTTl4= X-Gm-Gg: ASbGnctUEsJg2ACNz5g/9T1bcmY8rqZk07QS6uy9HTvCYFjHBt3wmzAmOCAHWMalZjp UrRj36jum6sM4I5JqWTTGfaGUvOmSP44J86sE3oS1UyxK16IYzTScSselnPYyrpessGqDdv+6Ul BcdfmCMD1fLRwBCHW+w+Rp6GQYYR2MnKV52cdT+6THpZWQHmQN0hnKeaDCdcnkWFvXIh+UyU38p hDyYqiIG4k8/7Dn5tym020UPzz5m6+tV5gYFrZVoboCGWk2XYfxlIH2HhqhKoFUjDPo5bzj5V98 geEKrzQTSelNrz8xKEnWZX30vXlEWfd61jHkbAiewy5ZTnyQhlUIGWr1htpY X-Google-Smtp-Source: AGHT+IF34qhqv3xIO9NNzcZh5fK7vqon1KbTtupxHGRr8Axwn6/+XVCjLb/ogfXTpG9fz+kT+EQwYg== X-Received: by 2002:a17:902:f606:b0:223:f9a4:3f99 with SMTP id d9443c01a7336-223f9a457a2mr63068485ad.29.1741221847212; Wed, 05 Mar 2025 16:44:07 -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.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Mar 2025 16:44:06 -0800 (PST) From: Charlie Jenkins Date: Wed, 05 Mar 2025 16:43:44 -0800 Subject: [PATCH v5 1/4] riscv: entry: Convert ret_from_fork() to C 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: <20250305-riscv_optimize_entry-v5-1-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=3515; i=charlie@rivosinc.com; h=from:subject:message-id; bh=gaq/v9YuAdYAHAPq6G2li6XLwEhlQb1rcc0R86FQKSc=; b=owGbwMvMwCXWx5hUnlvL8Y3xtFoSQ/qJ95cmmFbUVL2sO39dKoPRMmj9pzsX2Qs+M8168p73+ VOJGWfbO0pZGMS4GGTFFFl4rjUwt97RLzsqWjYBZg4rE8gQBi5OAZhI7VFGhsfPeK/yBc58PmH/ xAU9hi8Wqni+fhxzylBn0evACz3GjdwMv9mKOK8wLMzQEsx4Ob15ef/Hi1oMOlvuvJux01vD0Td nMhcA X-Developer-Key: i=charlie@rivosinc.com; a=openpgp; fpr=7D834FF11B1D8387E61C776FFB10D1F27D6B1354 Move the main section of ret_from_fork() to C to allow inlining of syscall_exit_to_user_mode(). Signed-off-by: Charlie Jenkins Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/asm-prototypes.h | 1 + arch/riscv/kernel/entry.S | 15 ++++++--------- arch/riscv/kernel/process.c | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/a= sm/asm-prototypes.h index cd627ec289f163a630b73dd03dd52a6b28692997..733ff609778797001006c33bba9= e3cc5b1f15387 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -52,6 +52,7 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_s); DECLARE_DO_ERROR_INFO(do_trap_ecall_m); DECLARE_DO_ERROR_INFO(do_trap_break); =20 +asmlinkage void ret_from_fork(void *fn_arg, int (*fn)(void *), struct pt_r= egs *regs); asmlinkage void handle_bad_stack(struct pt_regs *regs); asmlinkage void do_page_fault(struct pt_regs *regs); asmlinkage void do_irq(struct pt_regs *regs); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 33a5a9f2a0d4e1eeccfb3621b9e518b88e1b0704..b2dc5e7c7b3a843fa4aa02eba2a= 911eb3ce31d1f 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -319,17 +319,14 @@ SYM_CODE_END(handle_kernel_stack_overflow) ASM_NOKPROBE(handle_kernel_stack_overflow) #endif =20 -SYM_CODE_START(ret_from_fork) +SYM_CODE_START(ret_from_fork_asm) call schedule_tail - beqz s0, 1f /* not from kernel thread */ - /* Call fn(arg) */ - move a0, s1 - jalr s0 -1: - move a0, sp /* pt_regs */ - call syscall_exit_to_user_mode + move a0, s1 /* fn_arg */ + move a1, s0 /* fn */ + move a2, sp /* pt_regs */ + call ret_from_fork j ret_from_exception -SYM_CODE_END(ret_from_fork) +SYM_CODE_END(ret_from_fork_asm) =20 #ifdef CONFIG_IRQ_STACKS /* diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 7c244de7718008947075357ea4502d56419d507c..7b0a0bfe29aec896c2bdd8976d8= 55dd390de88d7 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -17,7 +17,9 @@ #include #include #include +#include =20 +#include #include #include #include @@ -36,7 +38,7 @@ unsigned long __stack_chk_guard __read_mostly; EXPORT_SYMBOL(__stack_chk_guard); #endif =20 -extern asmlinkage void ret_from_fork(void); +extern asmlinkage void ret_from_fork_asm(void); =20 void noinstr arch_cpu_idle(void) { @@ -206,6 +208,14 @@ int arch_dup_task_struct(struct task_struct *dst, stru= ct task_struct *src) return 0; } =20 +asmlinkage void ret_from_fork(void *fn_arg, int (*fn)(void *), struct pt_r= egs *regs) +{ + if (unlikely(fn)) + fn(fn_arg); + + syscall_exit_to_user_mode(regs); +} + int copy_thread(struct task_struct *p, const struct kernel_clone_args *arg= s) { unsigned long clone_flags =3D args->flags; @@ -242,7 +252,7 @@ int copy_thread(struct task_struct *p, const struct ker= nel_clone_args *args) p->thread.riscv_v_flags =3D 0; if (has_vector() || has_xtheadvector()) riscv_v_thread_alloc(p); - p->thread.ra =3D (unsigned long)ret_from_fork; + p->thread.ra =3D (unsigned long)ret_from_fork_asm; p->thread.sp =3D (unsigned long)childregs; /* kernel sp */ return 0; } --=20 2.43.0 From nobody Sun Feb 8 19:48:50 2026 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 BE386487BF for ; Thu, 6 Mar 2025 00:44:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741221852; cv=none; b=Ly7KpgcPjKoUQfa5H9U2uNVB1XIeAfJevzIcjlzqczi6R+5Uf6Gv+3rGjsoSoPH/GQw2B2IgdsztQtS61Q/154pvYQqwH04uaBNtds2pveIjuqFiIpWVxItkuJ273vDGDwcWAZ9NmD7lhXNF8Tdb9/S59djY2De85kA++5+e5UM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741221852; c=relaxed/simple; bh=LoTSopBOka5A9kpDiPFFurbF+1mjOb81UBycxnBlNbA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=R5MxF5MWjxWTOoTu+Auf/1FklfIVIEdE1IqIklDw749yQDNCNEKSD6uxYcReMa7g5nS0+Xp/xl9S7VrtTjlikDjoqJGz00PWrWBk9DLfeFb2Sv2s2mySTeUoZaqCfptrOjXiNCP/Kzywwp45pDp92x93bkl1P8Yxo4j+M1M0dxM= 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=h4uHD4+k; arc=none smtp.client-ip=209.85.214.171 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="h4uHD4+k" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2235189adaeso2147555ad.0 for ; Wed, 05 Mar 2025 16:44:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741221849; x=1741826649; 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=ehMUm3J1yyvZGiQlan49h9651rjeLktXjDjteQa4YzI=; b=h4uHD4+k0Ln0p30rwrlS23bOEfOAaclc9uXb9P7QsovsIw37FX6O8s6maZuaQCl1PE LrDqID8tch3sabExKE4WwRfxdGW/K29ztVCFB8GYF6fK7A/w2uZlXvY2IUB0D7jGqleM fh82vnk1ds4KCP2kt5WoBkD40S7TbXQY66AcL82F2sxrVcLp6697F5KTN2C+mc6O/7EI yErzQrLB4y2D9ezjHzH5ViZrmZZNMwOSROd1rPJfxgE3ZAcALzdZ+u1cDyyOQtuAo+FD G2MSb4Qwh3GNz3cKBCHbbGv1Mys7i8iE/RIay4FeJxqi/Phqi2J4CESgQfVzhzn4pT8O R2yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741221849; x=1741826649; 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=ehMUm3J1yyvZGiQlan49h9651rjeLktXjDjteQa4YzI=; b=E1+YVtHqWnEIISkGQR9JlgiGgr5CgHGT1HimW2xLy8Zkqpjoru8xKyD3bHb+GBUcwI TMqWKX0pjDjg3uiXR7UNaTEe0vVHyy6ixqpSHNLy/vBgZjpPsWNCQ+gvNC8SFy0hoGnu i4JVaFomnQvG36/Ik1pXqn1LBFKexDyTt1gR73bR296qj4yNVO9jWKgegQXVuknaN+SJ JqQJnWUYxr7BchwchdFEQ8SYnkAWuoPYfLVT/sGPyZzQOEik6n+P/7Z0G4vjCN/GQnzI 9z0Yc/bLWOd8oci4i7fa0TtG7HbGZwqDxzowUKrh/tgX4i/xgLNTLZvU4CtwNN7Z11SG qZnA== X-Forwarded-Encrypted: i=1; AJvYcCW2nvIAhmCj+/Maz0hE5j6wjk56jSk0RWeCgotqcLAWubjaXF3qvrnQFAZ/klvP43P1y1q6rP3MhyFjh9I=@vger.kernel.org X-Gm-Message-State: AOJu0Ywg1zS+HBgFN5Y9hRJbET7WY1NqhSbr+OrNIh7tD6NZMwh7TPIa qH4mKrljk+/eJqJ+QFRpRqz7TZhwbfHdNFRR8lvHUIIri/TcvGQqvCx/G9d8hl0= X-Gm-Gg: ASbGncuiiPakWVmmmcJCX7PRZHuukQIhQbV/z6fM2fLmMeYA5yDX1K2h3s1ritOkicG NYozF2l/0zxHkl9uHuPBNlyoOWIt+jvQTdMUO/tyZp0Q5d+g2YypTmvvp1kIfOGbJHsT70/RSrp poSjIl26pYYn7Rgsd3Y4klV7lAWJ0+XL6tQql8L83kviWhVabja+ak97AUVBIhOv6K0V5cUgKBi TvLRg3QsPDLdqGq7s1BxB8EacsVeJbpy8GIsEVSNTW50x1rJqctDJdYrSiubRZI9gd92Ql6k06S HYEoJcjjWF0pdiHIW4GXf/wD44aPlI9SV782TxtZ4VWJ8JmfxBfnU7Apes0F X-Google-Smtp-Source: AGHT+IHUzwBDw/uha6a2fjM59Txc8WQN/Vrb5TcHaRq3eYqWZnWT8m/ex64QjFGpr4eyefI929SBSw== X-Received: by 2002:a17:902:c943:b0:220:e91f:4408 with SMTP id d9443c01a7336-2240948d12fmr21414815ad.22.1741221848994; Wed, 05 Mar 2025 16:44:08 -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.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Mar 2025 16:44:08 -0800 (PST) From: Charlie Jenkins Date: Wed, 05 Mar 2025 16:43:45 -0800 Subject: [PATCH v5 2/4] riscv: entry: Split ret_from_fork() into user and kernel 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: <20250305-riscv_optimize_entry-v5-2-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=4464; i=charlie@rivosinc.com; h=from:subject:message-id; bh=LoTSopBOka5A9kpDiPFFurbF+1mjOb81UBycxnBlNbA=; b=owGbwMvMwCXWx5hUnlvL8Y3xtFoSQ/qJ95dzi+tVwqu5bU9Lb1zHVqURfOznouv2s2e7/J+z/ ItZ042DHaUsDGJcDLJiiiw81xqYW+/olx0VLZsAM4eVCWQIAxenAEwk7wIjw6S+bwF+haUsh6ez hRkGTla4Vckfo5V87KSvdlx+58+kXYwM3Z1PzoZozOg7tNdk7WpRtQWuZe9XxbPb9S5Xu/P274Z 8TgA= X-Developer-Key: i=charlie@rivosinc.com; a=openpgp; fpr=7D834FF11B1D8387E61C776FFB10D1F27D6B1354 This function was unified into a single function in commit ab9164dae273 ("riscv: entry: Consolidate ret_from_kernel_thread into ret_from_fork"). However that imposed a performance degradation. Partially reverting this commit to have ret_from_fork() split again results in a 1% increase on the number of times fork is able to be called per second. Signed-off-by: Charlie Jenkins --- arch/riscv/include/asm/asm-prototypes.h | 3 ++- arch/riscv/kernel/entry.S | 13 ++++++++++--- arch/riscv/kernel/process.c | 17 +++++++++++------ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/a= sm/asm-prototypes.h index 733ff609778797001006c33bba9e3cc5b1f15387..bfc8ea5f9319b19449ec59493b4= 5b926df888832 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -52,7 +52,8 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_s); DECLARE_DO_ERROR_INFO(do_trap_ecall_m); DECLARE_DO_ERROR_INFO(do_trap_break); =20 -asmlinkage void ret_from_fork(void *fn_arg, int (*fn)(void *), struct pt_r= egs *regs); +asmlinkage void ret_from_fork_kernel(void *fn_arg, int (*fn)(void *), stru= ct pt_regs *regs); +asmlinkage void ret_from_fork_user(struct pt_regs *regs); asmlinkage void handle_bad_stack(struct pt_regs *regs); asmlinkage void do_page_fault(struct pt_regs *regs); asmlinkage void do_irq(struct pt_regs *regs); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index b2dc5e7c7b3a843fa4aa02eba2a911eb3ce31d1f..0fb338000c6dc0358742cd03497= fa54b9e9d1aec 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -319,14 +319,21 @@ SYM_CODE_END(handle_kernel_stack_overflow) ASM_NOKPROBE(handle_kernel_stack_overflow) #endif =20 -SYM_CODE_START(ret_from_fork_asm) +SYM_CODE_START(ret_from_fork_kernel_asm) call schedule_tail move a0, s1 /* fn_arg */ move a1, s0 /* fn */ move a2, sp /* pt_regs */ - call ret_from_fork + call ret_from_fork_kernel j ret_from_exception -SYM_CODE_END(ret_from_fork_asm) +SYM_CODE_END(ret_from_fork_kernel_asm) + +SYM_CODE_START(ret_from_fork_user_asm) + call schedule_tail + move a0, sp /* pt_regs */ + call ret_from_fork_user + j ret_from_exception +SYM_CODE_END(ret_from_fork_user_asm) =20 #ifdef CONFIG_IRQ_STACKS /* diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 7b0a0bfe29aec896c2bdd8976d855dd390de88d7..485ec7a80a56097e8905cd6395a= f29633846b5c8 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -38,7 +38,8 @@ unsigned long __stack_chk_guard __read_mostly; EXPORT_SYMBOL(__stack_chk_guard); #endif =20 -extern asmlinkage void ret_from_fork_asm(void); +extern asmlinkage void ret_from_fork_kernel_asm(void); +extern asmlinkage void ret_from_fork_user_asm(void); =20 void noinstr arch_cpu_idle(void) { @@ -208,14 +209,18 @@ int arch_dup_task_struct(struct task_struct *dst, str= uct task_struct *src) return 0; } =20 -asmlinkage void ret_from_fork(void *fn_arg, int (*fn)(void *), struct pt_r= egs *regs) +asmlinkage void ret_from_fork_kernel(void *fn_arg, int (*fn)(void *), stru= ct pt_regs *regs) { - if (unlikely(fn)) - fn(fn_arg); + fn(fn_arg); =20 syscall_exit_to_user_mode(regs); } =20 +asmlinkage void ret_from_fork_user(struct pt_regs *regs) +{ + syscall_exit_to_user_mode(regs); +} + int copy_thread(struct task_struct *p, const struct kernel_clone_args *arg= s) { unsigned long clone_flags =3D args->flags; @@ -238,6 +243,7 @@ int copy_thread(struct task_struct *p, const struct ker= nel_clone_args *args) =20 p->thread.s[0] =3D (unsigned long)args->fn; p->thread.s[1] =3D (unsigned long)args->fn_arg; + p->thread.ra =3D (unsigned long)ret_from_fork_kernel_asm; } else { *childregs =3D *(current_pt_regs()); /* Turn off status.VS */ @@ -247,12 +253,11 @@ int copy_thread(struct task_struct *p, const struct k= ernel_clone_args *args) if (clone_flags & CLONE_SETTLS) childregs->tp =3D tls; childregs->a0 =3D 0; /* Return value of fork() */ - p->thread.s[0] =3D 0; + p->thread.ra =3D (unsigned long)ret_from_fork_user_asm; } p->thread.riscv_v_flags =3D 0; if (has_vector() || has_xtheadvector()) riscv_v_thread_alloc(p); - p->thread.ra =3D (unsigned long)ret_from_fork_asm; p->thread.sp =3D (unsigned long)childregs; /* kernel sp */ return 0; } --=20 2.43.0 From nobody Sun Feb 8 19:48:50 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 3BD6655897 for ; Thu, 6 Mar 2025 00:44:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741221852; cv=none; b=ZiwAN0xsnWNB26ju+dFUUIQWhOMhQaFQ8iNvDDRoX4fNtAzg0TLqOH4qzI5NWpUvIKk453zK1x3XjpNwFNyWggHDQ4rIait5DFDULcemiV/lDHj+TPMgFbf2mYasfUknMTMyYwyF4BO8Dn/7fT9J1bU46lDOCpnlgvokO7jT9q8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741221852; c=relaxed/simple; bh=ji6jPR39ZKDby+guv8CCAiBLyKbpGz9Kynms9BH9fJU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VvYBgQpe5CUTMkU5PSba0K08PF+KCQUbUhRYNZYAqNHCSBWkiOdXsXGfGpVpL+yaaVp9ul0gzXD42eEry+N4XBGnU3+wSA79PDSVxTg2Gui72mc15iJ5mND2v1L6H3zBF67kLtOK9aSrj75eQ7kfLhpda0hAxYV5AKyJSa7qD2g= 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=y2vyUXv6; arc=none smtp.client-ip=209.85.214.174 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="y2vyUXv6" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2234daaf269so1276345ad.3 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=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=a+zSCWqqyKc551VynXTcxotuXwo5gv9XFD/e4PnRFpw=; b=y2vyUXv6cacQEc7h+UALaqa2SiHIJRtjC5pITflQWbsP1Ual9onsH9rbTKz1IV8BGs QhNnufTrfMgHR2XtMY8/oB1yzYhRWAD1csDFIo4U/+pY8nRmT3ROAahvj1YgZQpM7/Db YLivUaebJ6nE6QbOwAmCwaR/L5k4ETlFi/GLDP1HZjOoo537t/JT74ofAU+IO3deztcO 5rs6MN6kE1kUTtwjcrJs+qGqHgNoK1NbVU1MU9+o6Y2KpPgdlAt1h4ccIf63N1yOoK+g wRMpqj9p83zXl2LYRqq2kKdMDnYWY3UX1fflDKqr1AQhwbC69paWIB8hIvIBshhqz53t 7E/g== 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=nfsN5o1ZUtVZOIm+wKY7CCLD7HSk1x/umN1AadKv+98Rvy2fNBRp0E5dAi0OGZpHv9 qquiivutRyT4znTUbo9YY1MyRAC22XpIuqb4BfY3I92kzJjYXfPs5e8/4FNoHYW7qdJq ezeCTAmR4tEryDiA1JiREZWkmFGdSG974m5GsLoOEm7MLQU1oUMp03NnL+ePAqRtiQql /4qTswIx5RoYlgR2vwPWtXtfrWZysOhCAyYCskkGvNiaijC2FOTjymYP78kX7myazOZI Hb9ods8oNpF85t0G1RhF9O+GM1UmkKqWHX0Vu9QZ+xmlws8CvvS2rQ8KNMnRYvTgIL3U bTOQ== X-Forwarded-Encrypted: i=1; AJvYcCUEtTUN2UZld3XbpVIaDwR0BwGBKw8OVnA0nbcLBhEFA4gI3TKOx+2m+V4CvPX25jTFfLWo5qGAW/S6Xwg=@vger.kernel.org X-Gm-Message-State: AOJu0YxllO+nerHBlmrnqjm3B7WAZzoGUMpP74zr7lMavDSEpEANYnZI 0CeX6+FHfIs33UVb5+Ss7j2Z7+5Td+O+gmcdIxnZAJdcLXhD2s9w++UzWwrnOKc= X-Gm-Gg: ASbGnctZBSEnR79+HNNZDiVUITjVmQl9ZbaGQlnYGL1PfJM/fgJIfTWv332g6vTejLH 1TQxfGJvLc007xzdRY3YEIbf0fNmsbCxGalnbpMxiemm8UdItMne0WslYYTHSBqo4XsuC7DlPkd 9zCesqn3kiKZHmMrvbZZEnzfMVAC8XXKmDXQn9VRY0r4/tVExgvz9lZEdlbceuOtcpfGAOmDC+X JnCTe2mZlmo68Nive0O+5Rp9IDV7TiDyuAMb/UZ0t5zLoru4Rd/5EyPDiXvfC9NoLqdMyb0wCKm /YcytohpgtcHx6rYOZkmzpTPsJwjfX6y8cwXdnQ4kMep7wIze4uHIzmymW43 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 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: <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 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/i= nclude/asm/asm-prototypes.h index 51f224bcfc654228ae423e9a066b25b35102a5b9..704066b4f7368be15be960fadbc= d6c2574bbf6c0 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_str= uct *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..2abc29e573810e000f2fef4646d= dca0dbb80eabe 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) =20 -SYM_CODE_START(ret_from_fork) +SYM_CODE_START(ret_from_fork_asm) UNWIND_HINT_REGS - bl schedule_tail # a0 =3D 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) =20 -SYM_CODE_START(ret_from_kernel_thread) +SYM_CODE_START(ret_from_kernel_thread_asm) UNWIND_HINT_REGS - bl schedule_tail # a0 =3D 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/proces= s.c index 6e58f65455c7ca3eae2e88ed852c8655a6701e5c..98bc60d7c550fcc0225e8452f81= a7d6cd7888015 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 =20 +#include #include #include #include @@ -47,6 +49,7 @@ #include #include #include +#include #include #include =20 @@ -63,8 +66,9 @@ EXPORT_SYMBOL(__stack_chk_guard); unsigned long boot_option_idle_override =3D IDLE_NO_OVERRIDE; EXPORT_SYMBOL(boot_option_idle_override); =20 -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); =20 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, stru= ct task_struct *src) return 0; } =20 +asmlinkage void noinstr __no_stack_protector ret_from_fork(struct task_str= uct *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 ker= nel_clone_args *args) p->thread.reg03 =3D childksp; p->thread.reg23 =3D (unsigned long)args->fn; p->thread.reg24 =3D (unsigned long)args->fn_arg; - p->thread.reg01 =3D (unsigned long)ret_from_kernel_thread; - p->thread.sched_ra =3D (unsigned long)ret_from_kernel_thread; + p->thread.reg01 =3D (unsigned long)ret_from_kernel_thread_asm; + p->thread.sched_ra =3D (unsigned long)ret_from_kernel_thread_asm; memset(childregs, 0, sizeof(struct pt_regs)); childregs->csr_euen =3D p->thread.csr_euen; childregs->csr_crmd =3D p->thread.csr_crmd; @@ -182,8 +203,8 @@ int copy_thread(struct task_struct *p, const struct ker= nel_clone_args *args) childregs->regs[3] =3D usp; =20 p->thread.reg03 =3D (unsigned long) childregs; - p->thread.reg01 =3D (unsigned long) ret_from_fork; - p->thread.sched_ra =3D (unsigned long) ret_from_fork; + p->thread.reg01 =3D (unsigned long) ret_from_fork_asm; + p->thread.sched_ra =3D (unsigned long) ret_from_fork_asm; =20 /* * New tasks lose permission to use the fpu. This accelerates context --=20 2.43.0 From nobody Sun Feb 8 19:48:50 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 5223978F45 for ; Thu, 6 Mar 2025 00:44:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741221853; cv=none; b=jee44+JbFILeKKv3Ehvr1I/sbfIooOVkCv5xV633VSz112DFknGfKpqhTGO8G7MN/HjhHgY+L+LRoKHdDd1ekg3OHoaSZH9qUJa/5u21yCZnrf4gtj76VfFF1TvLCgDsjGAtc+EuMRW+zq3pCCdtjwCxhbzgtSWhguN6DswU1OU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741221853; c=relaxed/simple; bh=j7ZO4FXV1Hf14yiJt7JLsFHCp0FLrXbd7xunBuFQ9j8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IUfUvvcJ4B0swdvqiRHJlxL40zxjWiSRIq9BAreTiBERDnlV2waninCAjDtUlb9GTm+JVUM8fp9Dc3gRRs32wRqYQ9izISr9eoP6STT5ea9tOROGB3S8zvhuxaAQ0FRNl7OZUIgxiMjSbcTV5sRU1gpm8tyzgA2QujezFzrRl7o= 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=uPht16Ve; arc=none smtp.client-ip=209.85.214.179 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="uPht16Ve" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-223785beedfso842015ad.1 for ; Wed, 05 Mar 2025 16:44:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741221852; x=1741826652; 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=p30t3bidsL8DH7wJ55Ht0+lyyt5ZTgdtBsNdg8zXD0s=; b=uPht16Vevt5Pro3U735j8NPJmveA4xj4p2utyJ6Poai4KyoNl72q8xfWkbsmPTBLmk 5mP+rLO30WvBZPTKQRViHzwJDzVtytZVPGxP0++ULSyzwZ/5RYyqjpvXdP5pTO5scMzF y3rKrhxNk7S+46VUr80uwHaFA3+5ycz9zoMiwzaCWjmVuBI/1m58bHZgwFacHBOJxU2f Qmvi59yJdpS3D9Z1uoT2hNYZzt2OK+CRp9ocH/hzThHvKA/SEQKaXejSS3u10YuRlyta 3t6KezPcJozv/KIH02/8c/dMUnT6nD1YVc7+i2sieYckOk96nWch1+MrHLre/Wzr+Dwp lSug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741221852; x=1741826652; 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=p30t3bidsL8DH7wJ55Ht0+lyyt5ZTgdtBsNdg8zXD0s=; b=Hdij4/rjzNDjGTJgT4+jK6luQKHpNA/uumR2vqX/gr0C3fFbVQ2j/SLARd+MGki4Bh z36dMQd2gcVfPuNo9mG9drp9hvzG0putL2xZT6CZAa2cetRtBkbBV+p+6TKEN+sRmjKQ lM5LqPddqUkzb0L6+GlFjoUy7ncPWrvz6p9OaGn5g6RR1XChJ8veg4RMjodgQyYjzB6O 8PbuiEkfyRaMe0B6Pf8TU/21zkxYMu6g6MTeLrAkdtyujBIAlHOOKj2X6dmut4cdKgcV Q9Aqlbgg7+920sYGxt3CIW7rzI9/7IcybLXw2yuHCw88hlVPIMzqu/zFF77p1ZV5MoAm Ju7Q== X-Forwarded-Encrypted: i=1; AJvYcCV+4QTt2e5GzvdWnAkV3lTaK7uWdoUsivVl2bCE4kP32vYvkLVCnBVtvZA0R6CX8GjHLyXa0cpGhszOPcE=@vger.kernel.org X-Gm-Message-State: AOJu0YzPputQiieF9KlqSKI0HMNFzfR4DbWsLMQNVX9QuoL4bnJsqGJn /K7lB2njBwYaLB4fJVZGZfvoAa+03QccBazRE5M3UF97wRR5Ud/BxEFJNjQe6S4= X-Gm-Gg: ASbGncv3JEdhEUaDRJQoYkFr07wAuvrv+1xS5xcvyivxfe8bGed/3gnIu/HFLcHjdO0 bB5PbGRqnOupMxhkHUuEipk6EoHnhia8oxo0l77dOtoxMVR2edJNzvvhFt+WPN3M9ZM6Ewxbgjk ixtczZDsGmgedIhF9iFYNmXnQrGEqEezexRyG7DpopFq4oILPtxsbsHMdyTCqd9iwkInnkRGSmO 6G+plB0VEU7tm1LT9lCh1OJlqEG9uW4mNZGMv33LrrS0tsftW0r+Z+n24oF8Y6rCRl6wvZFeEHY xgrYihaahYuACQoTz6u8J0pNA1gon/d2tbgzoIaWVdwm/Vh0FJcfb8YHILrK X-Google-Smtp-Source: AGHT+IE3udje6D9ToJozjElnL03nykz3gt3O3YbFzCSzOAeX2/0ggHbBiX1+w1/L3X8PtcJZLOlRYA== X-Received: by 2002:a17:902:d549:b0:224:3c9:19ae with SMTP id d9443c01a7336-22403c91a6cmr43245075ad.34.1741221851852; Wed, 05 Mar 2025 16:44:11 -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.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Mar 2025 16:44:11 -0800 (PST) From: Charlie Jenkins Date: Wed, 05 Mar 2025 16:43:47 -0800 Subject: [PATCH v5 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: <20250305-riscv_optimize_entry-v5-4-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=5141; i=charlie@rivosinc.com; h=from:subject:message-id; bh=j7ZO4FXV1Hf14yiJt7JLsFHCp0FLrXbd7xunBuFQ9j8=; b=owGbwMvMwCXWx5hUnlvL8Y3xtFoSQ/qJ95d9ndq3LQ44de6FoLjnP4Wflp+6lvkeeNEuvN8vl k/soW5mRykLgxgXg6yYIgvPtQbm1jv6ZUdFyybAzGFlAhnCwMUpABNRWMDIsEH+59MDmer/fzAU vvxYZjyFw/HQm6w1f54Er/xks8PGu5mRoa1Us0Hzc7CdgfrJc8aZ75wz9rt7hrUu4uo1/VOqxcH FAQA= 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 Reviewed-by: Alexandre Ghiti --- 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..f94f3fdf15fc0091223cc9f7b82= 3970302e67312 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 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); + 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