Received: by 2002:a05:6a10:144:0:0:0:0 with SMTP id 4csp946669pxw; Sat, 9 Apr 2022 06:08:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRZjIsoP8otGWqirTr95ArZEs132vRzULwFthXocG+FFldoFIddVdR5Q0x6ZxU8j5b4Zge X-Received: by 2002:a17:907:97cf:b0:6df:846f:ad0a with SMTP id js15-20020a17090797cf00b006df846fad0amr23302331ejc.286.1649509712964; Sat, 09 Apr 2022 06:08:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649509712; cv=none; d=google.com; s=arc-20160816; b=H9+XYicEZnuObIkUX7pc+sKGoDLw1dFiG8kB4rvmkIxZviyDrObea+gBxO6dk/Aels AMjmxxn70k1Rv2PQIBNtsB8a+D4pXrCcCrDfyVIo1YaQsGaMQwtX+V3yPbD8d+VP9NiW 07F1J/U+UdyEOXph0YAt3EOKGYDUK+/E9En9G0vRux0L5AMi8ZCbSxXazg+vsIMVaxQj Cs30RSyb8ut7h7U/z3Y600T4UKja2ZGXYRTc7CLQVZnOl50poYQIAXC7SKQE0bVMKxBh 3FSViUR01BAthWrHr/KCJrn89mj5uol6YunkxQgGNgFDuXHAIVorx79x60WGgtDdp+6D ARPg== 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=XUTKl/IaJCwrxr2khPWm7F441fPDzuP0bn51KMo02uE=; b=vV8FVBvg6GB69/wuz9moWgQZUoc5apEZq62ggJFCCQHC0hU233Sl9D+ZjBYg/p1lRJ ezK0r49Eu0ROWYNGM8Fcp0tpmk180Zk10hcd3jk20rw3fSXMur0LxDzRDXN2nKxJzZbd CiZGvm6Rkx/HNilNCqCxUsSwKdibE/Pdyqe144ZCoNYUMFeepqaSJRuI8jPfOd3B3uJT gHj4Wxmee48doOqiK0FmBFwfW77ZQH+fXH7Za609SdmAdx34sou1Fs3W/WUSlcRQ56t0 spkRoc59EvoIkIEUnAgLtwKXgN79wuzNczSVhjOXuw0HtHDsTl0NcPNOzvDUZUCI0mbv DlgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=LwKcKIta; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t7-20020a170906a10700b006e76f053445si2238930ejy.55.2022.04.09.06.08.07; Sat, 09 Apr 2022 06:08:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=LwKcKIta; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S237399AbiDHPK1 (ORCPT + 99 others); Fri, 8 Apr 2022 11:10:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233605AbiDHPK0 (ORCPT ); Fri, 8 Apr 2022 11:10:26 -0400 Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70712167D6 for ; Fri, 8 Apr 2022 08:08:21 -0700 (PDT) Received: by mail-lj1-x229.google.com with SMTP id u19so2264026ljd.11 for ; Fri, 08 Apr 2022 08:08:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=XUTKl/IaJCwrxr2khPWm7F441fPDzuP0bn51KMo02uE=; b=LwKcKItaVj506SbKhpxTEJLegJ7vxBPkSMYOBoTAw5zD3zRG011lCJGgQp/iMROOLV rkpIxxYVFg5jGurHfNEhdUFSAqumasv7PScMzOwzQ/vW2+xv18Nf9+T/KiU/jI40q/+n /+IOc5J0R/RNx3ZUJmcfghFmtotsRaMrt23FrB0Z3XvpDmiX7Bg423ovFjzS5+zqdrx5 /zqtPiw8XIoeet06K1K2mFq7Lx+Ot22dg15AmyFbQnhnt5wKnXDQJKgWoiUvAquu2hTu wSXT1CKtGbsjkVmNIBtN/NgT49/TRHHbVJOBKKsOizmAuHoQTZujVKzIuwTizgEN9KU3 vQYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=XUTKl/IaJCwrxr2khPWm7F441fPDzuP0bn51KMo02uE=; b=CpR8yvnyYn4TY2+qNHUElOV42eeqdeuaLCu8OsPrtkGThLkI/AmILBd59Mqqxe45b7 NRXU9rsBoFxucVRB/Ae60wnWI3z49EJpKDaVfsRp9QMI98qB7/IPZ2YnAlnyi7T50t0e O8QQrqv/s+809V9FT0326R4wB3rrIqs20GmgPk3X0DWJd1vTpCgWU9oas6b0KU/KWxFe JvqiF3DXKajU+FV92Ggse3N7hBAUW8JDc4EWy/e5IFBxTn3DB64lnlhJlL7pyIALKq49 6n60qPY0DgGHom8u+g1uBRqTdMiw/FaZpUEzDzGmCSPNP5+xnUrFwzXmNkkADevgA5Yt t8Vw== X-Gm-Message-State: AOAM5331JStYhXArT5eAN8CmtlpolDt/JlDvY5f9uCOrHCA9AnYTSkjR 08H2wbYmgvWH8LPWJVxgaQFx1KK9m7STMPSx7bcYYw== X-Received: by 2002:a2e:bd09:0:b0:24b:9e3:30c6 with SMTP id n9-20020a2ebd09000000b0024b09e330c6mr11355360ljq.282.1649430499181; Fri, 08 Apr 2022 08:08:19 -0700 (PDT) MIME-Version: 1.0 References: <20220407195908.633003-1-pgonda@google.com> In-Reply-To: From: Peter Gonda Date: Fri, 8 Apr 2022 09:08:07 -0600 Message-ID: Subject: Re: [PATCH v3] KVM: SEV: Mark nested locking of vcpu->lock To: John Sperbeck Cc: kvm list , David Rientjes , Sean Christopherson , Paolo Bonzini , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 Thu, Apr 7, 2022 at 3:17 PM John Sperbeck wrote: > > On Thu, Apr 7, 2022 at 12:59 PM 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. Nope. Good catch, I didn't realize there was a limit 8 subclasses: [ 509.093776] BUG: looking up invalid subclass: 8 [ 509.098314] turning off the locking correctness validator. [ 509.103800] CPU: 185 PID: 28570 Comm: sev_migrate_tes Tainted: G O 5.17.0-dbg-DEV #24 [ 509.112925] Hardware name: Google, Inc. Arcadia_IT_80/Arcadia_IT_80, BIOS 30.6.12-gce 09/27/2021 [ 509.126386] Call Trace: [ 509.128835] [ 509.130939] dump_stack_lvl+0x6c/0x9a [ 509.134609] dump_stack+0x10/0x12 [ 509.137925] look_up_lock_class+0xf1/0x130 [ 509.142027] register_lock_class+0x54/0x730 [ 509.146214] __lock_acquire+0x85/0xf00 [ 509.149964] ? lock_is_held_type+0xff/0x170 [ 509.154154] lock_acquire+0xca/0x210 [ 509.157730] ? sev_lock_vcpus_for_migration+0x82/0x150 [ 509.162872] __mutex_lock_common+0xe4/0xe30 [ 509.167054] ? sev_lock_vcpus_for_migration+0x82/0x150 [ 509.172194] ? sev_lock_vcpus_for_migration+0x82/0x150 [ 509.177335] ? rcu_lock_release+0x17/0x20 [ 509.181348] mutex_lock_killable_nested+0x20/0x30 [ 509.186053] sev_lock_vcpus_for_migration+0x82/0x150 [ 509.191019] sev_vm_move_enc_context_from+0x190/0x750 [ 509.196072] ? lock_release+0x20e/0x290 [ 509.199912] kvm_vm_ioctl_enable_cap+0x29d/0x320 [ 509.204531] kvm_vm_ioctl+0xc58/0x1060 [ 509.208285] ? __this_cpu_preempt_check+0x13/0x20 [ 509.212989] __se_sys_ioctl+0x77/0xc0 [ 509.216656] __x64_sys_ioctl+0x1d/0x20 [ 509.220408] do_syscall_64+0x44/0xa0 [ 509.223987] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 509.229041] RIP: 0033:0x7f91b8531347 [ 509.232618] Code: 5d c3 cc 48 8b 05 f9 2f 07 00 64 c7 00 26 00 00 00 48 c7 c0 ff ff ff ff c3 cc cc cc cc cc cc cc cc cc cc b8 10 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d c9 2f 07 00 f7 d8 64 89 01 48 [ 509.251371] RSP: 002b:00007ffef7feb778 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 [ 509.258940] RAX: ffffffffffffffda RBX: 0000000000af6210 RCX: 00007f91b8531347 [ 509.266073] RDX: 00007ffef7feb790 RSI: 000000004068aea3 RDI: 0000000000000018 [ 509.273207] RBP: 00007ffef7feba10 R08: 000000000020331b R09: 000000000000000f [ 509.280338] R10: 0000000000000001 R11: 0000000000000246 R12: 0000000000af8df0 [ 509.287470] R13: 0000000000afa3e0 R14: 0000000000000000 R15: 0000000000af7800 [ 509.294607] > > > > Warning example: > > ============================================ > > WARNING: possible recursive locking detected > > 5.17.0-dbg-DEV #15 Tainted: G O > > -------------------------------------------- > > sev_migrate_tes/18859 is trying to acquire lock: > > ffff8d672d484238 (&vcpu->mutex){+.+.}-{3:3}, at: sev_lock_vcpus_for_migration+0x7e/0x150 > > but task is already holding lock: > > ffff8d67703f81f8 (&vcpu->mutex){+.+.}-{3:3}, at: sev_lock_vcpus_for_migration+0x7e/0x150 > > other info that might help us debug this: > > Possible unsafe locking scenario: > > CPU0 > > ---- > > lock(&vcpu->mutex); > > lock(&vcpu->mutex); > > *** DEADLOCK *** > > May be due to missing lock nesting notation > > 3 locks held by sev_migrate_tes/18859: > > #0: ffff9302f91323b8 (&kvm->lock){+.+.}-{3:3}, at: sev_vm_move_enc_context_from+0x96/0x740 > > #1: ffff9302f906a3b8 (&kvm->lock/1){+.+.}-{3:3}, at: sev_vm_move_enc_context_from+0xae/0x740 > > #2: ffff8d67703f81f8 (&vcpu->mutex){+.+.}-{3:3}, at: sev_lock_vcpus_for_migration+0x7e/0x150 > > > > Fixes: b56639318bb2b ("KVM: SEV: Add support for SEV intra host migration") > > Reported-by: John Sperbeck > > Suggested-by: David Rientjes > > Suggested-by: Sean Christopherson > > Cc: Paolo Bonzini > > Cc: kvm@vger.kernel.org > > Cc: linux-kernel@vger.kernel.org > > Signed-off-by: Peter Gonda > > > > --- > > > > V3 > > * Updated signature to enum to self-document argument. > > * Updated comment as Seanjc@ suggested. > > > > 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 | 22 +++++++++++++++++----- > > 1 file changed, 17 insertions(+), 5 deletions(-) > > > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > > index 75fa6dd268f0..f66550ec8eaf 100644 > > --- a/arch/x86/kvm/svm/sev.c > > +++ b/arch/x86/kvm/svm/sev.c > > @@ -1591,14 +1591,26 @@ static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) > > atomic_set_release(&src_sev->migration_in_progress, 0); > > } > > > > +/* > > + * To suppress lockdep false positives, subclass all vCPU mutex locks by > > + * assigning even numbers to the source vCPUs and odd numbers to destination > > + * vCPUs based on the vCPU's index. > > + */ > > +enum sev_migration_role { > > + SEV_MIGRATION_SOURCE = 0, > > + SEV_MIGRATION_TARGET, > > + SEV_NR_MIGRATION_ROLES, > > +}; > > > > -static int sev_lock_vcpus_for_migration(struct kvm *kvm) > > +static int sev_lock_vcpus_for_migration(struct kvm *kvm, > > + enum sev_migration_role role) > > { > > struct kvm_vcpu *vcpu; > > unsigned long i, j; > > > > - kvm_for_each_vcpu(i, vcpu, kvm) { > > - if (mutex_lock_killable(&vcpu->mutex)) > > + kvm_for_each_vcpu(i, vcpu, kvm) { > > + if (mutex_lock_killable_nested( > > + &vcpu->mutex, i * SEV_NR_MIGRATION_ROLES + role)) > > goto out_unlock; > > } > > > > @@ -1745,10 +1757,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, SEV_MIGRATION_SOURCE); > > if (ret) > > goto out_dst_cgroup; > > - ret = sev_lock_vcpus_for_migration(source_kvm); > > + ret = sev_lock_vcpus_for_migration(source_kvm, SEV_MIGRATION_TARGET); > > if (ret) > > goto out_dst_vcpu; > > > > -- > > 2.35.1.1178.g4f1659d476-goog > > > > Does sev_migrate_tests survive lockdep checking if > NR_MIGRATE_TEST_VCPUS is changed to 16?