Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp886606lqc; Fri, 8 Mar 2024 15:06:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVrnYkFNOc8b3jrZl/W8jzrYzh1HrePZCXQ5cbG9Xy9JVZ87Dsuw2Yy84Pby1rB1f+QAVUdA7XkJhEVtkIobDu47Uz5DepPmwNX/nBJ8w== X-Google-Smtp-Source: AGHT+IFdAK+k4qmUC3bP3XOI0fqL8lb61XyIX0u4AB/LepxsD2IS6NmoklTJtcBBaj+9I12avwTp X-Received: by 2002:a05:6a21:789d:b0:1a1:22ba:5b68 with SMTP id bf29-20020a056a21789d00b001a122ba5b68mr126189pzc.38.1709939184770; Fri, 08 Mar 2024 15:06:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709939184; cv=pass; d=google.com; s=arc-20160816; b=JK+MmF/IBpomn3k1/HTRghJUiJQiz++lOwDjQWPXSFEmyPEbZlxIirIi4bgOlhrXNv iaSDsvht2+cTPM3FDi4W9d4QYyymTUbnRGnzhMSaS3buXVkmzLj2BVpuFYQcsS40c+sF z9y+p4yJHEp+l8wYW34WKOxIgAcyFXky+Uf8rsUPDg9fWLdPvu3HW+Yf56NwXDhzPCFB rDuYBvF3EDa3J/VxuCMw9YYYK1Ifu/0OwvCHsTLlVZ7+erRDhYUhoGOQyDyB/6PBhfka Q1yiOGMglS932GBAPlRPFYKpAcdxAq+U7clgYlLtYHAjSw9qcd/pY0l3ypblD1ER/VmR B5Ww== 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 :date:subject:cc:to:from:dkim-signature; bh=6LsYRp/Tbqx78yCSxYtu/4eHgx9ZiAm6cb+pYYvgz1s=; fh=AON1tP1R2s0GAvYSrU1I/R6N81mWhzuaPDd8Cwaumc8=; b=iWthc67f1x3LXPUv51qpTYV4H4bMhXqfCo+Dh5Iex1q3Gsu0trBJsCBdSl3dbbineo NN1gA3V46RUkQE9ZDPAkmpFC17SIrt6FBTm2x5oEflgNYnvia4uU9Q6ceXV4JDRVhyU5 Sy5JzC9LLOjietedDtlfbDQbPOQLb0hY3NZ0gy4yAmyAL3qnNTQuJ3Bw0Ye7odLgEJmS U1XRa1XZBWBN8ShK65Gw92bpljZgqulWv3Hf+5aR3AqJo088A9OKSgeSKWFFYc+waRXJ Mz31WKgLB8filXtp5QipWHLsC7HTWKCU1WeQIgf9nC2EqQ5a4yJG3gy0MPAQjtGleMpi FNDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=McKRwrg5; 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-97677-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97677-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id n12-20020a170903110c00b001dd6524161fsi310657plh.552.2024.03.08.15.06.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 15:06:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97677-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=McKRwrg5; 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-97677-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97677-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 55539282EAD for ; Fri, 8 Mar 2024 23:06:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 410043BBF2; Fri, 8 Mar 2024 23:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="McKRwrg5" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 A1BEC2747F for ; Fri, 8 Mar 2024 23:06:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709939172; cv=none; b=GANo8XjSlIpbWc2rr6QJkXxuVoJC5zj0/eiqwuZN487y9phphq/+DYeGIiDf+tcgVjkqPzEGkb4/ScRaCTtYtqYYVv4cpujob4Dc+ks3C01irFQsreaY3lIpUEKdIOjdDSU8GbbVY0fXM/l/HKW4iLtqNnwWx0IOhtu/7/Le470= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709939172; c=relaxed/simple; bh=RRNuwGOodP63KJuqXtOzgGIXmJxXs5aKT4xUnjW/L3M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=taNBu6Z7VhY7hQhl/CNdPSXBm7Vom+Q6b0s2NQYx/+WJBIl35vx16JXaty5AfLNNYMNeT5QOUGkKAZy3gsDNze59XiALa7TCklikcajQCIsl12n06+zz4rKAgFP/ahsgk90bQFDVyBagyzrYrtzf+4PratKWByRwntLRnIK3bqM= 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=McKRwrg5; arc=none smtp.client-ip=170.10.129.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=1709939169; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6LsYRp/Tbqx78yCSxYtu/4eHgx9ZiAm6cb+pYYvgz1s=; b=McKRwrg5Nrs8EFxWonx5pcJiJuLAEfmrbr1L2Z9PMWZrLq6PUrnIBtwy+bGKp52Km88cmh TGTZB3apjEoO5Ygo5PrhDCun9Wd8Iuz935Iq2VFDYeJMEkj6cbsZ5uF16rPP+xViciKtWd A7pQADTGQ+RUM9Ovi+oiherCVlJvClg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-221-aSK38dycPjW6tp_5qnzF2A-1; Fri, 08 Mar 2024 18:06:08 -0500 X-MC-Unique: aSK38dycPjW6tp_5qnzF2A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 960B2800264; Fri, 8 Mar 2024 23:06:07 +0000 (UTC) Received: from omen.home.shazbot.org (unknown [10.22.8.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8D44436FF; Fri, 8 Mar 2024 23:06:06 +0000 (UTC) From: Alex Williamson To: alex.williamson@redhat.com Cc: kvm@vger.kernel.org, eric.auger@redhat.com, clg@redhat.com, reinette.chatre@intel.com, linux-kernel@vger.kernel.org, kevin.tian@intel.com, stable@vger.kernel.org Subject: [PATCH v2 2/7] vfio/pci: Lock external INTx masking ops Date: Fri, 8 Mar 2024 16:05:23 -0700 Message-ID: <20240308230557.805580-3-alex.williamson@redhat.com> In-Reply-To: <20240308230557.805580-1-alex.williamson@redhat.com> References: <20240308230557.805580-1-alex.williamson@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 Mask operations through config space changes to DisINTx may race INTx configuration changes via ioctl. Create wrappers that add locking for paths outside of the core interrupt code. In particular, irq_type is updated holding igate, therefore testing is_intx() requires holding igate. For example clearing DisINTx from config space can otherwise race changes of the interrupt configuration. This aligns interfaces which may trigger the INTx eventfd into two camps, one side serialized by igate and the other only enabled while INTx is configured. A subsequent patch introduces synchronization for the latter flows. Cc: stable@vger.kernel.org Fixes: 89e1f7d4c66d ("vfio: Add PCI device driver") Reported-by: Reinette Chatre Reviewed-by: Kevin Tian Reviewed-by: Reinette Chatre Signed-off-by: Alex Williamson --- drivers/vfio/pci/vfio_pci_intrs.c | 34 +++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c index 136101179fcb..75c85eec21b3 100644 --- a/drivers/vfio/pci/vfio_pci_intrs.c +++ b/drivers/vfio/pci/vfio_pci_intrs.c @@ -99,13 +99,15 @@ static void vfio_send_intx_eventfd(void *opaque, void *unused) } /* Returns true if the INTx vfio_pci_irq_ctx.masked value is changed. */ -bool vfio_pci_intx_mask(struct vfio_pci_core_device *vdev) +static bool __vfio_pci_intx_mask(struct vfio_pci_core_device *vdev) { struct pci_dev *pdev = vdev->pdev; struct vfio_pci_irq_ctx *ctx; unsigned long flags; bool masked_changed = false; + lockdep_assert_held(&vdev->igate); + spin_lock_irqsave(&vdev->irqlock, flags); /* @@ -143,6 +145,17 @@ bool vfio_pci_intx_mask(struct vfio_pci_core_device *vdev) return masked_changed; } +bool vfio_pci_intx_mask(struct vfio_pci_core_device *vdev) +{ + bool mask_changed; + + mutex_lock(&vdev->igate); + mask_changed = __vfio_pci_intx_mask(vdev); + mutex_unlock(&vdev->igate); + + return mask_changed; +} + /* * If this is triggered by an eventfd, we can't call eventfd_signal * or else we'll deadlock on the eventfd wait queue. Return >0 when @@ -194,12 +207,21 @@ static int vfio_pci_intx_unmask_handler(void *opaque, void *unused) return ret; } -void vfio_pci_intx_unmask(struct vfio_pci_core_device *vdev) +static void __vfio_pci_intx_unmask(struct vfio_pci_core_device *vdev) { + lockdep_assert_held(&vdev->igate); + if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0) vfio_send_intx_eventfd(vdev, NULL); } +void vfio_pci_intx_unmask(struct vfio_pci_core_device *vdev) +{ + mutex_lock(&vdev->igate); + __vfio_pci_intx_unmask(vdev); + mutex_unlock(&vdev->igate); +} + static irqreturn_t vfio_intx_handler(int irq, void *dev_id) { struct vfio_pci_core_device *vdev = dev_id; @@ -563,11 +585,11 @@ static int vfio_pci_set_intx_unmask(struct vfio_pci_core_device *vdev, return -EINVAL; if (flags & VFIO_IRQ_SET_DATA_NONE) { - vfio_pci_intx_unmask(vdev); + __vfio_pci_intx_unmask(vdev); } else if (flags & VFIO_IRQ_SET_DATA_BOOL) { uint8_t unmask = *(uint8_t *)data; if (unmask) - vfio_pci_intx_unmask(vdev); + __vfio_pci_intx_unmask(vdev); } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { struct vfio_pci_irq_ctx *ctx = vfio_irq_ctx_get(vdev, 0); int32_t fd = *(int32_t *)data; @@ -594,11 +616,11 @@ static int vfio_pci_set_intx_mask(struct vfio_pci_core_device *vdev, return -EINVAL; if (flags & VFIO_IRQ_SET_DATA_NONE) { - vfio_pci_intx_mask(vdev); + __vfio_pci_intx_mask(vdev); } else if (flags & VFIO_IRQ_SET_DATA_BOOL) { uint8_t mask = *(uint8_t *)data; if (mask) - vfio_pci_intx_mask(vdev); + __vfio_pci_intx_mask(vdev); } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { return -ENOTTY; /* XXX implement me */ } -- 2.44.0