From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.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 3F7D31A4F1F for ; Thu, 9 Jan 2025 23:27:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736465235; cv=none; b=AUOwdmqLwk1DB7Qo6Z50ueD8xnHVi5Lq4hGTEcXSyksUPQrGdg4nnRJQ8l+BauCqKsbx7dpe/hUwlZkxn6YSc3Ic4aaGd+j0caKvQsqjbFnzybWnkdFqmGwEgAZPCC0IwKaU9IUgK4Q4pBy0+M2WcWv8YvoJQZ5c6LiHP+CUuUY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736465235; c=relaxed/simple; bh=ZVjdGoEAqKfPPJc3jYgbyh7CjqMI0mQSqaK/A+J1h6M=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=sOSUrRp15sqdZiM8SNNJ9HZ4JWqYoe1+WxZBmgG+8SmnkFXAToS6WSepm3LCI6NvSqzNsi/bLKncPoMYc8gMhJuB41u7h0mC4aDJ56jmCan9J4L10S96saITqkMQF6iky9gyxcXwdL+Rtj/bwi96+Bp0sfAKsn7m3ileSIMfTI8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0YkbVch/; arc=none smtp.client-ip=209.85.222.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0YkbVch/" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-7b6e8814842so138092185a.0 for ; Thu, 09 Jan 2025 15:27:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736465232; x=1737070032; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=jtVdikFS+ajJy72HDorDhGlrpLpK/sj81PI5siVFo2E=; b=0YkbVch/BsPKa1oNJJt68SKTYCdum8dtaqRw2XmaXLslC7669zkk4s8T2BU1w8Qpwg ydIgkA5buPTajHSNNOoxmVJ2ru64bO7Avxji0sqs8OohwmFIxAFYMHQtc8U0uKrGiLw1 3JRSGpVveTuOaEDIt2ugjxuvngf2djmMieoXKebv+WlyBvsQlYC6QPb5WC5oWJqdwla+ TdGOMG5NVERt5m9zlEA0//1Q6E1vQXfz1Q0ln9usdLb62zSi0tP8Ns+NtwfUm3j3/cDU Sica2mOOMeqRf1hxWbCscnhkGfSndh1xMlO8Aftcie97AhWJjskVc0fWisYXNlWr3UBm qN/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736465232; x=1737070032; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jtVdikFS+ajJy72HDorDhGlrpLpK/sj81PI5siVFo2E=; b=WUhTt/r9BnpgoLHJjOH/t9KEdSUj7hC4Ubk2kHcJGi2HcqCJUWvDyXn8QLB3SNImGF mzJgL7JFcOJfSmJhmdAFpU4eCL6Dlr7Tzp6cNSc5C4fxrysjkWna6kvxWa92LWJPPbia VfZlbENUA5lKv+AhSqRMiEuffIrBuqSYR9m4k94MQIyHDt+ovOMZBRRSYEALZrE+QkXl QDW9ALJqi+15rqd32bX6QISqUBm7dUhqoNARygaxXIkxROOeFgSVvWPp0qjjgQ8HJhSb BjWdQ8tBo4oIF+eRcK87pg2xBj9ZiNvLr7ymMKY94lDtZAZZFoyo5ghJXdN04xTq8gVP 84lA== X-Forwarded-Encrypted: i=1; AJvYcCVxw2Y+wLgkHF4kxksVmku5o24a3+NH35VG13aMwvwr0Kl0YFcWcdZPrsyI+fcdf4A905WBmyFgrVebBuk=@vger.kernel.org X-Gm-Message-State: AOJu0YzEEtxPQ0QNW4R7hMFRKBz6Ua+KT3KBsuLUbyL0Mbj0pF4NflkL E5RYLJCT3PBVg7ChXXzZ9MGFMYJOHGPNTg5ZN0Esp9fx6ql8ECmke1Iiep7T3W8hYJDE6ENowMh vM+keaHGnD+F6qRHTpOmCi0nk0T8ptaiRLPrc X-Gm-Gg: ASbGncv5QPm6egVEBIX/v/XZi1yRWTwoAkd59r6E/+xiWSEfpl7l7KMbKU/H1dVixBh uRHARI9rUx1Sq5H885FZp/q1lMSYsCOxut3gqcIgH0icV273JmH5trij9i1VRkZE34RV/ X-Google-Smtp-Source: AGHT+IFyiEztkvRjglzDf9z+nqtrxgN9c621L5wb0NB0D2G7mGtuPYUvJC/VNx8N0ROnMp188WfC1Qv7vl8w7l2vTH8= X-Received: by 2002:a05:620a:28ca:b0:7b6:e8c3:4b60 with SMTP id af79cd13be357-7bcd975a284mr1192947485a.28.1736465231896; Thu, 09 Jan 2025 15:27:11 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <3ecfa4ff-9916-4ac1-8464-c1b6615b832a@citrix.com> In-Reply-To: <3ecfa4ff-9916-4ac1-8464-c1b6615b832a@citrix.com> From: Yosry Ahmed Date: Thu, 9 Jan 2025 15:26:35 -0800 X-Gm-Features: AbW1kvakSC1-ULQoiEft9NGCdsD8bRViVBZLoCCM4SnnaZAW6yZXfZF7OI60xYI Message-ID: Subject: Re: [PATCH v3 00/12] AMD broadcast TLB invalidation To: Andrew Cooper Cc: akpm@linux-foundation.org, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, jackmanb@google.com, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, luto@kernel.org, mingo@redhat.com, nadav.amit@gmail.com, peterz@infradead.org, reijiw@google.com, riel@surriel.com, tglx@linutronix.de, x86@kernel.org, zhengqi.arch@bytedance.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jan 9, 2025 at 3:00=E2=80=AFPM Andrew Cooper wrote: > > On 09/01/2025 9:32 pm, Yosry Ahmed wrote: > > On Wed, Jan 8, 2025 at 6:47=E2=80=AFPM Andrew Cooper wrote: > >>>> I suspect AMD wouldn't tell us exactly ;) > >>> Well, ideally they would just tell us the conditions under which CPUs > >>> respond to the broadcast TLB flush or the expectations around latency= . > >> [Resend, complete this time] > >> > >> Disclaimer. I'm not at AMD; I don't know how they implement it; I'm > >> just a random person on the internet. But, here are a few things that > >> might be relevant to know. > >> > >> AMD's SEV-SNP whitepaper [1] states that RMP permissions "are cached i= n > >> the CPU TLB and related structures" and also "When required, hardware > >> automatically performs TLB invalidations to ensure that all processors > >> in the system see the updated RMP entry information." > >> > >> That sentence doesn't use "broadcast" or "remote", but "all processors= " > >> is a pretty clear clue. Broadcast TLB invalidations are a building > >> block of all the RMP-manipulation instructions. > >> > >> Furthermore, to be useful in this context, they need to be ordered wit= h > >> memory. Specifically, a new pagewalk mustn't start after an > >> invalidation, yet observe the stale RMP entry. > >> > >> > >> x86 CPUs do have reasonable forward-progress guarantees, but in order = to > >> achieve forward progress, they need to e.g. guarantee that one memory > >> access doesn't displace the TLB entry backing a different memory acces= s > >> from the same instruction, or you could livelock while trying to > >> complete a single instruction. > >> > >> A consequence is that you can't safely invalidate a TLB entry of an > >> in-progress instruction (although this means only the oldest instructi= on > >> in the pipeline, because everything else is speculative and potentiall= y > >> transient). > >> > >> > >> INVLPGB invalidations are interrupt-like from the point of view of the > >> remote core, but are microarchitectural and can be taken irrespective = of > >> the architectural Interrupt and Global Interrupt Flags. As a > >> consequence, they'll need wait until an instruction boundary to be > >> processed. While not AMD, the Intel RAR whitepaper [2] discusses the > >> handling of RARs on the remote processor, and they share a number of > >> constraints in common with INVLPGB. > >> > >> > >> Overall, I'd expect the INVLPGB instructions to be pretty quick in and > >> of themselves; interestingly, they're not identified as architecturall= y > >> serialising. The broadcast is probably posted, and will be dealt with > >> by remote processors on the subsequent instruction boundary. TLBSYNC = is > >> the barrier to wait until the invalidations have been processed, and > >> this will block for an unspecified length of time, probably bounded by > >> the "longest" instruction in progress on a remote CPU. e.g. I expect = it > >> probably will suck if you have to wait for a WBINVD instruction to > >> complete on a remote CPU. > >> > >> That said, architectural IPIs have the same conditions too, except on > >> top of that you've got to run a whole interrupt handler. So, with > >> reasonable confidence, however slow TLBSYNC might be in the worst case= , > >> it's got absolutely nothing on the overhead of doing invalidations the > >> old fashioned way. > > Generally speaking, I am not arguing that TLB flush IPIs are worse > > than INLPGB/TLBSYNC, I think we should expect the latter to perform > > better in most cases. > > > > But there is a difference here because the processor executing TLBSYNC > > cannot serve interrupts or NMIs while waiting for remote CPUs, because > > they have to be served at an instruction boundary, right? > > That's as per the architecture, yes. NMIs do have to be served on > instruction boundaries. An NMI that becomes pending while a TLBSYNC is > in progress will have to wait until the TLBSYNC completes. > > (Probably. REP string instructions and AVX scatter/gather have explicit > behaviours that them them be interrupted, and to continue from where > they left off when the interrupt handler returns. Depending on how > TLBSYNC is implemented, it's just possible it has this property too.) That would be great actually, if that's the case all my concerns go away. > > > Unless > > TLBSYNC is an exception to that rule, or its execution is considered > > completed before remote CPUs respond (i.e. the CPU executes it quickly > > then enters into a wait doing "nothing"). > > > > There are also intriguing corner cases that are not documented. For > > example, you mention that it's reasonable to expect that a remote CPU > > does not serve TLBSYNC except at the instruction boundary. > > INVLPGB needs to wait for an instruction boundary in order to be processe= d. > > All TLBSYNC needs to do is wait until it's certain that all the prior > INVLPGBs issued by this CPU have been serviced. > > > What if > > that CPU is executing TLBSYNC? Do we have to wait for its execution to > > complete? Is it possible to end up in a deadlock? This goes back to my > > previous point about whether TLBSYNC is a special case or when it's > > considered to have finished executing. > > Remember that the SEV-SNP instruction (PSMASH, PVALIDATE, > RMP{ADJUST,UPDATE,QUERY,READ}) have an INVLPGB/TLBSYNC pair under the > hood. You can execute these instructions on different CPUs in parallel. > > It's certainly possible AMD missed something and there's and there's a > deadlock case in there. But Google do offer SEV-SNP VMs and have the > data and scale to know whether such a deadlock is happening in practice. I am not familiar with SEV-SNP so excuse my ignorance. I am also pretty sure that the percentage of SEV-SNP workloads is very low compared to the workloads that would start using INVLPGB/TLBSYNC after this series. So if there's a dormant bug or a rare scenario where the TLBSYNC latency is massive, it may very well be newly uncovered now. > > > > > I am sure people thought about that and I am probably worried over > > nothing, but there's little details here so one has to speculate. > > > > Again, sorry if I am making a fuss over nothing and it's all in my head= . > > It's absolutely a valid question to ask. > > But x86 is full of longer delays than this. The GIF for example can > block NMIs until the hypervisor is complete with the world switch, and > it's left as an exercise to software not to abuse this. Taking an SMI > will be orders of magnitude more expensive than anything discussed here. Right. What is happening here just seems like something that happens more frequently and therefore is more likely to run into cases with absurd delays. It would be great if someone from AMD could shed some light on what is to be reasonably expected from TLBSYNC here. Anyway, thanks a lot for all your (very informative) responses :)