Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp106054pxj; Fri, 7 May 2021 04:55:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyAmOLcWzR/HA2Qi7DuxkTYJ43ABOjWcsFKt9FmA94kbFed6ZgBQz5c9O0oi2ZqZdyE09dJ X-Received: by 2002:a17:906:a055:: with SMTP id bg21mr9596088ejb.554.1620388541988; Fri, 07 May 2021 04:55:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620388541; cv=none; d=google.com; s=arc-20160816; b=Ly2sfs7oDag2XRZb0kAnE8evIpeyclMS3H5IGBVWeJ2XkttXAS9d/FGT6y1QTRiczP X2psZk4mxCW8nTz3t8NpoaN3BOU5HxafkGmByyQn9m+YZLFW0bSr5yWO1UAeMnBzkC8q 0/hWPNRAoqZ3mTVAzBbSn03QzxCyDXX+ZEjyBtDaVAqqNC3dXcFjzGlxeTdHWYEEF2E4 4OFZSJ/7oC+WfgkYJ1iF7LZr8BFA4Tx625KJr5WfxmTQ1OPD2t3l8fincgckYb0nov7r 1MsXiXk420bEURBot9H3yPxzp4lZykxdP8LAuPpB064p5wqfMUiy4yd9FkzjmCJkaNbO IlQw== 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=xfCXoiSRUf8EHAREMqARq69JqzDyk9XVPqTBvkTg9rI=; b=B8m5Ddo2SO65DcpD741JF1S+gy2d/CrBvo1ETMd0BEjrmdlnR91HwH1ae8LWs/Xpdk xcazto0LI/om1u/ExUVIXziwhauUTQSYT/+O51sGjoCzWTZkjc83aU6gZ0J4AHfCSfur CTu8HE9/a2QA4UezAY89xqwgTAiLpd04giRX2kLnujf7opCSQfCmvdmAfftW0VAqiXHo GvW5FJMeqXgy6/JFusKPUHTe4Ypu7Merwphp9aQ8wOke+dsrOluRltRaYilbnJ6b5InK Phsg++rCLLlVg4vnVCULNmCI1UgKMxaxR1KsZ5nBeR2oKHbkW/C9dKNDk79VEUq7/tbJ 0/Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gVXu3HtB; 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 g9si4486395edm.502.2021.05.07.04.55.17; Fri, 07 May 2021 04:55:41 -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=gVXu3HtB; 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 S235003AbhEGInY (ORCPT + 99 others); Fri, 7 May 2021 04:43:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27317 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234506AbhEGInX (ORCPT ); Fri, 7 May 2021 04:43:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620376944; 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=xfCXoiSRUf8EHAREMqARq69JqzDyk9XVPqTBvkTg9rI=; b=gVXu3HtBW746PzK5QEAJ6LcGT94mF1scIFBKDJN3KUOnvf57yzYyECOOakQqDMQS9UfhOa mJBCAfO7sSEdeHssy8zXg9RJH3Pa97uf3yjq4u/JgYg8ihSmX7phxHGpoR+Qjqr1YZQPpp YdLhXM0C7Sff084CZi/2KheHANc58vw= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-157-s14pUELRP_uo8pwHCI0oqA-1; Fri, 07 May 2021 04:42:22 -0400 X-MC-Unique: s14pUELRP_uo8pwHCI0oqA-1 Received: by mail-wr1-f69.google.com with SMTP id a7-20020adfc4470000b029010d875c66edso3265514wrg.23 for ; Fri, 07 May 2021 01:42:22 -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=xfCXoiSRUf8EHAREMqARq69JqzDyk9XVPqTBvkTg9rI=; b=AqBCt2OsY4nx48a41uXLXkFnGe++pBiJgJj1LKTYViU3lqAQZVpLog5rdHfZQ8AqEZ +ZIhNtKhkr/IfLPX7PpheFvBbLbXWH8a9YQPfF1XcWdlRSJT/4WXOIdqt2CV7wxUF11+ Oh2jSqE7z8ZlKS5S7I0/FlWapou0Ltg2OptRRAc3wbrQ+HHZdIzZ9jNbZJlMP0DFYby6 hFIpckdxoePBZ3/wRIqvP9LzvvBBCkhtzmCEa5h2XrDvKYYxBx5A07ODG9nzgTAeDxyG 5m+5qs4HRVMZhcRPdm8T3guUdO4XXfE6uO6z7PUF4QJL6xabwj2APARYMkxz7YKhQsK5 jHyg== X-Gm-Message-State: AOAM532rzlwjzjnrkrYE8vGNhlvvP0RoP6paCwnUjSBsQAFklAs+EKN7 uewwol34/vX7GtnZqlati16JVAr1thPiSuupDbVA0R/SFoU6S+JSyL3kmD/C5qAsQxB4b+Zosr1 327EHOFm6+2bRjWC3aBgJnQXm X-Received: by 2002:a5d:400f:: with SMTP id n15mr6846893wrp.274.1620376941329; Fri, 07 May 2021 01:42:21 -0700 (PDT) X-Received: by 2002:a5d:400f:: with SMTP id n15mr6846875wrp.274.1620376941115; Fri, 07 May 2021 01:42:21 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e? ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id n10sm7744773wrw.37.2021.05.07.01.42.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 07 May 2021 01:42:20 -0700 (PDT) To: Ben Gardon , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Peter Xu , Sean Christopherson , 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: Fri, 7 May 2021 10:42:19 +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: <20210506184241.618958-8-bgardon@google.com> 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 06/05/21 20:42, Ben Gardon wrote: > In preparation for lazily allocating the rmaps when the TDP MMU is in > use, protect the rmaps with SRCU. Unfortunately, this requires > propagating a pointer to struct kvm around to several functions. Thinking more about it, this is not needed because all reads of the rmap array are guarded by the load-acquire of kvm->arch.memslots_have_rmaps. That is, the pattern is always if (!load-acquire(memslot_have_rmaps)) return; ... = __gfn_to_rmap(...) slots->arch.rmap[x] = ... store-release(memslot_have_rmaps, true) where the load-acquire/store-release have the same role that srcu_dereference/rcu_assign_pointer had before this patch. We also know that any read that misses the check has the potential for a NULL pointer dereference, so it *has* to be like that. That said, srcu_dereference has zero cost unless debugging options are enabled, and it *is* true that the rmap can disappear if kvm->srcu is not held, so I lean towards keeping this change and just changing the commit message like this: --------- 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. -------- Thanks, Paolo > > Suggested-by: Paolo Bonzini > Signed-off-by: Ben Gardon > --- > arch/x86/kvm/mmu/mmu.c | 57 +++++++++++++++++++++++++----------------- > arch/x86/kvm/x86.c | 6 ++--- > 2 files changed, 37 insertions(+), 26 deletions(-)