Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2691655rdb; Mon, 5 Feb 2024 14:49:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IEr8sIdXovoAUFYcOVy15BmBvkOZOnJbBHak4uIyujAbBkssQl+CslcZgr/HbRTpDtoqEQl X-Received: by 2002:a05:6a00:80db:b0:6dd:d0cd:3e9d with SMTP id ei27-20020a056a0080db00b006ddd0cd3e9dmr917918pfb.10.1707173379488; Mon, 05 Feb 2024 14:49:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707173379; cv=pass; d=google.com; s=arc-20160816; b=RNyOsQKjNyprWHOQE7Wv7/JpPkiIw3tvoZF2it1l34Qt/VyScwhV3zseoOhgpWJpWg MB7cjQ0h7xAGNFxNjWJLg18A/1jpc2DBN4F66zR+f0chYxiWtFlnt6rz4CVBqG4Z7rUG XjxL2GfbFN+iGdDRCqdXnEyvygT4S96ZeApkKgXB6d+KgGGzuRhMWDHyXIqTkt++zfVl XmY4Ypi2HiZ5HwqvCyloSr0axo6zHP0gVMrbCI6ptjbC+RuodOlH/rFElsluGyvE2UaG THZzPZGsdhs98gQf+AXZAH1YJDvUNV5ZKlCpZ6ofNXcGoC/PWmTYIIIR5x3iuQQL/PR/ 8FWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=b/wNE6tBX1rJP2YgS+TTAYHWSHylnSq7QWSxIGxGiQU=; fh=m6+Z6e9dRXtsrxSEyNs16bV+ib49E/HMFA8QkfzZao8=; b=A9nfPFu5UrOA9JiC8lHtjCDJTIzin7u/jcNeurEoEO9iwHyXHlYKO9OPwIcMcfBXjP GM8lJ4bRe0ZjIM3f1pHUxGVsPOTA3ijkD9XmfM7v5/Smyf+P6NClma3Y4B7jvMdv7csj te99B02p2WNMOvsk0SarYSbBIDvhJ57Wfj9PvX0JSm2NWBsvuVejQH7UYizY1fMcuArX UgbpbMRJ98Jpe+S5/41gBlKnSDLnPKvh7PbkfTWfeucvlQ6UqG768rduQ1f6G4bCrTYs SRWFmyqy5MuvXp4LSao4H5Rhfd8809vmgNB6S4033eBZD0NBC7BBWF6D5HYktqZRCngX KvuQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WBzFp770; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-54024-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54024-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=1; AJvYcCUoHtZ+Phu1gMhw1FTozBBSdTwUKaSrG/qZfvebqlAiv5xGMTfDZNLrVFrVlgo7a4JV+SIi+Udtku7FNE3APDdiYhckTaX1f1dV1jAcgw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y188-20020a62cec5000000b006e0276672cbsi484212pfg.137.2024.02.05.14.49.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 14:49:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54024-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WBzFp770; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-54024-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54024-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 54F53292E0D for ; Mon, 5 Feb 2024 22:36:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 001A5446B7; Mon, 5 Feb 2024 22:36:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WBzFp770" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2EEF1EB3E for ; Mon, 5 Feb 2024 22:36:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707172578; cv=none; b=QW6IYF9U9S1ycXBuxqhgIZOr20iV8dlB0Ac0/grCifsiNyBfrN0jdlME/qhx5EMd8dcY8c24F3Hnitk5LM6M58AgoFHQr8I37Ee/aR9dSWQwBsItIM4XzsnOZW4G+rBOCzxtnrBjxzRShLduTeen83UtxhYjjsnNZ8QxB2ilImQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707172578; c=relaxed/simple; bh=YfcVwTdvsAFjLJ7bKvK6o7HK5jBfK7DuYGaXvtDcedI=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ufcREou4SXfpox2LYT1klACb0CYRGP/QQoH/exr3CAJs9tcMqu88ebBbgyvYWy2byvS4UpawvfonP67fvsQuI0i6+5Dgt40AqB8Nx/NRjy9+Ivf+kOg0XkHWVpB0IQkvnjiJqyigFHjI9gxYisZiQBK3JF9wUfwq6aZZGp4ZWg0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WBzFp770; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707172574; 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=b/wNE6tBX1rJP2YgS+TTAYHWSHylnSq7QWSxIGxGiQU=; b=WBzFp770OM+Vi3Fl+dvdDJbH5q3V1vk9t20460q51cY2Dx3NCpPemsxaVE1Zaqf40dlCuS Kxyn5r+bsVtKt1zZQll2eW3AM08BwqqR69F5BEzyEyju58viTQUWLCmBjpg2CuH5y1key8 M6/OzV+ZD9ENfHkHVVIeeRvDywc9iqA= Received: from mail-io1-f70.google.com (mail-io1-f70.google.com [209.85.166.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-380-RCkf8caxMjm3hM4pP2msrw-1; Mon, 05 Feb 2024 17:36:13 -0500 X-MC-Unique: RCkf8caxMjm3hM4pP2msrw-1 Received: by mail-io1-f70.google.com with SMTP id ca18e2360f4ac-7bfffd9b47fso16098139f.1 for ; Mon, 05 Feb 2024 14:36:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707172572; x=1707777372; h=content-transfer-encoding:mime-version: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=b/wNE6tBX1rJP2YgS+TTAYHWSHylnSq7QWSxIGxGiQU=; b=HAmq+jFzeXNC0F6eVkOnJxOUyWdIFcUZjnKzcp4fmeKcsqGdZGP0O5F7YJByv8HgCU qDfziMB9ovxuxWwFLBY1KCA6owOoBQA2Tw9W9IcpFJNIe5ZTp/W30a8faAGeQi03v8wj Dg9npUIO33VHVMMQMJ7nR7iQNNoXvUve/9cjNeHjFTCYa70ObUX1lyrSMQejUH/R/wx5 e/YxSZEN4YvTaJGzXYRZEASrzGHnZSIep/DqaPXIA+LljxkSvam7vRM64ENpa2lYJxlN KleYyB1Q1FA5UmdtVQZugjpXdciSj8lbqVhcGiNdkPw6V8cqsjUlZMnoWmss1BHkQiRT 1Czw== X-Gm-Message-State: AOJu0Yyxz0EhO9VtfOHDhUDwMPcWbPqOeRM8VpZpaZhjB3J9ITWp4wIH id25Lrm+9j3m4huCVXsWHC58ePU7TQm1E4u4MTz3RyNIyma4rrWF2Hm7/Iwq+8KtIPmsgarwuMn b9gohsbgemvRrW63oQnjA7jxSZ+WbQc5OLhE+pc9vv3rY9UnVUWLlQhb09K0Mhg== X-Received: by 2002:a6b:7319:0:b0:7bc:3ceb:6552 with SMTP id e25-20020a6b7319000000b007bc3ceb6552mr1154908ioh.5.1707172572386; Mon, 05 Feb 2024 14:36:12 -0800 (PST) X-Received: by 2002:a6b:7319:0:b0:7bc:3ceb:6552 with SMTP id e25-20020a6b7319000000b007bc3ceb6552mr1154887ioh.5.1707172572139; Mon, 05 Feb 2024 14:36:12 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUxCfQVCbR6pJ43tYfgys9BBxKwFqEwjwo0/e6nDvW1ISVo8+mYOlffXtXJFJ14F5XcWgVsTOM0/lWGsGjCeeNchPDFH6U3hjMlvIG1E/RpYzwQGn0Vv4F4oxP1aj4rqM92Q0HFDG4zjxmMcBz1AO3qedBjQNf04lKdRX9wtMIZvpMUjWdZ93DF52L0/dCbRRKYFW+JiFR04cUr4o+fnRJuTIfaB+MMf/ib0D8Ss4mZDeEpB642IQV1ncmLOznxeLucxdn4e4erxQ0Evji5NNA7dQciS5FDGurXe85llqZsv85H1mngThVSQ08NPoGTn9iVie8QFA== Received: from redhat.com ([38.15.36.11]) by smtp.gmail.com with ESMTPSA id 25-20020a0566380a5900b00471374f17a3sm190892jap.136.2024.02.05.14.36.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 14:36:11 -0800 (PST) Date: Mon, 5 Feb 2024 15:34:52 -0700 From: Alex Williamson To: Reinette Chatre Cc: jgg@nvidia.com, yishaih@nvidia.com, shameerali.kolothum.thodi@huawei.com, kevin.tian@intel.com, kvm@vger.kernel.org, dave.jiang@intel.com, ashok.raj@intel.com, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH 03/17] vfio/pci: Consistently acquire mutex for interrupt management Message-ID: <20240205153452.4a9bddfd.alex.williamson@redhat.com> In-Reply-To: References: X-Mailer: Claws Mail 4.2.0 (GTK 3.24.38; x86_64-redhat-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 1 Feb 2024 20:56:57 -0800 Reinette Chatre wrote: > vfio_pci_set_irqs_ioctl() is the entrypoint for interrupt management > via the VFIO_DEVICE_SET_IRQS ioctl(). The igate mutex is obtained > before calling vfio_pci_set_irqs_ioctl() for management of all interrupt > types to protect against concurrent changes to the eventfds associated > with device request notification and error interrupts. > > The igate mutex is not acquired consistently. The mutex is always > (for all interrupt types) acquired from within vfio_pci_ioctl_set_irqs() > before calling vfio_pci_set_irqs_ioctl(), but vfio_pci_set_irqs_ioctl() is > called via vfio_pci_core_disable() without the mutex held. The latter > is expected to be correct if the code flow can be guaranteed that > the provided interrupt type is not a device request notification or error > interrupt. The latter is correct because it's always a physical interrupt type (INTx/MSI/MSIX), vdev->irq_type dictates this, and the interrupt code prevents the handler from being called after the interrupt is disabled. It's intentional that we don't acquire igate here since we only need to prevent a race with concurrent user access, which cannot occur in the fd release path. The igate mutex is acquired consistently, where it's required. It would be more forthcoming to describe that potential future emulated device interrupts don't make the same guarantees, but if that's true, why can't they? > Move igate mutex acquire and release into vfio_pci_set_irqs_ioctl() > to make the locking consistent irrespective of interrupt type. > This is one step closer to contain the interrupt management locking > internals within the interrupt management code so that the VFIO PCI > core can trigger management of the eventfds associated with device > request notification and error interrupts without needing to access > and manipulate VFIO interrupt management locks and data. If all we want to do is move the mutex into vfio_pci_intr.c then we could rename to __vfio_pci_set_irqs_ioctl() and create a wrapper around it that grabs the mutex. The disable path could use the lockless version and we wouldn't need to clutter the exit path unlocking the mutex as done below. Thanks, Alex > Signed-off-by: Reinette Chatre > --- > Note to maintainers: > Originally formed part of the IMS submission below, but is not > specific to IMS. > https://lore.kernel.org/lkml/cover.1696609476.git.reinette.chatre@intel.com > > drivers/vfio/pci/vfio_pci_core.c | 3 --- > drivers/vfio/pci/vfio_pci_intrs.c | 10 ++++++++-- > 2 files changed, 8 insertions(+), 5 deletions(-) > > diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c > index 1cbc990d42e0..d2847ca2f0cb 100644 > --- a/drivers/vfio/pci/vfio_pci_core.c > +++ b/drivers/vfio/pci/vfio_pci_core.c > @@ -1214,12 +1214,9 @@ static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev, > return PTR_ERR(data); > } > > - mutex_lock(&vdev->igate); > - > ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index, hdr.start, > hdr.count, data); > > - mutex_unlock(&vdev->igate); > kfree(data); > > return ret; > diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c > index 69ab11863282..97a3bb22b186 100644 > --- a/drivers/vfio/pci/vfio_pci_intrs.c > +++ b/drivers/vfio/pci/vfio_pci_intrs.c > @@ -793,7 +793,9 @@ int vfio_pci_set_irqs_ioctl(struct vfio_pci_core_device *vdev, uint32_t flags, > int (*func)(struct vfio_pci_core_device *vdev, unsigned int index, > unsigned int start, unsigned int count, uint32_t flags, > void *data) = NULL; > + int ret = -ENOTTY; > > + mutex_lock(&vdev->igate); > switch (index) { > case VFIO_PCI_INTX_IRQ_INDEX: > switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) { > @@ -838,7 +840,11 @@ int vfio_pci_set_irqs_ioctl(struct vfio_pci_core_device *vdev, uint32_t flags, > } > > if (!func) > - return -ENOTTY; > + goto out_unlock; > + > + ret = func(vdev, index, start, count, flags, data); > +out_unlock: > + mutex_unlock(&vdev->igate); > + return ret; > > - return func(vdev, index, start, count, flags, data); > }