Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3445734pxb; Mon, 4 Apr 2022 17:13:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNdpnDMt6fbqPwZVi9UzltYDC1A7xVFllerFcng/BKIHRuKVHLOtriFuPCLZUfB6TohSoE X-Received: by 2002:a05:6a00:198d:b0:4fb:3204:fa8 with SMTP id d13-20020a056a00198d00b004fb32040fa8mr658496pfl.48.1649117625777; Mon, 04 Apr 2022 17:13:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649117625; cv=none; d=google.com; s=arc-20160816; b=bsdNqV3OqcruaL9SIEo457lJFA7RHYqAHMJDo0aEnr6vm+7bkttM4AdqK4l0Dnl34g C317AF3ruW2l5QeFsqsgTzvVhtdKgOoVihx4GGJgtYvJ5rChGfp+Cr9+BWsiqLxQg6nU 9A25vTZDPbYs7QWg9aeDh+53IaN3w93Cp0zkt+Ke2wjgYO8mPm05opKx5dExV3qdCV6z ObRbo0HCG4+JOl5ps1aeNQ40Z68cjGI/PzMb5Us3EPGYGkL8Grn68Yyo8O3Sby1bXxs2 X/3XGakAX7UgZjKI6P14Fn9VzFO4Wstwk7O5L6LteJAGYC+713lDvQiLNuFZqhyms3rU PckA== 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=o6Jps2kjgfU371gs4sTPnWDwSSEcT5HsdO32Ve4rCA0=; b=kEGZL+EjQTonBWB6xCFDmRjqpGXAyNsSGVx5jernHrCLXfGexohRlC5wnvkQDuyffT suab9XYYAWYIqYzsodLgM82FQcwS6TLXIY/c4D+WqyWRpTKqJ99U0LwctiXyZm3r0deL KjCWJfAxJ5S2t+cLIb0Ttk02ht+qwFZE8IhpdJC26gLXYfRLad3LtV7UothPMmsnKnZL 9b64iakbReaPFOY1UhnuGjqbLeJl4ZHrXKz6SARn//pK836h/u29R+2SrnQE5YAHYWqt ZQpgbqEIuJO4IPxhBrunrySO5qZRhG06nj8YaE9h3fYGUEHXELwFHi8w+l7LfwOrQwiO hM6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=byVdCGvS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j16-20020a056a00235000b004fa7630aa94si11502220pfj.126.2022.04.04.17.13.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 17:13:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=byVdCGvS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4D3026D4F0; Mon, 4 Apr 2022 16:44:02 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1382817AbiDDVcN (ORCPT + 99 others); Mon, 4 Apr 2022 17:32:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380582AbiDDUhM (ORCPT ); Mon, 4 Apr 2022 16:37:12 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAE5626AC8 for ; Mon, 4 Apr 2022 13:35:14 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id f3so10086560pfe.2 for ; Mon, 04 Apr 2022 13:35:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=o6Jps2kjgfU371gs4sTPnWDwSSEcT5HsdO32Ve4rCA0=; b=byVdCGvSfTN+YF9id0vNf0etolMj+qC4TWGVuif01wYvsnFC5p7Eu60+KIbGKtf23I L1qp2tXp3H1qKh8ID/YgvO4Ybb1z7p3HWhN/kQORzGDYtqImGFuN05iPemY1YyLo4EzI 3ishUmMXZXenx+gdi8i6jAO6QIsdYFre3qQfqv3c3d4NhyYCjDwI/rNOFfiGfolSeoJP JoUrVPOPl/asUmrsT3wtoKAXy+HrlTccZUOmda/dERxQSHdN9IX5cmNpAB5G7kpgZMEj 5AYpDcqrlhuTWK3zQUnxcB8C3vaEQWMzOovPhBnMh0pIKDJOBuMdUWGM19ni3OsuEgaN T+LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=o6Jps2kjgfU371gs4sTPnWDwSSEcT5HsdO32Ve4rCA0=; b=tID1hB467hE7EcMPqnPGeyDK8orotpD8SKKvO6uvQbgk6xyjQMHQeFUcw1RMGbNdo/ bz350ZoCP6h6Fub6h71/PCct59rhqpAcICI6bvMsMD2Rm9A9oC5VQnl84SRZ+4J2fwgg yUd8jAxNb5DOg6lQD3e2ELfqQHwRjhrQbdaABFkEViCwZSoQOaefbCW2z3rDCd7iY6uC 35SeNhvgr3cgdL1lQI0r5HqXD89EKxQB4eZ+tAPrZjsGkFjuIur1U2J0NApjj6RDsDFV sPYHFq2/BbBS3eh16BTkH9ZykffoyWc6Izqq7BzjgZa6xvFsAWBrRJ/j0loWKNO12yTQ H73Q== X-Gm-Message-State: AOAM532IToWvJxYmraUkPFznmrGz2nNn/rfluiLXF4JYtfFJjDWQnVbu xnvUSa05SGuvx7HbI90BjFnFgQ== X-Received: by 2002:a63:3e47:0:b0:382:366:64ea with SMTP id l68-20020a633e47000000b00382036664eamr1365270pga.210.1649104513958; Mon, 04 Apr 2022 13:35:13 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id s26-20020a62e71a000000b004fde8486500sm7989821pfh.126.2022.04.04.13.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 13:35:12 -0700 (PDT) Date: Mon, 4 Apr 2022 20:35:09 +0000 From: Sean Christopherson To: Peter Gonda Cc: kvm@vger.kernel.org, John Sperbeck , David Rientjes , Paolo Bonzini , linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: SEV: Mark nested locking of vcpu->lock Message-ID: References: <20220404194605.1569855-1-pgonda@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220404194605.1569855-1-pgonda@google.com> X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 04, 2022, Peter Gonda wrote: > svm_vm_migrate_from() uses sev_lock_vcpus_for_migration() to lock all > source and target vcpu->locks. Mark the nested subclasses to avoid false > positives from lockdep. > > Fixes: b56639318bb2b ("KVM: SEV: Add support for SEV intra host migration") > Reported-by: John Sperbeck > Suggested-by: David Rientjes > Signed-off-by: Peter Gonda > Cc: Paolo Bonzini > Cc: Sean Christopherson > Cc: kvm@vger.kernel.org > Cc: linux-kernel@vger.kernel.org > --- > > Tested by running sev_migrate_tests with lockdep enabled. Before we see > a warning from sev_lock_vcpus_for_migration(). After we get no warnings. > > --- > arch/x86/kvm/svm/sev.c | 12 +++++++----- > 1 file changed, 7 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index 75fa6dd268f0..8f77421c1c4b 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c > @@ -1591,15 +1591,16 @@ static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) > atomic_set_release(&src_sev->migration_in_progress, 0); > } > > - > -static int sev_lock_vcpus_for_migration(struct kvm *kvm) > +static int sev_lock_vcpus_for_migration(struct kvm *kvm, unsigned int *subclass) > { > struct kvm_vcpu *vcpu; > unsigned long i, j; > > kvm_for_each_vcpu(i, vcpu, kvm) { > - if (mutex_lock_killable(&vcpu->mutex)) > + if (mutex_lock_killable_nested(&vcpu->mutex, *subclass)) > goto out_unlock; > + > + ++(*subclass); This is rather gross, and I'm guessing it adds extra work for the non-lockdep case, assuming the compiler isn't so clever that it can figure out that the result is never used. Not that this is a hot path... Does each lock actually need a separate subclass? If so, why don't the other paths that lock all vCPUs complain? If differentiating the two VMs is sufficient, then we can pass in SINGLE_DEPTH_NESTING for the second round of locks. If a per-vCPU subclass is required, we can use the vCPU index and assign evens to one and odds to the other, e.g. this should work and compiles to a nop when LOCKDEP is disabled (compile tested only). It's still gross, but we could pretty it up, e.g. add defines for the 0/1 param. diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 75fa6dd268f0..9be35902b809 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1591,14 +1591,13 @@ static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) atomic_set_release(&src_sev->migration_in_progress, 0); } - -static int sev_lock_vcpus_for_migration(struct kvm *kvm) +static int sev_lock_vcpus_for_migration(struct kvm *kvm, int mod) { struct kvm_vcpu *vcpu; unsigned long i, j; kvm_for_each_vcpu(i, vcpu, kvm) { - if (mutex_lock_killable(&vcpu->mutex)) + if (mutex_lock_killable_nested(&vcpu->mutex, i * 2 + mod)) goto out_unlock; } @@ -1745,10 +1744,10 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) charged = true; } - ret = sev_lock_vcpus_for_migration(kvm); + ret = sev_lock_vcpus_for_migration(kvm, 0); if (ret) goto out_dst_cgroup; - ret = sev_lock_vcpus_for_migration(source_kvm); + ret = sev_lock_vcpus_for_migration(source_kvm, 1); if (ret) goto out_dst_vcpu;