Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1382899rwb; Wed, 26 Jul 2023 11:35:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlF4dFwHXzaZTZ5kqHeGnzZhiIVwCVQhMg8YLILPEfvd65IYZ2rOtEyNYkT/GzWQ5QTnUNqH X-Received: by 2002:aa7:d7c3:0:b0:522:2a3c:6eb with SMTP id e3-20020aa7d7c3000000b005222a3c06ebmr1867077eds.39.1690396511718; Wed, 26 Jul 2023 11:35:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690396511; cv=none; d=google.com; s=arc-20160816; b=0e0SfYO2uUTaiFW0A/f6jpjo67Gj7pBpoJy1lCqCW/U2tCMSqpY8IxbJ48w7jzJdjZ iVWff3G3Qgz9sOjsU4GjMh9W3zL/FioRrxa/kQLIJVy/hXbfWXGopZcCNadszaCR2yO/ pIcBFsnHn5cSNQF4+ogxVkVmGrLbTo7WAk8lR8787KJ8AzeKyR+H1qWO9f/y1UzY87R/ Uwe4kTWPzpEpUBIJt1Ms0oixtJCRRmwAmmkPTHNaSm/ZOahZt1eEtg4GXcBns6kvZiPS CHWcfiV6oSeRWgLUQtZHlAgIADgA6wqaLI86iGuZSSlwInQdD7Qj+Fe3g12RjVBViBnj aTVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=eGukCLwIpbf54ieqMBZB+shCMT6w9p/EZv5cgNQmWkc=; fh=XkzGMLt41z4/EmtK+nHc+uQE2BDAzOw4iEEEbAEffOU=; b=K6c9S8xguZNErghYW6M/ZaPv+mKysBG09jKPtTeK9J9PtlAd5UqiV+VXdemzwRHFsn a3d1m2diV7NZHbFHi8IWoSqiSUeleIm4vTgj1HVPlzKJ2PRSSaqllNFd+rtTECzqfsUM X7vvNHUSI+s83EYAj/RpFECAN5q2Iw0usu4+3S25vGCC3cUt2uofEbtuawYi6QsVcY9U 9LU64t2i+34TFTBhFZBcJLTI+nmneSDCMgasMg328puQlQn5/cGe4cWOhd4LCr/twhzk SDlC0JUdJYnr2+1WXKTvSAXmAZn9I/KQShUUDIY3sTTBPBszHgjHZjwsPCcqAwK9YBRl dnSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Y2MP3vGP; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l11-20020aa7d94b000000b005223ebc9b97si3515861eds.131.2023.07.26.11.34.46; Wed, 26 Jul 2023 11:35:11 -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=@redhat.com header.s=mimecast20190719 header.b=Y2MP3vGP; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230207AbjGZSMX (ORCPT + 99 others); Wed, 26 Jul 2023 14:12:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230192AbjGZSMW (ORCPT ); Wed, 26 Jul 2023 14:12:22 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 555E2212F for ; Wed, 26 Jul 2023 11:11:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690395094; 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=eGukCLwIpbf54ieqMBZB+shCMT6w9p/EZv5cgNQmWkc=; b=Y2MP3vGPnEdiZ/Xer1W3dYzpOJtXzBmYwo+Cllji3GIYbVjIgfP1+M2Yop0hlY/83TjQfd phjswfdrIKbn+Rp9o7KpDbjbbxfiTh13Lh6sI7ASJvds14i1dTOmV/pjIXJs30cCMDNMDg p/aF8FffRPAcGWySPqzYCkfhGZjRuXU= Received: from mail-io1-f72.google.com (mail-io1-f72.google.com [209.85.166.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-657-FmN3Ca8_OPm68sa_eBF-Fw-1; Wed, 26 Jul 2023 14:11:33 -0400 X-MC-Unique: FmN3Ca8_OPm68sa_eBF-Fw-1 Received: by mail-io1-f72.google.com with SMTP id ca18e2360f4ac-786f4056ac0so3550039f.1 for ; Wed, 26 Jul 2023 11:11:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690395092; x=1690999892; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=eGukCLwIpbf54ieqMBZB+shCMT6w9p/EZv5cgNQmWkc=; b=BO0V5IKEsiz+FDxhI9liGnZewt36ixxdZ75yPmwCliO7ldt057LePVXzXTElzFc6/N BQ8ndV0IK4GH4bA+x8qBHxYW64FsJJvpA/b8GoXbwbw15EKnWJgcjK9/vREE94KSTQL+ 5o4nhCY0+pP1WezyZCPuLNqUJLBg7nrU3tXY/ki2SiUJ0VmtnhXOfJXJYJls4j6TZ8Fn 59VavyabuL44/DAB3miyfVtXi3bfrVjecq+UtIq0HLxnad0X2W/29DHYzwxkGI9vD3r4 cojKAcpeXmQO8DWtVrTvu8RZb/pbKhL3p/fjId3th3gX7Sl2RiXnIaYnH7arBEWI/EZD JA7A== X-Gm-Message-State: ABy/qLZkBHT+x8AxNrhLhFsUDfXhYvQ/JrIbQMNKgzaifIL53zumnP2Z 0GETufE+wvxdwuOd/t1PBSgfBcra+fP5JHv0wKSOaJw4LQXvfcBwugBYo7LQnuoD+QBqUR3TCKt Ey5TqfmcjQ7SlWCfDBnkSjePG X-Received: by 2002:a5e:860e:0:b0:786:25a3:ef30 with SMTP id z14-20020a5e860e000000b0078625a3ef30mr2821757ioj.7.1690395092345; Wed, 26 Jul 2023 11:11:32 -0700 (PDT) X-Received: by 2002:a5e:860e:0:b0:786:25a3:ef30 with SMTP id z14-20020a5e860e000000b0078625a3ef30mr2821733ioj.7.1690395092104; Wed, 26 Jul 2023 11:11:32 -0700 (PDT) Received: from redhat.com ([38.15.60.12]) by smtp.gmail.com with ESMTPSA id z25-20020a02ceb9000000b0042b3e86dfd8sm4452392jaq.141.2023.07.26.11.11.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jul 2023 11:11:31 -0700 (PDT) Date: Wed, 26 Jul 2023 12:11:30 -0600 From: Alex Williamson To: Paolo Bonzini Cc: Dmitry Torokhov , Greg KH , Sean Christopherson , Roxana Bradescu , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 1/2] kvm/vfio: ensure kvg instance stays around in kvm_vfio_group_add() Message-ID: <20230726121130.7117a238.alex.williamson@redhat.com> In-Reply-To: <20230714224538.404793-1-dmitry.torokhov@gmail.com> References: <20230714224538.404793-1-dmitry.torokhov@gmail.com> Organization: Red Hat MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 Hey Paolo, I'll pull this through the vfio tree unless you have a particular interest. Thanks, Alex On Fri, 14 Jul 2023 15:45:32 -0700 Dmitry Torokhov wrote: > kvm_vfio_group_add() creates kvg instance, links it to kv->group_list, > and calls kvm_vfio_file_set_kvm() with kvg->file as an argument after > dropping kv->lock. If we race group addition and deletion calls, kvg > instance may get freed by the time we get around to calling > kvm_vfio_file_set_kvm(). > > Previous iterations of the code did not reference kvg->file outside of > the critical section, but used a temporary variable. Still, they had > similar problem of the file reference being owned by kvg structure and > potential for kvm_vfio_group_del() dropping it before > kvm_vfio_group_add() had a chance to complete. > > Fix this by moving call to kvm_vfio_file_set_kvm() under the protection > of kv->lock. We already call it while holding the same lock when vfio > group is being deleted, so it should be safe here as well. > > Fixes: 2fc1bec15883 ("kvm: set/clear kvm to/from vfio_group when group add/delete") > Reviewed-by: Alex Williamson > Signed-off-by: Dmitry Torokhov > --- > > v3: added Alex's reviewed-by > > v2: updated commit description with the correct "Fixes" tag (per Alex), > expanded commit description to mention issues with the earlier > implementation of kvm_vfio_group_add(). > > virt/kvm/vfio.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c > index 9584eb57e0ed..cd46d7ef98d6 100644 > --- a/virt/kvm/vfio.c > +++ b/virt/kvm/vfio.c > @@ -179,10 +179,10 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) > list_add_tail(&kvg->node, &kv->group_list); > > kvm_arch_start_assignment(dev->kvm); > + kvm_vfio_file_set_kvm(kvg->file, dev->kvm); > > mutex_unlock(&kv->lock); > > - kvm_vfio_file_set_kvm(kvg->file, dev->kvm); > kvm_vfio_update_coherency(dev); > > return 0;