From patchwork Tue Feb 20 06:59:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 203507 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp286992dyc; Tue, 20 Feb 2024 01:35:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXkzQMRzNnSZL5H/kduFQj3RygCM7DbzQFIpZDK2grgU57X1Ws0CUb2M0Nw0fXHzQ2yUKpKO+reddAEBBIrBCRJoeIa2Q== X-Google-Smtp-Source: AGHT+IE0V+rS6qE9vlN4RhCvrqN/R6hjxE9vyzkmDHepr/gPCQIaDTBvIxIxoAa8BPYlS3hhPrg9 X-Received: by 2002:a05:690c:d96:b0:608:2324:129a with SMTP id da22-20020a05690c0d9600b006082324129amr7101018ywb.27.1708421733900; Tue, 20 Feb 2024 01:35:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708421733; cv=pass; d=google.com; s=arc-20160816; b=uESQlF20pKWCMwj90xZcuGOA/mSYE+Z428B6y0tnsbV+nrYoxnKlAXvntHrxKjyKH1 nftnaTrDWoxM8k051VOpZGJJJnEA+fZwR235AWTQnYcJC8Y9q3weB20ooElsoqo/mKQK EQjC+wCjomWyPPw4YwyiRN5pejlpl1X+x/IGXaOcu3h4beFTJqIs6MKXkcVQA2C3FDNU f6+BJ5E9ByyPoD9hyXJqdO8d2f9yrz1FQNGvtdKicuM8tMLaCWrhm1cTDFkgFnsDoJXi QFVFwJdnem7PTjjxkOMHXLtoFWd5rbQyDQhThvGQRBvOZgcRJf4C8AI90k6QQhMMoIp4 VWHg== 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=c4RS5M5rDM6vEAhFpQ7CnNw9aDfog6AmtRKe3a2QiwA=; fh=udYP2z1ldoyU8sf8J7LRL7rlIhpGzjuWe86ueFl5GJ8=; b=ev+7/t3f4OCRG5ZFmqvf+Ht3+AvqUSwFTnjCUwT5T1nsjvLn/iSoiulFc4xTNHTo1P sNzAZasYMwaEwsWi3FmjXN7nxr9/SFVabkTQG7AnDOLi5CC3+1Iq27Ws6+lCGtKrJaH8 PtPn1GY72ILhwdrC/Iult8DYHP7Swg0t0rcO/JKSZwhAVlIJZQzUn1YrOBP6iihpTMla lo/DgEMRFeVYJiMfdofsPOK4aj/u8paaHEnVyGlolotuviEXFoiglREQja7y1L8T9gSa x9iKGYORvfNceaxowXFoYOywL/xAoqoknbID5qU+BSFw+n4yPmxyfHSiVdY3Kjjs+fOW UxSg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WX8AXyZM; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-72450-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72450-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v36-20020a05622a18a400b0042dd9493110si8783392qtc.347.2024.02.20.01.35.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 01:35:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72450-ouuuleilei=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=@intel.com header.s=Intel header.b=WX8AXyZM; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-72450-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72450-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 5E1091C2251C for ; Tue, 20 Feb 2024 07:06:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EB93B5A7A7; Tue, 20 Feb 2024 07:05:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="WX8AXyZM" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (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 E27C75A10E for ; Tue, 20 Feb 2024 07:05:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708412742; cv=none; b=FHf1MUvD9sj1qv/WXG89Rt12mLvBspzV3sMmwmkBkNcqiCW8q1AH2k79h85riIWxKpSLEKg9Vq37PSgaT6nszTT+dt8k59AHybUAC1IAZqsCRe/Ni8OUNeYmuB3COWk3CVKAh5mByzq2vuS5Io6R7kuAJIK/8J3ifTFInHwVqOM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708412742; c=relaxed/simple; bh=zPMYb8T01iUrJHQctk16vFfd7/Rkjuot0PPcWuhjqHQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z3BBpjV/ty7lqmRnSB743YOYCTrMX467WQ25ZeWHMFsHdPmj2miMnO7XH12h3EP9KsbUZ9vy34BwBy6YOeTDj2H3vDMh2slJJx+J6ILl3D/Tm6rjSoZjh+NUPKsNbbjTIdFpZ3h7UinxaOfQsKFl+7QQOzvYSEIN09beX94MEf8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=WX8AXyZM; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708412741; x=1739948741; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zPMYb8T01iUrJHQctk16vFfd7/Rkjuot0PPcWuhjqHQ=; b=WX8AXyZMyi11JQp2My2UDfjB4bS0vLuG/0lmwkhywOTL5aqv4E1I6tah z04xnvDnsskA/C3u3icao0yELXIZ/tu+lSuZUQetcSstTHN6EJaVIsvvT 3yyUl8VlCpTcYNz1sgOJV/uyjHcCIMwRZB8Sjn30F7YLm6KAURmjaiRKf 8yGIKzi8/DOSmDnTYsgvtiEpGraNMXgv3/aYB83TEbGepeyhfztQCJwXY GLW4gDOozy5rJkPdHh/KTb1cN6aHCJKG8SbSXZ1Sb3BdqyIUCk9PWeRkm HGHu+9E/A6S7QFa66oEBG9SiYNiZAMlL8lV0F6zgPnhX5be+1MohqgpOT A==; X-IronPort-AV: E=McAfee;i="6600,9927,10989"; a="2366570" X-IronPort-AV: E=Sophos;i="6.06,172,1705392000"; d="scan'208";a="2366570" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Feb 2024 23:05:41 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,172,1705392000"; d="scan'208";a="4683775" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmviesa010.fm.intel.com with ESMTP; 19 Feb 2024 23:05:37 -0800 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian Cc: Huang Jiaqing , Ethan Zhao , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu , Jason Gunthorpe Subject: [PATCH v2 1/2] iommu/vt-d: Use rbtree to track iommu probed devices Date: Tue, 20 Feb 2024 14:59:38 +0800 Message-Id: <20240220065939.121116-2-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240220065939.121116-1-baolu.lu@linux.intel.com> References: <20240220065939.121116-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791410027543819504 X-GMAIL-MSGID: 1791410027543819504 Use a red-black tree(rbtree) to track devices probed by the driver's probe_device callback. These devices need to be looked up quickly by a source ID when the hardware reports a fault, either recoverable or unrecoverable. Fault reporting paths are critical. Searching a list in this scenario is inefficient, with an algorithm complexity of O(n). An rbtree is a self-balancing binary search tree, offering an average search time complexity of O(log(n)). This significant performance improvement makes rbtrees a better choice. Furthermore, rbtrees are implemented on a per-iommu basis, eliminating the need for global searches and further enhancing efficiency in critical fault paths. The rbtree is protected by a spin lock with interrupts disabled to ensure thread-safe access even within interrupt contexts. Co-developed-by: Huang Jiaqing Signed-off-by: Huang Jiaqing Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- drivers/iommu/intel/iommu.h | 8 ++++ drivers/iommu/intel/dmar.c | 3 +- drivers/iommu/intel/iommu.c | 88 ++++++++++++++++++++++++++++++++++++- 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index eaef932ad7c9..2b67ad0d6fe9 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -722,6 +722,11 @@ struct intel_iommu { struct q_inval *qi; /* Queued invalidation info */ u32 iommu_state[MAX_SR_DMAR_REGS]; /* Store iommu states between suspend and resume.*/ + /* rb tree for all probed devices */ + struct rb_root device_rbtree; + /* protect the device_rbtree */ + spinlock_t device_rbtree_lock; + #ifdef CONFIG_IRQ_REMAP struct ir_table *ir_table; /* Interrupt remapping info */ struct irq_domain *ir_domain; @@ -755,6 +760,8 @@ struct device_domain_info { struct intel_iommu *iommu; /* IOMMU used by this device */ struct dmar_domain *domain; /* pointer to domain */ struct pasid_table *pasid_table; /* pasid table */ + /* device tracking node(lookup by PCI RID) */ + struct rb_node node; #ifdef CONFIG_INTEL_IOMMU_DEBUGFS struct dentry *debugfs_dentry; /* pointer to device directory dentry */ #endif @@ -1081,6 +1088,7 @@ void free_pgtable_page(void *vaddr); void iommu_flush_write_buffer(struct intel_iommu *iommu); struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *parent, const struct iommu_user_data *user_data); +struct device *device_rbtree_find(struct intel_iommu *iommu, u16 rid); #ifdef CONFIG_INTEL_IOMMU_SVM void intel_svm_check(struct intel_iommu *iommu); diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c index 23cb80d62a9a..f9b63c2875f7 100644 --- a/drivers/iommu/intel/dmar.c +++ b/drivers/iommu/intel/dmar.c @@ -1095,7 +1095,8 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd) iommu->agaw = agaw; iommu->msagaw = msagaw; iommu->segment = drhd->segment; - + iommu->device_rbtree = RB_ROOT; + spin_lock_init(&iommu->device_rbtree_lock); iommu->node = NUMA_NO_NODE; ver = readl(iommu->reg + DMAR_VER_REG); diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 61bb35046ea4..acfe27bd3448 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -96,6 +96,81 @@ static phys_addr_t root_entry_uctp(struct root_entry *re) return re->hi & VTD_PAGE_MASK; } +static int device_rid_cmp_key(const void *key, const struct rb_node *node) +{ + struct device_domain_info *info = + rb_entry(node, struct device_domain_info, node); + const u16 *rid_lhs = key; + + if (*rid_lhs < PCI_DEVID(info->bus, info->devfn)) + return -1; + + if (*rid_lhs > PCI_DEVID(info->bus, info->devfn)) + return 1; + + return 0; +} + +static int device_rid_cmp(struct rb_node *lhs, const struct rb_node *rhs) +{ + struct device_domain_info *info = + rb_entry(lhs, struct device_domain_info, node); + u16 key = PCI_DEVID(info->bus, info->devfn); + + return device_rid_cmp_key(&key, rhs); +} + +/* + * Looks up an IOMMU-probed device using its source ID. + * + * Returns the pointer to the device if there is a match. Otherwise, + * returns NULL. + * + * Note that this helper doesn't guarantee that the device won't be + * released by the iommu subsystem after being returned. The caller + * should use its own synchronization mechanism to avoid the device + * being released during its use if its possibly the case. + */ +struct device *device_rbtree_find(struct intel_iommu *iommu, u16 rid) +{ + struct device_domain_info *info = NULL; + struct rb_node *node; + unsigned long flags; + + spin_lock_irqsave(&iommu->device_rbtree_lock, flags); + node = rb_find(&rid, &iommu->device_rbtree, device_rid_cmp_key); + if (node) + info = rb_entry(node, struct device_domain_info, node); + spin_unlock_irqrestore(&iommu->device_rbtree_lock, flags); + + return info ? info->dev : NULL; +} + +static int device_rbtree_insert(struct intel_iommu *iommu, + struct device_domain_info *info) +{ + struct rb_node *curr; + unsigned long flags; + + spin_lock_irqsave(&iommu->device_rbtree_lock, flags); + curr = rb_find_add(&info->node, &iommu->device_rbtree, device_rid_cmp); + spin_unlock_irqrestore(&iommu->device_rbtree_lock, flags); + if (WARN_ON(curr)) + return -EEXIST; + + return 0; +} + +static void device_rbtree_remove(struct device_domain_info *info) +{ + struct intel_iommu *iommu = info->iommu; + unsigned long flags; + + spin_lock_irqsave(&iommu->device_rbtree_lock, flags); + rb_erase(&info->node, &iommu->device_rbtree); + spin_unlock_irqrestore(&iommu->device_rbtree_lock, flags); +} + /* * This domain is a statically identity mapping domain. * 1. This domain creats a static 1:1 mapping to all usable memory. @@ -4329,25 +4404,34 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev) } dev_iommu_priv_set(dev, info); + ret = device_rbtree_insert(iommu, info); + if (ret) + goto free; if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev)) { ret = intel_pasid_alloc_table(dev); if (ret) { dev_err(dev, "PASID table allocation failed\n"); - kfree(info); - return ERR_PTR(ret); + goto clear_rbtree; } } intel_iommu_debugfs_create_dev(info); return &iommu->iommu; +clear_rbtree: + device_rbtree_remove(info); +free: + kfree(info); + + return ERR_PTR(ret); } static void intel_iommu_release_device(struct device *dev) { struct device_domain_info *info = dev_iommu_priv_get(dev); + device_rbtree_remove(info); dmar_remove_one_dev_info(dev); intel_pasid_free_table(dev); intel_iommu_debugfs_remove_dev(info); From patchwork Tue Feb 20 06:59:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 203508 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp287036dyc; Tue, 20 Feb 2024 01:35:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWxX0H2msAGvv5FuvB6AEDu+5e6hbonnAazRUw9cY+Zd2ljUsdW2T1R1ygW5nAh29qPuiT2IaPLoXuMa0p60RqZnN1nBQ== X-Google-Smtp-Source: AGHT+IEjjgnyp2pSYxEwiDYAeDLUw+e4y6zI03O6CpOx/L6lD+cZIyLvxkuPDn3e/v+Bcf9fmUMM X-Received: by 2002:a17:902:ea04:b0:1db:cb13:6792 with SMTP id s4-20020a170902ea0400b001dbcb136792mr8426222plg.5.1708421739557; Tue, 20 Feb 2024 01:35:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708421739; cv=pass; d=google.com; s=arc-20160816; b=YghjHO0I2p3yspV6a+tFIclH5O0JlSwFtDZEzgFiqTtY2MZT2mjR6REuFkD7I/Idc0 JiSFmBf8lMQkJJrwJAfVxxpuH4BHWdr0ptJ6h845rL098bN2T0uH/Ye9keWM1OHX/LvH IM9+O/wyLFUiZKUL5rI9k45fZlSHMdntwOzA1zEzO6ARdkQHxe9V95nKQhR4HP2q221w d4VIOyt3QB7QSknahb0xopZKi4bnytGaDMIyq41K5QsQRdRcx5dRpR2xxaMXB8E8XLJd oQUYus1a6sSx9Eztfq0TX9TeckRklJVQiAnm6inrkMCSkkkeBFNCO+dMrPgmjolgmSk8 8fdA== 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=/ZpoTJ2zT8BNB1TFMXwPDz+rc6dL91lxMBruq/zZIMg=; fh=esDB3+0OCkV0k6lekB+/rVc1ngU7XdLUIuu2HCpqCtw=; b=N4aUkFaSBYHkX4Tk2pZel/75EjAsyzBqA8iXOhrghlt6jCeTtT/hskL7Fs+VuITMAB pdm9QUU9LbwwS3VW2J5mQChRoElbl1dn2HvsznxGgv9jzh7y/gMjH/huXOD2zJeN9rei h7IMig8xTr0cY4HoFn1+GeW1B8zjMOKMEAtK8bHOahG65pb4+GEre5g5Ce8r8gUv2KN/ bBhiO45AahTWbxkkEgQIB639S+2nGO9PYGkIZNefq9Qk/Iyypi5lReNXowAxxbMaVVlE nSnGBH+s8fRcD5ZHWTIMwpQ80hMRPZzFEWZPazSOXYGwAQQnygvEVfByvCNC9cxNMBZh nrTw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cTelXNx0; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-72451-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72451-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i3-20020a170902c94300b001dbfd897929si2725251pla.618.2024.02.20.01.35.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 01:35:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72451-ouuuleilei=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=@intel.com header.s=Intel header.b=cTelXNx0; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-72451-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72451-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 E53322836CE for ; Tue, 20 Feb 2024 07:06:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 227335B202; Tue, 20 Feb 2024 07:05:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="cTelXNx0" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (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 B61BA5A4FF for ; Tue, 20 Feb 2024 07:05:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708412745; cv=none; b=VEbTTQOcNHo+t2+jOdpr7scIpAfRkPP25WLkTU6lWhaxMoAis7Q3orJUSStTkkVrT16FFxivpbG6d0HxAvwKlVxYmze9GcBFcHxe1hRCX/GkbiznBFislG+/ikIZHE3fCvnYtnVekOOC4KsNTfYuMhBrPHdw7dBmtFaKgM61RYI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708412745; c=relaxed/simple; bh=ZbpO3w8GfTzpY3DoVWRz5vgwWK+8/2qXMylcU+ZyyLY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Tbx++h9YifgpE4EJ0ShIh5ojJQS3hQrBJNNICKxuULnPVDIo74iZ66QRFsLylVfKtw2VIFezrAMq5tgSMrvnwqAEEESm+yTdH5f2BQ0/3oGQajEmigQGWNI/ah+Y4pCFiIjAHEMtMM6W++2FTHyDxWTn6k5lOqY8uIW+UtSv9dc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=cTelXNx0; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708412744; x=1739948744; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZbpO3w8GfTzpY3DoVWRz5vgwWK+8/2qXMylcU+ZyyLY=; b=cTelXNx0pU6V7a6X/ZLhg2wAq8U9kfkQcqODPPpLLOk9rjp9/RGKme1B IF+KFbBsGhfpwq4Y5ZBh9S+xvw5fIpeO0CNxR8G+uqxskHlQQqfxvy697 pI1QLxibfR1Ly3g0aTZbxpX95q0kxwJeadU60loJ5McIj86Cto3lM52z3 6W9RvtclccF5IIFpazgnzJMw+b0nkQBxjaHKEaTwG2M9RpZioBj8CeuVB 4EFHlsiGo4k8aePPjF5Be7Gk8jXDqDx2qWEq3OnUOdUCrxso1TETGbHw9 Fjn3WfBZPstmH6WLj1NEtppggq+oCS1CgmNx8LrbwSuQgjg6glYID2nSM w==; X-IronPort-AV: E=McAfee;i="6600,9927,10989"; a="2366578" X-IronPort-AV: E=Sophos;i="6.06,172,1705392000"; d="scan'208";a="2366578" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Feb 2024 23:05:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,172,1705392000"; d="scan'208";a="4683784" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmviesa010.fm.intel.com with ESMTP; 19 Feb 2024 23:05:40 -0800 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian Cc: Huang Jiaqing , Ethan Zhao , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH v2 2/2] iommu/vt-d: Use device rbtree in iopf reporting path Date: Tue, 20 Feb 2024 14:59:39 +0800 Message-Id: <20240220065939.121116-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240220065939.121116-1-baolu.lu@linux.intel.com> References: <20240220065939.121116-1-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791410034410269881 X-GMAIL-MSGID: 1791410034410269881 The existing I/O page fault handler currently locates the PCI device by calling pci_get_domain_bus_and_slot(). This function searches the list of all PCI devices until the desired device is found. To improve lookup efficiency, replace it with device_rbtree_find() to search the device within the probed device rbtree. The I/O page fault is initiated by the device, which does not have any synchronization mechanism with the software to ensure that the device stays in the probed device tree. Theoretically, a device could be released by the IOMMU subsystem after device_rbtree_find() and before iopf_get_dev_fault_param(), which would cause a use-after-free problem. Add a mutex to synchronize the I/O page fault reporting path and the IOMMU release device path. This lock doesn't introduce any performance overhead, as the conflict between I/O page fault reporting and device releasing is very rare. Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe --- drivers/iommu/intel/iommu.h | 2 ++ drivers/iommu/intel/dmar.c | 1 + drivers/iommu/intel/iommu.c | 3 +++ drivers/iommu/intel/svm.c | 17 +++++++++-------- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 2b67ad0d6fe9..404d2476a877 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -719,6 +719,8 @@ struct intel_iommu { #endif struct iopf_queue *iopf_queue; unsigned char iopfq_name[16]; + /* Synchronization between fault report and iommu device release. */ + struct mutex iopf_lock; struct q_inval *qi; /* Queued invalidation info */ u32 iommu_state[MAX_SR_DMAR_REGS]; /* Store iommu states between suspend and resume.*/ diff --git a/drivers/iommu/intel/dmar.c b/drivers/iommu/intel/dmar.c index f9b63c2875f7..d14797aabb7a 100644 --- a/drivers/iommu/intel/dmar.c +++ b/drivers/iommu/intel/dmar.c @@ -1097,6 +1097,7 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd) iommu->segment = drhd->segment; iommu->device_rbtree = RB_ROOT; spin_lock_init(&iommu->device_rbtree_lock); + mutex_init(&iommu->iopf_lock); iommu->node = NUMA_NO_NODE; ver = readl(iommu->reg + DMAR_VER_REG); diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index acfe27bd3448..6743fe6c7a36 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4430,8 +4430,11 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev) static void intel_iommu_release_device(struct device *dev) { struct device_domain_info *info = dev_iommu_priv_get(dev); + struct intel_iommu *iommu = info->iommu; + mutex_lock(&iommu->iopf_lock); device_rbtree_remove(info); + mutex_unlock(&iommu->iopf_lock); dmar_remove_one_dev_info(dev); intel_pasid_free_table(dev); intel_iommu_debugfs_remove_dev(info); diff --git a/drivers/iommu/intel/svm.c b/drivers/iommu/intel/svm.c index b644d57da841..dda276e28325 100644 --- a/drivers/iommu/intel/svm.c +++ b/drivers/iommu/intel/svm.c @@ -645,7 +645,7 @@ static irqreturn_t prq_event_thread(int irq, void *d) struct intel_iommu *iommu = d; struct page_req_dsc *req; int head, tail, handled; - struct pci_dev *pdev; + struct device *dev; u64 address; /* @@ -691,21 +691,22 @@ static irqreturn_t prq_event_thread(int irq, void *d) if (unlikely(req->lpig && !req->rd_req && !req->wr_req)) goto prq_advance; - pdev = pci_get_domain_bus_and_slot(iommu->segment, - PCI_BUS_NUM(req->rid), - req->rid & 0xff); /* * If prq is to be handled outside iommu driver via receiver of * the fault notifiers, we skip the page response here. */ - if (!pdev) + mutex_lock(&iommu->iopf_lock); + dev = device_rbtree_find(iommu, req->rid); + if (!dev) { + mutex_unlock(&iommu->iopf_lock); goto bad_req; + } - intel_svm_prq_report(iommu, &pdev->dev, req); - trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1, + intel_svm_prq_report(iommu, dev, req); + trace_prq_report(iommu, dev, req->qw_0, req->qw_1, req->priv_data[0], req->priv_data[1], iommu->prq_seq_number++); - pci_dev_put(pdev); + mutex_unlock(&iommu->iopf_lock); prq_advance: head = (head + sizeof(*req)) & PRQ_RING_MASK; }