Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp2833866rwb; Fri, 20 Jan 2023 07:58:16 -0800 (PST) X-Google-Smtp-Source: AMrXdXs0qeQwBnmhjTByhYQftWj4N/uzcG8gUr9swy+1cDHJIdM/ObbbNbI0SLQ6fcCNmNQgiOrq X-Received: by 2002:aa7:c252:0:b0:49e:6a6c:cf50 with SMTP id y18-20020aa7c252000000b0049e6a6ccf50mr6662710edo.23.1674230296562; Fri, 20 Jan 2023 07:58:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674230296; cv=none; d=google.com; s=arc-20160816; b=TaCS/yptgkgXuIxCP0q3rlhgQrQzIQdeqLx3+sbPrsgsFqp/yVKiW5QR/iY8I9p817 2cd2IJZYAJwpiQQrGcOKDPNcrPWXEGPKuJJzJMta2u2PJ6Jsp0V/N+zmzp+qDhzhbd+E N2Cc9IEqSy13N1pN2JHKUFEuD/4et16MV32Rgj+Y6yQk1n4M20Q9PWa3PnntJV4cBNtp jSb5OwSO0/si+c5YeCZKfXEjo6GRJA6oNcDe0hsP7zF79SaoZI+ZgjqZTa0GmDiPmbDz J1YAnkHu5ozVDAL0anUB2I231DQRLmxmbmioChbJODlCHkq0jRqW7XGwlO2ylgktEp3z GanA== 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=aavit6jM1lC6qQPOxTCnu44ZFH15mB1QS8MkjGvi8Yk=; b=C38okt1X9EPCdY2vWW+GgsKRq+P0w/mEY4Kyxyv8NY43FfjED2xwBs9Y4mdX07U68c frAt/NgyfHFkt46oN4l3rbLybXx2B4ImRfFKxVpGH1tqLohQBD4RClsTsoO58GGKMHaC wpq2biPGAZzLjhGZncCPdJJYqYPSohDs9tKyDV08WETgf5SKknsz0QgMu8L0Eobi8vGD ZvPVZJW439W731Zygz4dT9fB14EK3L/t21xORwrJHy87Hb1tuIi5re/cPnY03R0l0tSE pBsZlZo5EABQmlh+TrMIFtzQAavzk6HwHOm0cW+0RNT9EOZ9KTzCbiexBhl5TZ3QA0g0 asHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FzioGuLf; 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 i6-20020a508706000000b0048ed1688e26si12023581edb.174.2023.01.20.07.58.03; Fri, 20 Jan 2023 07:58:16 -0800 (PST) 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=FzioGuLf; 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 S231537AbjATPua (ORCPT + 50 others); Fri, 20 Jan 2023 10:50:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231522AbjATPu2 (ORCPT ); Fri, 20 Jan 2023 10:50:28 -0500 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 BA929975A for ; Fri, 20 Jan 2023 07:49:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674229778; 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=aavit6jM1lC6qQPOxTCnu44ZFH15mB1QS8MkjGvi8Yk=; b=FzioGuLfDcqO84fcjy1n7N2RheG21iX4NV3oHFLpPp5IiDMEInlYJNZqnxp7rGe+JQ7UsG WUyBkWrhzKkcjgYpZ/W6133zw8dQstnne5jndqh155yUuXynB5akamZeiPL4YKsWVBOYW3 3qNvoswjvaLstV3JuaJ+a3IO4V38NMU= Received: from mail-io1-f71.google.com (mail-io1-f71.google.com [209.85.166.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-619-Yj8CTgtlMP67qm46Ef-6tg-1; Fri, 20 Jan 2023 10:49:37 -0500 X-MC-Unique: Yj8CTgtlMP67qm46Ef-6tg-1 Received: by mail-io1-f71.google.com with SMTP id d8-20020a6bcd08000000b00707712b91f9so2951099iog.3 for ; Fri, 20 Jan 2023 07:49:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=aavit6jM1lC6qQPOxTCnu44ZFH15mB1QS8MkjGvi8Yk=; b=QP4vx5rMZXqdV4DRNUmcKQ9TpM7CG+spP0FFcEh3Ha9NMOaeJVRaUZncPBZ52C9uU6 FehHVr3KQ4ssoAyO9CACb5iZHgWxxUWSZlSPAP4tfpJtYjP7tSDCqQlpzdBehyElJaUv qPO7BXRFwrVbyD884YkY34KBj5ofkK2x6+OtagaNIIrHqUj7zifzd2JDqO1HOJUFPQlf 6iOVcvsS21Aryj5Sfry6vj4zXf5zcYVucpCPnrfKe4GE0IwTzVkShubmPp2LivCO/8ts LBrf3pkh+SK1galtD3CfaXJkruGpnHheTklBPEDETvrBl8kX8WsbGetoakFdayzXIEV4 XDQg== X-Gm-Message-State: AFqh2kq7ath6HMF9XLlTQ4DgY4JLqu8h53gzuijbCbrbxB8EaFZKjiql PJB1HEU85OoOggVdLZqPwrdgBJJGp5raPKmC4v1/vzkl7sMY0MeWcnBPTXY0TpTlYWSPo9B7s/v oYYrz+pbK9/NYXfdKEEBet4Ji X-Received: by 2002:a05:6e02:cc6:b0:30f:3dfe:c670 with SMTP id c6-20020a056e020cc600b0030f3dfec670mr4626060ilj.25.1674229776952; Fri, 20 Jan 2023 07:49:36 -0800 (PST) X-Received: by 2002:a05:6e02:cc6:b0:30f:3dfe:c670 with SMTP id c6-20020a056e020cc600b0030f3dfec670mr4626033ilj.25.1674229776663; Fri, 20 Jan 2023 07:49:36 -0800 (PST) Received: from redhat.com ([38.15.36.239]) by smtp.gmail.com with ESMTPSA id z18-20020a92d192000000b0030c0217dde6sm851618ilz.0.2023.01.20.07.49.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jan 2023 07:49:36 -0800 (PST) Date: Fri, 20 Jan 2023 08:49:33 -0700 From: Alex Williamson To: Matthew Rosato Cc: "Liu, Yi L" , "pbonzini@redhat.com" , "jgg@nvidia.com" , "Tian, Kevin" , "cohuck@redhat.com" , "farman@linux.ibm.com" , "pmorel@linux.ibm.com" , "borntraeger@linux.ibm.com" , "frankja@linux.ibm.com" , "imbrenda@linux.ibm.com" , "david@redhat.com" , "akrowiak@linux.ibm.com" , "jjherne@linux.ibm.com" , "pasic@linux.ibm.com" , "zhenyuw@linux.intel.com" , "Wang, Zhi A" , "Christopherson, , Sean" , "linux-s390@vger.kernel.org" , "kvm@vger.kernel.org" , "intel-gvt-dev@lists.freedesktop.org" , "intel-gfx@lists.freedesktop.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] kvm/vfio: Fix potential deadlock on vfio group_lock Message-ID: <20230120084933.40f59c89.alex.williamson@redhat.com> In-Reply-To: <6eff63d8-d825-aecd-12b5-e8dbf55f4372@linux.ibm.com> References: <20230120150528.471752-1-yi.l.liu@intel.com> <6eff63d8-d825-aecd-12b5-e8dbf55f4372@linux.ibm.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_H2,SPF_HELO_NONE,SPF_NONE 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 Fri, 20 Jan 2023 10:45:40 -0500 Matthew Rosato wrote: > On 1/20/23 10:08 AM, Liu, Yi L wrote: > >> From: Liu, Yi L > >> Sent: Friday, January 20, 2023 11:05 PM > >> > >> Currently it is possible that the final put of a KVM reference comes from > >> vfio during its device close operation. This occurs while the vfio group > >> lock is held; however, if the vfio device is still in the kvm device list, > >> then the following call chain could result in a deadlock: > >> > >> VFIO holds group->group_lock/group_rwsem > >> -> kvm_put_kvm > >> -> kvm_destroy_vm > >> -> kvm_destroy_devices > >> -> kvm_vfio_destroy > >> -> kvm_vfio_file_set_kvm > >> -> vfio_file_set_kvm > >> -> try to hold group->group_lock/group_rwsem > >> > >> The key function is the kvm_destroy_devices() which triggers destroy cb > >> of kvm_device_ops. It calls back to vfio and try to hold group_lock. So > >> if this path doesn't call back to vfio, this dead lock would be fixed. > >> Actually, there is a way for it. KVM provides another point to free the > >> kvm-vfio device which is the point when the device file descriptor is > >> closed. This can be achieved by providing the release cb instead of the > >> destroy cb. Also rename kvm_vfio_destroy() to be kvm_vfio_release(). > >> > >> /* > >> * Destroy is responsible for freeing dev. > >> * > >> * Destroy may be called before or after destructors are called > >> * on emulated I/O regions, depending on whether a reference is > >> * held by a vcpu or other kvm component that gets destroyed > >> * after the emulated I/O. > >> */ > >> void (*destroy)(struct kvm_device *dev); > >> > >> /* > >> * Release is an alternative method to free the device. It is > >> * called when the device file descriptor is closed. Once > >> * release is called, the destroy method will not be called > >> * anymore as the device is removed from the device list of > >> * the VM. kvm->lock is held. > >> */ > >> void (*release)(struct kvm_device *dev); > >> > >> Fixes: 421cfe6596f6 ("vfio: remove VFIO_GROUP_NOTIFY_SET_KVM") > >> Reported-by: Alex Williamson > >> Suggested-by: Kevin Tian > >> Reviewed-by: Jason Gunthorpe > >> Signed-off-by: Yi Liu > > > > More background can be found in Mathew's work. > > https://lore.kernel.org/kvm/20230114000351.115444-1-mjrosato@linux.ibm.com/T/#u > > > > Thanks Yi. > > Reviewed-by: Matthew Rosato > > One small nit: There is a comment at the very end of > kvm_vfio_release on the kfree(dev) that still references .destroy, > this should be updated to .release I've fixed this locally, s/destroy/release/ in that comment. Thanks, Alex