Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2796436pxj; Mon, 10 May 2021 10:54:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJymk0p4jYFwnwi9fIKHrwddiHg2XoyUjWidaWTRUA9IVo2fm3wbQsNEeIL9vVHx3fXXJPiL X-Received: by 2002:a92:dc44:: with SMTP id x4mr23121780ilq.209.1620669296232; Mon, 10 May 2021 10:54:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620669296; cv=none; d=google.com; s=arc-20160816; b=PzW54739HzmBBwzRrSH8a1vLY0J4a0C8iQmfOJskns/kwiFBfZxdYN/DhumnqqkgK9 mrZuNsNG98XtBteRlZUYj/IJshCQhpW636AQr2pQOIUAhS5MV/0fRmiPPw2s5U0CJNzw WdS17fTaM+nnypwoDZmYiqhC+7h07Ru1Z5kMqOH558Y9LpXY1uI9/6Digwiy3GBUSl2/ UpB7aeWoS33/TWu6BOvsScOTldbslTX+3VGVw+E6epyEJpMr4W7we0HC44OS9WCndfsc sNt32eIuoGtOTn+CJ+KLMa1/iV3MaCrlWFMpLQZsvvy/1IpazjdKXAHYPyILepDCeSwh yLNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to:dkim-signature; bh=ZRMKkN3puVHnbiCCjdkC/0aI/Ng/WMP6VZjBrfm3u3I=; b=oz3hgeecN5p6cLr2YSxpEPMANkDB4Nx07dASyvmUtWs7BP7O8VIPDZYn+8dnDHS0o8 nB0vkP2vLZxv+ooQq8Atw6OcocrYrTCgZveGjQsb3C6Q3wH+qMBpyQdR/rvquK9GLBlc TPAummj7MYVA6MHyZeVBubrOvIZYnOEe4KUcH7ybNuDU2Gfe26KSU5bWZUs4QYFF4Qtv K/pFq2+QImnAztIHgq04nqXLIeACG0y7/Wa+hPYWMFdYlAHZ1eQMjDnzP8X6DvWEtp1v AczPgyHdKBCXRpwBHPCBWQqVlDVZ5nknlJ1SXQVRgsbR3luPKaAv+wJ3+pQIEIVsWzPc c9hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RBBKMaQR; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q13si19030709ilg.125.2021.05.10.10.54.42; Mon, 10 May 2021 10:54:56 -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=@redhat.com header.s=mimecast20190719 header.b=RBBKMaQR; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233161AbhEJRyu (ORCPT + 99 others); Mon, 10 May 2021 13:54:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:29228 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231512AbhEJRyu (ORCPT ); Mon, 10 May 2021 13:54:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620669225; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZRMKkN3puVHnbiCCjdkC/0aI/Ng/WMP6VZjBrfm3u3I=; b=RBBKMaQRxLgB6GnQNiSXeS7nh1aaYbKT0veTdR8EvRC3I4dC630aQth6cr9CCypXelAKSZ LUzfOi3ZV3dopS3fZEBTkaSEcOJps9haJn2ULIUeRXJc9gVEXlgCa98Csa9cdvKA7f4qms wU48ESwTTCXsMJC6QN9zf17vTI5dzq8= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-412-X-JjoIH9PheSiKoIK8B2bA-1; Mon, 10 May 2021 13:53:41 -0400 X-MC-Unique: X-JjoIH9PheSiKoIK8B2bA-1 Received: by mail-wm1-f72.google.com with SMTP id 7-20020a05600c2307b02901432673350dso3757643wmo.9 for ; Mon, 10 May 2021 10:53:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:references:from:subject:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ZRMKkN3puVHnbiCCjdkC/0aI/Ng/WMP6VZjBrfm3u3I=; b=OfLklJP0KKwQkwopBry/PmxOMh1C77EImbM7oxMX+76n/a4hsULC9eAVmdJcFne7Qk m4hpn3NOq+a8NqR/3Bug3sHrJX6IvwGs5ZZR5V07w9SivDqfILmJBOtN6LbtPYvf1W+5 Q/+OG53C5whUwJIgzNjrlxw8FudQ93sK0X7RzuvC9Z97W1r7gAAHhtcptZB0mdGUAE5N USQcLJDbG0ZMeULzxVbO72LTTenmzpyZ6lp33CFZ0a8zsyqCM6BtUyI3O4h8X08ABrDt MMqKqxdvi5s4BTgtS9sDiuBauTZobMpbpcJY15Y3TBVyYx+Hcyw1NYYuMwESfMw+DS1X GgHw== X-Gm-Message-State: AOAM53032EStbDmGYMHnIKODCpRl2IJkx9/aO9Te8CWBKTgLcpkVyOJG MLr9OcfvNyWQjlU5bXDzTqJff27P7xj/FHFwG0BKYgf2GNQVOx+ANcFv+ZDaBaUmfJB2QxHAjf0 9qvCA4L07BJ66gTKAYz7IPeyO X-Received: by 2002:a05:600c:41d4:: with SMTP id t20mr397216wmh.46.1620669219692; Mon, 10 May 2021 10:53:39 -0700 (PDT) X-Received: by 2002:a05:600c:41d4:: with SMTP id t20mr397186wmh.46.1620669219388; Mon, 10 May 2021 10:53:39 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id n123sm285028wme.24.2021.05.10.10.53.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 May 2021 10:53:38 -0700 (PDT) To: Sean Christopherson 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 References: <20210506184241.618958-1-bgardon@google.com> <20210506184241.618958-8-bgardon@google.com> From: Paolo Bonzini Subject: Re: [PATCH v3 7/8] KVM: x86/mmu: Protect rmaps independently with SRCU Message-ID: Date: Mon, 10 May 2021 19:53:36 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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. (BTW an associated what?) 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). 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. Paolo > Not to mention that to truly respect the __rcu annotation, deleting > the rmaps would also have to be done "independently" with the correct > rcu_assign_pointer() and synchronize_srcu() logic. >