Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3674451iob; Mon, 2 May 2022 02:40:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzc4WzvmRwpGj0/mqcSf7gQ3rGGEDGEkJQ6MEuTbcuobXAzstC53J1Gq+VYelL4NmrDzKxu X-Received: by 2002:a05:6512:2813:b0:473:a51f:4c48 with SMTP id cf19-20020a056512281300b00473a51f4c48mr603962lfb.198.1651484433747; Mon, 02 May 2022 02:40:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651484433; cv=none; d=google.com; s=arc-20160816; b=by5jUC1a7Fr/YLWeqIk2MiGrn7ERASkXvLTVDl8wn80HuUYM3321xOwShPdqmEKVPC HNhUi8LpHhqOsrVH26HE3X907qfsvDFy/xB6j33OPedKmLbITQuWwDbYxDHUlxnyVSWh RrCQDCNsK30RJCBTAcCDOFs4A7acZCuZAT57UCbGx0xhjwCHgcnhJhLo0rby4TTj0dUN p8WZAXTfvuTfQT769PUTECuCDbf7f8Nmu5RrGbYI7R6uDzgN4ljw5OCYrcLcuKVAnsoz k/ytXVlZmYKe8WqJl2ygBRT13b+ZRyvi4d4AhEE3SuP9f4qK6Zc4aZXfO/VEWOyVKsRi vU/A== 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=/dhlPgQ9KVUBw4oSLwyQU5Spph3PG9xofANcDFrXo7E=; b=mz5BwX/YKivjGe3P8d3+JIRnupok413TG8YG2GIviPZYkXwcJOPgt/o/XJTqQ+fQjy Q6CVQ6RKKfHxa8w4fZD0PRAhPbxqQjmKLFvjw7JjpfgVNy3EZXgrA8NBtjkls2NLCqIJ kjKAhbWaltyT4egLIGcp74Foktv/6la9Wvc1jSdFZKdwUuKXTu0KPVnqCFrZ786bWlxG wv5v2H+7Wp3DHtMBJykV4iJsqVF1m1LmXATxJ2XMKNfbDDI2UsrK7l5D1NJ3nxWvVrSW MjlB7ZHbaN6q8GO21D3+0E8uKperC063EPl5lYMe5ygYsz+tANBLEsxMFsmzUAZ+JmKu z11Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=RoLY63DF; 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 u14-20020a2e91ce000000b0024f296dfad9si14828936ljg.369.2022.05.02.02.40.06; Mon, 02 May 2022 02:40:33 -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=RoLY63DF; 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 S1379440AbiD2RQL (ORCPT + 99 others); Fri, 29 Apr 2022 13:16:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379435AbiD2RQJ (ORCPT ); Fri, 29 Apr 2022 13:16:09 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC2D1532F0 for ; Fri, 29 Apr 2022 10:12:50 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id 17so11289620lji.1 for ; Fri, 29 Apr 2022 10:12:50 -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=/dhlPgQ9KVUBw4oSLwyQU5Spph3PG9xofANcDFrXo7E=; b=RoLY63DFknlPSUqnhr2f6i4XvnblXUfEvWc48Qfp47ytOxUYDfq5x6v8xwa0da9phh lVJw8ZhSf1guh83THMDW91u57kU8MiNVUsKbCrLsKi2tM/+FCLYD9t2keBNidVSJq3+4 387quv6oqOKeYzv4bWrGPnMxohnfxaQUsEUgh3R1pGB94pmxr8JLb+0xZyuLXiG3RI4O z/5Y3yDZl91fkqmFymD1km9dlgUE+6yrGUPmsqHlrYR0pIUjKPQ5ybZvUYS+rGeVr+Xa fL+YKJ/ZnMVQQ0pt9pr2Z2cnxjdguNWtHhaJVmhNLmD3rx3DeyrC9blAVTCT+V9xtggH J2rw== 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=/dhlPgQ9KVUBw4oSLwyQU5Spph3PG9xofANcDFrXo7E=; b=mXASd+nIi6U4hr6eLv71ZpF1AWK5Ulw5BAgasuUi9B5vXyNdwzNxQzMDfSEZw5NvBS ztz19gCpueL1k6h8itBY5Y4gPNwZDLEH/qznAc56zM7veKmHm9HLsRI+q6asDHJLcI+H edrNQxxRb6WkeeZZX8nvRPHrHS5H2pfm93I1Z4KWxODu9zOikBN/DIMHdATHj8l1IFEM 3FGh9+y1iwRqsR6vxHv0nr+ZVZqpFU/NaEgMolXpX6wy5H66/GOmos3fWg/utqfYhY55 QFSZOxH3NB/k30HnPWlsKtiLpMSpJmWgdoQPKS69ekr7wiTvutnMik/ZQhghlsSkaaYr qcsg== X-Gm-Message-State: AOAM530Fc7S6nk3pE66iAlKD0CEfCLHZtB8biXQBWGMNoZUIDJVIxev6 rIDYQmoQ+ff6qkmwR1muDRWHLZlPZXRd/kaURLbLpA== X-Received: by 2002:a2e:3c0e:0:b0:24f:25ff:659f with SMTP id j14-20020a2e3c0e000000b0024f25ff659fmr140994lja.426.1651252368836; Fri, 29 Apr 2022 10:12:48 -0700 (PDT) MIME-Version: 1.0 References: <20220407195908.633003-1-pgonda@google.com> <62e9ece1-5d71-f803-3f65-2755160cf1d1@redhat.com> <4c0edc90-36a1-4f4c-1923-4b20e7bdbb4c@redhat.com> <0d282be4-d612-374d-84ba-067994321bab@redhat.com> <8a2c5f8c-503c-b4f0-75e7-039533c9852d@redhat.com> <4afce434-ab25-66d6-76f4-3a987f64e88e@redhat.com> In-Reply-To: <4afce434-ab25-66d6-76f4-3a987f64e88e@redhat.com> From: Peter Gonda Date: Fri, 29 Apr 2022 11:12:36 -0600 Message-ID: Subject: Re: [PATCH v3] KVM: SEV: Mark nested locking of vcpu->lock To: Paolo Bonzini Cc: John Sperbeck , kvm list , David Rientjes , Sean Christopherson , 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, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 Fri, Apr 29, 2022 at 9:59 AM Paolo Bonzini wrote: > > On 4/29/22 17:51, Peter Gonda wrote: > >> No, you don't need any of this. You can rely on there being only one > >> depmap, otherwise you wouldn't need the mock releases and acquires at > >> all. Also the unlocking order does not matter for deadlocks, only the > >> locking order does. You're overdoing it. :) > > > > Hmm I'm slightly confused here then. If I take your original suggestion of: > > > > bool acquired = false; > > kvm_for_each_vcpu(...) { > > if (acquired) > > mutex_release(&vcpu->mutex.dep_map, > > _THIS_IP_); <-- Warning here > > if (mutex_lock_killable_nested(&vcpu->mutex, role) > > goto out_unlock; > > acquired = true; > > > > """ > > [ 2810.088982] ===================================== > > [ 2810.093687] WARNING: bad unlock balance detected! > > [ 2810.098388] 5.17.0-dbg-DEV #5 Tainted: G O > > [ 2810.103788] ------------------------------------- > > Ah even if the contents of the dep_map are the same for all locks, it > also uses the *pointer* to the dep_map to track (class, subclass) -> > pointer and checks for a match. > > So yeah, prev_cpu is needed. The unlock ordering OTOH is irrelevant so > you don't need to visit the xarray backwards. Sounds good. Instead of doing this prev_vcpu solution we could just keep the 1st vcpu for source and target. I think this could work since all the vcpu->mutex.dep_maps do not point to the same string. Lock: bool acquired = false; kvm_for_each_vcpu(...) { if (mutex_lock_killable_nested(&vcpu->mutex, role) goto out_unlock; acquired = true; if (acquired) mutex_release(&vcpu->mutex, role) } Unlock bool acquired = true; kvm_for_each_vcpu(...) { if (!acquired) mutex_acquire(&vcpu->mutex, role) mutex_unlock(&vcpu->mutex); acquired = false; } So when locking we release all but the first dep_maps. Then when unlocking we acquire all but the first dep_maps. Thoughts? > > Paolo >