From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (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 736DD22423A for ; Fri, 8 May 2026 08:05:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778227521; cv=none; b=NTnOYnhsG12p/fOya3o1zUEiY4uhk6iKctz+DUnH3sX1I+sGnsIqshG5yT4cb3NRILoBralkrBB4phAZoa/bwT6JrmLCCObXIWXu414FoNFmB7+knO2JFq56N6OInTysM62ao35C198UoPZq9zszwMU7cmZtuwxogc8nTK4CHmA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778227521; c=relaxed/simple; bh=8Oq0IEMqk8q2uLf6zfqne9PyQwiSnyGHR8zvmvFCHAs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=NFXcIzGrmLQp21WC6ytYnQLlK/izfptajE0tIiIpP2Rb6m9brB2lmZoMWGNmLVRjrddOXoR+jW9Tn0rKmo7+p5ju80QbxClYiaoh/QFmqQhk7s0hoTx7AmqfggLkFNOzR4Em5rjtKYMfppnVfFRWD5d1notyq/DRm4FG4ek4v1U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=cQUcQv7a; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=cQUcQv7a; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="cQUcQv7a"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="cQUcQv7a" 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) 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= 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 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 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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()); - if (this_cpu_read(xen_lazy_mode) == XEN_LAZY_MMU) { - arch_leave_lazy_mmu_mode(); - } + lazy_mmu_mode_pause(); enter_lazy(XEN_LAZY_CPU); } @@ -436,8 +434,7 @@ static void xen_end_context_switch(struct task_struct *next) 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); } 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(); } +/** + * lazy_mmu_mode_resume_task() - Resume the lazy MMU mode for a specific task. + * + * 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 = &task->lazy_mmu_state; + + VM_WARN_ON_ONCE(state->pause_count == 0); + + if (--state->pause_count == 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 = ¤t->lazy_mmu_state; - - if (in_interrupt()) - return; - - VM_WARN_ON_ONCE(state->pause_count == 0); - - if (--state->pause_count == 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) {} -- 2.54.0