From nobody Sat May 30 11:18:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1778227539; cv=none; d=zohomail.com; s=zohoarc; b=lj7gkhR7CequfCAwjZDmxKHwvO8d81k1J2l/swZIt3+W0f4YNKiqUU/uVkOKrcq98GdBflFolARt9i+jwQIf1ndxHFdIn1Cvf0Zd2bff2rpqAJcoqPhax8mM5OY5V0jLeqDOdwtOTXAXX8uys4SOFmzLRIiHgczs+vVmkKty8ak= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1778227539; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zin7zp/9oBAHtr4qfaN5VFN9hhxtKHeI7hC3NPeLalg=; b=DDyomwT72e0c4xNZSxJlzjy8+V+5j73GLXFCy1z7T5amyn+BW8KhV0URhuRW149QCbj+BZ7/SI8WcHn7osqBP4ML3lhCFChrOc/oW52AJ94cY+KsGTxzzWa8SEG0MMIyQhk1TLkQmCDvsTYO/DJaW3kyEb02h776CT1i5ztM0UU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1778227539704767.8249005468759; Fri, 8 May 2026 01:05:39 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1303272.1576736 (Exim 4.92) (envelope-from ) id 1wLGD2-0005ei-EX; Fri, 08 May 2026 08:05:20 +0000 Received: by outflank-mailman (output) from mailman id 1303272.1576736; Fri, 08 May 2026 08:05:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wLGD2-0005eb-Bp; Fri, 08 May 2026 08:05:20 +0000 Received: by outflank-mailman (input) for mailman id 1303272; Fri, 08 May 2026 08:05:19 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wLGD1-0005eV-1p for xen-devel@lists.xenproject.org; Fri, 08 May 2026 08:05:19 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wLGCy-00DZG1-T9 for xen-devel@lists.xenproject.org; Fri, 08 May 2026 10:05:18 +0200 Received: from [10.42.69.9] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69fd993d-bab6-0a2a0a5309dd-0a2a4509afb0-4 for ; Fri, 08 May 2026 10:05:18 +0200 Received: from [195.135.223.131] (helo=smtp-out2.suse.de) by tlsNG-bad1c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.1) (envelope-from ) id 69fd993e-2497-0a2a45090019-c387df83b228-3 for ; Fri, 08 May 2026 10:05:18 +0200 Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id A41BF5C480; Fri, 8 May 2026 08:05:17 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 32B27593A7; Fri, 8 May 2026 08:05:17 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id oHwnCz2Z/WlsPwAAD6G6ig (envelope-from ); Fri, 08 May 2026 08:05:17 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=susede1 header.d=suse.com header.i="@suse.com" header.h="From:Date:Message-ID:To:Cc:MIME-Version:Content-Transfer-Encoding"; dkim=pass header.s=susede1 header.d=suse.com header.i="@suse.com" header.h="From:Date:Message-ID:To:Cc:MIME-Version:Content-Transfer-Encoding" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1778227517; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=zin7zp/9oBAHtr4qfaN5VFN9hhxtKHeI7hC3NPeLalg=; b=cQUcQv7a3lNGXcjekKFXsChUBFrlQ7V8GcZILNGJXroQuGu8Uc9zLmO3xF/IYkxLmIGOsU 3Ua2wlHDk3UA0TMOxVJADwq+mwMIAiHXGu/pTbcvENmhIXRNMTJJzedX3hfiz6MA62M6K4 h3wKKxjV/nSnHAbaPrSRFH6tShevb/w= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=cQUcQv7a DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1778227517; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=zin7zp/9oBAHtr4qfaN5VFN9hhxtKHeI7hC3NPeLalg=; b=cQUcQv7a3lNGXcjekKFXsChUBFrlQ7V8GcZILNGJXroQuGu8Uc9zLmO3xF/IYkxLmIGOsU 3Ua2wlHDk3UA0TMOxVJADwq+mwMIAiHXGu/pTbcvENmhIXRNMTJJzedX3hfiz6MA62M6K4 h3wKKxjV/nSnHAbaPrSRFH6tShevb/w= From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-mm@kvack.org Cc: kevin.brodsky@arm.com, mmarek@invisiblethingslab.com, Juergen Gross , Boris Ostrovsky , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , xen-devel@lists.xenproject.org Subject: [PATCH] x86/xen: Fix lazy mmu handling across context switch Date: Fri, 8 May 2026 10:05:14 +0200 Message-ID: <20260508080514.454607-1-jgross@suse.com> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Level: X-Rspamd-Action: no action X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_TWELVE(0.00)[21]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email,suse.com:mid,imap1.dmz-prg2.suse.org:rdns,imap1.dmz-prg2.suse.org:helo]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FROM_EQ_ENVFROM(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:106:10:150:64:167:received]; TO_DN_SOME(0.00)[]; DKIM_TRACE(0.00)[suse.com:+] X-Rspamd-Queue-Id: A41BF5C480 X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spam-Flag: NO X-Spam-Score: -3.01 X-purgate-ID: tlsNG-bad1c0/1778227518-37175A53-532E17D9/0/0 X-purgate-type: clean X-purgate-size: 3371 X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1778227543343154100 Content-Type: text/plain; charset="utf-8" The recent rework of mmu lazy mode has resulted in problems when running as a Xen PV guest. Enabling lazy mmu mode for the new context during context switch is done from the arch_end_context_switch() hook, but when calling this hook current hasn't been changed yet, so the lazy mmu mode state of the wrong task is modified. Additionally it is much cleaner to use lazy_mmu_mode_pause() and lazy_mmu_mode_resume() in the Xen context switch hooks, as it avoids conditionals in those hooks. In order not having to add another hook to be called after switching current, modify lazy_mmu_mode_resume() to use a new sub-function which takes a task pointer as parameter. This new sub-function can then be used in the xen_end_context_switch() hook. Fixes: 291b3abed657 ("x86/xen: use lazy_mmu_state when context-switching") Signed-off-by: Juergen Gross --- arch/x86/xen/enlighten_pv.c | 7 ++----- include/linux/pgtable.h | 33 ++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c index ed2d7a3756ce..67bb6bf6d240 100644 --- a/arch/x86/xen/enlighten_pv.c +++ b/arch/x86/xen/enlighten_pv.c @@ -424,9 +424,7 @@ static void xen_start_context_switch(struct task_struct= *prev) { BUG_ON(preemptible()); =20 - if (this_cpu_read(xen_lazy_mode) =3D=3D XEN_LAZY_MMU) { - arch_leave_lazy_mmu_mode(); - } + lazy_mmu_mode_pause(); enter_lazy(XEN_LAZY_CPU); } =20 @@ -436,8 +434,7 @@ static void xen_end_context_switch(struct task_struct *= next) =20 xen_mc_flush(); leave_lazy(XEN_LAZY_CPU); - if (__task_lazy_mmu_mode_active(next)) - arch_enter_lazy_mmu_mode(); + lazy_mmu_mode_resume_task(next); } =20 static unsigned long xen_store_tr(void) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index cdd68ed3ae1a..83a099bf2038 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -326,6 +326,28 @@ static inline void lazy_mmu_mode_pause(void) arch_leave_lazy_mmu_mode(); } =20 +/** + * lazy_mmu_mode_resume_task() - Resume the lazy MMU mode for a specific t= ask. + * + * Like lazy_mmu_mode_resume() below, but with a task specified. + * Must be called only by lazy_mmu_mode_resume() or during context switch. + * Must never be called in interrupt context. + * + * Must match a call to lazy_mmu_mode_pause(). + * + * Has no effect if called: + * - While paused (inside another pause()/resume() pair) + */ +static inline void lazy_mmu_mode_resume_task(struct task_struct *task) +{ + struct lazy_mmu_state *state =3D &task->lazy_mmu_state; + + VM_WARN_ON_ONCE(state->pause_count =3D=3D 0); + + if (--state->pause_count =3D=3D 0 && state->enable_count > 0) + arch_enter_lazy_mmu_mode(); +} + /** * lazy_mmu_mode_resume() - Resume the lazy MMU mode. * @@ -341,15 +363,8 @@ static inline void lazy_mmu_mode_pause(void) */ static inline void lazy_mmu_mode_resume(void) { - struct lazy_mmu_state *state =3D ¤t->lazy_mmu_state; - - if (in_interrupt()) - return; - - VM_WARN_ON_ONCE(state->pause_count =3D=3D 0); - - if (--state->pause_count =3D=3D 0 && state->enable_count > 0) - arch_enter_lazy_mmu_mode(); + if (!in_interrupt()) + lazy_mmu_mode_resume_task(current); } #else static inline void lazy_mmu_mode_enable(void) {} --=20 2.54.0