From patchwork Mon Mar 13 20:41:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Pan X-Patchwork-Id: 13173249 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 A1F9FC76195 for ; Mon, 13 Mar 2023 20:38:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230193AbjCMUiK (ORCPT ); Mon, 13 Mar 2023 16:38:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230151AbjCMUiJ (ORCPT ); Mon, 13 Mar 2023 16:38:09 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C66DC86B1; Mon, 13 Mar 2023 13:38:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678739887; x=1710275887; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1el/pTz7vQc91OOUaA4iBOglmnT0mWTAcowQ+03ZrIo=; b=O8DanLdL3LNqPTLAJfgSl7myyKgW1R8JuN+yh9qqhTc4EGOVR+/slbD1 9DTA7Tc7BTvcQiIWhVpIt8vgy0VmbbuqJeNN/uRQklvkIUBwzNKSoYPyz RwsxS7NM6frFEgYT/KbnstfxeqQaFedAV00feEkckwSMKddLdg5BwyL8J BM9eym0X2k06wWlEqu0gXXuUuXtUPxhtD52+zmNZ1yszXGFZN+MRgiWko SkAy7uMgMpqqDNvDxXQjwfweSRe4LJSqXAhj/w7CGhK8KBTVmCA+YdMPD miiM5ai01k/PEViWuvM4q0h9gWb80en/pZ9/v9jVuVsKTwG3eKs2ZhwhK Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10648"; a="334732072" X-IronPort-AV: E=Sophos;i="5.98,257,1673942400"; d="scan'208";a="334732072" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Mar 2023 13:38:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10648"; a="802584781" X-IronPort-AV: E=Sophos;i="5.98,257,1673942400"; d="scan'208";a="802584781" Received: from srinivas-otcpl-7600.jf.intel.com (HELO jacob-builder.jf.intel.com) ([10.54.39.106]) by orsmga004.jf.intel.com with ESMTP; 13 Mar 2023 13:38:05 -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 v6 4/7] iommu/sva: Stop using ioasid_set for SVA Date: Mon, 13 Mar 2023 13:41:55 -0700 Message-Id: <20230313204158.1495067-5-jacob.jun.pan@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230313204158.1495067-1-jacob.jun.pan@linux.intel.com> References: <20230313204158.1495067-1-jacob.jun.pan@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: dmaengine@vger.kernel.org From: Jason Gunthorpe Instead SVA drivers can use a simple global IDA to allocate PASIDs for each mm_struct. Future work would be to allow drivers using the SVA APIs to reserve global PASIDs from this IDA for their internal use, eg with the DMA API PASID support. Signed-off-by: Jason Gunthorpe Signed-off-by: Jacob Pan Reviewed-by: Lu Baolu --- v5: - Put removing iommu_sva_find() to a separate patch (Kevin) - Make pasid allocation range to be inclusive (Tina) - Simplified return code handling (Baolu) v4: - Keep GFP_ATOMIC flag for PASID allocation, will changed to GFP_KERNEL in a separate patch. --- drivers/iommu/iommu-sva.c | 43 ++++++++++++++------------------------- drivers/iommu/iommu-sva.h | 2 -- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 4f357ef14f04..d4640731727a 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -9,47 +9,34 @@ #include "iommu-sva.h" static DEFINE_MUTEX(iommu_sva_lock); -static DECLARE_IOASID_SET(iommu_sva_pasid); +static DEFINE_IDA(iommu_global_pasid_ida); -/** - * iommu_sva_alloc_pasid - Allocate a PASID for the mm - * @mm: the mm - * @min: minimum PASID value (inclusive) - * @max: maximum PASID value (inclusive) - * - * Try to allocate a PASID for this mm, or take a reference to the existing one - * provided it fits within the [@min, @max] range. On success the PASID is - * available in mm->pasid and will be available for the lifetime of the mm. - * - * Returns 0 on success and < 0 on error. - */ -int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max) +/* Allocate a PASID for the mm within range (inclusive) */ +static int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max) { int ret = 0; - ioasid_t pasid; - if (min == INVALID_IOASID || max == INVALID_IOASID || - min == 0 || max < min) + if (!pasid_valid(min) || !pasid_valid(max) || + min == 0 || max < min) return -EINVAL; mutex_lock(&iommu_sva_lock); /* Is a PASID already associated with this mm? */ if (pasid_valid(mm->pasid)) { - if (mm->pasid < min || mm->pasid >= max) + if (mm->pasid < min || mm->pasid > max) ret = -EOVERFLOW; goto out; } - pasid = ioasid_alloc(&iommu_sva_pasid, min, max, mm); - if (!pasid_valid(pasid)) - ret = -ENOMEM; - else - mm->pasid = pasid; + ret = ida_alloc_range(&iommu_global_pasid_ida, min, max, GFP_ATOMIC); + if (ret < 0) + goto out; + mm->pasid = ret; + ret = 0; out: mutex_unlock(&iommu_sva_lock); return ret; } -EXPORT_SYMBOL_GPL(iommu_sva_alloc_pasid); /** * iommu_sva_bind_device() - Bind a process address space to a device @@ -221,8 +208,8 @@ iommu_sva_handle_iopf(struct iommu_fault *fault, void *data) void mm_pasid_drop(struct mm_struct *mm) { - if (pasid_valid(mm->pasid)) { - ioasid_free(mm->pasid); - mm->pasid = INVALID_IOASID; - } + if (likely(!pasid_valid(mm->pasid))) + return; + + ida_free(&iommu_global_pasid_ida, mm->pasid); } diff --git a/drivers/iommu/iommu-sva.h b/drivers/iommu/iommu-sva.h index 102eae1817a2..c22d0174ad61 100644 --- a/drivers/iommu/iommu-sva.h +++ b/drivers/iommu/iommu-sva.h @@ -8,8 +8,6 @@ #include #include -int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max); - /* I/O Page fault */ struct device; struct iommu_fault;