Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3576460rdg; Tue, 17 Oct 2023 22:07:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHFU9tAnNA237Vv4QtPhCmD/a/mdfkefM3gWbsYx3VC9Xqv8N6RYDbNRS3AuJ0+QajUE05o X-Received: by 2002:a05:6a21:114e:b0:154:3f13:1bb7 with SMTP id oi14-20020a056a21114e00b001543f131bb7mr3697363pzb.49.1697605642259; Tue, 17 Oct 2023 22:07:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697605642; cv=none; d=google.com; s=arc-20160816; b=UbmLM0bF04lwEUctWi18/sfnUgY3Ossi2f6brQ3byf/FwB1cXrDo3SL0u+AKcUtuoF J6zy17kE/JKSnFwfuEzXvirr1fHW/cbjO4ZNv4zxaPiZlrFzwjbb7m1UFAJCmC7oCSbW 8v5ZuTzwGkyqstMKcwkgjcwB8Ya+LFF7nbFg1UfpH7/RDn6KwQH0jb6LU2OguhnLdKL+ CwNh2TgfWh0zMCDgDPHzlEbgLMCiVqqY2QOhzTkDhNWEsa/tFbs00xpvoqOrTggR2yZX hsqqIeRe09BYwRa7UiN4R2khyt8TCQHIIsx72gDj0rm+PngJtEXKaVAHyM4io1zfEr/e 7qnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=9ThFmNb53fImqmguLOZbTLdsQaiFtU4XhllXOaRJNcM=; fh=EuzH5lq/5R7ZpwjN9F0o8LZll1NVcXCrMgVes9t75ng=; b=sW3igyvz5e+XrqG4+xmbkRtpu4UIu8DJ6FMuZUebmOXfnYPktTigiKxZL16wX8g6TL jvqikqvpAx5UjZIt3B6p+WB0wcu1K2uoAU1kPhIgcrX9QF+Wg41uOPk1RZmRLUlAUP6s elgTaX+XPL36nMV447MLtmxFUhBVglyq6PNjfnllmH9EzWFrwcAvFqhC+4u4T9q1Isip 3rRJ3cRVafB6T66ir3fe0vMf6z/WB8apJ0z3Rr1StmI3+zAcR9yuGOYQI9j4RA3Ddsxp xBJJ/EANMKRMy5JFu3KxEuNTzRLaWE5Q14p7jRgxe5VmB+GRr1juGXT7AqSiqkts+l06 EUjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=T1JfhL00; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id cl15-20020a056a02098f00b0059ceeb24a48si1393821pgb.680.2023.10.17.22.07.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 22:07:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=T1JfhL00; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id DDCD980BA6B4; Tue, 17 Oct 2023 22:07:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229461AbjJRFHE (ORCPT + 99 others); Wed, 18 Oct 2023 01:07:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229453AbjJRFHD (ORCPT ); Wed, 18 Oct 2023 01:07:03 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5948FEA for ; Tue, 17 Oct 2023 22:07:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697605621; x=1729141621; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=1DhaoZRu99Prl8MkDJbYmg+5hgrE3uIsCY5cwtlP/jw=; b=T1JfhL00vOpIm7ZrDuUK68fE/3s+0TxlVlDAssmEVsYGsKwIpqFkRkix e93lw5z/JbNYGGQHDCzYmI2mm01rQ1tfyQQq9FzB4WqTxaRTD21y4p/BE LVne7XXFiMW96Jic6Va95RZIww7KS/l3w+5zt8IN0kyVj+3VII1SD/pcr EUKEI+Kbjjp32yiOOU5xsHuD4k8COztOn9H6oMDa66+3tPqEdPNQDYvRT y0WGmX6Mi/lgKKpkhaiA27EAr6vhu9NxX6IT446t57zNzAJVxKl/kHusW qtiKI1nHxF7+67vN/jY5dDf+ISDlH/lLb3uJqU5nrCHnwVOAdzY3ZJlSY g==; X-IronPort-AV: E=McAfee;i="6600,9927,10866"; a="388802372" X-IronPort-AV: E=Sophos;i="6.03,234,1694761200"; d="scan'208";a="388802372" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2023 22:07:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10866"; a="822288635" X-IronPort-AV: E=Sophos;i="6.03,234,1694761200"; d="scan'208";a="822288635" Received: from sqa-gate.sh.intel.com (HELO spr-2s5.tsp.org) ([10.239.48.212]) by fmsmga008.fm.intel.com with ESMTP; 17 Oct 2023 22:06:57 -0700 From: Tina Zhang To: iommu@lists.linux.dev, linux-kernel@vger.kernel.org Cc: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian , Nicolin Chen , Michael Shavit , Vasant Hegde , Tina Zhang Subject: [PATCH v9 0/5] Share sva domains with all devices bound to a mm Date: Wed, 18 Oct 2023 13:06:35 +0800 Message-Id: <20231018050640.24936-1-tina.zhang@intel.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 17 Oct 2023 22:07:18 -0700 (PDT) This series is to share sva(shared virtual addressing) domains with all devices bound to one mm. Problem ------- In the current iommu core code, sva domain is allocated per IOMMU group, when device driver is binding a process address space to a device (which is handled in iommu_sva_bind_device()). If one than more device is bound to the same process address space, there must be more than one sva domain instance, with each device having one. In other words, the sva domain doesn't share between those devices bound to the same process address space, and that leads to two problems: 1) device driver has to duplicate sva domains with enqcmd, as those sva domains have the same PASID and are relevant to one virtual address space. This makes the sva domain handling complex in device drivers. 2) IOMMU driver cannot get sufficient info of the IOMMUs that have devices behind them bound to the same virtual address space, when handling mmu_notifier_ops callbacks. As a result, IOMMU IOTLB invalidation is performed per device instead of per IOMMU, and that may lead to superfluous IOTLB invalidation issue, especially in a virtualization environment where all devices may be behind one virtual IOMMU. Solution -------- This patch-set tries to fix those two problems by allowing sharing sva domains with all devices bound to a mm. To achieve this, a new structure pointer is introduced to mm to replace the old PASID field, which can keep the info of PASID as well as the corresponding shared sva domains. Besides, function iommu_sva_bind_device() is updated to ensure a new sva domain can only be allocated when the old ones cannot work for the IOMMU. With these changes, a device driver can expect one sva domain could work for per PASID instance(e.g., enqcmd PASID instance), and therefore may get rid of handling sva domain duplication. Besides, IOMMU driver (e.g., intel vt-d driver) can get sufficient info (e.g., the info of the IOMMUs having their devices bound to one virtual address space) when handling mmu_notifier_ops callbacks, to remove the redundant IOTLB invalidations. Arguably there shouldn't be more than one sva_domain with the same PASID, and in any sane configuration there should be only 1 type of IOMMU driver that needs only 1 SVA domain. However, in reality, IOMMUs on one platform may not be identical to each other. Thus, attaching a sva domain that has been successfully bound to device A behind a IOMMU A, to device B behind IOMMU B may get failed due to the difference between IOMMU A and IOMMU B. In this case, a new sva domain with the same PASID needs to be allocated to work with IOMMU B. That's why we need a list to keep sva domains of one PASID. For the platform where IOMMUs are compatible to each other, there should be one sva domain in the list. v8: - CC more people - CC iommu@lists.linux.dev mailing list. When sending version 7, some issue happened in my CC list and that caused version 7 wasn't sent to iommu@lists.linux.dev. - Rebase to v6.6-rc6 and make a few format changes. v7: https://lore.kernel.org/lkml/20231012030112.82270-1-tina.zhang@intel.com/ - Add mm_pasid_init() back and do zeroing mm->iommu_mm pointer in mm_pasid_init() to avoid the use-after-free/double-free problem. - Update the commit message of patch "iommu: Add mm_get_enqcmd_pasid() helper function". v6: https://lore.kernel.org/linux-iommu/20231011065132.102676-1-tina.zhang@intel.com/ - Rename iommu_sva_alloc_pasid() to iommu_alloc_mm_data(). - Hold the iommu_sva_lock before invoking iommu_alloc_mm_data(). - Remove "iommu: Introduce mm_get_pasid() helper function" patch, because SMMUv3 decides to use mm_get_enqcmd_pasid() instead and other users are using iommu_sva_get_pasid() to get the pasid value. Besides, the iommu core accesses iommu_mm_data in the critical section protected by iommu_sva_lock. So no need to add another helper to retrieve PASID atomically. v5: https://lore.kernel.org/linux-iommu/20230925023813.575016-1-tina.zhang@intel.com/ - Order patch "iommu/vt-d: Remove mm->pasid in intel_sva_bind_mm()" first in this series. - Update commit message of patch "iommu: Introduce mm_get_pasid() helper function" - Use smp_store_release() & READ_ONCE() in storing and loading mm's pasid value. v4: https://lore.kernel.org/linux-iommu/20230912125936.722348-1-tina.zhang@intel.com/ - Rebase to v6.6-rc1. v3: https://lore.kernel.org/linux-iommu/20230905000930.24515-1-tina.zhang@intel.com/ - Add a comment describing domain->next. - Expand explanation of why PASID isn't released in iommu_sva_unbind_device(). - Add a patch to remove mm->pasid in intel_sva_bind_mm() v2: https://lore.kernel.org/linux-iommu/20230827084401.819852-1-tina.zhang@intel.com/ - Add mm_get_enqcmd_pasid(). - Update commit message. v1: https://lore.kernel.org/linux-iommu/20230808074944.7825-1-tina.zhang@intel.com/ RFC: https://lore.kernel.org/linux-iommu/20230707013441.365583-1-tina.zhang@intel.com/ Tina Zhang (5): iommu/vt-d: Remove mm->pasid in intel_sva_bind_mm() iommu: Add mm_get_enqcmd_pasid() helper function mm: Add structure to keep sva information iommu: Support mm PASID 1:n with sva domains mm: Deprecate pasid field arch/x86/kernel/traps.c | 2 +- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 14 +-- drivers/iommu/intel/svm.c | 14 +-- drivers/iommu/iommu-sva.c | 94 +++++++++++-------- include/linux/iommu.h | 38 +++++++- include/linux/mm_types.h | 3 +- mm/init-mm.c | 3 - 7 files changed, 108 insertions(+), 60 deletions(-) -- 2.39.3