From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 950E9322C99 for ; Tue, 25 Nov 2025 14:13:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764080036; cv=none; b=QDcgpkljvqA6mkLiA4DAVZ97CuAYL6eR9c+c3lKoR58ugwYCnfXM0eMCh1d3XOauktAp2/kPpNod1IEjx8jGq5WnNu40nDozAKiCpqxQETZmNe1cnIkBB0ZNibwrkW8Exm+cM0FZpLgIvIQfhznjLj1HH8pDkcepE25cYP3ZCZ4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764080036; c=relaxed/simple; bh=vmAxAdtBdBPAYy3/MG+wU8uP+oKefkt/UnAeEvBcakg=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=C9x0hsOrGgNC91Xk/TGyFgu8ICrsK48ARDVTyjhJGahR0xXFlH2xV4EzcuEiUQSujWeRBcnQXXOGfJY5SRxk7Sps8+5lSXNbjOJRpLF2yca5+8S5efigD8ZPGFrHDCQn3tFKwjhDrTzjK8Qe1rkmIILPyD/oMEhrP5Va1G8QXTc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WCFs1Enb; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=k3jbReCj; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WCFs1Enb"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="k3jbReCj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1764080033; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=wOE4pVZbQk3NRzTWk4bK9iK9S5Rl6Cg6Yr2VD/4CLLg=; b=WCFs1EnbW0dICr2gg1LFHJox4vf8fvNZxl+Nzzhd3p2g21y8nyOWvLPzKRwF80u1AXUPxF PoYDGEpi/UyqplM0Qj5XipfP/S2nZ96ws7EuKlzJKxpcA+r2FH/5H40eGBC4R0rvVhoJpI yn2WcRZt+0AnKdKe+FU3Hu0x1ous42M= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-230-MPQ4YQzeOAig901GHxYF6A-1; Tue, 25 Nov 2025 09:13:52 -0500 X-MC-Unique: MPQ4YQzeOAig901GHxYF6A-1 X-Mimecast-MFC-AGG-ID: MPQ4YQzeOAig901GHxYF6A_1764080031 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4779981523fso56399615e9.2 for ; Tue, 25 Nov 2025 06:13:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1764080031; x=1764684831; darn=vger.kernel.org; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=wOE4pVZbQk3NRzTWk4bK9iK9S5Rl6Cg6Yr2VD/4CLLg=; b=k3jbReCj9ht0ZqHrtcJVcgwM3b1d2Pt4GukpgjDN2KAN8j3gRBo78lyCoeIAinnw1n uikf1n0Y9lnk3ODWmSNEwIHHaUfjsrWNx4Pn4c5nMyOpTRuAMY16RPjogM1WtOCjdhOc 0z/3X9E5syYBalrtAGYmU6G27T9alrnNOfioBPjbrkQZ2FcVe8NI6FRKk+8/NUSqms2w M2etWp4tUxxMNd+20cVJYKLdeloYxC7ewrADO5+5Wdgmie1M8Yj+H3kyGLOQ7tlFv1LL uBAib7G6gyNfg2lCYUbuSxym53/8EZ3XYdgYRp6Jd3aRz+hMrQpLilj8pof5g1h2+d7m wy/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764080031; x=1764684831; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wOE4pVZbQk3NRzTWk4bK9iK9S5Rl6Cg6Yr2VD/4CLLg=; b=qo/YZeDLhWUh5qW73NTmlpAEyEy5FtKbqaih2ZENtU58deaHMADrkKCfzFkcHqTmZs 0hQ3zx/l/jeec3ji4t3GbWL4LPj9XSUOMvoeqXPi/XJ5aDUqP1yYNwjMhMRBhkTqLJE5 KYMvDr46mYdaNZ/k3GWSIEexVtM16Sfw5BabnoE6Mlc7JC5FpoR4G/OGf8U+gEF21ZJz isbShy2mqO1I33T8PHqSBAUfDauWJJChp4Y+RDMME6oRB0eQdeiF8Yv8hOK+RDSij6qX iU54EFemg5N+/kx1s37wnWAed1WMKY5lAGL3HRfbm5GI1ik3+/+UlToBaQSn7wMTmuTp xthQ== X-Forwarded-Encrypted: i=1; AJvYcCUj8zYs9B+26ue/aJxNwVbjWpt+K+AcUPQAZbsJt/k3XeklB7XTEXvmVycLcdcYF6bJWacGoHq9I8iaDoM=@vger.kernel.org X-Gm-Message-State: AOJu0YxpLzuaO8aZTVbkBUIrAW9ROKww3xPCQOn8cby+1b4n1RkiHc52 eJMrgPylJ/PUZ5Q0SvX0EoZOmtJZL6HXaD2hJsyxe/7VGG/kNQ/3yFglYE0IrfzJcFPKMaKYphj 1sQ4onxz/01pLCUpXce1748b97i1jbNLaimB/KT5su6HLS72FoUykNjrc/Kafnq99Pg== X-Gm-Gg: ASbGncu4AnUBYFDLod4kQXBSZICZ3UeaEccanGA4quhrTqwn2Baaatn7aKx3LhVyTzU smZFAAkoTfs8lR+oIabKp5nIHNxAClg2KDBIIAxZk3NQHUY/UE9g4GNxO3NmZN7yqlTSMnpQ3QH qF7v7TP2hvi51g60zzovZtdyVTctBxCTtBVHT1+xTucBK3nYZXvAl+gbMO/042p7zHTR4BxPi0m EgAs0+t3SMwfpomQFpMQZJT05At/Fw9GK5KOEZQBFa8wEMTOsorhfg/gINaoMBqM+m37sm5XuKX rZ11QkvbyyWMGR6sTh8HXgKUhma6hydDDlmN5EEX9xYTsrgMRINUdJpvwEifykMe/TviwQ/bl+3 3wjx+eD2VSM4Ex0wwxmw9b+B1h0WcqNQD67h6SWxOx8ZomCpEXLYDnc9L X-Received: by 2002:a05:600c:3111:b0:477:b734:8c53 with SMTP id 5b1f17b1804b1-477c10deffamr169341975e9.12.1764080030927; Tue, 25 Nov 2025 06:13:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IFtA8JzDWqwpv+zLW5Ygo6VxUlFOBnDYO5W2Qk6szEUT54U+0BafPLKh5CrRqs4rY7IKqQsQw== X-Received: by 2002:a05:600c:3111:b0:477:b734:8c53 with SMTP id 5b1f17b1804b1-477c10deffamr169341405e9.12.1764080030289; Tue, 25 Nov 2025 06:13:50 -0800 (PST) Received: from vschneid-thinkpadt14sgen2i.remote.csb (213-44-135-146.abo.bbox.fr. [213.44.135.146]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-477bf1e86b3sm306305045e9.6.2025.11.25.06.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Nov 2025 06:13:49 -0800 (PST) From: Valentin Schneider To: Dave Hansen , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rcu@vger.kernel.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Arnaldo Carvalho de Melo , Josh Poimboeuf , Paolo Bonzini , Arnd Bergmann , Frederic Weisbecker , "Paul E. McKenney" , Jason Baron , Steven Rostedt , Ard Biesheuvel , Sami Tolvanen , "David S. Miller" , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Uladzislau Rezki , Mathieu Desnoyers , Mel Gorman , Andrew Morton , Masahiro Yamada , Han Shen , Rik van Riel , Jann Horn , Dan Carpenter , Oleg Nesterov , Juri Lelli , Clark Williams , Yair Podemsky , Marcelo Tosatti , Daniel Wagner , Petr Tesarik , Shrikanth Hegde Subject: Re: [RFC PATCH v7 30/31] x86/mm, mm/vmalloc: Defer kernel TLB flush IPIs under CONFIG_COALESCE_TLBI=y In-Reply-To: <2837ea3e-c0b8-46b0-b8da-bf06906d124d@intel.com> References: <20251114150133.1056710-1-vschneid@redhat.com> <20251114151428.1064524-10-vschneid@redhat.com> <2837ea3e-c0b8-46b0-b8da-bf06906d124d@intel.com> Date: Tue, 25 Nov 2025 15:13:47 +0100 Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain On 21/11/25 09:50, Dave Hansen wrote: > On 11/21/25 09:37, Valentin Schneider wrote: >> On 19/11/25 10:31, Dave Hansen wrote: >>> On 11/14/25 07:14, Valentin Schneider wrote: >>>> +static bool flush_tlb_kernel_cond(int cpu, void *info) >>>> +{ >>>> + return housekeeping_cpu(cpu, HK_TYPE_KERNEL_NOISE) || >>>> + per_cpu(kernel_cr3_loaded, cpu); >>>> +} >>> >>> Is it OK that 'kernel_cr3_loaded' can be be stale? Since it's not part >>> of the instruction that actually sets CR3, there's a window between when >>> 'kernel_cr3_loaded' is set (or cleared) and CR3 is actually written. >>> >>> Is that OK? >>> >>> It seems like it could lead to both unnecessary IPIs being sent and for >>> IPIs to be missed. >>> >> >> So the pattern is >> >> SWITCH_TO_KERNEL_CR3 >> FLUSH >> KERNEL_CR3_LOADED := 1 >> >> KERNEL_CR3_LOADED := 0 >> SWITCH_TO_USER_CR3 >> >> >> The 0 -> 1 transition has a window between the unconditional flush and the >> write to 1 where a remote flush IPI may be omitted. Given that the write is >> immediately following the unconditional flush, that would really be just >> two flushes racing with each other, > > Let me fix that for you. When you wrote "a remote flush IPI may be > omitted" you meant to write: "there's a bug." ;) > Something like that :-) > In the end, KERNEL_CR3_LOADED==0 means, "you don't need to send this CPU > flushing IPIs because it will flush the TLB itself before touching > memory that needs a flush". > > SWITCH_TO_KERNEL_CR3 > FLUSH > // On kernel CR3, *AND* not getting IPIs > KERNEL_CR3_LOADED := 1 > >> but I could punt the kernel_cr3_loaded >> write above the unconditional flush. > > Yes, that would eliminate the window, as long as the memory ordering is > right. You not only need to have the KERNEL_CR3_LOADED:=1 CPU set that > variable, you need to ensure that it has seen the page table update. > I assumed the page table update would be a self-synchronizing operation, but that betrays how little I know about x86; /me goes back to reading >> The 1 -> 0 transition is less problematic, worst case a remote flush races >> with the CPU returning to userspace and it'll get interrupted back to >> kernelspace. > > It's also not just "returning to userspace". It could well be *in* > userspace by the point the IPI shows up. It's not the end of the world, > and the window isn't infinitely long. But there certainly is still a > possibility of getting spurious interrupts for the precious NOHZ_FULL > task while it's in userspace. IME it's okay if the application is just starting as it needs to do some initialization anyway (mlockall & friends), i.e. it's not executing actual useful payload from the get go. If it's resuming from an interference, well we'd be making things worse. I'm thinking the worst case is if this becomes a repeating pattern, but then that means even without those deferral hacks the isolated CPUs would be bombarded by IPIs in the first place.