Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3677077pxj; Tue, 11 May 2021 09:27:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwG1zFroU1IzDm41fVkcE1bj6BcfCh/1+PjFGi1rPW6tyD1SLbXljiElxzmft/VGekXjzwp X-Received: by 2002:a2e:b2d1:: with SMTP id 17mr22523848ljz.318.1620750425135; Tue, 11 May 2021 09:27:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620750425; cv=none; d=google.com; s=arc-20160816; b=T/HNuvGair4Y+hgDeWhhHu45rmHx+MClNf+DQN1v8yrVsYcqZUrU+Ej0RI7HSPJCzj Fi6eEmzUpOOODwpaZhP2BKU8URlZPhZ+Iw+kcRBQtAy4uw9Fmf4k8JDba3hgSetiaYd0 mK+yUjhQH6ubvySLkbKhcqohO2/v9dswnXbKtuLZYeoMX7IrR+PFVNn7zQ+mhp88zgFy 2qbwrNR6/3PAThrmpnV3fVeEMO8Lzb2wYauAZ2Cl95mp8iMSz/pzEe4FaL5xRlJoXG2C 473IQdQ6ypbraXfjUMAiJ2XcNUoGS7nzU11x5y15m8v0j/VhvcoU9HrT6vv+92XJuP3l t7Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=4jXZs11HGH/EBNKvji1vJOWsgckIXclQ4pDhYDnWyh8=; b=n79K2np2ZUhxEErRCQFiLT4OR2+P6+P5tdT8V7MSrrGqhMk+2u5iFHPrpuRX8AN/zO 0AveAK6R/VAA1Oyxzj9RVTbuonIyet7BlEpllRvGaTgap2rfPS0yiyo8xXoKGumqB/k6 Lrmwyl0zzKy+Ed59Qsp2r9ixjvCB4lh5ssOkR5LB8zWtUbjj8MPVBl7qSkQvx3p9SKTZ WgemyxLyXD6fl6mDm+aFpZ5KWsHSQ5EcqD4sLfmZ8L5W5ej9MMhVMgPEr/ookzwmZF8h RTfjem3jcsjccrQN2UZf9BwZYDzem0GvhFNVfFJVHriSzjeRYs0+CQf67Y8yQfGHwgK7 Qudw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=K2ME3kMt; 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 f4si9436295lfr.263.2021.05.11.09.26.24; Tue, 11 May 2021 09:27:05 -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=K2ME3kMt; 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 S231259AbhEKQXg (ORCPT + 99 others); Tue, 11 May 2021 12:23:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229921AbhEKQXe (ORCPT ); Tue, 11 May 2021 12:23:34 -0400 Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45F2EC061574 for ; Tue, 11 May 2021 09:22:28 -0700 (PDT) Received: by mail-io1-xd2f.google.com with SMTP id p8so18661267iol.11 for ; Tue, 11 May 2021 09:22:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=4jXZs11HGH/EBNKvji1vJOWsgckIXclQ4pDhYDnWyh8=; b=K2ME3kMtsLEUOJHmWigd7iBeW3kUpwwkNwrk+bKCUi/GAasFcn1ZjPJxEIfRUxKkzo XRE8SZWXoPSy5xuqDorvmCuof0ODWKu/NdabOTyAcWb7p+aUwc1tjXljnZdNnx1AYUMi K9Hwu53wrQporys+PQgWjJpUROgDva5Yr49pRar93gWXGvHUd0CtDGbRZMXD2mGzHyoE XVElnc5EEp9MpIkPo0gJ1x0jdQv3K7wXFDJgZr57Xw/TPT/I7yCAVa4dJ2dYoS3pX4PA uxp/UYSQYsNgvutVCPQZ1nAA+4UVrXiUaeQ4grJfNjcnce9ZoYU+C+vE6bJ8WENSY3Og 83aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=4jXZs11HGH/EBNKvji1vJOWsgckIXclQ4pDhYDnWyh8=; b=fsMGXYpKXRYlkHMQrjj58MEnZ5Xl4nYL40h7YJIFVGGs5Z543If1J+SrIGq5URw51H NzyEOdk4HsZHqvC/H/c7PG/fyZC39r1nyBQ1WswZVHJcBuyB57AcLtPwUOdHtMKk+cT6 dNFjPfPcg8NJ710DQPbTqiOwPwFUUXZXcObRZxMAv1i/Rk/yR4Vq/c/TeGxO0x7sSFmm 9YekJWVlmBMcjpsS4GQKC41Q3UVhLxrcsBJ3znHK24+uRljxNvHmJjDN+XLmhiRpHE+k ALUwnqCFsSi2V4ek0KsjYxiQaASnaK4D3SA8DrRB2FhGSw8fH/S20dZAvuAH9Hh/M2qV /LHQ== X-Gm-Message-State: AOAM533F9wihitqDYCwGccDZnSt+ev8hEg3LRdpY6ZGuSZKldTKP7oEq uLBOvtXDqt3/Hhcr9WsNTNCYHJ4Wfvc1xpV1u9VW1w== X-Received: by 2002:a5d:850c:: with SMTP id q12mr23352649ion.189.1620750147514; Tue, 11 May 2021 09:22:27 -0700 (PDT) MIME-Version: 1.0 References: <20210506184241.618958-1-bgardon@google.com> <20210506184241.618958-8-bgardon@google.com> In-Reply-To: From: Ben Gardon Date: Tue, 11 May 2021 09:22:16 -0700 Message-ID: Subject: Re: [PATCH v3 7/8] KVM: x86/mmu: Protect rmaps independently with SRCU To: Sean Christopherson Cc: Paolo Bonzini , LKML , kvm , Peter Xu , Peter Shier , Yulei Zhang , Wanpeng Li , Xiao Guangrong , Kai Huang , Keqian Zhu Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 10, 2021 at 11:28 AM Sean Christopherson wrote: > > 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. I'm inclined to agree with Sean that the extra RCU annotations are probably unnecessary since we're already doing the srcu dereference for all the slots. I'll move all these RCU annotations to their own patch and put it at the end of the series when I send v4.