From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53435C43460 for ; Fri, 7 May 2021 08:42:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1CF93613D8 for ; Fri, 7 May 2021 08:42:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235025AbhEGInZ (ORCPT ); Fri, 7 May 2021 04:43:25 -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-Google-Smtp-Source: ABdhPJwoFMyEIZk1TCndnag6cii3qK499Zt0/XLGrLq/Nt25lWx4sb8T0QrY1Xct3knS7xGtliLwCw== 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(-)