From nobody Tue Feb 10 04:23:42 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06387C7618A for ; Mon, 20 Mar 2023 19:04:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231363AbjCTTEN (ORCPT ); Mon, 20 Mar 2023 15:04:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230243AbjCTTDi (ORCPT ); Mon, 20 Mar 2023 15:03:38 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F0AC34C19; Mon, 20 Mar 2023 11:55:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679338552; x=1710874552; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EQdjWDWKokNf3osIh/i7zKmgzVK+4sLJSkv4CZDiQfg=; b=a7RUbIlltFbvR0t/WeZm70s2eULmAmi3O4HQhzrb79sO9RZOyMSkWbvS 8gRQGxLjeOmL4of4an5UvIzSz+flWlk+uDsVh6+iz4ke8wSaTr+mXINWx cUXAYG8X5OAsVWQgwQmJOM2hGi/czayQJ332a6wEkzt84rMzuEWeSUs2V rzW2wA/Jmy8qzmvbPCw8ng9bzpycubT693IUK+4ZLCaYJOjWYI5bPtOr9 osQyZMLzmoupo0OEXIhtGxSdYhTQ4RLqdn625f+PfCVN4nAt7NwnmlQ/o ywqeJNQB7GaBCq+wQeZMUzpbKJ/cDsjMpihX/TsKVJt8++sw41LOhwVMr w==; X-IronPort-AV: E=McAfee;i="6600,9927,10655"; a="366473718" X-IronPort-AV: E=Sophos;i="5.98,276,1673942400"; d="scan'208";a="366473718" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Mar 2023 11:55:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10655"; a="927076461" X-IronPort-AV: E=Sophos;i="5.98,276,1673942400"; d="scan'208";a="927076461" Received: from srinivas-otcpl-7600.jf.intel.com (HELO jacob-builder.jf.intel.com) ([10.54.39.106]) by fmsmga006.fm.intel.com with ESMTP; 20 Mar 2023 11:55:15 -0700 From: Jacob Pan To: LKML , iommu@lists.linux.dev, Jason Gunthorpe , "Lu Baolu" , Joerg Roedel , Jean-Philippe Brucker , "Robin Murphy" , Dave Hansen , Thomas Gleixner , X86 Kernel , bp@alien8.de, "H. Peter Anvin" , Peter Zijlstra , corbet@lwn.net, vkoul@kernel.org, dmaengine@vger.kernel.org, linux-doc@vger.kernel.org Cc: "Will Deacon" , David Woodhouse , Raj Ashok , "Tian, Kevin" , Yi Liu , "Yu, Fenghua" , Dave Jiang , Kirill Shutemov , Tony Luck , Jacob Pan Subject: [PATCH v7 2/7] iommu/sva: Move PASID helpers to sva code Date: Mon, 20 Mar 2023 11:59:05 -0700 Message-Id: <20230320185910.2643260-3-jacob.jun.pan@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230320185910.2643260-1-jacob.jun.pan@linux.intel.com> References: <20230320185910.2643260-1-jacob.jun.pan@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Preparing to remove IOASID infrastructure, PASID management will be under SVA code. Decouple mm code from IOASID. Reviewed-by: Jason Gunthorpe Signed-off-by: Jacob Pan --- v7: - use CONFIG_IOMMU_SVA around PASID helpers in fork.c, Fixes a compile problem. v6: - put helpers under iommu.h instead of iommu-helper.h v5: - move definition of helpers to iommu code to be consistent with declarations. (Kevin) - fix patch partitioning bug (Baolu) v4: - delete and open code mm_set_pasid - keep mm_init_pasid() as inline for fork performance --- drivers/iommu/iommu-sva.c | 10 +++++++++- include/linux/ioasid.h | 6 +----- include/linux/iommu.h | 14 +++++++++++++- include/linux/sched/mm.h | 26 -------------------------- kernel/fork.c | 5 +++++ 5 files changed, 28 insertions(+), 33 deletions(-) diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 24bf9b2b58aa..fcfdc80a3939 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -44,7 +44,7 @@ int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t = min, ioasid_t max) if (!pasid_valid(pasid)) ret =3D -ENOMEM; else - mm_pasid_set(mm, pasid); + mm->pasid =3D pasid; out: mutex_unlock(&iommu_sva_lock); return ret; @@ -238,3 +238,11 @@ iommu_sva_handle_iopf(struct iommu_fault *fault, void = *data) =20 return status; } + +void mm_pasid_drop(struct mm_struct *mm) +{ + if (pasid_valid(mm->pasid)) { + ioasid_free(mm->pasid); + mm->pasid =3D INVALID_IOASID; + } +} diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h index af1c9d62e642..bdee937da907 100644 --- a/include/linux/ioasid.h +++ b/include/linux/ioasid.h @@ -4,8 +4,8 @@ =20 #include #include +#include =20 -#define INVALID_IOASID ((ioasid_t)-1) typedef unsigned int ioasid_t; typedef ioasid_t (*ioasid_alloc_fn_t)(ioasid_t min, ioasid_t max, void *da= ta); typedef void (*ioasid_free_fn_t)(ioasid_t ioasid, void *data); @@ -40,10 +40,6 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasi= d, int ioasid_register_allocator(struct ioasid_allocator_ops *allocator); void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); int ioasid_set_data(ioasid_t ioasid, void *data); -static inline bool pasid_valid(ioasid_t ioasid) -{ - return ioasid !=3D INVALID_IOASID; -} =20 #else /* !CONFIG_IOASID */ static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 6595454d4f48..d3f81dc6e4dd 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -13,7 +13,6 @@ #include #include #include -#include #include =20 #define IOMMU_READ (1 << 0) @@ -192,6 +191,8 @@ enum iommu_dev_features { }; =20 #define IOMMU_PASID_INVALID (-1U) +typedef unsigned int ioasid_t; +#define INVALID_IOASID ((ioasid_t)-1) =20 #ifdef CONFIG_IOMMU_API =20 @@ -1172,7 +1173,16 @@ static inline bool tegra_dev_iommu_get_stream_id(str= uct device *dev, u32 *stream return false; } =20 +static inline bool pasid_valid(ioasid_t ioasid) +{ + return ioasid !=3D INVALID_IOASID; +} #ifdef CONFIG_IOMMU_SVA +static inline void mm_pasid_init(struct mm_struct *mm) +{ + mm->pasid =3D INVALID_IOASID; +} +void mm_pasid_drop(struct mm_struct *mm); struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm); void iommu_sva_unbind_device(struct iommu_sva *handle); @@ -1192,6 +1202,8 @@ static inline u32 iommu_sva_get_pasid(struct iommu_sv= a *handle) { return IOMMU_PASID_INVALID; } +static inline void mm_pasid_init(struct mm_struct *mm) {} +static inline void mm_pasid_drop(struct mm_struct *mm) {} #endif /* CONFIG_IOMMU_SVA */ =20 #endif /* __LINUX_IOMMU_H */ diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index 2a243616f222..da9712a3ba73 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -8,7 +8,6 @@ #include #include #include -#include =20 /* * Routines for handling mm_structs @@ -451,29 +450,4 @@ static inline void membarrier_update_current_mm(struct= mm_struct *next_mm) } #endif =20 -#ifdef CONFIG_IOMMU_SVA -static inline void mm_pasid_init(struct mm_struct *mm) -{ - mm->pasid =3D INVALID_IOASID; -} - -/* Associate a PASID with an mm_struct: */ -static inline void mm_pasid_set(struct mm_struct *mm, u32 pasid) -{ - mm->pasid =3D pasid; -} - -static inline void mm_pasid_drop(struct mm_struct *mm) -{ - if (pasid_valid(mm->pasid)) { - ioasid_free(mm->pasid); - mm->pasid =3D INVALID_IOASID; - } -} -#else -static inline void mm_pasid_init(struct mm_struct *mm) {} -static inline void mm_pasid_set(struct mm_struct *mm, u32 pasid) {} -static inline void mm_pasid_drop(struct mm_struct *mm) {} -#endif - #endif /* _LINUX_SCHED_MM_H */ diff --git a/kernel/fork.c b/kernel/fork.c index f68954d05e89..a53be375a2dd 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -97,6 +97,7 @@ #include #include #include +#include =20 #include #include @@ -794,7 +795,9 @@ void __mmdrop(struct mm_struct *mm) mmu_notifier_subscriptions_destroy(mm); check_mm(mm); put_user_ns(mm->user_ns); +#ifdef CONFIG_IOMMU_SVA=09 mm_pasid_drop(mm); +#endif =20 for (i =3D 0; i < NR_MM_COUNTERS; i++) percpu_counter_destroy(&mm->rss_stat[i]); @@ -1140,7 +1143,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm= , struct task_struct *p, mm_init_cpumask(mm); mm_init_aio(mm); mm_init_owner(mm, p); +#ifdef CONFIG_IOMMU_SVA=09 mm_pasid_init(mm); +#endif RCU_INIT_POINTER(mm->exe_file, NULL); mmu_notifier_subscriptions_init(mm); init_tlb_flush_pending(mm); --=20 2.25.1