Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp454202lqt; Thu, 6 Jun 2024 08:21:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV0Iv20lD4XvDhjuoIdf/pgWp44hQHhl43JO1XbKqhsCSA30GEs0AE/ksxEqSSScM8vYX0sKEJYn/Ud74LcGwpxKXvgHdMKYGJvxia47A== X-Google-Smtp-Source: AGHT+IH37Yu4lYZGa5nFxexqHZqLPMTjV2XMdlf40fzGbzN19xFpRB4Mu7Xykg639SU7vXPkGWEV X-Received: by 2002:a05:6808:3081:b0:3d1:fe2e:f896 with SMTP id 5614622812f47-3d20429b7eemr7392026b6e.16.1717687281734; Thu, 06 Jun 2024 08:21:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717687281; cv=pass; d=google.com; s=arc-20160816; b=vo6299QWr4PTUBBTHhc1Vzo6Ji+xuWo4hXzmQ8RxmXODsMdG5QGFUoLdaTp7UJnLsW 9mTMm+CM3/Zrej7plSIKD/v6qnSed+r0c/IKFywc6ddAGDavQCNlVuyNWYixXRkThRbw e0/yf66yZghJOOW6k23LQOKrYAbhSblBWk0hH+G+3M7Zk2Sgoi78AbsVm/MheWN76Lij hIMzAHu9t8Lhox6jfJ33LZkYIp21dJQguIGzJXDzvwpjRnN129cPDeMC2qTFahZQdR2A A8aLD1PS6eGMQflwUloBDZbKTJ+HDXukW3Y8Pn+ieZOoC6I5qHrA+m3d7uj8F7J8mfo3 dEoA== 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=Y7OMGNBU3xf1RKRryYKaOS5GooOxwOsrzKzqesFig8Y=; fh=FoqT00vYu0eKrzoJsUHyzK6y55Z9MS6auMyAW517+J4=; b=UKCigxdlo/cRrkOELCRxfV3znXzm0GdsIu/avc//01AtNXRdP+rzcZmOmqqoV4+ynO lVFLAjmWtCCuqxV5yn4UyMPfzW9sOZzJqCPCCAqc3n/5pIl7sc300mNaM/Wn2lt/AjOH 5DhVMnoAf21HRPqrrhkbPQMAeIxU9lq4TrH/y/ln+ZYLbXjNT4Wmp7nkiHTYhvrh4cXb EqS7nx2LEoGEtXlJioDEl3JWW1b4GOmJKpxk6EgPadzWhjesVqP67OraTO5P30uSyfkE 3oFf/hhHi+uiEcWkDIlGDDF+x7c0LvYtPDTn7N8Z8dAQljP6arqQQZdUo3RjfZXm1XhT E59Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amazon.co.uk header.s=amazon201209 header.b="Bw/Mls7+"; arc=pass (i=1 spf=pass spfdomain=amazon.co.uk dkim=pass dkdomain=amazon.co.uk dmarc=pass fromdomain=amazon.co.uk); spf=pass (google.com: domain of linux-kernel+bounces-204556-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204556-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.co.uk Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-44038b89db3si10469821cf.787.2024.06.06.08.21.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 08:21:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-204556-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.co.uk header.s=amazon201209 header.b="Bw/Mls7+"; arc=pass (i=1 spf=pass spfdomain=amazon.co.uk dkim=pass dkdomain=amazon.co.uk dmarc=pass fromdomain=amazon.co.uk); spf=pass (google.com: domain of linux-kernel+bounces-204556-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204556-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.co.uk 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 62F811C26283 for ; Thu, 6 Jun 2024 15:21:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 32BD11991B9; Thu, 6 Jun 2024 15:11:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="Bw/Mls7+" Received: from smtp-fw-9106.amazon.com (smtp-fw-9106.amazon.com [207.171.188.206]) (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 8F084196C78; Thu, 6 Jun 2024 15:11:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=207.171.188.206 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717686694; cv=none; b=S818T2Q736kGajbLDQYlLXJeBDV9Xt0kXDCDkGXOB4OFxaWTGu5ygS2Kgii5f/Ec2sqDuFevK8G9Jp8bs7+ur7yVoQp5JEykJ59w73TS9PGUQ7w7An42hQpr3uuhesiQfG89U0nfo7PWtyPBKjWc/uzXzpatZJGhDmVf+6YzxsE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717686694; c=relaxed/simple; bh=oLClCNPMBOwIEqCYHCcIcgTsA5symXpGgSGwK7I1mp4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lli+Z26XWjksqz7xznAFJ5uz5Me329jrkAnCbrzn/WA+OpvO6/arZGts1OAkvTy2qeDSM225U9vB3Zggv6zMpGUUCzsxe4ideRny7232qo5fmBLz5gl7p+AbLZwSw7UpVA2RlWtJ/fDjCDtC8C/1v3W1GoPPDXdkIP75KgejaYQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (1024-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=Bw/Mls7+; arc=none smtp.client-ip=207.171.188.206 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1717686693; x=1749222693; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Y7OMGNBU3xf1RKRryYKaOS5GooOxwOsrzKzqesFig8Y=; b=Bw/Mls7+Z5+Nq6iqq/Ifqtz/bvSh3hkBX2CatJlOEKRImuyYC3mnHMuZ jZzu8gmzH4j5m495zsA7IKPAtYbrFBNZOnaiB3Nr4sEbO9a+MHnNn701/ QUcu6N3HXdAph6OmBs+N2pyv8d4RhQ8WNyftia/FhmrT7ekqh8VyCmRHS A=; X-IronPort-AV: E=Sophos;i="6.08,219,1712620800"; d="scan'208";a="731106053" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-9106.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2024 15:11:26 +0000 Received: from EX19MTAEUB001.ant.amazon.com [10.0.10.100:4098] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.27.80:2525] with esmtp (Farcaster) id 6f1f703b-326c-41bc-8642-19683345393b; Thu, 6 Jun 2024 15:11:24 +0000 (UTC) X-Farcaster-Flow-ID: 6f1f703b-326c-41bc-8642-19683345393b Received: from EX19D007EUB001.ant.amazon.com (10.252.51.82) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Thu, 6 Jun 2024 15:11:24 +0000 Received: from EX19MTAUWB001.ant.amazon.com (10.250.64.248) by EX19D007EUB001.ant.amazon.com (10.252.51.82) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Thu, 6 Jun 2024 15:11:23 +0000 Received: from dev-dsk-fgriffo-1c-69b51a13.eu-west-1.amazon.com (10.13.244.152) by mail-relay.amazon.com (10.250.64.254) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34 via Frontend Transport; Thu, 6 Jun 2024 15:11:21 +0000 From: Fred Griffoul To: CC: Fred Griffoul , Alex Williamson , Jason Gunthorpe , Yi Liu , Kevin Tian , Eric Auger , Stefan Hajnoczi , "Christian Brauner" , Ankit Agrawal , "Reinette Chatre" , Ye Bin , , Subject: [PATCH v3 2/2] vfio/pci: add msi interrupt affinity support Date: Thu, 6 Jun 2024 15:10:13 +0000 Message-ID: <20240606151017.41623-3-fgriffo@amazon.co.uk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240606151017.41623-1-fgriffo@amazon.co.uk> References: <20240606151017.41623-1-fgriffo@amazon.co.uk> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain The usual way to configure a device interrupt from userland is to write the /proc/irq//smp_affinity or smp_affinity_list files. When using vfio to implement a device driver or a virtual machine monitor, this may not be ideal: the process managing the vfio device interrupts may not be granted root privilege, for security reasons. Thus it cannot directly control the interrupt affinity and has to rely on an external command. This patch extends the VFIO_DEVICE_SET_IRQS ioctl() with a new data flag to specify the affinity of interrupts of a vfio pci device. The CPU affinity mask argument must be a subset of the process cpuset, otherwise an error -EPERM is returned. The vfio_irq_set argument shall be set-up in the following way: - the 'flags' field have the new flag VFIO_IRQ_SET_DATA_AFFINITY set as well as VFIO_IRQ_SET_ACTION_TRIGGER. - the variable-length 'data' field is a cpu_set_t structure, as for the sched_setaffinity() syscall, the size of which is derived from 'argsz'. Signed-off-by: Fred Griffoul --- drivers/vfio/pci/vfio_pci_core.c | 26 +++++++++++++++++---- drivers/vfio/pci/vfio_pci_intrs.c | 39 +++++++++++++++++++++++++++++++ drivers/vfio/vfio_main.c | 13 +++++++---- include/uapi/linux/vfio.h | 10 +++++++- 4 files changed, 79 insertions(+), 9 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 80cae87fff36..b89df562fb5c 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1192,6 +1192,7 @@ static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev, { unsigned long minsz = offsetofend(struct vfio_irq_set, count); struct vfio_irq_set hdr; + cpumask_var_t mask; u8 *data = NULL; int max, ret = 0; size_t data_size = 0; @@ -1207,9 +1208,21 @@ static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev, return ret; if (data_size) { - data = memdup_user(&arg->data, data_size); - if (IS_ERR(data)) - return PTR_ERR(data); + if (hdr.flags & VFIO_IRQ_SET_DATA_AFFINITY) { + if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) + return -ENOMEM; + + ret = copy_from_user(mask, &arg->data, data_size); + if (ret) + goto out; + + data = (u8 *)mask; + + } else { + data = memdup_user(&arg->data, data_size); + if (IS_ERR(data)) + return PTR_ERR(data); + } } mutex_lock(&vdev->igate); @@ -1218,7 +1231,12 @@ static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev, hdr.count, data); mutex_unlock(&vdev->igate); - kfree(data); + +out: + if (hdr.flags & VFIO_IRQ_SET_DATA_AFFINITY) + free_cpumask_var(mask); + else + kfree(data); return ret; } diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c index 8382c5834335..58fc751e75f1 100644 --- a/drivers/vfio/pci/vfio_pci_intrs.c +++ b/drivers/vfio/pci/vfio_pci_intrs.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "vfio_pci_priv.h" @@ -675,6 +676,41 @@ static int vfio_pci_set_intx_trigger(struct vfio_pci_core_device *vdev, return 0; } +static int vfio_pci_set_msi_affinity(struct vfio_pci_core_device *vdev, + unsigned int start, unsigned int count, + struct cpumask *irq_mask) +{ + struct vfio_pci_irq_ctx *ctx; + cpumask_var_t allowed_mask; + unsigned int i; + int err = 0; + + if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL)) + return -ENOMEM; + + cpuset_cpus_allowed(current, allowed_mask); + if (!cpumask_subset(irq_mask, allowed_mask)) { + err = -EPERM; + goto finish; + } + + for (i = start; i < start + count; i++) { + ctx = vfio_irq_ctx_get(vdev, i); + if (!ctx) { + err = -EINVAL; + break; + } + + err = irq_set_affinity(ctx->producer.irq, irq_mask); + if (err) + break; + } + +finish: + free_cpumask_var(allowed_mask); + return err; +} + static int vfio_pci_set_msi_trigger(struct vfio_pci_core_device *vdev, unsigned index, unsigned start, unsigned count, uint32_t flags, void *data) @@ -691,6 +727,9 @@ static int vfio_pci_set_msi_trigger(struct vfio_pci_core_device *vdev, if (!(irq_is(vdev, index) || is_irq_none(vdev))) return -EINVAL; + if (flags & VFIO_IRQ_SET_DATA_AFFINITY) + return vfio_pci_set_msi_affinity(vdev, start, count, data); + if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { int32_t *fds = data; int ret; diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index e97d796a54fb..e87131d45059 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1505,23 +1505,28 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs, size = 0; break; case VFIO_IRQ_SET_DATA_BOOL: - size = sizeof(uint8_t); + size = hdr->count * sizeof(uint8_t); break; case VFIO_IRQ_SET_DATA_EVENTFD: - size = sizeof(int32_t); + size = hdr->count * sizeof(int32_t); + break; + case VFIO_IRQ_SET_DATA_AFFINITY: + size = hdr->argsz - minsz; + if (size > cpumask_size()) + size = cpumask_size(); break; default: return -EINVAL; } if (size) { - if (hdr->argsz - minsz < hdr->count * size) + if (hdr->argsz - minsz < size) return -EINVAL; if (!data_size) return -EINVAL; - *data_size = hdr->count * size; + *data_size = size; } return 0; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 2b68e6cdf190..5ba2ca223550 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -580,6 +580,12 @@ struct vfio_irq_info { * * Note that ACTION_[UN]MASK specify user->kernel signaling (irqfds) while * ACTION_TRIGGER specifies kernel->user signaling. + * + * DATA_AFFINITY specifies the affinity for the range of interrupt vectors. + * It must be set with ACTION_TRIGGER in 'flags'. The variable-length 'data' + * array is a CPU affinity mask 'cpu_set_t' structure, as for the + * sched_setaffinity() syscall argument: the 'argsz' field is used to check + * the actual cpu_set_t size. */ struct vfio_irq_set { __u32 argsz; @@ -587,6 +593,7 @@ struct vfio_irq_set { #define VFIO_IRQ_SET_DATA_NONE (1 << 0) /* Data not present */ #define VFIO_IRQ_SET_DATA_BOOL (1 << 1) /* Data is bool (u8) */ #define VFIO_IRQ_SET_DATA_EVENTFD (1 << 2) /* Data is eventfd (s32) */ +#define VFIO_IRQ_SET_DATA_AFFINITY (1 << 6) /* Data is cpu_set_t */ #define VFIO_IRQ_SET_ACTION_MASK (1 << 3) /* Mask interrupt */ #define VFIO_IRQ_SET_ACTION_UNMASK (1 << 4) /* Unmask interrupt */ #define VFIO_IRQ_SET_ACTION_TRIGGER (1 << 5) /* Trigger interrupt */ @@ -599,7 +606,8 @@ struct vfio_irq_set { #define VFIO_IRQ_SET_DATA_TYPE_MASK (VFIO_IRQ_SET_DATA_NONE | \ VFIO_IRQ_SET_DATA_BOOL | \ - VFIO_IRQ_SET_DATA_EVENTFD) + VFIO_IRQ_SET_DATA_EVENTFD | \ + VFIO_IRQ_SET_DATA_AFFINITY) #define VFIO_IRQ_SET_ACTION_TYPE_MASK (VFIO_IRQ_SET_ACTION_MASK | \ VFIO_IRQ_SET_ACTION_UNMASK | \ VFIO_IRQ_SET_ACTION_TRIGGER) -- 2.40.1