Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2823472pxj; Mon, 10 May 2021 11:31:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+KOoRYuRVXCF10rL/90z8zB2QM0faOPJbU+oBhFwPPtk5hCVbKZaP8H50Hzhevx4T1MTt X-Received: by 2002:a17:906:f9d7:: with SMTP id lj23mr27472127ejb.392.1620671460330; Mon, 10 May 2021 11:31:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620671460; cv=none; d=google.com; s=arc-20160816; b=GcISHz+3yjZD7JRf4Owf607fr1kHk8Qpp09udmRqhRj4JFhFux2iNRTnAG2+oQw33F L8ef3P48FerHflgDfHGD/Sb9cB2s0tpYjE+4/sm304P3Q3TNlMqyMide0ks9EG5d2eor lhDPKL2m2WWwUQHIuLE3iraOHkXk5c2mzsu17150NSbnHsuZ6T9XzMo6MBvWyr9Km6Qs 3oWFuWtQGzIqTezmtavhKnfv8DfCIRzFw/dUY5voF/wi3AthAow2jn2svFOtH9CXBeYw OAq12kyRUzKNGyP40s9S4it/5pTE1Y2Mxi/IWTuTWtXiF9enPslX3Vw7qYAyuh7wNAPS V0ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ZgpaffSujtHNJ5LjhNfC/DukRnISOwF7Vy02GDD16AE=; b=riZMZm0Aj1zVqeyvbm8kEmCrW1ZG3GzvdaB1EBlKqZFr01mEvRbsO7RZ2L98vN3LS4 2CN7Fm7pitK/hsPZTeAtAiszIgh8CYXf1867d6O/LeqjzIXt2Xm5iw5kJ+eS7dEY98bT T9n5mIW0QMvl+KguOFyEW6BQeY43X1+EGS6QIM9edNhxtzQWWbKvHXNaY+ck1skD2Kuv BpymSu4KKBcWULVhYyA06YWIkGslHxOyDMud6vc/Y7ZW8dKeOEnlx72y+2Hpm2ERKAYj MOWuUrBLboareCGalUB+vctUArk8yy0PXEI9W8hHqfeG1my/G14Oe3PI/ZucFsl6zXEL AzKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DkmtsBN5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 5si10300550ejw.423.2021.05.10.11.30.35; Mon, 10 May 2021 11:31:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DkmtsBN5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232386AbhEJS3t (ORCPT + 99 others); Mon, 10 May 2021 14:29:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231538AbhEJS3t (ORCPT ); Mon, 10 May 2021 14:29:49 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55DDEC061574 for ; Mon, 10 May 2021 11:28:44 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id c17so14226008pfn.6 for ; Mon, 10 May 2021 11:28:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ZgpaffSujtHNJ5LjhNfC/DukRnISOwF7Vy02GDD16AE=; b=DkmtsBN56yN77odOMd87wgYicJgsfSNRc43qNKbvBXiTxDYogtZWKgoqkKBAmKb11h cJvETHV9SgeuA5i/WetBSFWnA6oYJdEt2A+aN39XN7vLo7t4iljLWe2hG0I/M7OEL9zF s4kYpiMY0lNlR1xuuLzijdaYHsAFZkVVq7eMQzbBDzjniG1ZITWP2NglttnSmlkU6fBy BRVUm0qG+cBc6BnWyZAn44uwz9Zv+z5PSvOUARXZyNNNUpOgmTZ6diSpmwvvpPctmyTU 3kNYIOimswz8DS0CsyKEGlZwNM138LYvA3iSMmEGHccV07iExYBKi5mcuieNfg3RI9e1 m50Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ZgpaffSujtHNJ5LjhNfC/DukRnISOwF7Vy02GDD16AE=; b=nNKLXags0IkMcVQ92ZgrlvvLwWPZw3cXfgnEa89RN7Ggch+fbe+GeuWJz/UEoF+4v/ AZ9QF0ajwzYB5RO7uC9PisqQUrVl0M0BgMWc4ggyYUY3N8qIte5WMP4oy3IfCc6iTJnM Tc2v/Qy7PykoBqTwSnc9IlQ86olYfV4Gg40gY8NZysc2EGLosTtao3m+qi5GBQbOWwcX lzhKAW1s9gMZwzPtGrv47OIhc8MeDYItOooHe9eRqGMhhCbOY8ZxfWgzYWIum8iZrHX1 eep+aijT1T1OqAKxet3pIA/aI7K7sUGXxsr27GDKHMNX+p0rFKVh8vlwpIirrQb3+O4M ZKQA== X-Gm-Message-State: AOAM5311NITS3IdVd2otOapZTgG/h7lxxT0ILLfdAYINt9qbcTXD7xnr lOHx8wYG3DFv84PR+LY05xFxuA== X-Received: by 2002:a63:cc57:: with SMTP id q23mr26359576pgi.357.1620671323614; Mon, 10 May 2021 11:28:43 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id i3sm149187pjv.30.2021.05.10.11.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 May 2021 11:28:42 -0700 (PDT) Date: Mon, 10 May 2021 18:28:39 +0000 From: Sean Christopherson To: Paolo Bonzini Cc: Ben Gardon , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Peter Xu , Peter Shier , Yulei Zhang , Wanpeng Li , Xiao Guangrong , Kai Huang , Keqian Zhu Subject: Re: [PATCH v3 7/8] KVM: x86/mmu: Protect rmaps independently with SRCU Message-ID: References: <20210506184241.618958-1-bgardon@google.com> <20210506184241.618958-8-bgardon@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 10, 2021, Paolo Bonzini wrote: > On 10/05/21 19:45, Sean Christopherson wrote: > > > > > > --------- > > > Currently, rmaps are always allocated and published together with a new > > > memslot, so the srcu_dereference for the memslots array already ensures that > > > the memory pointed to by slots->arch.rmap is zero at the time > > > slots->arch.rmap. However, they still need to be accessed in an SRCU > > > read-side critical section, as the whole memslot can be deleted outside > > > SRCU. > > > -------- > > I disagree, sprinkling random and unnecessary __rcu/SRCU annotations does more > > harm than good. Adding the unnecessary tag could be quite misleading as it > > would imply the rmap pointers can_change_ independent of the memslots. > > > > Similary, adding rcu_assign_pointer() in alloc_memslot_rmap() implies that its > > safe to access the rmap after its pointer is assigned, and that's simply not > > true since an rmap array can be freed if rmap allocation for a different memslot > > fails. Accessing the rmap is safe if and only if all rmaps are allocated, i.e. > > if arch.memslots_have_rmaps is true, as you pointed out. > > This about freeing is a very good point. > > > Furthermore, to actually gain any protection from SRCU, there would have to be > > an synchronize_srcu() call after assigning the pointers, and that _does_ have an > > associated. > > ... but this is incorrect (I was almost going to point out the below in my > reply to Ben, then decided I was pointing out the obvious; lesson learned). > > synchronize_srcu() is only needed after *deleting* something, which in this No, synchronization is required any time the writer needs to ensure readers have recognized the change. E.g. making a memslot RO, moving a memslot's gfn base, adding an MSR to the filter list. I suppose you could frame any modification as "deleting" something, but IMO that's cheating :-) > case is done as part of deleting the memslots---it's perfectly fine to batch > multiple synchronize_*() calls given how expensive some of them are. Yes, but the shortlog says "Protect rmaps _independently_ with SRCU", emphasis mine. If the rmaps are truly protected independently, then they need to have their own synchronization. Setting all rmaps could be batched under a single synchronize_srcu(), but IMO batching the rmaps with the memslot itself would be in direct contradiction with the shortlog. > (BTW an associated what?) Doh. "associated memslot." > So they still count as RCU-protected in my opinion, just because reading > them outside SRCU is a big no and ought to warn (it's unlikely that it > happens with rmaps, but then we just had 2-3 bugs like this being reported > in a short time for memslots so never say never). Yes, but that interpretation holds true for literally everything that is hidden behind an SRCU-protected pointer. E.g. this would also be wrong, it's just much more obviously broken: bool kvm_is_gfn_writable(struct kvm* kvm, gfn_t gfn) { struct kvm_memory_slot *slot; int idx; idx = srcu_read_lock(&kvm->srcu); slot = gfn_to_memslot(kvm, gfn); srcu_read_unlock(&kvm->srcu); return slot && !(slot->flags & KVM_MEMSLOT_INVALID) && !(slot->flags & KVM_MEM_READONLY); } > However, rcu_assign_pointer is not needed because the visibility of the rmaps > is further protected by the have-rmaps flag (to be accessed with > load-acquire/store-release) and not just by the pointer being there and > non-NULL. Yes, and I'm arguing that annotating the rmaps as __rcu is wrong because they themselves are not protected by SRCU. The memslot that contains the rmaps is protected by SRCU, and because of that asserting SRCU is held for read will hold true. But, if the memslot code were changed to use a different protection scheme, e.g. a rwlock for argument's sake, then the SRCU assertion would fail even though the rmap logic itself didn't change.