Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp6632406rwl; Wed, 22 Mar 2023 13:16:41 -0700 (PDT) X-Google-Smtp-Source: AK7set+cKdNT3aVxImr/Y8g8XP8r3+n1wz7g5FlQw7C3gp8fh2BBnqn2VN1C9PQWdlrCVWGHyXCX X-Received: by 2002:a17:906:2443:b0:920:3263:d643 with SMTP id a3-20020a170906244300b009203263d643mr7957645ejb.72.1679516201254; Wed, 22 Mar 2023 13:16:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679516201; cv=none; d=google.com; s=arc-20160816; b=oH53yAXpagWuSVvtxJfSsIO0O0MT9KuqzUsB7uzmEyYPB9S9Oanp+fR/JgJ4KrsV+v C54iaTU2I8JtB9ZM0jwZr4ZlDdpnDd40Yw+XIsj+wrDUKbPLpUfyOHD/XhgJM0hr+A7G 8nUz6QVUdyXPmasNF+ZUNmwmyfrl/Y+iQVp5izmNSeZ+jI0yzjpEhEY/5mN5S46ZT8z6 rteUS6SLBPpFF9DJJMa7ianJcAz3moBekN0ZVqx7hVZj6I7wI1dkbjEU+xl2TrcFOghR 8jzTnN2/wvmIk/e+oM0TTMEICp7Jq8vWQ2x3WdwV1jxUVoXP+cFASpEdu9wO4BcVhB67 dX+Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pzMs0ZsbG7MzfTSRzibCANh+KQNxuxZ6azzoFHj9L7A=; b=jVWHe/oMxnOVrVLRj0cwSG25TNs6ejRZU+ATSrKm1iQ6uUzpUr1TZt8VaNDZr07nim EOyfjd9HWAOajkLmho0GSHcGbJsp9p7lcq1qkjdsXOZDk/cghhTdSZW+2+a5OUwoT1Ac ffdllaysUAn9oKZbGOOmO495ZIN+b6gqMXY5pkVkQdEXNEsKzjh2gkYaJ9LCnccYuNLj SUHgHWPQnilmQbMLp8kG23610z9frJ00VGYQgaCAF6MSIRWc1G0mB0iBbfzD6Zu875BQ 7Ntfoc7FBVxDXDq1Ao9sXeWxqrKH0056zZhJNjXHFVMnVuZ6hpcBACfnTyjTqVeC9E1g jUqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VkHiRxsi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw40-20020a1709066a2800b009333dc257b0si14764450ejc.350.2023.03.22.13.16.16; Wed, 22 Mar 2023 13:16:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VkHiRxsi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232220AbjCVUPt (ORCPT + 99 others); Wed, 22 Mar 2023 16:15:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232056AbjCVUOW (ORCPT ); Wed, 22 Mar 2023 16:14:22 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9A7D72B37; Wed, 22 Mar 2023 13:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679515530; x=1711051530; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lKVfWMpIPWoiZ/aB8Wo2J/u99AjpxombAxUewYxaeoQ=; b=VkHiRxsi/gEsuogTvQDYaVCue3xNXGBWG1PAku3H+jD1bcpM7oOSyqMG Ww4Y/IQusue8xeOzXOwWrrbma/w1UUMkEqf90XMih9EJKZgoEblPUFBQm +1OlBbBooQR2s8JBl5Y9q2aPBTnqMr4uosSILVHBqU+g8G69jx3753fy/ bEnrQSjqEv4U7RWk4lh4HG9sOrNpmqxuP2jnv3+4VaSIbSMaIUgvcjKG9 BDqJ1ZAuo95VsYf317NMMBMX3j1+c0/BWbfbT/INJxnuKhJiheJTq5vHf GI5w0SH/hQWS1LFn41jAIwsdakTmE9KhCOo8cJTKG1IJkAGwcHy/4jLBw Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10657"; a="339356809" X-IronPort-AV: E=Sophos;i="5.98,282,1673942400"; d="scan'208";a="339356809" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Mar 2023 13:04:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10657"; a="682039328" X-IronPort-AV: E=Sophos;i="5.98,282,1673942400"; d="scan'208";a="682039328" Received: from srinivas-otcpl-7600.jf.intel.com (HELO jacob-builder.jf.intel.com) ([10.54.39.106]) by orsmga002.jf.intel.com with ESMTP; 22 Mar 2023 13:04:04 -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 v8 2/7] iommu/sva: Move PASID helpers to sva code Date: Wed, 22 Mar 2023 13:07:58 -0700 Message-Id: <20230322200803.869130-3-jacob.jun.pan@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230322200803.869130-1-jacob.jun.pan@linux.intel.com> References: <20230322200803.869130-1-jacob.jun.pan@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- v8: -remove dummy inline pasid_valid() and #ifdef CONFIG_IOMMU_SVA 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 | 11 +---------- include/linux/iommu.h | 14 +++++++++++++- include/linux/sched/mm.h | 26 -------------------------- kernel/fork.c | 1 + 5 files changed, 24 insertions(+), 38 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 = -ENOMEM; else - mm_pasid_set(mm, pasid); + mm->pasid = pasid; out: mutex_unlock(&iommu_sva_lock); return ret; @@ -238,3 +238,11 @@ iommu_sva_handle_iopf(struct iommu_fault *fault, void *data) return status; } + +void mm_pasid_drop(struct mm_struct *mm) +{ + if (pasid_valid(mm->pasid)) { + ioasid_free(mm->pasid); + mm->pasid = INVALID_IOASID; + } +} diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h index af1c9d62e642..734bf0e036ee 100644 --- a/include/linux/ioasid.h +++ b/include/linux/ioasid.h @@ -4,8 +4,8 @@ #include #include +#include -#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 *data); typedef void (*ioasid_free_fn_t)(ioasid_t ioasid, void *data); @@ -40,10 +40,6 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, 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 != INVALID_IOASID; -} #else /* !CONFIG_IOASID */ static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, @@ -74,10 +70,5 @@ static inline int ioasid_set_data(ioasid_t ioasid, void *data) return -ENOTSUPP; } -static inline bool pasid_valid(ioasid_t ioasid) -{ - return false; -} - #endif /* CONFIG_IOASID */ #endif /* __LINUX_IOASID_H */ 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 #define IOMMU_READ (1 << 0) @@ -192,6 +191,8 @@ enum iommu_dev_features { }; #define IOMMU_PASID_INVALID (-1U) +typedef unsigned int ioasid_t; +#define INVALID_IOASID ((ioasid_t)-1) #ifdef CONFIG_IOMMU_API @@ -1172,7 +1173,16 @@ static inline bool tegra_dev_iommu_get_stream_id(struct device *dev, u32 *stream return false; } +static inline bool pasid_valid(ioasid_t ioasid) +{ + return ioasid != INVALID_IOASID; +} #ifdef CONFIG_IOMMU_SVA +static inline void mm_pasid_init(struct mm_struct *mm) +{ + mm->pasid = 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_sva *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 */ #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 /* * Routines for handling mm_structs @@ -451,29 +450,4 @@ static inline void membarrier_update_current_mm(struct mm_struct *next_mm) } #endif -#ifdef CONFIG_IOMMU_SVA -static inline void mm_pasid_init(struct mm_struct *mm) -{ - mm->pasid = INVALID_IOASID; -} - -/* Associate a PASID with an mm_struct: */ -static inline void mm_pasid_set(struct mm_struct *mm, u32 pasid) -{ - mm->pasid = pasid; -} - -static inline void mm_pasid_drop(struct mm_struct *mm) -{ - if (pasid_valid(mm->pasid)) { - ioasid_free(mm->pasid); - mm->pasid = 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..a0ea68a2bd19 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -97,6 +97,7 @@ #include #include #include +#include #include #include -- 2.25.1