Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1679881ioo; Fri, 27 May 2022 14:11:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyXqrAD/wO+6BP9LfIPNzX9+nrjC7fTiWRgwXTiasbEMSXswou2pSuijnkvc6x+8AsrkYgg X-Received: by 2002:a17:907:7f26:b0:6ff:ab8:e85 with SMTP id qf38-20020a1709077f2600b006ff0ab80e85mr16244301ejc.281.1653685867711; Fri, 27 May 2022 14:11:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653685867; cv=none; d=google.com; s=arc-20160816; b=huJwcRkNoy+8+qN1l++EbP8hNArMsJC0xVhwoLCKOei9m+UNOULsmcCkB6KNHaogE3 Gim5d+n2pYBPPyqBjN1CTumZB7J9E1vxMJE7cts81LAJ2gyJaZ20IoifFHmNtM1j/mOc H96dtwOdyGWI4ZwmNYjWWhPmir+vv9mlSxazjwmA75dRxS6ev0XRn02eHyH3XHCgPsO0 OYR5f6r06q2IYdIFhvjOpmKNwp3Am0f89I/DpZcZuFqLcs38UwONPtp23QfGRNoAcAW/ faSdZdl0edfs4acUN0bxMFQ5zw7IZrnJ+5K/MsCGs87wWGyKeu+Mgnk7e3WSJSPe+aLk 88nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :reply-to:user-agent:mime-version:date:message-id:dkim-signature; bh=x7+rNrqppJOmcQuwjq3loMO1MRTA5ucu5Lqod708gUo=; b=JCTQ/PlwSV6x2hj/Znh1/LYpEJFc9+BT5Vh9bpDvZsXBl4jkZR7HQTfS9jEs840XrR d1LPl7/2mdQnAu8FJWWvlydmiD2WiKSN0sozeA0mBsDTnEuC2viW/2DqJBHW59MDqcyy rLIhFvS0mviimgO8whDwfT0F8eNQf9ZMPI/KgK+UIBt+bn5bt8pS1zEMr1nl5+xymb9Y cyrnBAUrPTC00VAEXCsmk0HyleCE7JVCPfEgwhDdSZjsLk0tj/GLQ9PDC1DqI8HOiNXQ OAfqEIBzPQSyNhOkrYGf0TIm926CMP08LKMP5fqq0FSFB7KNc6yd2zfPxCetvdwlxTUB +k0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=lsvVzIcP; 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=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q14-20020a056402518e00b0042afe8df656si1001440edd.248.2022.05.27.14.10.41; Fri, 27 May 2022 14:11:07 -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=@ibm.com header.s=pp1 header.b=lsvVzIcP; 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=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352591AbiE0Mnh (ORCPT + 99 others); Fri, 27 May 2022 08:43:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352898AbiE0MnZ (ORCPT ); Fri, 27 May 2022 08:43:25 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EACC17E23; Fri, 27 May 2022 05:40:41 -0700 (PDT) Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24RCHxZu017194; Fri, 27 May 2022 12:40:38 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : reply-to : subject : to : cc : references : from : in-reply-to : content-type : content-transfer-encoding; s=pp1; bh=x7+rNrqppJOmcQuwjq3loMO1MRTA5ucu5Lqod708gUo=; b=lsvVzIcPdgJQPfsL+/VxgfBWqTuh747Pd54pn/r+oDTEmkhzeRVrviSGAquNBg80ZPDa HaLx8h0gvLtC4Sap/6QWkjXpjdkUYpGPks5nbDK+vb/5MiXzWkWVXgfa2ewuWF78wOZK 0G+OMF08lMPHm0Cj8PTfn+dwCiJ1f6y4k5ZyFdwfJVmJr7OSYceTVdOuePToTL5O3PRu lf00eYT5Phr71aByrRVy87oJ9OnIeXWDX511UQVDetsxw9Tp7HAIYHjSFKFXwbEzTlDR i5G/woqCtSrn50QwfCx9KIQn/gKskCBsG5/esZr7vN+0TiCmZKpUyYXB0nUCYOXD+X46 Kw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3gaxhcgd4c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 27 May 2022 12:40:38 +0000 Received: from m0098414.ppops.net (m0098414.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 24RCKUZQ021905; Fri, 27 May 2022 12:40:38 GMT Received: from ppma05wdc.us.ibm.com (1b.90.2fa9.ip4.static.sl-reverse.com [169.47.144.27]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3gaxhcgd45-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 27 May 2022 12:40:38 +0000 Received: from pps.filterd (ppma05wdc.us.ibm.com [127.0.0.1]) by ppma05wdc.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 24RCbGIX017016; Fri, 27 May 2022 12:40:37 GMT Received: from b03cxnp08026.gho.boulder.ibm.com (b03cxnp08026.gho.boulder.ibm.com [9.17.130.18]) by ppma05wdc.us.ibm.com with ESMTP id 3g93vbvbb9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 27 May 2022 12:40:37 +0000 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 24RCeasD31392198 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 27 May 2022 12:40:36 GMT Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6481B78069; Fri, 27 May 2022 12:40:36 +0000 (GMT) Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A19F678060; Fri, 27 May 2022 12:40:35 +0000 (GMT) Received: from [9.60.75.219] (unknown [9.60.75.219]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP; Fri, 27 May 2022 12:40:35 +0000 (GMT) Message-ID: <2c2d7563-c614-e4f8-7826-73deba6d489b@linux.ibm.com> Date: Fri, 27 May 2022 08:40:35 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Reply-To: jjherne@linux.ibm.com Subject: Re: [PATCH v19 08/20] s390/vfio-ap: introduce new mutex to control access to the KVM pointer Content-Language: en-US To: Tony Krowiak , linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: freude@linux.ibm.com, borntraeger@de.ibm.com, cohuck@redhat.com, mjrosato@linux.ibm.com, pasic@linux.ibm.com, alex.williamson@redhat.com, kwankhede@nvidia.com, fiuczy@linux.ibm.com References: <20220404221039.1272245-1-akrowiak@linux.ibm.com> <20220404221039.1272245-9-akrowiak@linux.ibm.com> From: "Jason J. Herne" Organization: IBM In-Reply-To: <20220404221039.1272245-9-akrowiak@linux.ibm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: RsNefrVn2vSPdcq6izBpkQPhIg6e1QeG X-Proofpoint-ORIG-GUID: w-XTH8Nx3R0gcqG_z-anZ8MMPiKqoQLo X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-27_03,2022-05-27_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 lowpriorityscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 malwarescore=0 impostorscore=0 clxscore=1015 bulkscore=0 suspectscore=0 priorityscore=1501 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2205270059 X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 4/4/22 18:10, Tony Krowiak wrote: > The vfio_ap device driver registers for notification when the pointer to > the KVM object for a guest is set. Recall that the KVM lock (kvm->lock) > mutex must be taken outside of the matrix_dev->lock mutex to prevent the > reporting by lockdep of a circular locking dependency (a.k.a., a lockdep > splat): > > * see commit 0cc00c8d4050 ("Fix circular lockdep when setting/clearing > crypto masks") > > * see commit 86956e70761b ("replace open coded locks for > VFIO_GROUP_NOTIFY_SET_KVM notification") > > With the introduction of support for hot plugging/unplugging AP devices > passed through to a KVM guest, a new guests_lock mutex is introduced to > ensure the proper locking order is maintained: > > struct ap_matrix_dev { > ... > struct mutex guests_lock; > ... > } > > The matrix_dev->guests_lock controls access to the matrix_mdev instances > that hold the state for AP devices that have been passed through to a > KVM guest. This lock must be held to control access to the KVM pointer > (matrix_mdev->kvm) while the vfio_ap device driver is using it to > plug/unplug AP devices passed through to the KVM guest. > > Keep in mind, the proper locking order must be maintained whenever > dynamically updating a KVM guest's APCB to plug/unplug adapters, domains > and control domains: > > 1. matrix_dev->guests_lock: required to use the KVM pointer - stored in > a struct ap_matrix_mdev instance - to update a KVM guest's APCB > > 2. matrix_mdev->kvm->lock: required to update a guest's APCB > > 3. matrix_dev->mdevs_lock: required to access data stored in a > struct ap_matrix_mdev instance. > > Signed-off-by: Tony Krowiak > --- > drivers/s390/crypto/vfio_ap_drv.c | 1 + > drivers/s390/crypto/vfio_ap_private.h | 6 ++++++ > 2 files changed, 7 insertions(+) > > diff --git a/drivers/s390/crypto/vfio_ap_drv.c b/drivers/s390/crypto/vfio_ap_drv.c > index 0a5acd151a9b..c258e5f7fdfc 100644 > --- a/drivers/s390/crypto/vfio_ap_drv.c > +++ b/drivers/s390/crypto/vfio_ap_drv.c > @@ -161,6 +161,7 @@ static int vfio_ap_matrix_dev_create(void) > > mutex_init(&matrix_dev->mdevs_lock); > INIT_LIST_HEAD(&matrix_dev->mdev_list); > + mutex_init(&matrix_dev->guests_lock); > > dev_set_name(&matrix_dev->device, "%s", VFIO_AP_DEV_NAME); > matrix_dev->device.parent = root_device; > diff --git a/drivers/s390/crypto/vfio_ap_private.h b/drivers/s390/crypto/vfio_ap_private.h > index 5262e02192a4..ec926f2f2930 100644 > --- a/drivers/s390/crypto/vfio_ap_private.h > +++ b/drivers/s390/crypto/vfio_ap_private.h > @@ -39,6 +39,11 @@ > * single ap_matrix_mdev device. It's quite coarse but we don't > * expect much contention. > * @vfio_ap_drv: the vfio_ap device driver > + * @guests_lock: mutex for controlling access to a guest that is using AP > + * devices passed through by the vfio_ap device driver. This lock > + * will be taken when the AP devices are plugged into or unplugged > + * from a guest, and when an ap_matrix_mdev device is added to or > + * removed from @mdev_list or the list is iterated. > */ > struct ap_matrix_dev { > struct device device; > @@ -47,6 +52,7 @@ struct ap_matrix_dev { > struct list_head mdev_list; > struct mutex mdevs_lock; > struct ap_driver *vfio_ap_drv; > + struct mutex guests_lock; > }; > > extern struct ap_matrix_dev *matrix_dev; In isolation... Reviewed-by: Jason J. Herne -- -- Jason J. Herne (jjherne@linux.ibm.com)