Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78933639 for ; Wed, 15 Nov 2023 02:02:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ipdqyf4h" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2809fb0027cso35506a91.2 for ; Tue, 14 Nov 2023 18:02:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700013774; x=1700618574; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Id6MwfiB5Pv0ZXowasXpEYmbdbnPyV9LKqTkNMYOdRU=; b=ipdqyf4hwrWsfHJl+d2Fy7mW01xPdaqv8RJOy6si3PqO82JDR8jsAAtiUgBFEQWQPq YPnniQ4AHp0ntMR2q+oXHCguOG8cx6c9F6EDDCN1h3MR0/0ZlIqmadqouTLcykgqWdPW fg53mBntn+0UsxGU5RTdSXnP9AH+L+sDt8KQsKc5vtKnbJOLCwutvN1Dx7gsiMypeJKQ zRQrLEBHbHb3JN7ZwU5rkTidEbnT4PM2wtwUUu6rj/P2ANtlYLNjCoDCLKZAC5iiiOCG csOK5da6DPMN836YLcgXNNRaXSW1qhlsWmKRii6EccTLM8hqiIFeEKZMWci51z94BqMY 9tmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700013774; x=1700618574; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Id6MwfiB5Pv0ZXowasXpEYmbdbnPyV9LKqTkNMYOdRU=; b=CbvjOhILXz8nyNUu6wX/OkwkRpuUlFKlNxe/XNRKLEeESg28wQbaP7NQAbtWp7vwGw WM9BKhPn7++dpaxIqcxt5Gl8xLryHlbZCPu2oRLY9gx/OpK4VAu3f91eBB9qQhd8KtYN I8ss2eVLI/DTrX5guX0mxCAAc0BvjcvZmpIqGjDXLbcM5lO8Lp2Vae1xna6czo1ZzHx6 bbcOf5tYlliUt50ZZRjdncBzx7xNutINWmtMP7fXJP4UTqX6WWzXGqnDXEGn9XF8CPQs kPOtxQvs6vUUudxh9bE62xs5HX9Ew1L6ADVF1PEcLGh1hL+lT8Ssiz3J/9GPorrERKzc ECxg== X-Gm-Message-State: AOJu0Yybd2N5t5A4RtIo82w6ImPf2QQR9kxDM2LB55FJ98J8+RI0g4OS cX/v9bhHHJLsIdfgLiK0/PQ= X-Google-Smtp-Source: AGHT+IECqFiO91Ny+OyS59YpVdL1lmTnIs1B2YgfSXtT4EjIvM1numDH3Yb/LVXNQ6gNzbxcSMNmww== X-Received: by 2002:a17:90b:3886:b0:27d:3ecb:3cbb with SMTP id mu6-20020a17090b388600b0027d3ecb3cbbmr9591264pjb.37.1700013773692; Tue, 14 Nov 2023 18:02:53 -0800 (PST) Received: from localhost.localdomain ([129.227.63.229]) by smtp.gmail.com with ESMTPSA id z5-20020a1709027e8500b001b8622c1ad2sm6487842pla.130.2023.11.14.18.02.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Nov 2023 18:02:53 -0800 (PST) From: yaozhenguo X-Google-Original-From: yaozhenguo To: yaozhenguo@jd.com, dwmw2@infradead.org, baolu.lu@linux.intel.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, alex.williamson@redhat.com Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Zhenguo Yao , Wenchao Yao , ZiHan Zhou Subject: [PATCH V1] vfio: add attach_group_by_node to control behavior of attaching group to domain Date: Wed, 15 Nov 2023 10:02:09 +0800 Message-Id: <20231115020209.4665-1-yaozhenguo@jd.com> X-Mailer: git-send-email 2.32.0 Precedence: bulk X-Mailing-List: iommu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Zhenguo Yao Groups will attach to one iommu_domain if ops and enforce_cache_coherency are equal. And all the iommu hardware share one pagetable by default. There are performance issue in some scenarios. For example: Host hardware topopy: node0 + PCIe RP0 ---+ GPU A100 | |---+ GPU A100 | |---+ NIC Mellanox CX6 | |---+ NIC Mellanox CX6 + PCIe RP1 ---+ GPU A100 |---+ GPU A100 |---+ NIC Mellanox CX6 |---+ NIC Mellanox CX6 node1 + PCIe RP0 ---+ GPU A100 | |---+ GPU A100 | |---+ NIC Mellanox CX6 | |---+ NIC Mellanox CX6 + PCIe RP1 ---+ GPU A100 |---+ GPU A100 |---+ NIC Mellanox CX6 |---+ NIC Mellanox CX6 We passthrough all NICs and GPU to VM, and emulate host hardware topopy. Mellanox CX6 ATS feature is enabled, GPU direct RDMA enabled. We test NCCL allreduce in VM at different cases. Case1: allreduce test use 4nic and 4GPU in numa0. Case2:allreduce test use 4nic and 4GPU in numa1. case3: allreduce test use 8nic and 8GPU. the result are below: | | algbw (GB/S) | | ------ | -------------| | case1 | 24 | | case2 | 32 | | case3 | 45 | We checked that IOMMU pagetable is allocated in numa1 when VM boot up. So, if IOTLB miss happan, IOMMU hardware in numa0 will access remote pagetable in numa1. This will drop performance. After apply this patch and attach_group_by_node is 1. Group in same node will attach to one domain. IOMMU will access there local pagetable. Performance is improved: | | algbw (GB/S) | | ------ | -------------| | case1 | 32 | | case2 | 32 | | case3 | 63 | Signed-off-by: Zhenguo Yao Co-developed-by: Wenchao Yao Signed-off-by: Wenchao Yao Co-developed-by: ZiHan Zhou Signed-off-by: ZiHan Zhou --- drivers/iommu/intel/iommu.c | 8 +++++++- drivers/vfio/vfio_iommu_type1.c | 33 +++++++++++++++++++++------------ include/linux/iommu.h | 1 + 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 3531b95..2c6d8f0 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -569,8 +569,10 @@ void domain_update_iommu_cap(struct dmar_domain *domain) * If RHSA is missing, we should default to the device numa domain * as fall back. */ - if (domain->nid == NUMA_NO_NODE) + if (domain->nid == NUMA_NO_NODE) { domain->nid = domain_update_device_node(domain); + domain->domain.nid = domain->nid; + } /* * First-level translation restricts the input-address to a @@ -1767,6 +1769,7 @@ static struct dmar_domain *alloc_domain(unsigned int type) return NULL; domain->nid = NUMA_NO_NODE; + domain->domain.nid = NUMA_NO_NODE; if (first_level_by_default(type)) domain->use_first_level = true; domain->has_iotlb_device = false; @@ -1808,6 +1811,8 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) info->refcnt = 1; info->did = num; info->iommu = iommu; + domain->nid = iommu->node; + domain->domain.nid = iommu->node; curr = xa_cmpxchg(&domain->iommu_array, iommu->seq_id, NULL, info, GFP_ATOMIC); if (curr) { @@ -1837,6 +1842,7 @@ void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) clear_bit(info->did, iommu->domain_ids); xa_erase(&domain->iommu_array, iommu->seq_id); domain->nid = NUMA_NO_NODE; + domain->domain.nid = NUMA_NO_NODE; domain_update_iommu_cap(domain); kfree(info); } diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index eacd6ec..6a5641e 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -59,6 +59,11 @@ module_param_named(dma_entry_limit, dma_entry_limit, uint, 0644); MODULE_PARM_DESC(dma_entry_limit, "Maximum number of user DMA mappings per container (65535)."); +static uint attach_group_by_node; +module_param_named(attach_group_by_node, + attach_group_by_node, uint, 0644); +MODULE_PARM_DESC(attach_group_by_node, + "Attach group to domain when it's in same node"); struct vfio_iommu { struct list_head domain_list; @@ -2287,19 +2292,23 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, if (d->domain->ops == domain->domain->ops && d->enforce_cache_coherency == domain->enforce_cache_coherency) { - iommu_detach_group(domain->domain, group->iommu_group); - if (!iommu_attach_group(d->domain, - group->iommu_group)) { - list_add(&group->next, &d->group_list); - iommu_domain_free(domain->domain); - kfree(domain); - goto done; - } + if ((attach_group_by_node == 1 && + d->domain->nid == domain->domain->nid) || + attach_group_by_node == 0) { + iommu_detach_group(domain->domain, group->iommu_group); + if (!iommu_attach_group(d->domain, + group->iommu_group)) { + list_add(&group->next, &d->group_list); + iommu_domain_free(domain->domain); + kfree(domain); + goto done; + } - ret = iommu_attach_group(domain->domain, - group->iommu_group); - if (ret) - goto out_domain; + ret = iommu_attach_group(domain->domain, + group->iommu_group); + if (ret) + goto out_domain; + } } } diff --git a/include/linux/iommu.h b/include/linux/iommu.h index ec289c1..c1330ed 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -123,6 +123,7 @@ struct iommu_domain { int users; }; }; + int nid; }; static inline bool iommu_is_dma_domain(struct iommu_domain *domain) -- 1.8.3.1