From patchwork Mon Dec 18 07:45:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Schnelle X-Patchwork-Id: 180190 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1086169dyi; Sun, 17 Dec 2023 23:46:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEBYF656N9ZFXxIm/RbU45h8KaJeb239mkdWHzRaPfpRoNRHJw0sQX+W8Aout2iAcWOVpaP X-Received: by 2002:a05:620a:4395:b0:77f:588:719b with SMTP id a21-20020a05620a439500b0077f0588719bmr21270218qkp.86.1702885615733; Sun, 17 Dec 2023 23:46:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702885615; cv=none; d=google.com; s=arc-20160816; b=1AyZQxAYwWqNvYuj23bRF0umlqIdRnYmQKCqf0qY+8NzHrEPwcXJfI/Q6eX17cpv2l L9Hal9Ep6GHRdpawZhqwBhA2079nbxSg2FbspB06cDtINrNAEXfsmfhprZ6q1Utd5zeR uVzd7gEpemmu5ZrQpxt3UYDqQvypaiWC8UDPAsycovAhT+YeIdw5yRUG8qe8JNb4TuEt cy0zm/km8lTw01jUX+xM4yQFC3lrX9FZKdFULiZngqjEdEr81JEECOUjR1wlCISj02EN lx8kq5SUZUS/C7BYZ+w8bYywStV12reDniG4GSsJTY7kZ5sTIINW3fLoLySrd3dp9mYe 5Hig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=O+m799fDNDuTFzAl1Uz02x4uU4icAhsA+6Orlujt//4=; fh=U2g0jiA3Tw7gpGNywwyfYnxP8tTSM1Q6YkOBxeYNVJ0=; b=LWoTekelmnslPTwnINUDFgirH/ChZykxjGtnZygs3PlkaFmL2CuYU6kU2dLLJZ0bKO 0wutrUKm6UmnSDeclC0JRCftjlJHCaQq+iMH5b5Hwp7sH52MMZzVeDKL0LY/FGNOf/Tl a9rSxAGlLltJWDTFZ/of2gM/rnu8SX9X8a0gdnKKSLopwJ3e1TsuR4kzTGHqVzFld74U ocbOevUxea0KwDtdaqBIuDx/CQkdSXcwQapcEdk3Fz/aMN3lT8DGSrJIVeySgzylpp2s Vg2Boma4Quc9pCjdG+9qYVWMEBJ94n69Iw7GuYtBiAKdmiNQq90Y9Zh7dDVfFTmTDn8o zubA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=klnx9C8E; spf=pass (google.com: domain of linux-kernel+bounces-3123-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3123-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y1-20020a05620a44c100b0077f2f4cda80si24053340qkp.62.2023.12.17.23.46.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 23:46:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3123-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=klnx9C8E; spf=pass (google.com: domain of linux-kernel+bounces-3123-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3123-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 780491C2248A for ; Mon, 18 Dec 2023 07:46:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B6EF711198; Mon, 18 Dec 2023 07:45:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="klnx9C8E" X-Original-To: linux-kernel@vger.kernel.org Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9833CF9D9 for ; Mon, 18 Dec 2023 07:45:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Received: from pps.filterd (m0353723.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3BI7c4TH008028; Mon, 18 Dec 2023 07:45:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=O+m799fDNDuTFzAl1Uz02x4uU4icAhsA+6Orlujt//4=; b=klnx9C8EMOskCaRlEPTCgPTo6Ym/UfjJrX+7NQH9Z/iNWNOWsixPSlcTmFR0mjcUoa2C vgBvtqXlV+P/zbzAxiS5nVOZ92cQDB3EjmY3ZT1nR8ULaGcFYJ8rtt6XciEAME3yvqOx T+GulfHNmzB4Q6dZl3alpSjDxDQDc5Y+UA+We4LPZTQRS6du9TAg/zEtK8EXITnaNlqh CK4NYT18egv2C+UkpYyuI3xsw7k0p9btuU43obUuIHAfVLA0HFVCaH+41BjBUq+tR5FZ 4H3odmoVdsuR2M7EcIDIW6qQ8qG8DNQ29dnK0m8Q9eNuZ1AUJyyNDMybEoWBC7v71xcm 1w== Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3v2huvg5ej-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 18 Dec 2023 07:45:26 +0000 Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 3BI7QqN4027073; Mon, 18 Dec 2023 07:45:26 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3v1rejq3kb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 18 Dec 2023 07:45:26 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 3BI7jO6U16122450 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Dec 2023 07:45:24 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5986120040; Mon, 18 Dec 2023 07:45:24 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4624E2004B; Mon, 18 Dec 2023 07:45:24 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTPS; Mon, 18 Dec 2023 07:45:24 +0000 (GMT) Received: by tuxmaker.boeblingen.de.ibm.com (Postfix, from userid 55390) id 11E17E0826; Mon, 18 Dec 2023 08:45:24 +0100 (CET) From: Sven Schnelle To: Thomas Gleixner , Peter Zijlstra , Andy Lutomirski Cc: linux-kernel@vger.kernel.org, Heiko Carstens Subject: [PATCH v2 1/3] entry: move exit to usermode functions to header file Date: Mon, 18 Dec 2023 08:45:18 +0100 Message-Id: <20231218074520.1998026-2-svens@linux.ibm.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231218074520.1998026-1-svens@linux.ibm.com> References: <20231218074520.1998026-1-svens@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: ONIUIVRsCoRPg6BhrHZAaXm3D6pucdIT X-Proofpoint-ORIG-GUID: ONIUIVRsCoRPg6BhrHZAaXm3D6pucdIT X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-18_04,2023-12-14_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=763 malwarescore=0 clxscore=1015 phishscore=0 mlxscore=0 suspectscore=0 adultscore=0 spamscore=0 lowpriorityscore=0 impostorscore=0 bulkscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2312180054 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785604987771327338 X-GMAIL-MSGID: 1785604987771327338 To allow inlining, move exit_to_user_mode() to entry-common.h. Signed-off-by: Sven Schnelle --- include/linux/entry-common.h | 52 +++++++++++++++++++++++++++++++++++- kernel/entry/common.c | 50 +++++----------------------------- 2 files changed, 58 insertions(+), 44 deletions(-) diff --git a/include/linux/entry-common.h b/include/linux/entry-common.h index d95ab85f96ba..b08aceb26e8e 100644 --- a/include/linux/entry-common.h +++ b/include/linux/entry-common.h @@ -7,6 +7,10 @@ #include #include #include +#include +#include +#include +#include #include @@ -258,6 +262,42 @@ static __always_inline void arch_exit_to_user_mode(void) { } */ void arch_do_signal_or_restart(struct pt_regs *regs); +/** + * exit_to_user_mode_loop - do any pending work before leaving to user space + */ +unsigned long exit_to_user_mode_loop(struct pt_regs *regs, + unsigned long ti_work); + +/** + * exit_to_user_mode_prepare - call exit_to_user_mode_loop() if required + * + * 1) check that interrupts are disabled + * 2) call tick_nohz_user_enter_prepare() + * 3) call exit_to_user_mode_loop() if any flags from + * EXIT_TO_USER_MODE_WORK are set + * 4) check that interrupts are still disabled + */ +static __always_inline void exit_to_user_mode_prepare(struct pt_regs *regs) +{ + unsigned long ti_work; + + lockdep_assert_irqs_disabled(); + + /* Flush pending rcuog wakeup before the last need_resched() check */ + tick_nohz_user_enter_prepare(); + + ti_work = read_thread_flags(); + if (unlikely(ti_work & EXIT_TO_USER_MODE_WORK)) + ti_work = exit_to_user_mode_loop(regs, ti_work); + + arch_exit_to_user_mode_prepare(regs, ti_work); + + /* Ensure that kernel state is sane for a return to userspace */ + kmap_assert_nomap(); + lockdep_assert_irqs_disabled(); + lockdep_sys_exit(); +} + /** * exit_to_user_mode - Fixup state when exiting to user mode * @@ -276,7 +316,17 @@ void arch_do_signal_or_restart(struct pt_regs *regs); * non-instrumentable. * The caller has to invoke syscall_exit_to_user_mode_work() before this. */ -void exit_to_user_mode(void); +static __always_inline void exit_to_user_mode(void) +{ + instrumentation_begin(); + trace_hardirqs_on_prepare(); + lockdep_hardirqs_on_prepare(); + instrumentation_end(); + + user_enter_irqoff(); + arch_exit_to_user_mode(); + lockdep_hardirqs_on(CALLER_ADDR0); +} /** * syscall_exit_to_user_mode_work - Handle work before returning to user mode diff --git a/kernel/entry/common.c b/kernel/entry/common.c index d7ee4bc3f2ba..113bd3e8e73e 100644 --- a/kernel/entry/common.c +++ b/kernel/entry/common.c @@ -123,29 +123,14 @@ noinstr void syscall_enter_from_user_mode_prepare(struct pt_regs *regs) instrumentation_end(); } -/* See comment for exit_to_user_mode() in entry-common.h */ -static __always_inline void __exit_to_user_mode(void) -{ - instrumentation_begin(); - trace_hardirqs_on_prepare(); - lockdep_hardirqs_on_prepare(); - instrumentation_end(); - - user_enter_irqoff(); - arch_exit_to_user_mode(); - lockdep_hardirqs_on(CALLER_ADDR0); -} - -void noinstr exit_to_user_mode(void) -{ - __exit_to_user_mode(); -} - /* Workaround to allow gradual conversion of architecture code */ void __weak arch_do_signal_or_restart(struct pt_regs *regs) { } -static unsigned long exit_to_user_mode_loop(struct pt_regs *regs, - unsigned long ti_work) +/** + * exit_to_user_mode_loop - do any pending work before leaving to user space + */ +__always_inline unsigned long exit_to_user_mode_loop(struct pt_regs *regs, + unsigned long ti_work) { /* * Before returning to user space ensure that all pending work @@ -190,27 +175,6 @@ static unsigned long exit_to_user_mode_loop(struct pt_regs *regs, return ti_work; } -static void exit_to_user_mode_prepare(struct pt_regs *regs) -{ - unsigned long ti_work; - - lockdep_assert_irqs_disabled(); - - /* Flush pending rcuog wakeup before the last need_resched() check */ - tick_nohz_user_enter_prepare(); - - ti_work = read_thread_flags(); - if (unlikely(ti_work & EXIT_TO_USER_MODE_WORK)) - ti_work = exit_to_user_mode_loop(regs, ti_work); - - arch_exit_to_user_mode_prepare(regs, ti_work); - - /* Ensure that kernel state is sane for a return to userspace */ - kmap_assert_nomap(); - lockdep_assert_irqs_disabled(); - lockdep_sys_exit(); -} - /* * If SYSCALL_EMU is set, then the only reason to report is when * SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall @@ -295,7 +259,7 @@ __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(); + exit_to_user_mode(); } noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs) @@ -308,7 +272,7 @@ noinstr void irqentry_exit_to_user_mode(struct pt_regs *regs) instrumentation_begin(); exit_to_user_mode_prepare(regs); instrumentation_end(); - __exit_to_user_mode(); + exit_to_user_mode(); } noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs)