Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp862214imw; Mon, 4 Jul 2022 22:38:24 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vHxSJfnhyuNuNSr/EkPBJyh3rFA2skR9WGBkpmN4qo2xFfrZnQ/ZER5SEu1PD/ntcuo2ro X-Received: by 2002:aa7:df12:0:b0:43a:4991:1725 with SMTP id c18-20020aa7df12000000b0043a49911725mr13878593edy.55.1656999504239; Mon, 04 Jul 2022 22:38:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656999504; cv=none; d=google.com; s=arc-20160816; b=iMIWLGEB5n2fW+QfB41PTHhGE192rCkjxx+lnfVWWGJWRmW04LljWRbvQgvBsJL7aF 0NKFGK7voyxwehWhxH7Ndo+g28Qqu7Cp3EJIfj2SCJdpTiqajor3jFwd8ZuskKfZ2Je+ XE2d90fKJyxKuh6+sxduK6/X83ggRvu1F/PmW38eiAhWbthkjkWjHh45JiYvKS6FLQkY R0cObA3fWrdHn/mBGcjalSIdFD+JpkWD5PHt2gO+Y9Ax7gPmXdvmtdf1fe2QhfZuUBY7 scccpin+2bo5u+kpDMC0nvw7YTO2ByNBNyu8GHaWSbbLis/sM4qZgRmxVTgiw66bizfz K8Mg== 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=28cnghokKDIyoh0gq0QE6FI/x3eywCuQ7P3lLYxTdYI=; b=TKtCoJ5zQ+ZSRtVJ9v6g+ZgvzAKIhRVzESO8MG4i193qcr5+1Mg5nCQUKckPHGsevE 9v/vhqDmPO0ST/8yu2Dy8iQExGl0p5wtBseDF6FPSQDNLT348jIUv/mfdPEgu/6VASJV Vx3bWVQv/SGTL7N/Xyknf47ZvGvBruAWjX0fkxUcJBkhzPCKyT1fmPMvwl3mw7eOqhlx rxDTr2N3mk22kZLmrkK/4VxzHN1EGHKVesTqORb+luwKqmYKpvCf4eZ2nEYS5vaKqgrg 4o5iiMVVjyHJ9FTwXw/icjJoLwSNzDL5sTT5B9rATpo8I+iyb0T1QczvoX+XquenMPVk /+TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=M068U6dn; 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 hd7-20020a170907968700b0070bf25f1732si13963482ejc.155.2022.07.04.22.37.59; Mon, 04 Jul 2022 22:38:24 -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=M068U6dn; 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 S230013AbiGEFNV (ORCPT + 99 others); Tue, 5 Jul 2022 01:13:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229823AbiGEFMu (ORCPT ); Tue, 5 Jul 2022 01:12:50 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C93E613D25 for ; Mon, 4 Jul 2022 22:12:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656997951; x=1688533951; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PfwS+hiqc+eUZB9Ki3ZTXVTOzQ5KVaVKWRARMEXqlhs=; b=M068U6dnFINA2vjClORJAXM14dUrTzoiF14BrvirA2c1ITsXTnH0w0Qu GG05Yu7MnWnBqMmBi9gGxDgASTUqmgxbk7AxzY1jUYgRBBXPvUJJc5kzR QWTmrlEIos65TGr3yxkTAojZgBcCadNhLRTWydB+iZHvcm4zq6Igxpgz7 op4CHwlDbpabCVimA5ksvU/pOoiLFbEYSvTDlTJEoKU8ShOyqjkLMWsrU 6ugqqCkZMBX8v3iG38zc5riBFKNG+j6i4VD2lrVjcc82sLzElQHJZHIsi 5+YPPsezHRtHewilIfJ1SPykaPC1n8+ns4V0xECxBLvLBjEAaa61e3gNs A==; X-IronPort-AV: E=McAfee;i="6400,9594,10398"; a="282011518" X-IronPort-AV: E=Sophos;i="5.92,245,1650956400"; d="scan'208";a="282011518" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jul 2022 22:12:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,245,1650956400"; d="scan'208";a="542807626" Received: from allen-box.sh.intel.com ([10.239.159.48]) by orsmga003.jf.intel.com with ESMTP; 04 Jul 2022 22:12:26 -0700 From: Lu Baolu To: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Ashok Raj , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Dave Jiang , Vinod Koul Cc: Eric Auger , Liu Yi L , Jacob jun Pan , Zhangfei Gao , Zhu Tony , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Lu Baolu , Jean-Philippe Brucker Subject: [PATCH v10 11/12] iommu: Per-domain I/O page fault handling Date: Tue, 5 Jul 2022 13:07:09 +0800 Message-Id: <20220705050710.2887204-12-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220705050710.2887204-1-baolu.lu@linux.intel.com> References: <20220705050710.2887204-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Tweak the I/O page fault handling framework to route the page faults to the domain and call the page fault handler retrieved from the domain. This makes the I/O page fault handling framework possible to serve more usage scenarios as long as they have an IOMMU domain and install a page fault handler in it. Some unused functions are also removed to avoid dead code. The iommu_get_domain_for_dev_pasid() which retrieves attached domain for a {device, PASID} pair is used. It will be used by the page fault handling framework which knows {device, PASID} reported from the iommu driver. We have a guarantee that the SVA domain doesn't go away during IOPF handling, because unbind() won't free the domain until all the pending page requests have been flushed from the pipeline. The drivers either call iopf_queue_flush_dev() explicitly, or in stall case, the device driver is required to flush all DMAs including stalled transactions before calling unbind(). This also renames iopf_handle_group() to iopf_handler() to avoid confusing. Signed-off-by: Lu Baolu Reviewed-by: Jean-Philippe Brucker Tested-by: Zhangfei Gao Tested-by: Tony Zhu --- drivers/iommu/io-pgfault.c | 68 +++++--------------------------------- 1 file changed, 9 insertions(+), 59 deletions(-) diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index aee9e033012f..d1c522f4ab34 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -69,69 +69,18 @@ static int iopf_complete_group(struct device *dev, struct iopf_fault *iopf, return iommu_page_response(dev, &resp); } -static enum iommu_page_response_code -iopf_handle_single(struct iopf_fault *iopf) -{ - vm_fault_t ret; - struct mm_struct *mm; - struct vm_area_struct *vma; - unsigned int access_flags = 0; - unsigned int fault_flags = FAULT_FLAG_REMOTE; - struct iommu_fault_page_request *prm = &iopf->fault.prm; - enum iommu_page_response_code status = IOMMU_PAGE_RESP_INVALID; - - if (!(prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID)) - return status; - - mm = iommu_sva_find(prm->pasid); - if (IS_ERR_OR_NULL(mm)) - return status; - - mmap_read_lock(mm); - - vma = find_extend_vma(mm, prm->addr); - if (!vma) - /* Unmapped area */ - goto out_put_mm; - - if (prm->perm & IOMMU_FAULT_PERM_READ) - access_flags |= VM_READ; - - if (prm->perm & IOMMU_FAULT_PERM_WRITE) { - access_flags |= VM_WRITE; - fault_flags |= FAULT_FLAG_WRITE; - } - - if (prm->perm & IOMMU_FAULT_PERM_EXEC) { - access_flags |= VM_EXEC; - fault_flags |= FAULT_FLAG_INSTRUCTION; - } - - if (!(prm->perm & IOMMU_FAULT_PERM_PRIV)) - fault_flags |= FAULT_FLAG_USER; - - if (access_flags & ~vma->vm_flags) - /* Access fault */ - goto out_put_mm; - - ret = handle_mm_fault(vma, prm->addr, fault_flags, NULL); - status = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID : - IOMMU_PAGE_RESP_SUCCESS; - -out_put_mm: - mmap_read_unlock(mm); - mmput(mm); - - return status; -} - -static void iopf_handle_group(struct work_struct *work) +static void iopf_handler(struct work_struct *work) { struct iopf_group *group; + struct iommu_domain *domain; struct iopf_fault *iopf, *next; enum iommu_page_response_code status = IOMMU_PAGE_RESP_SUCCESS; group = container_of(work, struct iopf_group, work); + domain = iommu_get_domain_for_dev_pasid(group->dev, + group->last_fault.fault.prm.pasid); + if (!domain || !domain->iopf_handler) + status = IOMMU_PAGE_RESP_INVALID; list_for_each_entry_safe(iopf, next, &group->faults, list) { /* @@ -139,7 +88,8 @@ static void iopf_handle_group(struct work_struct *work) * faults in the group if there is an error. */ if (status == IOMMU_PAGE_RESP_SUCCESS) - status = iopf_handle_single(iopf); + status = domain->iopf_handler(&iopf->fault, + domain->fault_data); if (!(iopf->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) @@ -242,7 +192,7 @@ int iommu_queue_iopf(struct iommu_fault *fault, void *cookie) group->last_fault.fault = *fault; INIT_LIST_HEAD(&group->faults); list_add(&group->last_fault.list, &group->faults); - INIT_WORK(&group->work, iopf_handle_group); + INIT_WORK(&group->work, iopf_handler); /* See if we have partial faults for this group */ list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) { -- 2.25.1