From patchwork Tue May 30 05:37:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100540 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1954667vqr; Mon, 29 May 2023 22:46:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4qT+AC/NJsyWULwOD518FOtA7J3u7VqPrG1R4hTKWrH2S22FJWTmXh5pvgkcRrYQIZbsOj X-Received: by 2002:a17:90b:4a09:b0:256:5a84:c837 with SMTP id kk9-20020a17090b4a0900b002565a84c837mr1365971pjb.39.1685425578762; Mon, 29 May 2023 22:46:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685425578; cv=none; d=google.com; s=arc-20160816; b=zWc4E9CzwcKWPovPqUGG/jhvJJx1P1BcYO7ocy8yRUCTRweYkP3xDavdH6GY/KaqAq oSJgSvK/IC990S9ciBSvztGiNagAQngD729sl5nW09/pPIomGUYOphyRPlDePuw9i5rR YRMDeLK3xoLNHuBj2ZJFlTu8VsNenN80ovlJyEV2cmgddS3rfiNM8RmkPQdXF3elj45b 0cGnoNqAWfqCHEAUeq70Z9DfVaTzExzdMceYqfv8/2nIQ1/wfdxqv9GXOjTZLlsZjVip ucrf9kYdjyf9XyMgFR9WXRGD94E+ZoVLruTiyL5jVBtq6EsjSsaw0kmOnhhifMJ+oJja ux4g== 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=f4EfnrFthdrzuObp8x4uAWWjEfbmxaP3K9vbzHR7i/c=; b=H8wo3VFzRmZhej+39NkO5ZXjI58R3QIHZ/VTpHJK0D0eSIBKtH0IrvI4EXkuAcYQUD +B/3msEIw5eaB06qzeqHaNzcTifG/cG28pKjhejWptOOaJL/ZUS9iux5R0osYKZfBvOU NPqd5fw74qpNd534DzGhqkP96LNhNQUN7Bu+0lTG374syGbvEybbZHMRE1W4wfuqbC3R /3HGeXbwGd7VbZeHFucGmqLFuvuTENQsER3+T2kVGCH/LKbsAcgFkLvKaAP4Bgf5F8+9 hsbOGrvSSbLopgWVddumB0RWcEnVoJeR+CYCZnqIuohgTYFPt+opuvodM5ZAVCBKETKp sYQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mGHEIFdb; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g7-20020a17090a640700b00256a7c340b1si1259382pjj.3.2023.05.29.22.46.07; Mon, 29 May 2023 22:46:18 -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=mGHEIFdb; 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 S230209AbjE3Fie (ORCPT + 99 others); Tue, 30 May 2023 01:38:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230202AbjE3Fib (ORCPT ); Tue, 30 May 2023 01:38:31 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C50C8EC; Mon, 29 May 2023 22:38:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425109; x=1716961109; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=juS/ea+8IMhhaMgeSEe/oqHBsTNMj3WFwDhkt8yVgZg=; b=mGHEIFdb+YrP9+rIXbhi4nq8IIcnZ8/LF6K692IC+pL57uSc/9W4GqVA /st6xwReGot20NC0WRkSxKPTS0sglDpoPgbrEv6hqiOFmQ6t4i0/Fswm7 KlReE/zOdCi2Odmd58fAMh2v0RAxt8pQI3h4iDaqgDysveDUiwo29i5OF rJYznATwjEk8vqqdXZpKkRkVky8opMDrFyKBqZ5QgEHJX+iQDmjV/M+Az U1WelLrfJNYdcsY4EAv0y0vnvpI/RnKE1tdv/n/SshHK2laB9oaPwhGqX wNbf6vyOhbuTPApd8gvJgCiMSCTR46eZrty69WntEwSzfnN+hnBZ/bVgF g==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579592" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579592" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369369" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369369" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:24 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 01/17] iommu: Move iommu fault data to linux/iommu.h Date: Tue, 30 May 2023 13:37:08 +0800 Message-Id: <20230530053724.232765-2-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767296811717420339?= X-GMAIL-MSGID: =?utf-8?q?1767296811717420339?= The iommu fault data is currently defined in uapi/linux/iommu.h, but is only used inside the iommu subsystem. Move it to linux/iommu.h, where it will be more accessible to kernel drivers. With this done, uapi/linux/iommu.h becomes empty and can be removed from the tree. And we can further discuss how to define the iommu fault data that iommufd could use to route the faults to user space and handle the fault response if needed. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 151 +++++++++++++++++++++++++++++++++- include/uapi/linux/iommu.h | 161 ------------------------------------- MAINTAINERS | 1 - 3 files changed, 150 insertions(+), 163 deletions(-) delete mode 100644 include/uapi/linux/iommu.h diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 8ff1bb3a4e1a..d6a93de7d1dd 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) @@ -43,6 +42,156 @@ struct iommu_sva; struct iommu_fault_event; struct iommu_dma_cookie; +#define IOMMU_FAULT_PERM_READ (1 << 0) /* read */ +#define IOMMU_FAULT_PERM_WRITE (1 << 1) /* write */ +#define IOMMU_FAULT_PERM_EXEC (1 << 2) /* exec */ +#define IOMMU_FAULT_PERM_PRIV (1 << 3) /* privileged */ + +/* Generic fault types, can be expanded IRQ remapping fault */ +enum iommu_fault_type { + IOMMU_FAULT_DMA_UNRECOV = 1, /* unrecoverable fault */ + IOMMU_FAULT_PAGE_REQ, /* page request fault */ +}; + +enum iommu_fault_reason { + IOMMU_FAULT_REASON_UNKNOWN = 0, + + /* Could not access the PASID table (fetch caused external abort) */ + IOMMU_FAULT_REASON_PASID_FETCH, + + /* PASID entry is invalid or has configuration errors */ + IOMMU_FAULT_REASON_BAD_PASID_ENTRY, + + /* + * PASID is out of range (e.g. exceeds the maximum PASID + * supported by the IOMMU) or disabled. + */ + IOMMU_FAULT_REASON_PASID_INVALID, + + /* + * An external abort occurred fetching (or updating) a translation + * table descriptor + */ + IOMMU_FAULT_REASON_WALK_EABT, + + /* + * Could not access the page table entry (Bad address), + * actual translation fault + */ + IOMMU_FAULT_REASON_PTE_FETCH, + + /* Protection flag check failed */ + IOMMU_FAULT_REASON_PERMISSION, + + /* access flag check failed */ + IOMMU_FAULT_REASON_ACCESS, + + /* Output address of a translation stage caused Address Size fault */ + IOMMU_FAULT_REASON_OOR_ADDRESS, +}; + +/** + * struct iommu_fault_unrecoverable - Unrecoverable fault data + * @reason: reason of the fault, from &enum iommu_fault_reason + * @flags: parameters of this fault (IOMMU_FAULT_UNRECOV_* values) + * @pasid: Process Address Space ID + * @perm: requested permission access using by the incoming transaction + * (IOMMU_FAULT_PERM_* values) + * @addr: offending page address + * @fetch_addr: address that caused a fetch abort, if any + */ +struct iommu_fault_unrecoverable { + __u32 reason; +#define IOMMU_FAULT_UNRECOV_PASID_VALID (1 << 0) +#define IOMMU_FAULT_UNRECOV_ADDR_VALID (1 << 1) +#define IOMMU_FAULT_UNRECOV_FETCH_ADDR_VALID (1 << 2) + __u32 flags; + __u32 pasid; + __u32 perm; + __u64 addr; + __u64 fetch_addr; +}; + +/** + * struct iommu_fault_page_request - Page Request data + * @flags: encodes whether the corresponding fields are valid and whether this + * is the last page in group (IOMMU_FAULT_PAGE_REQUEST_* values). + * When IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID is set, the page response + * must have the same PASID value as the page request. When it is clear, + * the page response should not have a PASID. + * @pasid: Process Address Space ID + * @grpid: Page Request Group Index + * @perm: requested page permissions (IOMMU_FAULT_PERM_* values) + * @addr: page address + * @private_data: device-specific private information + */ +struct iommu_fault_page_request { +#define IOMMU_FAULT_PAGE_REQUEST_PASID_VALID (1 << 0) +#define IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE (1 << 1) +#define IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA (1 << 2) +#define IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID (1 << 3) + __u32 flags; + __u32 pasid; + __u32 grpid; + __u32 perm; + __u64 addr; + __u64 private_data[2]; +}; + +/** + * struct iommu_fault - Generic fault data + * @type: fault type from &enum iommu_fault_type + * @padding: reserved for future use (should be zero) + * @event: fault event, when @type is %IOMMU_FAULT_DMA_UNRECOV + * @prm: Page Request message, when @type is %IOMMU_FAULT_PAGE_REQ + * @padding2: sets the fault size to allow for future extensions + */ +struct iommu_fault { + __u32 type; + __u32 padding; + union { + struct iommu_fault_unrecoverable event; + struct iommu_fault_page_request prm; + __u8 padding2[56]; + }; +}; + +/** + * enum iommu_page_response_code - Return status of fault handlers + * @IOMMU_PAGE_RESP_SUCCESS: Fault has been handled and the page tables + * populated, retry the access. This is "Success" in PCI PRI. + * @IOMMU_PAGE_RESP_FAILURE: General error. Drop all subsequent faults from + * this device if possible. This is "Response Failure" in PCI PRI. + * @IOMMU_PAGE_RESP_INVALID: Could not handle this fault, don't retry the + * access. This is "Invalid Request" in PCI PRI. + */ +enum iommu_page_response_code { + IOMMU_PAGE_RESP_SUCCESS = 0, + IOMMU_PAGE_RESP_INVALID, + IOMMU_PAGE_RESP_FAILURE, +}; + +/** + * struct iommu_page_response - Generic page response information + * @argsz: User filled size of this data + * @version: API version of this structure + * @flags: encodes whether the corresponding fields are valid + * (IOMMU_FAULT_PAGE_RESPONSE_* values) + * @pasid: Process Address Space ID + * @grpid: Page Request Group Index + * @code: response code from &enum iommu_page_response_code + */ +struct iommu_page_response { + __u32 argsz; +#define IOMMU_PAGE_RESP_VERSION_1 1 + __u32 version; +#define IOMMU_PAGE_RESP_PASID_VALID (1 << 0) + __u32 flags; + __u32 pasid; + __u32 grpid; + __u32 code; +}; + /* iommu fault flags */ #define IOMMU_FAULT_READ 0x0 #define IOMMU_FAULT_WRITE 0x1 diff --git a/include/uapi/linux/iommu.h b/include/uapi/linux/iommu.h deleted file mode 100644 index 65d8b0234f69..000000000000 --- a/include/uapi/linux/iommu.h +++ /dev/null @@ -1,161 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ -/* - * IOMMU user API definitions - */ - -#ifndef _UAPI_IOMMU_H -#define _UAPI_IOMMU_H - -#include - -#define IOMMU_FAULT_PERM_READ (1 << 0) /* read */ -#define IOMMU_FAULT_PERM_WRITE (1 << 1) /* write */ -#define IOMMU_FAULT_PERM_EXEC (1 << 2) /* exec */ -#define IOMMU_FAULT_PERM_PRIV (1 << 3) /* privileged */ - -/* Generic fault types, can be expanded IRQ remapping fault */ -enum iommu_fault_type { - IOMMU_FAULT_DMA_UNRECOV = 1, /* unrecoverable fault */ - IOMMU_FAULT_PAGE_REQ, /* page request fault */ -}; - -enum iommu_fault_reason { - IOMMU_FAULT_REASON_UNKNOWN = 0, - - /* Could not access the PASID table (fetch caused external abort) */ - IOMMU_FAULT_REASON_PASID_FETCH, - - /* PASID entry is invalid or has configuration errors */ - IOMMU_FAULT_REASON_BAD_PASID_ENTRY, - - /* - * PASID is out of range (e.g. exceeds the maximum PASID - * supported by the IOMMU) or disabled. - */ - IOMMU_FAULT_REASON_PASID_INVALID, - - /* - * An external abort occurred fetching (or updating) a translation - * table descriptor - */ - IOMMU_FAULT_REASON_WALK_EABT, - - /* - * Could not access the page table entry (Bad address), - * actual translation fault - */ - IOMMU_FAULT_REASON_PTE_FETCH, - - /* Protection flag check failed */ - IOMMU_FAULT_REASON_PERMISSION, - - /* access flag check failed */ - IOMMU_FAULT_REASON_ACCESS, - - /* Output address of a translation stage caused Address Size fault */ - IOMMU_FAULT_REASON_OOR_ADDRESS, -}; - -/** - * struct iommu_fault_unrecoverable - Unrecoverable fault data - * @reason: reason of the fault, from &enum iommu_fault_reason - * @flags: parameters of this fault (IOMMU_FAULT_UNRECOV_* values) - * @pasid: Process Address Space ID - * @perm: requested permission access using by the incoming transaction - * (IOMMU_FAULT_PERM_* values) - * @addr: offending page address - * @fetch_addr: address that caused a fetch abort, if any - */ -struct iommu_fault_unrecoverable { - __u32 reason; -#define IOMMU_FAULT_UNRECOV_PASID_VALID (1 << 0) -#define IOMMU_FAULT_UNRECOV_ADDR_VALID (1 << 1) -#define IOMMU_FAULT_UNRECOV_FETCH_ADDR_VALID (1 << 2) - __u32 flags; - __u32 pasid; - __u32 perm; - __u64 addr; - __u64 fetch_addr; -}; - -/** - * struct iommu_fault_page_request - Page Request data - * @flags: encodes whether the corresponding fields are valid and whether this - * is the last page in group (IOMMU_FAULT_PAGE_REQUEST_* values). - * When IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID is set, the page response - * must have the same PASID value as the page request. When it is clear, - * the page response should not have a PASID. - * @pasid: Process Address Space ID - * @grpid: Page Request Group Index - * @perm: requested page permissions (IOMMU_FAULT_PERM_* values) - * @addr: page address - * @private_data: device-specific private information - */ -struct iommu_fault_page_request { -#define IOMMU_FAULT_PAGE_REQUEST_PASID_VALID (1 << 0) -#define IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE (1 << 1) -#define IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA (1 << 2) -#define IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID (1 << 3) - __u32 flags; - __u32 pasid; - __u32 grpid; - __u32 perm; - __u64 addr; - __u64 private_data[2]; -}; - -/** - * struct iommu_fault - Generic fault data - * @type: fault type from &enum iommu_fault_type - * @padding: reserved for future use (should be zero) - * @event: fault event, when @type is %IOMMU_FAULT_DMA_UNRECOV - * @prm: Page Request message, when @type is %IOMMU_FAULT_PAGE_REQ - * @padding2: sets the fault size to allow for future extensions - */ -struct iommu_fault { - __u32 type; - __u32 padding; - union { - struct iommu_fault_unrecoverable event; - struct iommu_fault_page_request prm; - __u8 padding2[56]; - }; -}; - -/** - * enum iommu_page_response_code - Return status of fault handlers - * @IOMMU_PAGE_RESP_SUCCESS: Fault has been handled and the page tables - * populated, retry the access. This is "Success" in PCI PRI. - * @IOMMU_PAGE_RESP_FAILURE: General error. Drop all subsequent faults from - * this device if possible. This is "Response Failure" in PCI PRI. - * @IOMMU_PAGE_RESP_INVALID: Could not handle this fault, don't retry the - * access. This is "Invalid Request" in PCI PRI. - */ -enum iommu_page_response_code { - IOMMU_PAGE_RESP_SUCCESS = 0, - IOMMU_PAGE_RESP_INVALID, - IOMMU_PAGE_RESP_FAILURE, -}; - -/** - * struct iommu_page_response - Generic page response information - * @argsz: User filled size of this data - * @version: API version of this structure - * @flags: encodes whether the corresponding fields are valid - * (IOMMU_FAULT_PAGE_RESPONSE_* values) - * @pasid: Process Address Space ID - * @grpid: Page Request Group Index - * @code: response code from &enum iommu_page_response_code - */ -struct iommu_page_response { - __u32 argsz; -#define IOMMU_PAGE_RESP_VERSION_1 1 - __u32 version; -#define IOMMU_PAGE_RESP_PASID_VALID (1 << 0) - __u32 flags; - __u32 pasid; - __u32 grpid; - __u32 code; -}; - -#endif /* _UAPI_IOMMU_H */ diff --git a/MAINTAINERS b/MAINTAINERS index 7e0b87d5aa2e..5f0bb02cfbb3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10837,7 +10837,6 @@ F: drivers/iommu/ F: include/linux/iommu.h F: include/linux/iova.h F: include/linux/of_iommu.h -F: include/uapi/linux/iommu.h IOSYS-MAP HELPERS M: Thomas Zimmermann From patchwork Tue May 30 05:37:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100548 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1962085vqr; Mon, 29 May 2023 23:04:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7FFC1T4StUAZj694FGP9xQbu1bTU0r70qvr86MB8eV/vx0IPxWsDD/gUypB0a9bZQTjkLs X-Received: by 2002:a17:903:1110:b0:1ac:528c:e71 with SMTP id n16-20020a170903111000b001ac528c0e71mr1632046plh.18.1685426683301; Mon, 29 May 2023 23:04:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426683; cv=none; d=google.com; s=arc-20160816; b=nk9Xm1MxSne+c1iNQp9Wm/ma9gWr9PH0/ZKH0ftbSr/wl1rbViL82qVp1iW+eLOj7+ AMpBygcp/Lbmo85OrmDWrTvb7YjdsryQ5JXy3IQLMgWGVqIBNFKj0QaxxFLPyV1iqLpU /eEEetAK5+bSM6/B+WoHMYS6rZb/u7sg2Ee9+84ln8TLQ8osnroS734EudXFa8FqCRSv mUUut2wiZVvF8bV5BhUwg+YMGy1CnJ56CX6fe4XVsMFhGwL96WUpq9YsWM/ifg1dsdrh 6bh6bPMvkOtCIZdL595jM0acq3BoTUdURBv+p3Y3kua5hP9vFYpSlDqC6PQqZy3CJP1a yafQ== 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=aN7IFQXTAFuFepqPqBh+/FQqrQcVnDF73a3DyEoG2S0=; b=LQzSc+wcbhzK3VV95m+zb2lEftbtod15rq9DuAC666kG7nZpfY5I16fJnI2plqE7nS x0jfQ/rUciodBxusfeY9qFmb0Au8pKCm7hrZQppw56CWcg5ywWGQ8MRH5lskuDa6LGQ1 dg7Jmxk++lK8nUpx22DAi38dRoXTUGL0xLq/gBxfzBr0iOooqcnEk2Id95BYwTM399Ep Xh/pr2tahqqTtaSMMwc/+M54yAMLTGciGtsMgads7jMpRC22xa84nw4ONjhB8JaWs+bX w0K5X6EqJMOZVdSqK5++KhFbfJeVNai78RdTPEPL+58DIYpop1B2tGjEkuSTJxexI9p5 NQiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=PLebcoLR; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j22-20020a170902759600b001aaef930752si1089819pll.647.2023.05.29.23.04.31; Mon, 29 May 2023 23:04:43 -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=PLebcoLR; 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 S230227AbjE3Fig (ORCPT + 99 others); Tue, 30 May 2023 01:38:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230208AbjE3Fie (ORCPT ); Tue, 30 May 2023 01:38:34 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0842FAD; Mon, 29 May 2023 22:38:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425112; x=1716961112; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4cYj8sTyvLBKZg/N54/ZRBBAlaG5CsvyE1vXMZic30o=; b=PLebcoLRb5OjFmUdFAOtZKVqGqbcDjj9LbpFXKwUhnudaVAFmYgZ64U5 zqPaoROMCtMC27FmhxuyKiKIJqibiU8N402QMpAkjtNA2kR7eCiHvNd8Z 9Mb1JyQpoiD9HPgP53uCaz7MvaTJ2KtOursNBta0m2VSAv+AI0OXSpuP+ fy6QVXaDhvsYPQ2WrUkUOetvnaa4e3bXXeBHX1RXHqNc9RUZBXcy2BriP GH/NgxwaWUHwirFNtRPVBrvTlCKNRBc6JVR5tkAyZfk3ePmJQs3kEJA0/ i7QCiiqsNRXYKL1dsCwRmLq5jkIIopknpE0BtRJwkWaXrsiDBCf6RWzc1 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579608" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579608" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369425" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369425" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:29 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 02/17] iommu: Support asynchronous I/O page fault response Date: Tue, 30 May 2023 13:37:09 +0800 Message-Id: <20230530053724.232765-3-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297969748250275?= X-GMAIL-MSGID: =?utf-8?q?1767297969748250275?= Add a new page response code, IOMMU_PAGE_RESP_ASYNC, to indicate that the domain's page fault handler doesn't respond the hardware immediately, but do it in an asynchronous way. The use case of this response code is the nested translation, where the first-stage page table is owned by the VM guest and any page fault on it should be propagated to the VM guest and page fault will be responded in a different thread context later. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 2 ++ drivers/iommu/io-pgfault.c | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index d6a93de7d1dd..fce7ad81206f 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -164,11 +164,13 @@ struct iommu_fault { * this device if possible. This is "Response Failure" in PCI PRI. * @IOMMU_PAGE_RESP_INVALID: Could not handle this fault, don't retry the * access. This is "Invalid Request" in PCI PRI. + * @IOMMU_PAGE_RESP_ASYNC: Will response later by calling iommu_page_response(). */ enum iommu_page_response_code { IOMMU_PAGE_RESP_SUCCESS = 0, IOMMU_PAGE_RESP_INVALID, IOMMU_PAGE_RESP_FAILURE, + IOMMU_PAGE_RESP_ASYNC, }; /** diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index e5b8b9110c13..83f8055a0e09 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -96,7 +96,8 @@ static void iopf_handler(struct work_struct *work) kfree(iopf); } - iopf_complete_group(group->dev, &group->last_fault, status); + if (status != IOMMU_PAGE_RESP_ASYNC) + iopf_complete_group(group->dev, &group->last_fault, status); kfree(group); } From patchwork Tue May 30 05:37:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100551 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1962524vqr; Mon, 29 May 2023 23:05:32 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ535BGFg8YsN79v0cWf+YIj8bmjeiOGfxoiO4zHDFITPlfPCESnd7pvqpKdN7DBuow8Wzb2 X-Received: by 2002:a17:903:244e:b0:1ac:63b6:f1ca with SMTP id l14-20020a170903244e00b001ac63b6f1camr1726859pls.0.1685426732412; Mon, 29 May 2023 23:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426732; cv=none; d=google.com; s=arc-20160816; b=qFOO6lWspB4YJlIQ4o+36BrOycR2ESLRpMjZ83/Je5Ure/LvZXDXf7gBb3YLDafDx6 NnIzBL0x/Qlw2eWV0uzdjDgbhOigGPmi+e5EZJnEGStFwd8WL/iPFS1KjpPyeB8vfCeR Ctz3psa5OmiOIYz7IAGmy1E71tMdgQNeMMjvaPIaEQmga+DC+JHbiJHn0MLX4c9GdCdw 94+YZ9g7QN6DaNtneNj33Wws7aLmzGgFW4KTyTcls2iy8KqAqplL0QoG3Gil0JM39eYK UTgcERpOqBzjXzjGmhVFllreSnLw4pK59TC4yG/ddWWrQisNXxw25kwbbOVb7uuL+tkD EE8w== 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=bgc87USjk/gQno7+0RDtAomonTDVzADRtP6oEdAW2MI=; b=mmKTdwINSjSY5Dp8sM5406yvDJyeip4FSUmVaNvbKLiqioKUfY6ndHQIA0RYHGhYab ovq0bKsZufuyzXabAADy0r6CcmyBmtHmERxbOSS8EAlY+Fn0P7jwfaPkr6dK5TPhjC0l uxXRKU0w4bdWZtn4LHZsITztSzfLKIUbIaqM/XyNQd5erKlo5/a/C+eAld9h0t1cg37/ Q4b/cIH+v/O4uO3bqmLY+mPOc+HOXMUU0yNsMh35TsyDC7GohelczcMaI3YncetC0K39 rXeS3qRalqpesrtcTDtrVt7a/riRSRwUKeTb/a8vgRBDhCvNarluKNR8hgJYU+dpsUL8 L47g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UvsvPeUu; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j22-20020a170902759600b001aaef930752si1089819pll.647.2023.05.29.23.05.20; Mon, 29 May 2023 23:05:32 -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=UvsvPeUu; 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 S230241AbjE3Fis (ORCPT + 99 others); Tue, 30 May 2023 01:38:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230202AbjE3Fim (ORCPT ); Tue, 30 May 2023 01:38:42 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8E10AD; Mon, 29 May 2023 22:38:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425119; x=1716961119; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1ZqasWAA5mVjNHn5DIUADADIinyg0cr8B//uQHoZisM=; b=UvsvPeUuSesiB8u0JipVkBQuxRQZvIhbsB5BjeJ6QBhoNbueq4XS1UyZ 26Gwo9C0YLZD+fnIzfm8MBeT8Dr2kQq2iJlkaRfkw70C1M/YrvwP9Vehz hK0Z0/GOHoxcUdv8EKI/U+qClbgb6CVN6iocevrDEGhSYn77fVX2LQUjH 8gQMqAxrQDAb8ybR5s1l9yVOB1S6Sbtnapro5OhUHRjMCn12hL2kT/3QB FphRlkqgqs5vBtwMLeNGk1tFnQVU2JV4IoLGhYWCbHyrvTYIkPS0mLuS6 n1H4q834FF8m0NGQDAdp/a5oJkM504lWzxW8peuwf6109PYV+V04UK8/t A==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579652" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579652" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369467" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369467" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:32 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 03/17] iommu: Add helper to set iopf handler for domain Date: Tue, 30 May 2023 13:37:10 +0800 Message-Id: <20230530053724.232765-4-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767298021097913381?= X-GMAIL-MSGID: =?utf-8?q?1767298021097913381?= To avoid open code everywhere. No intentional functionality change. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 10 ++++++++++ drivers/iommu/iommu.c | 3 +-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index fce7ad81206f..f554328528bc 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -273,6 +273,16 @@ static inline bool iommu_is_dma_domain(struct iommu_domain *domain) return domain->type & __IOMMU_DOMAIN_DMA_API; } +static inline void +iommu_domain_set_iopf_handler(struct iommu_domain *domain, + enum iommu_page_response_code (*handler)(struct iommu_fault *fault, + void *data), + void *data) +{ + domain->iopf_handler = handler; + domain->fault_data = data; +} + enum iommu_cap { IOMMU_CAP_CACHE_COHERENCY, /* IOMMU_CACHE is supported */ IOMMU_CAP_NOEXEC, /* IOMMU_NOEXEC flag */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 13a2e0e26884..fd65ed1d3642 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3419,8 +3419,7 @@ struct iommu_domain *iommu_sva_domain_alloc(struct device *dev, domain->type = IOMMU_DOMAIN_SVA; mmgrab(mm); domain->mm = mm; - domain->iopf_handler = iommu_sva_handle_iopf; - domain->fault_data = mm; + iommu_domain_set_iopf_handler(domain, iommu_sva_handle_iopf, mm); return domain; } From patchwork Tue May 30 05:37:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100553 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1962583vqr; Mon, 29 May 2023 23:05:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6QJKI5MBkgAaBFW/9JJHZSb2mqfRS0E7joaBHCLl1lbC2WR6Px+xrxrSrLpZOaocKaD76W X-Received: by 2002:a17:90b:254:b0:256:8825:d48f with SMTP id fz20-20020a17090b025400b002568825d48fmr1455442pjb.29.1685426736226; Mon, 29 May 2023 23:05:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426736; cv=none; d=google.com; s=arc-20160816; b=zyKvDpzXh6UOwwOi6B508kdivg92DB6uPKUhZvVG9B17ML8FZNK74iJHiYDm8UegwU 9ZraCVlrZen52jxOin+j1jEjHLvEvlLAi8FGsX1U77Ymp8mqMSgWDjG3dClUCgEaaoTG 5qQ1Q8+aa4O4ph3JdXdC9+TjRvRV8UOzY4T18OyGIl+b8MHKJxBZ6E839BOARo/UgEWI QTKMgSOveMdB3ouw/Xr/w5F4aAUYk8sSSBqjMD3uMTAXN79zNrsMXiXmqQnDW0+sy77m 1okCeNhaboXqjGWWzWItFksi7/zygI20cV7RqU3zNyBMJpSCfG+ZzEAWnC2LVI218/3i +Bvg== 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=IWqc0LkXCG0otrlAgLeg7p/RG2euoxDAGCH9seC84Is=; b=ghG906AItcFMy/LjrYOA5SG78HLnDA/jDxeyI9Qz3VTTvnBSIiWh02XE10+4TIKl5X wphuKZnbaHctOUvSYrmy61Ynfu6pmWbecRFQLWcKxesa9IwN+97dkafdBGZ2F6j1EGG2 3ipz/dkMfq+SzNtw9o1RUc2fDKv1J9fNKuJR0BsWA90bqzITnN6SGtZ1oDN7B21WvRKq 7TdYjR2yHT8hjTTSAER0cSelGHXVvaYUe79kZAhaMw4GK81kvRlxQgDy6SD06aQLk5vq y2Di0J2WuAZco6FOBIhXmo3xgtfdMm+rxMNykEEWeZwM0aN0OIYos4gqNgxHGB2MPmsC 4L6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GtToZCRE; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j3-20020a636e03000000b0053481a225desi8745364pgc.340.2023.05.29.23.05.21; Mon, 29 May 2023 23:05:36 -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=GtToZCRE; 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 S229926AbjE3Fiu (ORCPT + 99 others); Tue, 30 May 2023 01:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229540AbjE3Fip (ORCPT ); Tue, 30 May 2023 01:38:45 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DDEF10A; Mon, 29 May 2023 22:38:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425119; x=1716961119; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=h1z/1a3RfZ5jKSl5h5yWsantwr32xIn20gPZtjOFYMo=; b=GtToZCREpNKxc5SOl7DuRNoIq/Tp6XopyV242inU8cT6RZgUVSrCpZf1 UqJCSWpsu4+ZyuG66lcsUcbWy+Ctzv+9YUjzKSq9hf/4DkMVhuQqiA2zh w6Tz8F/FEb7jw2SVTGM/Kh9PbLQnkRcrqRAL5TCE0Nc8hiP84UZ5M1KDz R8SozzEY4ctEx6K62Byy0FLHVqrRUia214D/gVtKm4jyOFYeCdLuOwYVU u4NaPJFsBfboRpXzXI2YpW6ydK0krBeKjUJbMA2A2aMyA9jc7QP52aBwl xeYpVyXvyvM6mxVmmi0ByJ7ShEaQVh0IQV7EBtU6amqHzVNNfrwJl36RJ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579666" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579666" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369506" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369506" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:36 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 04/17] iommu: Pass device parameter to iopf handler Date: Tue, 30 May 2023 13:37:11 +0800 Message-Id: <20230530053724.232765-5-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767298025404041115?= X-GMAIL-MSGID: =?utf-8?q?1767298025404041115?= So that IOMMUFD can route the io page fault to the user space with the device id, which was generated when the user space bound the device to an IOAS. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 2 ++ drivers/iommu/iommu-sva.h | 5 +++-- drivers/iommu/io-pgfault.c | 2 +- drivers/iommu/iommu-sva.c | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index f554328528bc..f69ac54dc583 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -254,6 +254,7 @@ struct iommu_domain { struct iommu_domain_geometry geometry; struct iommu_dma_cookie *iova_cookie; enum iommu_page_response_code (*iopf_handler)(struct iommu_fault *fault, + struct device *dev, void *data); void *fault_data; union { @@ -276,6 +277,7 @@ static inline bool iommu_is_dma_domain(struct iommu_domain *domain) static inline void iommu_domain_set_iopf_handler(struct iommu_domain *domain, enum iommu_page_response_code (*handler)(struct iommu_fault *fault, + struct device *dev, void *data), void *data) { diff --git a/drivers/iommu/iommu-sva.h b/drivers/iommu/iommu-sva.h index 54946b5a7caf..5333d6a26047 100644 --- a/drivers/iommu/iommu-sva.h +++ b/drivers/iommu/iommu-sva.h @@ -23,7 +23,8 @@ struct iopf_queue *iopf_queue_alloc(const char *name); void iopf_queue_free(struct iopf_queue *queue); int iopf_queue_discard_partial(struct iopf_queue *queue); enum iommu_page_response_code -iommu_sva_handle_iopf(struct iommu_fault *fault, void *data); +iommu_sva_handle_iopf(struct iommu_fault *fault, + struct device *dev, void *data); #else /* CONFIG_IOMMU_SVA */ static inline int iommu_queue_iopf(struct iommu_fault *fault, void *cookie) @@ -63,7 +64,7 @@ static inline int iopf_queue_discard_partial(struct iopf_queue *queue) } static inline enum iommu_page_response_code -iommu_sva_handle_iopf(struct iommu_fault *fault, void *data) +iommu_sva_handle_iopf(struct iommu_fault *fault, struct device *dev, void *data) { return IOMMU_PAGE_RESP_INVALID; } diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index 83f8055a0e09..dedc2ea70970 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -88,7 +88,7 @@ static void iopf_handler(struct work_struct *work) * faults in the group if there is an error. */ if (status == IOMMU_PAGE_RESP_SUCCESS) - status = domain->iopf_handler(&iopf->fault, + status = domain->iopf_handler(&iopf->fault, group->dev, domain->fault_data); if (!(iopf->fault.prm.flags & diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 9821bc44f5ac..02574a49275a 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -157,7 +157,7 @@ EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); * I/O page fault handler for SVA */ enum iommu_page_response_code -iommu_sva_handle_iopf(struct iommu_fault *fault, void *data) +iommu_sva_handle_iopf(struct iommu_fault *fault, struct device *dev, void *data) { vm_fault_t ret; struct vm_area_struct *vma; From patchwork Tue May 30 05:37:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100545 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1959496vqr; Mon, 29 May 2023 23:00:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5xnhA5uyZSLrQLyrUIj/GH2y+csDuFHNW8RTAQodvX8l3bZXAiSlvB4cEoM7pOpM2XzbAI X-Received: by 2002:a17:902:708a:b0:1b0:4883:2e03 with SMTP id z10-20020a170902708a00b001b048832e03mr1273541plk.40.1685426430613; Mon, 29 May 2023 23:00:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426430; cv=none; d=google.com; s=arc-20160816; b=g/h6AR7rFRII8LH0xZfig2Dkc57hzkXje4rIYs8YSkVDZoxLitVVPG0FMqLWyVCuD9 BXLSYXDPl0stmdQURTwsc05Zr9WcjRYiX3CYZqtHVOCdSUz0RQR7vBbMpoY5PFB+B6Ly 54jlkGqLqUEIHAfIBGmqayOTldNPK5zR2rvFJ9Ed7TKboBZRP1if00JVkewdo3XS8WqZ kj65QOedC4ROI4sSN93x3s7fXURNjvsQDbl46NSWxVyD7DpBPUxkwLleFMgmKk95aBOP lUTxe1IUl4Ylv4VyLQMh1gNbZ0wRo0M7h6vItWZQcvicEcl1pa0jEgk+dwCyHP8fHjU/ Ypbg== 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=bVvhf8bbqg/YIYwg1ud+I9EPz7g8aOkK1ULjei9cd8M=; b=c4tTt7wze6WvjBrz/0997/Sk4zR4Wtu2Ug5kdhKw6C71TfnMmcUFAMkLLqZTfcj13B mM0U5HMvzslZq3YGsjckKG7EB37BQAlOQO9QizAZeej32O6CYRxaeUnzDkftSaHq/ZQP ZVN72LN2aHnrfxtx6jGScPRmAYVeQUtRisd16/EsdOSBUOnnNSjs0SfE+aJQHkp6/WA1 Clz3BPQ00FRH8fq/f6bSggJcOh1+Lg11Wnu6LO5m6GTS4UFpZehcYQn5btyy6hvK/Nl9 WF/rH3NIjttrhj5IQGbtKD35JxjG8ky2xgOPNZb+XG2MR/wDjmJA13tqoKHS6e6azGG0 BF5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="U/cWZ4Wq"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j22-20020a170902759600b001aaef930752si1089819pll.647.2023.05.29.23.00.17; Mon, 29 May 2023 23:00:30 -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="U/cWZ4Wq"; 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 S230299AbjE3FjG (ORCPT + 99 others); Tue, 30 May 2023 01:39:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230239AbjE3Fiv (ORCPT ); Tue, 30 May 2023 01:38:51 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4925F9; Mon, 29 May 2023 22:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425123; x=1716961123; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Gr1PrXHXDMunpLUzMb5OW1a71V2+XwQ9sviJMqeoaWE=; b=U/cWZ4WqUmSKWxOaqI2CPDZSTJehFGmeT3ftilIZ9vu2zbelFlGe99rz 4XHVQ2Mxem4lxyrXFDw4FU438ZU6TIbnlAV5thM6aqzPEkMadWX0wPCW0 R0DOBKFyaAYMgjrh+8ruqfvNWqVOYJqz/qHKxveyHcHqSupDiAbGWPZ7H DYmFephLk8/odqQMIGqXswA9FfKrSt7le/5pcl4jT75kDdAZlbRhPYBNm +nlbBCNE9fZbbmGb0EpwVgCibjnZUGU9vKy/z6FQWsSel5Ckhq3mcJP50 8asSFI5s19ax1tfPxPRP11XqWjpKbODpGxjhLT+c4tGjvoNVVxYtJb0XS Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579684" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579684" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369541" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369541" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:39 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 05/17] iommu: Split IO page fault handling from SVA Date: Tue, 30 May 2023 13:37:12 +0800 Message-Id: <20230530053724.232765-6-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297704806295379?= X-GMAIL-MSGID: =?utf-8?q?1767297704806295379?= The current IO page fault handling framework is tightly coupled with the SVA implementation, as SVA is the only use case that requires IO page fault handling. However, with the introduction of nested translation, the first level page table is now managed by userspace. This means that any IO page fault generated for this first level IO address should be routed to userspace and handled there. To support this, we need to split the IO page fault handling framework from the SVA implementation, and make it generic for all use cases. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 8 ++++++ drivers/iommu/{iommu-sva.h => io-pgfault.h} | 26 +++++-------------- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +- drivers/iommu/intel/iommu.c | 2 +- drivers/iommu/intel/svm.c | 2 +- drivers/iommu/io-pgfault.c | 2 +- drivers/iommu/iommu-sva.c | 2 +- drivers/iommu/iommu.c | 2 +- drivers/iommu/Kconfig | 4 +++ drivers/iommu/Makefile | 3 ++- drivers/iommu/intel/Kconfig | 1 + 12 files changed, 29 insertions(+), 27 deletions(-) rename drivers/iommu/{iommu-sva.h => io-pgfault.h} (69%) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index f69ac54dc583..c201704f9aea 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -1399,6 +1399,9 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm); void iommu_sva_unbind_device(struct iommu_sva *handle); u32 iommu_sva_get_pasid(struct iommu_sva *handle); +enum iommu_page_response_code +iommu_sva_handle_iopf(struct iommu_fault *fault, + struct device *dev, void *data); #else static inline struct iommu_sva * iommu_sva_bind_device(struct device *dev, struct mm_struct *mm) @@ -1417,6 +1420,11 @@ static inline u32 iommu_sva_get_pasid(struct iommu_sva *handle) static inline void mm_pasid_init(struct mm_struct *mm) {} static inline bool mm_valid_pasid(struct mm_struct *mm) { return false; } static inline void mm_pasid_drop(struct mm_struct *mm) {} +static inline enum iommu_page_response_code +iommu_sva_handle_iopf(struct iommu_fault *fault, struct device *dev, void *data) +{ + return IOMMU_PAGE_RESP_INVALID; +} #endif /* CONFIG_IOMMU_SVA */ #endif /* __LINUX_IOMMU_H */ diff --git a/drivers/iommu/iommu-sva.h b/drivers/iommu/io-pgfault.h similarity index 69% rename from drivers/iommu/iommu-sva.h rename to drivers/iommu/io-pgfault.h index 5333d6a26047..587844e36554 100644 --- a/drivers/iommu/iommu-sva.h +++ b/drivers/iommu/io-pgfault.h @@ -1,18 +1,15 @@ /* SPDX-License-Identifier: GPL-2.0 */ /* - * SVA library for IOMMU drivers + * I/O page fault helpers for IOMMU drivers */ -#ifndef _IOMMU_SVA_H -#define _IOMMU_SVA_H +#ifndef _IOMMU_PGFAULT_H +#define _IOMMU_PGFAULT_H -#include - -/* I/O Page fault */ struct device; struct iommu_fault; struct iopf_queue; -#ifdef CONFIG_IOMMU_SVA +#ifdef CONFIG_IOMMU_PGFAULT int iommu_queue_iopf(struct iommu_fault *fault, void *cookie); int iopf_queue_add_device(struct iopf_queue *queue, struct device *dev); @@ -22,11 +19,8 @@ int iopf_queue_flush_dev(struct device *dev); struct iopf_queue *iopf_queue_alloc(const char *name); void iopf_queue_free(struct iopf_queue *queue); int iopf_queue_discard_partial(struct iopf_queue *queue); -enum iommu_page_response_code -iommu_sva_handle_iopf(struct iommu_fault *fault, - struct device *dev, void *data); -#else /* CONFIG_IOMMU_SVA */ +#else /* CONFIG_IOMMU_PGFAULT */ static inline int iommu_queue_iopf(struct iommu_fault *fault, void *cookie) { return -ENODEV; @@ -62,11 +56,5 @@ static inline int iopf_queue_discard_partial(struct iopf_queue *queue) { return -ENODEV; } - -static inline enum iommu_page_response_code -iommu_sva_handle_iopf(struct iommu_fault *fault, struct device *dev, void *data) -{ - return IOMMU_PAGE_RESP_INVALID; -} -#endif /* CONFIG_IOMMU_SVA */ -#endif /* _IOMMU_SVA_H */ +#endif /* CONFIG_IOMMU_PGFAULT */ +#endif /* _IOMMU_PGFAULT_H */ diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index a5a63b1c947e..a6401500585b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -10,7 +10,7 @@ #include #include "arm-smmu-v3.h" -#include "../../iommu-sva.h" +#include "../../io-pgfault.h" #include "../../io-pgtable-arm.h" struct arm_smmu_mmu_notifier { diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index 8ec4ee5270b1..021e72eade5f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -30,7 +30,7 @@ #include "arm-smmu-v3.h" #include "../../dma-iommu.h" -#include "../../iommu-sva.h" +#include "../../io-pgfault.h" static bool disable_bypass = true; module_param(disable_bypass, bool, 0444); diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 42288bd449a0..7473531c7568 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -26,7 +26,7 @@ #include "iommu.h" #include "../dma-iommu.h" #include "../irq_remapping.h" -#include "../iommu-sva.h" +#include "../io-pgfault.h" #include "pasid.h" #include "cap_audit.h" #include "perfmon.h" diff --git a/drivers/iommu/intel/svm.c b/drivers/iommu/intel/svm.c index e95b339e9cdc..243edc81db75 100644 --- a/drivers/iommu/intel/svm.c +++ b/drivers/iommu/intel/svm.c @@ -22,7 +22,7 @@ #include "iommu.h" #include "pasid.h" #include "perf.h" -#include "../iommu-sva.h" +#include "../io-pgfault.h" #include "trace.h" static irqreturn_t prq_event_thread(int irq, void *d); diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index dedc2ea70970..7e735369a041 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -11,7 +11,7 @@ #include #include -#include "iommu-sva.h" +#include "io-pgfault.h" /** * struct iopf_queue - IO Page Fault queue diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index 02574a49275a..585ee56e29d9 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -7,7 +7,7 @@ #include #include -#include "iommu-sva.h" +#include "io-pgfault.h" static DEFINE_MUTEX(iommu_sva_lock); static DEFINE_IDA(iommu_global_pasid_ida); diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index fd65ed1d3642..cace57c066f4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -36,7 +36,7 @@ #include "dma-iommu.h" #include "iommu-priv.h" -#include "iommu-sva.h" +#include "io-pgfault.h" #include "iommu-priv.h" diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index db98c3f86e8c..92ecaf21b355 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -157,6 +157,9 @@ config IOMMU_DMA config IOMMU_SVA bool +config IOMMU_PGFAULT + bool + config FSL_PAMU bool "Freescale IOMMU support" depends on PCI @@ -402,6 +405,7 @@ config ARM_SMMU_V3_SVA bool "Shared Virtual Addressing support for the ARM SMMUv3" depends on ARM_SMMU_V3 select IOMMU_SVA + select IOMMU_PGFAULT select MMU_NOTIFIER help Support for sharing process address spaces with devices using the diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile index 769e43d780ce..ff5c69c7cb02 100644 --- a/drivers/iommu/Makefile +++ b/drivers/iommu/Makefile @@ -27,6 +27,7 @@ obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o obj-$(CONFIG_S390_IOMMU) += s390-iommu.o obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o -obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o io-pgfault.o +obj-$(CONFIG_IOMMU_SVA) += iommu-sva.o +obj-$(CONFIG_IOMMU_PGFAULT) += io-pgfault.o obj-$(CONFIG_SPRD_IOMMU) += sprd-iommu.o obj-$(CONFIG_APPLE_DART) += apple-dart.o diff --git a/drivers/iommu/intel/Kconfig b/drivers/iommu/intel/Kconfig index 2e56bd79f589..0c2d9202f8ff 100644 --- a/drivers/iommu/intel/Kconfig +++ b/drivers/iommu/intel/Kconfig @@ -15,6 +15,7 @@ config INTEL_IOMMU select DMA_OPS select IOMMU_API select IOMMU_IOVA + select IOMMU_PGFAULT select NEED_DMA_MAP_STATE select DMAR_TABLE select SWIOTLB From patchwork Tue May 30 05:37:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100546 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1961046vqr; Mon, 29 May 2023 23:02:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4G+bqO+M3Glc0ZohehmiNiUWy0UBKNHM7qNOYHmjPXhF+enSczFFE+hagvvSbZ8ZjOOOeV X-Received: by 2002:a05:6a20:4283:b0:10c:90ac:442d with SMTP id o3-20020a056a20428300b0010c90ac442dmr1543285pzj.13.1685426572991; Mon, 29 May 2023 23:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426572; cv=none; d=google.com; s=arc-20160816; b=svHa06pCxC8hxp4Pk25mjVb5z9ZwYs0/5qrKvqOI/RxNltSoahiK3yBZa30xu4NNZo JV4tJs9rco5EsOZrUFVzkU584iA2dKCHIGmT6kMi2Fuqm8SLXs0j2vvrzottmWoQ3GvB O28XH+qb9cIh26oiuRxVTt6VsWPFTum5k6VqupZRr5QOaJYYPIXKvTFa/xX9qt1jBIHS wOs/bN0jGuKPWgXWQYcGQxapK+L8qFXmq6CWdJ5/e35c7VmQu0kfeTUXOaM/nLFg1C+x T/kgQwFn8Jijg6qD/TDArwl6ovTURdQCgGpnYTSde/WD0AM9xrTcHlfd1L/rZP46Xe1m 3Kxw== 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=hkAYm5U5LAntypxtV2nF6PJ9P4UuRt8Gutq94JTJwYE=; b=Tt9SdqwhPI+vpOBx2GaurG255Ya4wtzTUui9sGze7h/WnKcxSnVbPRxDcuuRyZhpmD /cxcg9jbKXdASjGnkCCt8UcynVILpggNNlPsAgLD9WrhXOXrV9UnibpvxnKFnSCKGysT 8p6bwk4mmfhLQh/AiX7YsZWsslkwSUBFHRRhJJELs8OT8tU7EcDgEFrr4eSRl+T56tuK vTC9ha2zLoOyLlwwfM9HoF7/CcjHzQGfXB/LUN/AUw6+M9WVDR16L1/QyrUPBIzKSDME te0uAMUIT5szaKOisYOfi2nIYQLl8s8879OAsAaKr5vXvIynor2b1/J0rYhRwZMSAsup iwXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BhZDgvEK; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t7-20020aa79467000000b00647514cfd36si1139891pfq.242.2023.05.29.23.02.40; Mon, 29 May 2023 23:02:52 -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=BhZDgvEK; 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 S230345AbjE3FkR (ORCPT + 99 others); Tue, 30 May 2023 01:40:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230313AbjE3Fjr (ORCPT ); Tue, 30 May 2023 01:39:47 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0729FEC; Mon, 29 May 2023 22:39:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425172; x=1716961172; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Y6jgEaCBH1za2zv3I11ejxRFyRn62+jMYqJd6rX08Vs=; b=BhZDgvEKW3OfNlunv3CyGbDPaAvQUrhY4KO35oqlvKGvvLlpaTb2nh+H jmh3auU/Yrj82o7VEK2DjyGBHLb0S/L5IL9+IhV/QNZyg7Sww2KTHYmyI QE3/tIVnLOfvRFKzDI/+OaZ1/SgUweYdBJelqoP8c4P/DaMAensgzfG1v fbj6tGBC/7799MKqWPg4a6v5vCczPG3nFd8aWB/IaJFh0mEFhxpRoyD7L mwnMFDDiPXkxM6XxTqy+48r29UXH5jvOjm2Vqa4lpwZnT5sihef6vvNSX 7VtlBuoGYbybH4rSlKl3kIknLjdCe7wfNg5QjWUf1gQIWqzxLxYKaFRf+ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579717" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579717" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369579" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369579" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:43 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 06/17] iommu: Add iommu page fault cookie helpers Date: Tue, 30 May 2023 13:37:13 +0800 Message-Id: <20230530053724.232765-7-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297854433582920?= X-GMAIL-MSGID: =?utf-8?q?1767297854433582920?= Add an xarray in iommu_fault_param as place holder for per-{device, pasid} fault cookie. The iommufd will use it to store the mapping of device object ID and the device pointer. This allows the iommufd to quickly retrieve the device object ID for a given {device, pasid} pair in the hot path of IO page fault delivery. Otherwise, the iommufd would have to maintain its own data structures to map {device, pasid} pairs to object IDs, and then look up the object ID on the critical path. This is not performance friendly. The iommufd is supposed to set the cookie when a fault capable domain is attached to the physical device or pasid, and clear the fault cookie when the domain is removed. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 2 ++ drivers/iommu/iommu-priv.h | 3 +++ drivers/iommu/iommu.c | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c201704f9aea..9b0058ac971c 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -617,12 +617,14 @@ struct iommu_fault_event { * @data: handler private data * @faults: holds the pending faults which needs response * @lock: protect pending faults list + * @pasid_cookie: per-pasid fault cookie */ struct iommu_fault_param { iommu_dev_fault_handler_t handler; void *data; struct list_head faults; struct mutex lock; + struct xarray pasid_cookie; }; /** diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h index a6e694f59f64..17ab989702a0 100644 --- a/drivers/iommu/iommu-priv.h +++ b/drivers/iommu/iommu-priv.h @@ -17,5 +17,8 @@ static inline const struct iommu_ops *dev_iommu_ops(struct device *dev) int iommu_group_replace_domain(struct iommu_group *group, struct iommu_domain *new_domain); +void *iommu_set_device_fault_cookie(struct device *dev, ioasid_t pasid, + void *cookie); +void *iommu_get_device_fault_cookie(struct device *dev, ioasid_t pasid); #endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index cace57c066f4..2f81be7f3a90 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1270,6 +1270,7 @@ int iommu_register_device_fault_handler(struct device *dev, param->fault_param->data = data; mutex_init(¶m->fault_param->lock); INIT_LIST_HEAD(¶m->fault_param->faults); + xa_init(¶m->fault_param->pasid_cookie); done_unlock: mutex_unlock(¶m->lock); @@ -1435,6 +1436,50 @@ int iommu_page_response(struct device *dev, } EXPORT_SYMBOL_GPL(iommu_page_response); +/** + * iommu_set_device_fault_cookie - Set a fault cookie for per-{device, pasid} + * @dev: the device to set the cookie + * @pasid: the pasid on this device + * @cookie: the opaque data + * + * Return the old cookie on success, or ERR_PTR(err#) on failure. + */ +void *iommu_set_device_fault_cookie(struct device *dev, ioasid_t pasid, + void *cookie) +{ + struct iommu_fault_param *fault_param; + void *curr; + + if (!dev->iommu || !dev->iommu->fault_param) + return ERR_PTR(-ENODEV); + + fault_param = dev->iommu->fault_param; + curr = xa_store(&fault_param->pasid_cookie, pasid, cookie, GFP_KERNEL); + + return xa_is_err(curr) ? ERR_PTR(xa_err(curr)) : curr; +} +EXPORT_SYMBOL_NS_GPL(iommu_set_device_fault_cookie, IOMMUFD_INTERNAL); + +/** + * iommu_get_device_fault_cookie - Get the fault cookie for {device, pasid} + * @dev: the device to set the cookie + * @pasid: the pasid on this device + * + * Return the cookie on success, or ERR_PTR(err#) on failure. + */ +void *iommu_get_device_fault_cookie(struct device *dev, ioasid_t pasid) +{ + struct iommu_fault_param *fault_param; + + if (!dev->iommu || !dev->iommu->fault_param) + return ERR_PTR(-ENODEV); + + fault_param = dev->iommu->fault_param; + + return xa_load(&fault_param->pasid_cookie, pasid); +} +EXPORT_SYMBOL_NS_GPL(iommu_get_device_fault_cookie, IOMMUFD_INTERNAL); + /** * iommu_group_id - Return ID for a group * @group: the group to ID From patchwork Tue May 30 05:37:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100543 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1958343vqr; Mon, 29 May 2023 22:57:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4FwmMkbVpLq+cBjmKRAa75F4Lq2/9oR80m8H4qMIjCbQxbMptJacocBoQYB/Y2mW6ZH7H9 X-Received: by 2002:a17:902:e892:b0:1b0:48e9:cddc with SMTP id w18-20020a170902e89200b001b048e9cddcmr1697821plg.28.1685426244467; Mon, 29 May 2023 22:57:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426244; cv=none; d=google.com; s=arc-20160816; b=HIJcOyra/5q9rODRvrqNci1ziAiY3snjSQdujgfuzmMKR9oSnLfD6D5eH2BTkJNT/y x0ibNz1il1cdh865DizJbDItAG/BJA6NG8zfMcf2cJj4gqEypudcmu0e3jq20Kp9Lnc4 8ch4xZp6VNR45056+UDumeVQYbcINx0yMlXu3sjyxla+Sy1HZzNsrWkQGe/Q5N3EHYy+ VUrzCQx1cMOsh6DQQSvoDv5guPNEFxz2iXNVrxMFrCp+Af0Q4OYyiq1WifA/Chxo/sq+ TAhcfuvyetCJOBa32noeSf7ynUm0jBLcovTb0dWgE3bdWKlNXYKSZRXK42l8c5armOC+ P2Ng== 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=1tCxjkz4fXISrTqic9mtd/uWUTT5euTgioSkntLjPSs=; b=qI3MufhL/DBmKC6dlKW4H1Ozc10vWcRql+9eORjHZK981vDCoPj9cmnAiiNyYW0CaH /0efcqHust5F8/0vaT3VUgN86XrUsTMRgBuFj7Bah3ten1QtBhwgvMlQAj1wa0r73WFU bKLyJqSx92pxFf40+WsgX6Dv+Jkb9RinKLnqo1+r8Pke8TqWoPJCuLpKBmjbLcVwlFq9 I5O/gV5jOHgbW8ZiyIFut/TYNKmvBl2I7ll68IzZCbNnN4wTS2ASbfGlwUMlcBWbfOLf wUnrlZ9YUkhshItDnEEK7S6SIYs80Pwi1Hz2l44HkqwPxSDZLCWvOQK2yP3yPc1uebaG og1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AnQvXiaR; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o5-20020a170902778500b001a1af4abdedsi10695103pll.212.2023.05.29.22.57.12; Mon, 29 May 2023 22:57: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=AnQvXiaR; 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 S230348AbjE3FkU (ORCPT + 99 others); Tue, 30 May 2023 01:40:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230315AbjE3Fjr (ORCPT ); Tue, 30 May 2023 01:39:47 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25A7C10C; Mon, 29 May 2023 22:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425172; x=1716961172; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5i0IWMRYP+ogX9K8FkpBx7ImnE4r1Jowb3nahAxxTSQ=; b=AnQvXiaRHwSBaSktRP1HDgXEJS5qagTdbtUAzyCzaitb7V/amNJv0bOX BJCY8aRok/vKNVvLj2HlEZLi+yeVKcA+sNQmydEoXN1OsdrP7kZSppyQE OUXrnNITpZJd83nuEEWq0f/Y3z9xBHtEr2BI9AteDHKVpM1xXLx3bO7R3 0o4WchEEPAW1hbeza/PGMHn3US7iZJqMyB0rS9+0IXwNx9n9I8bp5Vkf9 WpI8bFLldrh5OM95vG2veYNhsCOQdZXSn+F0bDueakBOxUDWRJthGkACM llb9oH9uiVS/7BlZax9cfDlwMnYA98WmqhI9uPDTtsfAG+RfpHVs2vQbd Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579732" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579732" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369620" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369620" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:47 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 07/17] iommufd: Add iommu page fault data Date: Tue, 30 May 2023 13:37:14 +0800 Message-Id: <20230530053724.232765-8-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297509336487295?= X-GMAIL-MSGID: =?utf-8?q?1767297509336487295?= For user to handle IO page faults generated by IOMMU hardware when walking the HWPT managed by the user. One example of the use case is nested translation, where the first-stage page table is managed by the user space. When allocating a user HWPT, the user could opt-in a flag named IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE, which indicates that user is capable of handling IO page faults generated for this HWPT. The user also needs to allocate an eventfd and input it in event_fd field of iommu_hwpt_alloc data. On a successful return of hwpt allocation, the user can listen to the event fd and retrieve the page faults by reading from the fd returned at out_fault_fd. The format of the page fault data is encoded in the format defined by struct iommu_hwpt_pgfault. The iommu_hwpt_pgfault is mostly like the iommu_fault with some new members like fault data size and the device object id where the page fault was originated from. Signed-off-by: Lu Baolu --- include/uapi/linux/iommufd.h | 44 +++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index e10e6f74cdf4..2c7c44c00da2 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -444,7 +444,11 @@ struct iommu_hwpt_arm_smmuv3 { /** * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC) * @size: sizeof(struct iommu_hwpt_alloc) - * @flags: Must be 0 + * @flags: Combination of IOMMU_HWPT_ALLOC_FLAGS_ flags + * - IOPF_CAPABLE: User is capable of handling IO page faults. @event_fd + * must be valid once this flag is set. On successful return, user can + * listen to @event_fd and retrieve faults by reading @out_fault_fd. + * The fault data is encoded in the format defined by iommu_hwpt_pgfault. * @dev_id: The device to allocate this HWPT for * @pt_id: The IOAS to connect this HWPT to * @out_hwpt_id: The ID of the new HWPT @@ -482,6 +486,7 @@ struct iommu_hwpt_arm_smmuv3 { struct iommu_hwpt_alloc { __u32 size; __u32 flags; +#define IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE (1 << 0) __u32 dev_id; __u32 pt_id; __u32 out_hwpt_id; @@ -489,6 +494,8 @@ struct iommu_hwpt_alloc { __u32 hwpt_type; __u32 data_len; __aligned_u64 data_uptr; + __u32 event_fd; + __u32 out_fault_fd; }; #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) @@ -705,6 +712,41 @@ struct iommu_hwpt_invalidate { }; #define IOMMU_HWPT_INVALIDATE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_INVALIDATE) +/** + * struct iommu_hwpt_pgfault - iommu page fault data + * @size: sizeof(struct iommu_hwpt_pgfault) + * @flags: Combination of IOMMU_PGFAULT_FLAGS_ flags. + * - PASID_VALID: @pasid field is valid + * - LAST_PAGE: the last page fault in a group + * - PRIV_DATA: @private_data field is valid + * - RESP_NEEDS_PASID: the page response must have the same + * PASID value as the page request. + * @dev_id: id of the originated device + * @pasid: Process Address Space ID + * @grpid: Page Request Group Index + * @perm: requested page permissions (IOMMU_PGFAULT_PERM_* values) + * @addr: page address + * @private_data: device-specific private information + */ +struct iommu_hwpt_pgfault { + __u32 size; + __u32 flags; +#define IOMMU_PGFAULT_FLAGS_PASID_VALID (1 << 0) +#define IOMMU_PGFAULT_FLAGS_LAST_PAGE (1 << 1) +#define IOMMU_PGFAULT_FLAGS_PRIV_DATA (1 << 2) +#define IOMMU_PGFAULT_FLAGS_RESP_NEEDS_PASID (1 << 3) + __u32 dev_id; + __u32 pasid; + __u32 grpid; + __u32 perm; +#define IOMMU_PGFAULT_PERM_READ (1 << 0) +#define IOMMU_PGFAULT_PERM_WRITE (1 << 1) +#define IOMMU_PGFAULT_PERM_EXEC (1 << 2) +#define IOMMU_PGFAULT_PERM_PRIV (1 << 3) + __u64 addr; + __u64 private_data[2]; +}; + /** * struct iommu_device_set_data - ioctl(IOMMU_DEVICE_SET_DATA) * @size: sizeof(struct iommu_device_set_data) From patchwork Tue May 30 05:37:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100550 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1962262vqr; Mon, 29 May 2023 23:05:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ51uBwfOFIPMrdK9XzAKz32tVKEvjZDiHJf/MoQXmn96lirPH8+FkXqkP9zRR63nYo6k4X4 X-Received: by 2002:a17:902:9002:b0:1a9:7ffb:5ed0 with SMTP id a2-20020a170902900200b001a97ffb5ed0mr1231054plp.59.1685426704223; Mon, 29 May 2023 23:05:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426704; cv=none; d=google.com; s=arc-20160816; b=M3gq/G/gwiPEYL8mSSnQhrj5FeFDjlbk9kw4BrafYTEt4TqOKBUknc8H4mPgYRLTcx /lunVF684OBk0B6dOXi9qjf3/bj0ZXKXhDRnzCSDGEsbSnJ6ET8OHT9c6qj7xz06o0Q+ 42JLEfgT5QOkhkaPxo2rsKIpQ/Q6ah955KMj6L8BsbHjIZ/xHDqJ7rVJsD2cdVt4/gEg n2Gggj0gDG9CuMUmdbgSh5AEJOiJoLM0JCDg7F1JwGF0CAnHdac+zpyQde6YFo1tIxQb A7e58yxhMx4RreUNW9XeiWY+2Wl/qAJv2E/bH8TyvO26tdUJDSw4RxVIMUrCLIc9BdO6 VIag== 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=9UFxImpCcyzPmjasm/coko1+f8DRoHaK0a9DsblsXCw=; b=haT1aVx36k/tmV34f/6CyOOk63rRzGWSVEEwZaALdeubYWbB0yMiTss6sLHdDwTOPD eokK+qgY0A5Z3gFp5gkg8JLFpYgPUQItEjSvWNPemGZRh/LAKZqF6fG40FQ5myMak21G w+RML8w0mDKa0jziik7hsqFYlTeWCi6mIfjLFpHXQYaiSvQQYvTU6KnC4XVcgNyE9nuw TOlfDstsekFUsalM9W+0y1WOXg09dV+z4Gd8jVg8Sx8OcOlOiAxKzqrh2X0WwPSm/4HC ij68PhYAsyY0x6mUvVZ/Wa3mQ0rYr+jTl+HIEvjzuqyo0+yrIyUrVhPgZ3KcFMV2+jtq tkKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WVcQzu9J; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020a170903024700b0019e2bd0fea8si11367133plh.143.2023.05.29.23.04.50; Mon, 29 May 2023 23:05:04 -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=WVcQzu9J; 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 S230320AbjE3Fkx (ORCPT + 99 others); Tue, 30 May 2023 01:40:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230285AbjE3FkR (ORCPT ); Tue, 30 May 2023 01:40:17 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1D95E51; Mon, 29 May 2023 22:40: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=1685425201; x=1716961201; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=emo/IqVPZFg6auY9BgZzkhRQc5NjjIUPKKD96DiiUS4=; b=WVcQzu9Jj4K3MJFTFI5hpaxmAeKryC/6cSelZXXYguUS41nImo82imdm nuwWxNUjkVyoJo9T2CR7X/r7+dXnPgjxkMR9CYZWX828cgHLhEpbVnjLO 8LtKdzjMcVaNGUCHvrgQevELJoE1aOoWFyMXVX74ESVx6zDxiaUzYOQyV CfO70aUYeoHoqHFiVwRW8cZoCcIkVtaYi4z0K5igVi7fAdr4smiLrnB6u G5f48bQOzgScug7zGjxoYngrPPs5kEReA+mncD/YyFRd83RJUodlq4iFO ceN02+Bc88gYMYugqEoKjLH4XWq0l/kHbi4wepfWkEASNdA3FU00kNeBR A==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579760" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579760" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369666" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369666" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:51 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 08/17] iommufd: IO page fault delivery initialization and release Date: Tue, 30 May 2023 13:37:15 +0800 Message-Id: <20230530053724.232765-9-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297991782251904?= X-GMAIL-MSGID: =?utf-8?q?1767297991782251904?= Add some housekeeping code for IO page fault dilivery. Add a fault field in the iommufd_hw_pagetable structure to store pending IO page faults and other related data. The fault field is allocated when an IOPF-capable user HWPT (indicated by IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE being set in the allocation user data) is allocated. This field exists until the HWPT is destroyed. This also implies that it is possible to determine whether a HWPT is IOPF capable by checking the fault field. When an IOPF-capable HWPT is attached to a device (could also be a PASID of a device in the future), a fault cookie is allocated and set to the device. The cookie is cleared and freed when HWPT is detached from the device. Signed-off-by: Yi Liu Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_private.h | 12 +++++ drivers/iommu/iommufd/device.c | 61 +++++++++++++++++++++++-- drivers/iommu/iommufd/hw_pagetable.c | 55 ++++++++++++++++++++++ 3 files changed, 125 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index e951815f5707..5ff139acc5c0 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -236,6 +236,13 @@ int iommufd_option_rlimit_mode(struct iommu_option *cmd, int iommufd_vfio_ioas(struct iommufd_ucmd *ucmd); +struct hw_pgtable_fault { + struct mutex mutex; + struct list_head deliver; + struct list_head response; + struct eventfd_ctx *trigger; +}; + /* * A HW pagetable is called an iommu_domain inside the kernel. This user object * allows directly creating and inspecting the domains. Domains that have kernel @@ -252,6 +259,7 @@ struct iommufd_hw_pagetable { bool msi_cookie : 1; /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; + struct hw_pgtable_fault *fault; }; struct iommufd_hw_pagetable * @@ -314,6 +322,10 @@ struct iommufd_device { bool has_user_data; }; +struct iommufd_fault_cookie { + struct iommufd_device *idev; +}; + static inline struct iommufd_device * iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id) { diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 29b212714e2c..3408f1fc3e9f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -374,6 +374,44 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup, return 0; } +static int iommufd_device_set_fault_cookie(struct iommufd_hw_pagetable *hwpt, + struct iommufd_device *idev, + ioasid_t pasid) +{ + struct iommufd_fault_cookie *fcookie, *curr; + + if (!hwpt->fault) + return 0; + + fcookie = kzalloc(sizeof(*fcookie), GFP_KERNEL); + if (!fcookie) + return -ENOMEM; + fcookie->idev = idev; + + curr = iommu_set_device_fault_cookie(idev->dev, pasid, fcookie); + if (IS_ERR(curr)) { + kfree(fcookie); + return PTR_ERR(curr); + } + kfree(curr); + + return 0; +} + +static void iommufd_device_unset_fault_cookie(struct iommufd_hw_pagetable *hwpt, + struct iommufd_device *idev, + ioasid_t pasid) +{ + struct iommufd_fault_cookie *curr; + + if (!hwpt->fault) + return; + + curr = iommu_set_device_fault_cookie(idev->dev, pasid, NULL); + WARN_ON(IS_ERR(curr)); + kfree(curr); +} + int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { @@ -398,6 +436,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, if (rc) goto err_unlock; + rc = iommufd_device_set_fault_cookie(hwpt, idev, 0); + if (rc) + goto err_unresv; + /* * Only attach to the group once for the first device that is in the * group. All the other devices will follow this attachment. The user @@ -408,17 +450,21 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, if (list_empty(&idev->igroup->device_list)) { rc = iommufd_group_setup_msi(idev->igroup, hwpt); if (rc) - goto err_unresv; + goto err_unset; rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) - goto err_unresv; + goto err_unset; idev->igroup->hwpt = hwpt; } + refcount_inc(&hwpt->obj.users); list_add_tail(&idev->group_item, &idev->igroup->device_list); mutex_unlock(&idev->igroup->lock); return 0; + +err_unset: + iommufd_device_unset_fault_cookie(hwpt, idev, 0); err_unresv: iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); err_unlock: @@ -433,6 +479,7 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev) mutex_lock(&idev->igroup->lock); list_del(&idev->group_item); + iommufd_device_unset_fault_cookie(hwpt, idev, 0); if (list_empty(&idev->igroup->device_list)) { iommu_detach_group(hwpt->domain, idev->igroup->group); idev->igroup->hwpt = NULL; @@ -502,9 +549,14 @@ iommufd_device_do_replace(struct iommufd_device *idev, if (rc) goto err_unresv; + iommufd_device_unset_fault_cookie(old_hwpt, idev, 0); + rc = iommufd_device_set_fault_cookie(hwpt, idev, 0); + if (rc) + goto err_unresv; + rc = iommu_group_replace_domain(igroup->group, hwpt->domain); if (rc) - goto err_unresv; + goto err_replace; if (hwpt->ioas != old_hwpt->ioas) { list_for_each_entry(cur, &igroup->device_list, group_item) @@ -526,6 +578,9 @@ iommufd_device_do_replace(struct iommufd_device *idev, /* Caller must destroy old_hwpt */ return old_hwpt; +err_replace: + iommufd_device_unset_fault_cookie(hwpt, idev, 0); + iommufd_device_set_fault_cookie(old_hwpt, idev, 0); err_unresv: list_for_each_entry(cur, &igroup->device_list, group_item) iopt_remove_reserved_iova(&hwpt->ioas->iopt, cur->dev); diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 47ec7ddd5f5d..d6d550c3d0cc 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -3,12 +3,16 @@ * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES */ #include +#include #include #include "../iommu-priv.h" #include "iommufd_private.h" #include "iommufd_test.h" +static struct hw_pgtable_fault *hw_pagetable_fault_alloc(int eventfd); +static void hw_pagetable_fault_free(struct hw_pgtable_fault *fault); + void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) { struct iommufd_hw_pagetable *hwpt = @@ -27,6 +31,9 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) if (hwpt->parent) refcount_dec(&hwpt->parent->obj.users); + + if (hwpt->fault) + hw_pagetable_fault_free(hwpt->fault); refcount_dec(&hwpt->ioas->obj.users); } @@ -255,6 +262,11 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) goto out_put_pt; } + if (!parent && (cmd->flags & IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE)) { + rc = -EINVAL; + goto out_put_pt; + } + if (klen) { if (!cmd->data_len) { rc = -EINVAL; @@ -282,6 +294,14 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) goto out_unlock; } + if (cmd->flags & IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE) { + hwpt->fault = hw_pagetable_fault_alloc(cmd->event_fd); + if (IS_ERR(hwpt->fault)) { + rc = PTR_ERR(hwpt->fault); + goto out_hwpt; + } + } + cmd->out_hwpt_id = hwpt->obj.id; rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); if (rc) @@ -346,3 +366,38 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) iommufd_put_object(&hwpt->obj); return rc; } + +static struct hw_pgtable_fault *hw_pagetable_fault_alloc(int eventfd) +{ + struct hw_pgtable_fault *fault; + int rc; + + fault = kzalloc(sizeof(*fault), GFP_KERNEL); + if (!fault) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&fault->deliver); + INIT_LIST_HEAD(&fault->response); + mutex_init(&fault->mutex); + + fault->trigger = eventfd_ctx_fdget(eventfd); + if (IS_ERR(fault->trigger)) { + rc = PTR_ERR(fault->trigger); + goto out_free; + } + + return fault; + +out_free: + kfree(fault); + return ERR_PTR(rc); +} + +static void hw_pagetable_fault_free(struct hw_pgtable_fault *fault) +{ + WARN_ON(!list_empty(&fault->deliver)); + WARN_ON(!list_empty(&fault->response)); + + eventfd_ctx_put(fault->trigger); + kfree(fault); +} From patchwork Tue May 30 05:37:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100542 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1956782vqr; Mon, 29 May 2023 22:52:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6eI96sbKcAGGQqjd/8ewaYDkNJqnR0dSZZ/H4jrv4o8we7f+G5EFksjqbZIKQjKzN246sc X-Received: by 2002:a05:6a00:1516:b0:643:90ee:587 with SMTP id q22-20020a056a00151600b0064390ee0587mr1390013pfu.18.1685425975664; Mon, 29 May 2023 22:52:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685425975; cv=none; d=google.com; s=arc-20160816; b=NSVHdz9mEMa68l7/nPUriQdYfGvplZmRMA4DqBTDvayocQgm5KB+QmGSg665IK+djV 8u4tuuHS8A6ufomeCsFCcJToUIbg2WhIjBx8UhbABtMIs2XAJBuCEskAP+jQQh34as6b //qicr/QZb3UH2VjzsBTmGVBSobdME8DpSprXnSftGXL6dH5LWBPH+NHsBWuot1Mze9z okZXlf0ktc5+TO0wzE4ELZXBe4M9o0vgtrU6piYw63Ap9s1DrFxSKd0b6xd+h+EkAGma G03Q2sN0W3iJG9HlAHUlPopF0ZoRA4Kep17LO0ZeIP8g727U+5n6kZMy9giPrxj1ztRm ypjw== 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=icf2ykjFeJfXDAMTtY5365Zmss7qsRnvuJXmIP9djp0=; b=H/N/kDwjkF0iicKiFZYDKVuLbThkY0scvQ6JAzuJ0vVFuFgHZ52E9LFzi1j6j2Vjvm n/J5PAtoKa4n1FB+11/QRRV/xDgeJK4Nr1r4GwCIfVUZeQ/EOGHt8Po5E8+WcuZpIgDQ VkJLEPhjLEBSdFcDE00ECE3f7yC5+0Fdm85xkXNjpQpS2XvzaYeVbaqbzhcLnH2DXgfm 8kocoio+qvAyuhbu53yI39e4u0oNj+VsLXtsk0AE2K132hqLCIGQk5yzmUTPlYTMz1Ic kJxilABW2Z+U88JcL7Y/G9CQ4UBq1lN17wwpO929pKvFYIcUeZnifpGT+CXreerqK5iZ jjNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gjoAjQ4a; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g6-20020aa796a6000000b0064d533abfd4si1182203pfk.189.2023.05.29.22.52.31; Mon, 29 May 2023 22:52:55 -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=gjoAjQ4a; 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 S230355AbjE3Fk7 (ORCPT + 99 others); Tue, 30 May 2023 01:40:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230324AbjE3Fka (ORCPT ); Tue, 30 May 2023 01:40:30 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDDF0100; Mon, 29 May 2023 22:40:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425216; x=1716961216; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RqHDTbRoy7dppD6/TLitl/uLv/Q9R1r91cywKn4gvas=; b=gjoAjQ4amXlFqo3NrT4uDzVlTO2jx37yuslPcAIMxkGT7w176rBcMN0k 77/SNO5sckZ4g4m+ou7Sw17+g4pFVSc5P//v2p4/p8Jmw1IlUjVwEO746 9VGKfhuc7NJEK8S9LaERbv9cQ7u1eImIav0WgvgD+icpb9ha8OHAFU+pc BRQLZdMpEmcBvthgkHRQb3KaoWTNvO1d8lgIwKFuLf3laR+7LHE+q9i2p YqaGk2D3+D/lCJr54TB6BoDJUX1YY1+AZt0zBvYAyN/N7CQiXAZhIXHFj EuaUhLqf6vXgSAeL7GpY4ZOxXcodANvXprxJaitF11GRKSKqfXZq9djUg A==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579795" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579795" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369716" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369716" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:55 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 09/17] iommufd: Add iommufd hwpt iopf handler Date: Tue, 30 May 2023 13:37:16 +0800 Message-Id: <20230530053724.232765-10-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297227973238939?= X-GMAIL-MSGID: =?utf-8?q?1767297227973238939?= The IOPF handler is responsible for delivering I/O page faults to user space. When an I/O page fault occurs, the fault is placed in the fault pending list of the hardware page table (HWPT). The HWPT then generates a fault event, which is used to notify user space of the fault. User space can then fetch the fault information from the HWPT and handle the fault accordingly. Signed-off-by: Yi Liu Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_private.h | 8 ++++ drivers/iommu/iommufd/hw_pagetable.c | 50 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 5ff139acc5c0..8ff7721ea922 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -243,6 +243,14 @@ struct hw_pgtable_fault { struct eventfd_ctx *trigger; }; +struct iommufd_fault { + struct device *dev; + ioasid_t pasid; + struct iommu_hwpt_pgfault fault; + /* List head at hw_pgtable_fault:deliver or response */ + struct list_head item; +}; + /* * A HW pagetable is called an iommu_domain inside the kernel. This user object * allows directly creating and inspecting the domains. Domains that have kernel diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index d6d550c3d0cc..4d07c7c0073e 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -12,6 +12,9 @@ static struct hw_pgtable_fault *hw_pagetable_fault_alloc(int eventfd); static void hw_pagetable_fault_free(struct hw_pgtable_fault *fault); +static enum iommu_page_response_code +iommufd_hw_pagetable_iopf_handler(struct iommu_fault *fault, + struct device *dev, void *data); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) { @@ -300,6 +303,10 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) rc = PTR_ERR(hwpt->fault); goto out_hwpt; } + + iommu_domain_set_iopf_handler(hwpt->domain, + iommufd_hw_pagetable_iopf_handler, + hwpt); } cmd->out_hwpt_id = hwpt->obj.id; @@ -367,6 +374,49 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) return rc; } +static void iommufd_compose_fault_message(struct iommu_fault *fault, + struct iommu_hwpt_pgfault *hwpt_fault, + unsigned int dev_id) +{ + hwpt_fault->size = sizeof(*hwpt_fault); + hwpt_fault->flags = fault->prm.flags; + hwpt_fault->dev_id = dev_id; + hwpt_fault->pasid = fault->prm.pasid; + hwpt_fault->grpid = fault->prm.grpid; + hwpt_fault->perm = fault->prm.perm; + hwpt_fault->addr = fault->prm.addr; + hwpt_fault->private_data[0] = fault->prm.private_data[0]; + hwpt_fault->private_data[1] = fault->prm.private_data[1]; +} + +static enum iommu_page_response_code +iommufd_hw_pagetable_iopf_handler(struct iommu_fault *fault, + struct device *dev, void *data) +{ + struct iommufd_hw_pagetable *hwpt = data; + struct iommufd_fault_cookie *cookie; + struct iommufd_fault *ifault; + + ifault = kzalloc(sizeof(*ifault), GFP_KERNEL); + if (!ifault) + return IOMMU_PAGE_RESP_FAILURE; + + cookie = iommu_get_device_fault_cookie(dev, fault->prm.pasid); + if (!cookie) + return IOMMU_PAGE_RESP_FAILURE; + + iommufd_compose_fault_message(fault, &ifault->fault, cookie->idev->obj.id); + ifault->dev = dev; + ifault->pasid = fault->prm.pasid; + + mutex_lock(&hwpt->fault->mutex); + list_add_tail(&ifault->item, &hwpt->fault->deliver); + eventfd_signal(hwpt->fault->trigger, 1); + mutex_unlock(&hwpt->fault->mutex); + + return IOMMU_PAGE_RESP_ASYNC; +} + static struct hw_pgtable_fault *hw_pagetable_fault_alloc(int eventfd) { struct hw_pgtable_fault *fault; From patchwork Tue May 30 05:37:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100552 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1962576vqr; Mon, 29 May 2023 23:05:36 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4WluRkp3V9g/UkrkwmQ7QhyEz5/xbNj6xHIU7nyFWV66Y+bQjc9kWAWnh59RJbNkt4pf2O X-Received: by 2002:a05:6a00:16c7:b0:64d:2487:5b20 with SMTP id l7-20020a056a0016c700b0064d24875b20mr1465741pfc.9.1685426735797; Mon, 29 May 2023 23:05:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426735; cv=none; d=google.com; s=arc-20160816; b=sKxYgiSDjN/+A0UctsbbZQ4GErynpHZgnsxNyKVu9wFDy5HTa/sQaLiVSSvAyFwGJq mQ3w1B47YuLICA6tn5vNxxHrleTMsr1Z5UV0//V+ErhUGG0W/8Qz3pEHSobjoRodOE+5 EQbR5SExJbhlu3MbvWpKj0xa7P7gzTfbEpkhr7xCAAwsv9MEHgB63wsHINFaxZ8Jp2kw uqwBnDviD8MMRUV5TlcHBs6oW1VdLM1OnJD2JmsklqqWr8F1izEyRSEQbvCr1IHV7S8N oPxsg44y59eRUKrksIq9zB/Mabsvatfl9fPoc0toiYVjmGjF0BfAsdaW+4LGPeW603Ke RJGQ== 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=WVA9a5CxQpSTKCAy8WezDK6CMXxBLlulo4Yv6WkODbM=; b=Qa0aKu5jIS7asIojJJF6XAqsmRMuoSmf+qhSAnh/qwYEkZaZfWj5E6k4lAGgegxyXX iiuosTdxQBOusoYvZKNw880oRZDfeqEZCJQySCH5L3l0UQybKncQBdqIbHALHQL6JAlX yYf2wBKYCmPSaz5qGAxNJqLP6Y8eE19toihs3g/KtsZ9U20lwOabFTaiGQEOMgCwyiBN RiPaYUWGIXFJ6HqNLeX373Ycdk1wBgXhsNT8RGJzbjx+++42jjVUWp6VCAIIpqaBQlJL jOGAiUt5dc70rlyo59R+xNN6SdiVXnI6Lirw4+4NzXhLrSLQFONjp0oVUx9gwk1oUMM2 ap3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UzZ4i+9q; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i22-20020a633c56000000b0053f265b0eecsi10271932pgn.133.2023.05.29.23.05.21; Mon, 29 May 2023 23:05:35 -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=UzZ4i+9q; 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 S230359AbjE3FlC (ORCPT + 99 others); Tue, 30 May 2023 01:41:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230296AbjE3Fkb (ORCPT ); Tue, 30 May 2023 01:40:31 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08C5B102; Mon, 29 May 2023 22:40:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425216; x=1716961216; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=07iXsgZBQsRNVYOgRRfnTYdWG3SMG8/nVfXEU8dn588=; b=UzZ4i+9quFmB847u3opdhv40JjcxnZZMtR5N5S+OmckjoF8h3fmmla9U IjNb7K73GUeUe1xUsuYdpql3MMlu8+tmbxevcpkzS8s9pEYRGP8ck9JlP +zPDY0pnDwC7Lgs3HLIccpEikf0ZS7pbngF9/izYQeNWAAbbAbn/Esj+P /ZMfaNDoN4ucXDLM4PhMFKbtNSbif+ko6Vub5+rHN8XZ5n6wHRVAJedaw 41NLI19YxS8q491J3aLKiRx+AvrKxg+m6rfJfNtm3RIgEj7a7F3iWeasc zSeG829H20VWXUCire9DvS1vY3HXpDvilkbq5/QSsrU6xg32HOFY1ltHn w==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579803" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579803" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:39:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369765" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369765" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:58 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 10/17] iommufd: Add IOMMU_HWPT_ALLOC_FLAGS_USER_PASID_TABLE for hwpt_alloc Date: Tue, 30 May 2023 13:37:17 +0800 Message-Id: <20230530053724.232765-11-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767298025235629668?= X-GMAIL-MSGID: =?utf-8?q?1767298025235629668?= This flag indicates that the architecture supports assigning the whole PASID table for a device to userspace. When this flag is set, the host kernel does not need to be involved in attaching or detaching HWPTs to any PASID of the device. For such architectures, the fault cookie is always saved as {device, 0}. Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_private.h | 1 + include/uapi/linux/iommufd.h | 3 +++ drivers/iommu/iommufd/hw_pagetable.c | 6 +++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 8ff7721ea922..67e5aa0f996e 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -241,6 +241,7 @@ struct hw_pgtable_fault { struct list_head deliver; struct list_head response; struct eventfd_ctx *trigger; + bool user_pasid_table; }; struct iommufd_fault { diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 2c7c44c00da2..63863e21d043 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -449,6 +449,8 @@ struct iommu_hwpt_arm_smmuv3 { * must be valid once this flag is set. On successful return, user can * listen to @event_fd and retrieve faults by reading @out_fault_fd. * The fault data is encoded in the format defined by iommu_hwpt_pgfault. + * - USER_PASID_TABLE: The architecture supports assigning the whole pasid + * table of a device to user. * @dev_id: The device to allocate this HWPT for * @pt_id: The IOAS to connect this HWPT to * @out_hwpt_id: The ID of the new HWPT @@ -487,6 +489,7 @@ struct iommu_hwpt_alloc { __u32 size; __u32 flags; #define IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE (1 << 0) +#define IOMMU_HWPT_ALLOC_FLAGS_USER_PASID_TABLE (1 << 1) __u32 dev_id; __u32 pt_id; __u32 out_hwpt_id; diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 4d07c7c0073e..ca3e4d92f2aa 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -304,6 +304,9 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) goto out_hwpt; } + if (cmd->flags & IOMMU_HWPT_ALLOC_FLAGS_USER_PASID_TABLE) + hwpt->fault->user_pasid_table = true; + iommu_domain_set_iopf_handler(hwpt->domain, iommufd_hw_pagetable_iopf_handler, hwpt); @@ -401,7 +404,8 @@ iommufd_hw_pagetable_iopf_handler(struct iommu_fault *fault, if (!ifault) return IOMMU_PAGE_RESP_FAILURE; - cookie = iommu_get_device_fault_cookie(dev, fault->prm.pasid); + cookie = iommu_get_device_fault_cookie(dev, + hwpt->fault->user_pasid_table ? 0 : fault->prm.pasid); if (!cookie) return IOMMU_PAGE_RESP_FAILURE; From patchwork Tue May 30 05:37:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100539 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1954618vqr; Mon, 29 May 2023 22:46:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7IF3DQjxwzCDncV+u8oelSTfHVY8H00Tmce4k8qok90c2xshN3dRVQeg/6tkuofBxaY+3/ X-Received: by 2002:a17:902:e807:b0:1b0:53dc:1f72 with SMTP id u7-20020a170902e80700b001b053dc1f72mr1433746plg.58.1685425570556; Mon, 29 May 2023 22:46:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685425570; cv=none; d=google.com; s=arc-20160816; b=c6SxRnt2S9EhBIvWOBqaYkmhU2LZR28urPMfqdjymBN6vNyAqWeoRBQPQ4kkLtiKfo F7cVByEMddxUFFiX/GIhfhdMCzKxnuiRZRmVWv4ac6tdo8NYE2p+6KLnEN3CmIVoLg5k BFIeRVFgzqpv3W2mZF5pA8JN4GxLaxi6IKKa1dbg9wwCCrdFuHByXUOQdAWrzdu3sAsR Vhj2+G9Y36rBmSGzTtzq7s8VKCh2BFUWK5Ty7/dZM/Z6ZD+eANP2Gn14UEkJhKYJqgny TPih31/KnImH8w0paVGS+WJYPExmG0DoMLnnKjnYWimlrmqTny27BT4n/X6+L4Zys3GR M06w== 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=6jwr21gxC5Rg+7/PaUhiTXXV6hJvr/If8mt0mvk+98Y=; b=VaYGhTvfze5j6OvtK9Y9uVUz6xE3WnMvlc3/t5NIcc291umgN8ufE9y7Cg3KGk5sx2 LKW2Zoi+7ZXfqDxzxll5+fKAhGaeqry33vJK57Yp1oIhw6bC5TcwcR2kKBU83S5FQ6Ac Tb1GxJ/AtPhXTRKIk+OSQzo1d+nSOoY17g094CihvLz70TpmoqZE3+R+nKNokKHdO+kV XsLrvHRS0i+3UK43/kheDrwzihriL5gXiHUEp+faxny+c5kwMH6ukT+PxUO6MTMYVqv7 AgE3n8Pa7Jp+eN20VnV4s296g4Q1luARnLnOGm1LgsglwwRXBO1h8v7tKdwVgbAMGdVl aH3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VIE0qjZG; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g18-20020a170902869200b001ac307c4ef4si8062699plo.301.2023.05.29.22.45.58; Mon, 29 May 2023 22:46:10 -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=VIE0qjZG; 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 S230374AbjE3FlK (ORCPT + 99 others); Tue, 30 May 2023 01:41:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230325AbjE3Fkc (ORCPT ); Tue, 30 May 2023 01:40:32 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDAD2103; Mon, 29 May 2023 22:40:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425218; x=1716961218; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=z6fZzsnppnyR7kpcIKhto0g6Vd7ftpsuL2xR0ieUO5k=; b=VIE0qjZGTE14lWmzfNCzPYAo5ofaZcgAUDJ4exbgCz05CKN0U9gb03+p YPKvjK3SEpYgEqOxFmKwe0gk7/yTFs+5jWbVj97qoGFrDyJZ8//wS9Gzk mAFW9/YFMeYAgT86MIlWXOAfh5NNApoocTWEl3rSYoRc9hKD1/eQJPs+D sXDJZ4t+4c4UlJIu3gScvlj4Y4/RZkccbQeTMe7l6rJO3wJcJYM1wstfh 3nn2zc8X/QtXhzsu19PwSZOPmTnen9eygJhbfFcjOM9ngUuEBS7GH/PBX t+aEqNjn8Jovpfv1YHRM3OxQk6HO4zNuUxGTV2khexsvWy4hVXnhZRCKB A==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579819" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579819" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:39:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369801" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369801" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:39:02 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 11/17] iommufd: Deliver fault messages to user space Date: Tue, 30 May 2023 13:37:18 +0800 Message-Id: <20230530053724.232765-12-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767296802847988121?= X-GMAIL-MSGID: =?utf-8?q?1767296802847988121?= Provide a read-only file interface that allows user space to obtain fault messages by sequentially reading the file. User space can determine whether all fault messages have been read by comparing the provided read buffer with the actually returned data length. Once a fault is read by the user, it will be moved from the pending list to the waiting-for-response list. Signed-off-by: Yi Liu Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_private.h | 2 + drivers/iommu/iommufd/hw_pagetable.c | 66 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 67e5aa0f996e..6da0ba9421d0 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -242,6 +242,8 @@ struct hw_pgtable_fault { struct list_head response; struct eventfd_ctx *trigger; bool user_pasid_table; + struct file *fault_file; + int fault_fd; }; struct iommufd_fault { diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index ca3e4d92f2aa..09377a98069b 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -4,6 +4,8 @@ */ #include #include +#include +#include #include #include "../iommu-priv.h" @@ -310,6 +312,8 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) iommu_domain_set_iopf_handler(hwpt->domain, iommufd_hw_pagetable_iopf_handler, hwpt); + + cmd->out_fault_fd = hwpt->fault->fault_fd; } cmd->out_hwpt_id = hwpt->obj.id; @@ -421,6 +425,62 @@ iommufd_hw_pagetable_iopf_handler(struct iommu_fault *fault, return IOMMU_PAGE_RESP_ASYNC; } +static ssize_t hwpt_fault_fops_read(struct file *filep, char __user *buf, + size_t count, loff_t *ppos) +{ + struct hw_pgtable_fault *fault = filep->private_data; + size_t fault_size = sizeof(struct iommu_fault); + struct iommufd_fault *ifault; + size_t done = 0; + + if (ppos || count % fault_size) + return -ESPIPE; + + mutex_lock(&fault->mutex); + while (!list_empty(&fault->deliver) && count > done) { + ifault = list_first_entry(&fault->deliver, struct iommufd_fault, item); + if (copy_to_user(buf + done, &ifault->fault, fault_size)) + break; + done += fault_size; + list_del_init(&ifault->item); + if (ifault->fault.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE) + list_add_tail(&ifault->item, &fault->response); + else + kfree(ifault); + } + mutex_unlock(&fault->mutex); + + return done; +} + +static const struct file_operations hwpt_fault_fops = { + .owner = THIS_MODULE, + .read = hwpt_fault_fops_read, +}; + +static int hw_pagetable_get_fault_fd(struct hw_pgtable_fault *fault) +{ + struct file *filep; + int fdno; + + fdno = get_unused_fd_flags(O_CLOEXEC); + if (fdno < 0) + return fdno; + + filep = anon_inode_getfile("[iommufd-pgfault]", &hwpt_fault_fops, + fault, O_RDONLY); + if (IS_ERR(filep)) { + put_unused_fd(fdno); + return PTR_ERR(filep); + } + + fd_install(fdno, filep); + fault->fault_file = filep; + fault->fault_fd = fdno; + + return 0; +} + static struct hw_pgtable_fault *hw_pagetable_fault_alloc(int eventfd) { struct hw_pgtable_fault *fault; @@ -440,8 +500,14 @@ static struct hw_pgtable_fault *hw_pagetable_fault_alloc(int eventfd) goto out_free; } + rc = hw_pagetable_get_fault_fd(fault); + if (rc) + goto out_put_eventfd; + return fault; +out_put_eventfd: + eventfd_ctx_put(fault->trigger); out_free: kfree(fault); return ERR_PTR(rc); From patchwork Tue May 30 05:37:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100544 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1958571vqr; Mon, 29 May 2023 22:58:10 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Z0GoiAynHwyId/pPT4Sr7xiwLOe83CA10zHKC8Mfkp2szQxvatgtbpMGFP2EGRh9vMAYy X-Received: by 2002:a17:90b:4a09:b0:23f:b609:e707 with SMTP id kk9-20020a17090b4a0900b0023fb609e707mr1520774pjb.2.1685426289646; Mon, 29 May 2023 22:58:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426289; cv=none; d=google.com; s=arc-20160816; b=JK6Ssj4bk7Vs89kxXgz3RZjTp15S8UIzHPGGHP0Qc+/Y5e/jQxB6RKDkUDOeSlIGVH 7ChW/8JoAw657ryukeISaVMkN37Bexct0FnE5uycQT+rShv95DGuGXDHViEyvwi9faEO 0phIcQwIhSUO249h3p3deEbeiBQ084l1Yq9O0I1L1RPJsLAlxGwmrTLxJA7bbe81asMS YSz/mnq5lYO146qKmzojVDAwYYokXqBSf+Az17fjvzmnEUW2Nq7YllyGtfdcUJshtEKV o+FLsDQesDmlY8UCC60XIQ1iPYgm+9cPu1dz9cVH+K09mbyVpCNQjSC4lDaEj++b6JRS fi1Q== 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=ZlPwp4QoqMDqIS+GurqfRkAisD6fAr6frJ0sRNhWkIM=; b=YvKIcgZdRVjZE/ZqZON/KXqtajyhF6SD+7OPp1FQkT4XT78IisfCqHloCCOeGoyrAC yTs3Hy0ByPG589+wjsgusW+ape5/Lhtq51ujMH7fWIs1xqjhA96BAB0JOr7f+6J8V9pj dtO1g37v1+fL+m3kzqzOTzJX0e8AjvSxtpFtVXdQWEJ9zA0cHKovkPqnDDZ8lxfYsVzt jvIS1f2jMVLKh4mO6s5Nfz+psjSr0E/4rTIgY2wonkvyyJ3Km4hkxVrMGCBv9FZepsbf icvR3mY6xzlnqZ0JYkte71Z7qjCF+qufDL4Pfzvz99VTH2wqRdgy0f07+c6KQrVBom8f gNjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="gXygGp/w"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i1-20020a17090a65c100b002536c5eb7cfsi6504822pjs.58.2023.05.29.22.57.57; Mon, 29 May 2023 22:58:09 -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="gXygGp/w"; 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 S230337AbjE3FlN (ORCPT + 99 others); Tue, 30 May 2023 01:41:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230333AbjE3Fke (ORCPT ); Tue, 30 May 2023 01:40:34 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97DFA10E; Mon, 29 May 2023 22:40:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425220; x=1716961220; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zQ3ebZ+WvKqSSMLvVYjG3PLHGPBP7fGn6L526ZX5WqY=; b=gXygGp/wWg0D3p4eNCwUPw01hjcQ+X1wI4mLFukZ0sU6ypfZQeWci3lM JwGC3FxpzddNfO2cGSvkTSVdz9KbFfxrPzNroRkQp2AUDRBKIkird6jIe HyYOmgUxAbqzIQoD+gpbHpRGwnLRoeoc+DT9OVpLUJChTE3hR3TQR5OQU 4oqPDPfBKsE46ruiM5tuTSq4hJLm7cdj7CouH7iSgTRsTXCKstR14jDsX y1T1xxlU6zLK8IlV1xGHUEMm+GUqLEQODeyY43dTvss3Ua2B+AJ39oeb+ 9kRKeiGtwqX1/SSPgum49j9jeY/GGVNZ/cGa8JmXvsbboceyPUHTfsL1C Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579853" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579853" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:39:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369842" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369842" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:39:06 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 12/17] iommufd: Add io page fault response support Date: Tue, 30 May 2023 13:37:19 +0800 Message-Id: <20230530053724.232765-13-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297556633922387?= X-GMAIL-MSGID: =?utf-8?q?1767297556633922387?= Externd the IOMMUFD framework to provide a user space API for responding to page faults. Signed-off-by: Yi Liu Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_private.h | 1 + include/uapi/linux/iommufd.h | 23 +++++++++++ drivers/iommu/iommufd/hw_pagetable.c | 54 +++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 3 ++ 4 files changed, 81 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 6da0ba9421d0..0985e83a611f 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -288,6 +288,7 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); void iommufd_hw_pagetable_abort(struct iommufd_object *obj); int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd); +int iommufd_hwpt_page_response(struct iommufd_ucmd *ucmd); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 63863e21d043..65bb856dd8fb 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -50,6 +50,7 @@ enum { IOMMUFD_CMD_HWPT_INVALIDATE, IOMMUFD_CMD_DEVICE_SET_DATA, IOMMUFD_CMD_DEVICE_UNSET_DATA, + IOMMUFD_CMD_PAGE_RESPONSE, }; /** @@ -779,4 +780,26 @@ struct iommu_device_unset_data { __u32 dev_id; }; #define IOMMU_DEVICE_UNSET_DATA _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_UNSET_DATA) + +/** + * struct iommu_hwpt_page_response - ioctl(IOMMUFD_CMD_PAGE_RESPONSE) + * @size: sizeof(struct iommu_hwpt_page_response) + * @flags: encodes whether the corresponding fields are valid + * (IOMMU_PGFAULT_FLAGS_* values) + * @hwpt_id: hwpt ID of target hardware page table for the response + * @dev_id: device ID of target device for the response + * @pasid: Process Address Space ID + * @grpid: Page Request Group Index + * @code: response code from &enum iommu_page_response_code + */ +struct iommu_hwpt_page_response { + __u32 size; + __u32 flags; + __u32 hwpt_id; + __u32 dev_id; + __u32 pasid; + __u32 grpid; + __u32 code; +}; +#define IOMMU_PAGE_RESPONSE _IO(IOMMUFD_TYPE, IOMMUFD_CMD_PAGE_RESPONSE) #endif diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 09377a98069b..c1f3ebdce796 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -521,3 +521,57 @@ static void hw_pagetable_fault_free(struct hw_pgtable_fault *fault) eventfd_ctx_put(fault->trigger); kfree(fault); } + +int iommufd_hwpt_page_response(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_page_response *cmd = ucmd->cmd; + struct iommu_page_response resp = {}; + struct iommufd_fault *curr, *next; + struct iommufd_hw_pagetable *hwpt; + struct iommufd_device *idev; + int rc = -EINVAL; + + hwpt = iommufd_get_hwpt(ucmd, cmd->hwpt_id); + if (IS_ERR(hwpt)) + return rc; + + if (!hwpt->parent || !hwpt->fault) + goto out_put_hwpt; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + goto out_put_hwpt; + + mutex_lock(&hwpt->fault->mutex); + list_for_each_entry_safe(curr, next, &hwpt->fault->response, item) { + if (curr->dev != idev->dev || curr->fault.grpid != cmd->grpid) + continue; + + if ((cmd->flags & IOMMU_PGFAULT_FLAGS_PASID_VALID) && + cmd->pasid != curr->fault.pasid) + break; + + if ((curr->fault.flags & IOMMU_PGFAULT_FLAGS_RESP_NEEDS_PASID) && + !(cmd->flags & IOMMU_PGFAULT_FLAGS_PASID_VALID)) + break; + + resp.version = IOMMU_PAGE_RESP_VERSION_1; + resp.pasid = cmd->pasid; + resp.grpid = cmd->grpid; + resp.code = cmd->code; + if (curr->fault.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) + resp.flags = IOMMU_PAGE_RESP_PASID_VALID; + + rc = iommu_page_response(idev->dev, &resp); + list_del_init(&curr->item); + kfree(curr); + break; + } + mutex_unlock(&hwpt->fault->mutex); + + iommufd_put_object(&idev->obj); +out_put_hwpt: + iommufd_put_object(&hwpt->obj); + + return rc; +} diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index bb39dc6f3b27..0c8988808f43 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -279,6 +279,7 @@ union ucmd_buffer { struct iommu_ioas_unmap unmap; struct iommu_option option; struct iommu_vfio_ioas vfio_ioas; + struct iommu_hwpt_page_response resp; #ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test; #endif @@ -335,6 +336,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { struct iommu_device_set_data, data_len), IOCTL_OP(IOMMU_DEVICE_UNSET_DATA, iommufd_device_unset_data, struct iommu_device_unset_data, dev_id), + IOCTL_OP(IOMMU_PAGE_RESPONSE, iommufd_hwpt_page_response, struct iommu_hwpt_page_response, + code), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif From patchwork Tue May 30 05:37:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100547 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1961160vqr; Mon, 29 May 2023 23:03:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ74s6/7WN5++B+EoDeOLshVm0xpXjA+jP3RhLDHOkLk58pKxpQOkDzWEZhWRXGFU7oVDq0C X-Received: by 2002:a17:902:8b8a:b0:1aa:d971:4623 with SMTP id ay10-20020a1709028b8a00b001aad9714623mr1316673plb.38.1685426583133; Mon, 29 May 2023 23:03:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426583; cv=none; d=google.com; s=arc-20160816; b=DXROHiJTPEdDZFZOmE2lnWVEjnAH0SijoA8q4Wgzk0v2XTD9Tz6gHFedWz715oEebG 1hIR5FA7agUfSvp0/7Ff6NQ6yQPOkblnlF23+/ejqdH25nIVHIZLH3IF/3MfAnZKtwRU JwpgRKgBePh64wIE5zM6iWDCnYIRaMOPBGxFeVHLK957YktRAPQqcrVYr+4nYH5w0re7 jLpqIHRTVa6cNYwgGkwHF+2rqfOV2gz2usfWzki2mu3GzKo5O5jdtA8LH8b+3ujtjphI qLKcuMbz/6/ftGEz9CkbuarpIZMY4e9+u5Ib9QdiP3buVRdCRo8Q+waw80Plv8uwLNKx sCpQ== 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=ClCq2540WXRxkVHG3uyjEFrz0lViyYJwVzoRrUzSBNU=; b=oP3xmm4USS6qwpg3736Lzhx21aSLxGbowu2JNMAZXQMTqZTJoIng9RHBOuZhFPBvWb mty/y2Z64JF5K7WqFCVevvl9CPAU3hNlkvMBrNoJKBPHpTBO83jZqnEySo5VVF3QmbWd mOMRzVIdH+5/aFIj9xZUvsPoo6rj9vJ4LIyOMbOUD2tifjp0ZJs7g/NnD4VqBN8mVp4a BCtETjeRCi+7PBnFsxIlcNLxmOAuALi8hgmdP2ET+8MliShhtt4dl8LefVR6WkwnPJ69 wt+U2DyB8TPM5uiXTyiQ21Qnq1gS9OemUOW0OvtTYe23iC0OnMAGrT7EB2luJ/GbMbs6 EKFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MSE07+G5; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j22-20020a170902759600b001aaef930752si1089819pll.647.2023.05.29.23.02.50; Mon, 29 May 2023 23:03:03 -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=MSE07+G5; 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 S230352AbjE3FlU (ORCPT + 99 others); Tue, 30 May 2023 01:41:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230297AbjE3Fki (ORCPT ); Tue, 30 May 2023 01:40:38 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15CF7118; Mon, 29 May 2023 22:40:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425221; x=1716961221; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2yv6wk0yhr7L3GSjmAj5VYe1jRJn9Erv0tqyPH6K18E=; b=MSE07+G5zPMmkFu7aM9g8QdHnDePLUoAnOzcj3bIiNb9NmIyTLc/o6o4 IKrZpoYjYQUuzTXgJPmrVjKE1nxvJT3u/ZmreYHOymn0oZJp+RRlxeIjR mmb14rWnFwHYLYIYgEjhzBMTqXyFTjzGvvmT6oGe9fpQHTOEpHPjM0qzd RXccPrdIXOwtgs/plH2ehSWQKwt/hQNnUkf6prfN1Ox9zD+Ob8WCPxx2b kE96EQew15G9e0Hh4Bhe8F7DyZkgXrgLZc/D9VYs1j50LP9pZ/SAaWETf GeeXvjuRU7gAQ1bxWUqSVrN+uj9Wav67FlWhN6SuisOMiuAOJkRHTMSCS g==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579863" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579863" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:39:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369869" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369869" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:39:11 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 13/17] iommufd: Add a timer for each iommufd fault data Date: Tue, 30 May 2023 13:37:20 +0800 Message-Id: <20230530053724.232765-14-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297864810111160?= X-GMAIL-MSGID: =?utf-8?q?1767297864810111160?= In case that user space failed to read or respond the pending faults. As the per-fault iommufd data will be possibly accessed in two different contexts: user reading/responding and the timer expiring, add a reference counter for each iommufd fault data and free the data only after all the reference counters are released. The page fault response timeout value is device-specific and indicates how long the bus/device will wait for a response to a page fault request. The timeout value is added to the per-device fault cookie. Ideally, it should be calculated according to the platform configuration (PCI, ACPI, device tree, etc.). This defines a default value of 1 second in case that no platform opt-in is available. This default value is roughly estimated and subject to be changed according to real use cases. Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/iommufd_private.h | 8 +++ drivers/iommu/iommufd/device.c | 3 + drivers/iommu/iommufd/hw_pagetable.c | 80 +++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 0985e83a611f..f5b8a53044c4 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -249,9 +249,12 @@ struct hw_pgtable_fault { struct iommufd_fault { struct device *dev; ioasid_t pasid; + struct iommufd_hw_pagetable *hwpt; struct iommu_hwpt_pgfault fault; /* List head at hw_pgtable_fault:deliver or response */ struct list_head item; + struct timer_list timer; + refcount_t users; }; /* @@ -336,6 +339,11 @@ struct iommufd_device { struct iommufd_fault_cookie { struct iommufd_device *idev; + /* + * The maximum number of milliseconds that a device will wait for a + * response to a page fault request. + */ + unsigned long timeout; }; static inline struct iommufd_device * diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 3408f1fc3e9f..6ad46638f4e1 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -374,6 +374,8 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup, return 0; } +#define IOMMUFD_DEFAULT_IOPF_TIMEOUT 1000 + static int iommufd_device_set_fault_cookie(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev, ioasid_t pasid) @@ -387,6 +389,7 @@ static int iommufd_device_set_fault_cookie(struct iommufd_hw_pagetable *hwpt, if (!fcookie) return -ENOMEM; fcookie->idev = idev; + fcookie->timeout = IOMMUFD_DEFAULT_IOPF_TIMEOUT; curr = iommu_set_device_fault_cookie(idev->dev, pasid, fcookie); if (IS_ERR(curr)) { diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index c1f3ebdce796..8c441fd72e1f 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "../iommu-priv.h" @@ -396,6 +397,60 @@ static void iommufd_compose_fault_message(struct iommu_fault *fault, hwpt_fault->private_data[1] = fault->prm.private_data[1]; } +static void drain_iopf_fault(struct iommufd_fault *ifault) +{ + struct iommu_page_response resp = { + .version = IOMMU_PAGE_RESP_VERSION_1, + .pasid = ifault->fault.pasid, + .grpid = ifault->fault.grpid, + .code = IOMMU_PAGE_RESP_FAILURE, + }; + + if (!(ifault->fault.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) + return; + + if ((ifault->fault.flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID) && + (ifault->fault.flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID)) + resp.flags = IOMMU_PAGE_RESP_PASID_VALID; + + iommu_page_response(ifault->dev, &resp); +} + +static void iommufd_put_fault(struct iommufd_fault *ifault) +{ + if (!ifault) + return; + + if (refcount_dec_and_test(&ifault->users)) + kfree(ifault); +} + +static int iommufd_fault_timer_teardown(struct iommufd_fault *ifault) +{ + int rc; + + rc = timer_delete(&ifault->timer); + if (rc) + iommufd_put_fault(ifault); + + return rc; +} + +static void iopf_timer_func(struct timer_list *t) +{ + struct iommufd_fault *ifault = from_timer(ifault, t, timer); + struct hw_pgtable_fault *fault = ifault->hwpt->fault; + + mutex_lock(&fault->mutex); + if (!list_empty(&ifault->item)) { + list_del_init(&ifault->item); + drain_iopf_fault(ifault); + } + mutex_unlock(&fault->mutex); + + iommufd_put_fault(ifault); +} + static enum iommu_page_response_code iommufd_hw_pagetable_iopf_handler(struct iommu_fault *fault, struct device *dev, void *data) @@ -416,6 +471,10 @@ iommufd_hw_pagetable_iopf_handler(struct iommu_fault *fault, iommufd_compose_fault_message(fault, &ifault->fault, cookie->idev->obj.id); ifault->dev = dev; ifault->pasid = fault->prm.pasid; + ifault->hwpt = hwpt; + refcount_set(&ifault->users, 2); + timer_setup(&ifault->timer, iopf_timer_func, 0); + mod_timer(&ifault->timer, jiffies + msecs_to_jiffies(cookie->timeout)); mutex_lock(&hwpt->fault->mutex); list_add_tail(&ifault->item, &hwpt->fault->deliver); @@ -443,10 +502,12 @@ static ssize_t hwpt_fault_fops_read(struct file *filep, char __user *buf, break; done += fault_size; list_del_init(&ifault->item); - if (ifault->fault.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE) + if (ifault->fault.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE) { list_add_tail(&ifault->item, &fault->response); - else - kfree(ifault); + } else { + iommufd_fault_timer_teardown(ifault); + iommufd_put_fault(ifault); + } } mutex_unlock(&fault->mutex); @@ -526,6 +587,7 @@ int iommufd_hwpt_page_response(struct iommufd_ucmd *ucmd) { struct iommu_hwpt_page_response *cmd = ucmd->cmd; struct iommu_page_response resp = {}; + struct iommufd_fault *ifault = NULL; struct iommufd_fault *curr, *next; struct iommufd_hw_pagetable *hwpt; struct iommufd_device *idev; @@ -547,6 +609,7 @@ int iommufd_hwpt_page_response(struct iommufd_ucmd *ucmd) if (curr->dev != idev->dev || curr->fault.grpid != cmd->grpid) continue; + ifault = curr; if ((cmd->flags & IOMMU_PGFAULT_FLAGS_PASID_VALID) && cmd->pasid != curr->fault.pasid) break; @@ -555,6 +618,15 @@ int iommufd_hwpt_page_response(struct iommufd_ucmd *ucmd) !(cmd->flags & IOMMU_PGFAULT_FLAGS_PASID_VALID)) break; + /* + * The timer has expired if it was not pending. Leave the + * response to the timer function. + */ + if (!iommufd_fault_timer_teardown(curr)) { + rc = -ETIMEDOUT; + break; + } + resp.version = IOMMU_PAGE_RESP_VERSION_1; resp.pasid = cmd->pasid; resp.grpid = cmd->grpid; @@ -564,11 +636,11 @@ int iommufd_hwpt_page_response(struct iommufd_ucmd *ucmd) rc = iommu_page_response(idev->dev, &resp); list_del_init(&curr->item); - kfree(curr); break; } mutex_unlock(&hwpt->fault->mutex); + iommufd_put_fault(ifault); iommufd_put_object(&idev->obj); out_put_hwpt: iommufd_put_object(&hwpt->obj); From patchwork Tue May 30 05:37:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100549 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1962223vqr; Mon, 29 May 2023 23:05:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5N49vsR1/eb0gwk+Iigj9HNeEUW8+rEIdvLbonz24OyDlZIvAdRgHH/vPIIHy/RsBCaZLF X-Received: by 2002:a05:6a20:918a:b0:10b:7400:cef7 with SMTP id v10-20020a056a20918a00b0010b7400cef7mr1604018pzd.17.1685426700574; Mon, 29 May 2023 23:05:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426700; cv=none; d=google.com; s=arc-20160816; b=NI8gx1wFN8Xyd/d8ig6r3ALFt+Wpp0CkIWJkObX8JqRhc9SCkpUZG1LLH6wodkFNaq hbmt75Ft+rqFWTi2lcRJvSqxGACqBz/K6AlAyBJGwlosllH+z1fTOzBPtrVO8/XK4PCP lqMQHVJCwOGS4ObGbvkQlD6OXsoLUoRT/eBLSpPopgXuE0VW5bz7a+1AVcyNU6XsFma3 HJIJZe5yn6s+L+6/fhnXcRFTduOhc55drOGfGdf/EtsR13R1pFWGeSHMNCqM8mshQida ox5/JxZjxY6Pfl8MposQS/eallL9nDYU4BxbBV3wM3mq3gAibraPPj/ev8mwztB16gxw Pt3Q== 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=2zkMWBECc90MoVvfjOmRSxTVSkbABlKryCdqJJA/cRI=; b=nkyhIWSXk389FwgXQ/ECfWegLhvVBqitlOwPgpmCiklUnaNG7TxnJvedSuIUN4ZUoM ON/f90bnYXtc3EpUB3HFE7hqvG8uhSEPWQwhlhzAddwuARQYAJWa+cMYgsu/450UbAwr Z6IRUGzKV/LOrQazTP1231MbNTS9F3FhWSXstAK1mMNYV0npXQP2HJ4A26Z5Mfmr1tg6 kvVLuf19Dg5HkJbdexWYoDLhut9CcfDzu1HA44PG6WbXPU8pO9wtr+FE+NXFJA4tBpky TtMk9kC7eCvLYHMata2pzpJ9mFiwm1WIk4TXk+itkBwFIG1mcaeesHmClgYhAPBOsu3G JA7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XUig3CMz; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u60-20020a17090a51c200b00250c9a70af9si4395621pjh.63.2023.05.29.23.04.45; Mon, 29 May 2023 23:05:00 -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=XUig3CMz; 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 S230023AbjE3Flk (ORCPT + 99 others); Tue, 30 May 2023 01:41:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230301AbjE3Fkk (ORCPT ); Tue, 30 May 2023 01:40:40 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1684011C; Mon, 29 May 2023 22:40:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425221; x=1716961221; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=m2M+k/wNJUpLwNpk+M4ahpND6qZ7v9d+7FJR9k8IhpI=; b=XUig3CMzlCFHMc2SGR3BCTOkCeVg+Rjbuu8FNXcIjeS8lUD9ivQ4z0+2 ZaGMW9S/S6Rf7sbJb+M02dQt/oMWPwiZfaXpI0WOlhbzFEVRrRjJg4nrA dnxcLxWjH/bOZJtOtKq0iR6ag5SYPwUJz8N9fi+SK4q1/bgC+Lxc5b0vw Mvn/Is7boI6zjjWQ7DPVsNo6uqzQoMYY3AJZrIBRUDjU4GatCFPejCMHY x8EIk9OOv6iEizearEIB4j05gaHE0/cpcXBezM2E2IljQbV/aaO8xBJgO j3W2BLO/YBcJUTRrJrNhLhgM3tKGnLP4VcKp+wxEAhAI75VEa2nJjhXy9 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579880" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579880" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:39:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369909" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369909" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:39:15 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 14/17] iommufd: Drain all pending faults when destroying hwpt Date: Tue, 30 May 2023 13:37:21 +0800 Message-Id: <20230530053724.232765-15-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297987815137527?= X-GMAIL-MSGID: =?utf-8?q?1767297987815137527?= When a HWPT is unexpectedly destroyed, drain all faults in the pending lists. It is safe because the iommu domain has been released and there will never be new io page faults anymore. Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/hw_pagetable.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 8c441fd72e1f..7f18e6bd76ec 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -574,11 +574,26 @@ static struct hw_pgtable_fault *hw_pagetable_fault_alloc(int eventfd) return ERR_PTR(rc); } +static void iommufd_fault_list_destroy(struct hw_pgtable_fault *fault, + struct list_head *list) +{ + struct iommufd_fault *ifault; + + mutex_lock(&fault->mutex); + while (!list_empty(list)) { + ifault = list_first_entry(list, struct iommufd_fault, item); + if (iommufd_fault_timer_teardown(ifault)) + drain_iopf_fault(ifault); + list_del_init(&ifault->item); + iommufd_put_fault(ifault); + } + mutex_unlock(&fault->mutex); +} + static void hw_pagetable_fault_free(struct hw_pgtable_fault *fault) { - WARN_ON(!list_empty(&fault->deliver)); - WARN_ON(!list_empty(&fault->response)); - + iommufd_fault_list_destroy(fault, &fault->deliver); + iommufd_fault_list_destroy(fault, &fault->response); eventfd_ctx_put(fault->trigger); kfree(fault); } From patchwork Tue May 30 05:37:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100538 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1954053vqr; Mon, 29 May 2023 22:44:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4P/aLKQ5+XUbEUaSXY4/TsYb8jkHq25TSeyvRfeffAHKtszapE0d4JnfIWPDgwOP6EfmOx X-Received: by 2002:a17:902:da82:b0:1b0:5ce9:adbb with SMTP id j2-20020a170902da8200b001b05ce9adbbmr1410235plx.45.1685425466675; Mon, 29 May 2023 22:44:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685425466; cv=none; d=google.com; s=arc-20160816; b=AG+S1g/0k0oUHNgbW2NsTE0Zzm4Psm3YdeqnAreWmg5IZURvtG8uX0ibbloHGnclJS O9Q+warfIeQZv8R8Byk/U4m6NueYs3wuE343gEF77JuQeTVy5gL0F2GXVdSpXsQJATPE 0XgXj5xCNjCEoD/X3ynDpVD+kvHrm775meoSpuUXbhfNK1uMayH0ZMe2wfxkQ4FJH6r+ 9guwmIRGzT4+xOuhw2BuiaA1Ztyv1MoD7Ac6A95BWjYD+wYQXvFKIU2yqUXSGkT+mK7w 0sGOY2q3Xtn8PE7NqLHdkkVRgNQ/AJfvKqtAfguCGRsqUCUoUa/Lh7KeXgUfYk54a9dL xQAw== 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=amSXkcIEV7/W4U73otFiZo7N+TMk6K46HEvEFTtrl7Y=; b=dDZFFlE11ZVWI/rzDAVfgVtY18CqkMjLbs6oNjbt6UgwrhxEzAz6ZtlSNFetOotES8 AMmX7YzfnDxJoa1l89SjPGniS2XgPZciR0GqhoZOLZLv6dmvZ6JQRHJCebOsaJrAIVBN o6IcdlPdXUZUxhENuHbAoha1V3a3URYtW4AVuWe9qASLgZZOo2v/yzzo5NBM6Dt5RsTA +yUz7TdXna0V4/jwD0RBhzlhMPNlngVKfvakhY/WvgMca+tXbzJtwCVugw/ftaMNZD8h oJ4V2xI3L1AVR91ZULEVvoLBxLMI7xz2V4aPgF4yh3iTxGy0E/MwC9wah5OAu2adclZx QDFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="gwk5/Eee"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ja7-20020a170902efc700b001b0499f7a90si2060902plb.513.2023.05.29.22.44.14; Mon, 29 May 2023 22:44:26 -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="gwk5/Eee"; 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 S230398AbjE3FlX (ORCPT + 99 others); Tue, 30 May 2023 01:41:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230298AbjE3Fkj (ORCPT ); Tue, 30 May 2023 01:40:39 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1663A11B; Mon, 29 May 2023 22:40:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425221; x=1716961221; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Il+8YMeI0EiWGMw/azL1HK4UGyN0VoQuqukfg2UvUJc=; b=gwk5/Eeedw/S3RRqvKH4go0vLyP1ct1NKAjxKVLkZtlluOGm3InV0KQM 7Gug8WtPe3egUnjpeJ7tbEPmSsfxYJNtDYzfECM3nRo9D+C9ZKUnbl7Da 48D5IkN4n+r3hqkHvi5ZYemtmNw3F3i7WeL1ni/XhIjtBwjB7aq/SeiFI ZRZtptsICpxo5JflPx8uEfCk9/Oe3HOguP9j1FZo5AEiVXYy5exaeBTZZ ig12HQtZEgRQWd8RjvwiZ76OiAG7nPLzUaV705tjnG7FoB+ClWjrM+7mI PFtDpfLBGEgFIR2IRnI1y2suQ6mRVC9xa0adoSKlwb0SANkMFVCeTZIQY A==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579896" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579896" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:39:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369946" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369946" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:39:18 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 15/17] iommufd: Allow new hwpt_alloc flags Date: Tue, 30 May 2023 13:37:22 +0800 Message-Id: <20230530053724.232765-16-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767296694018376495?= X-GMAIL-MSGID: =?utf-8?q?1767296694018376495?= We have completed all puzzles for IO page fault delivery so far. We can allow user space to opt-in the flags of IOMMU_HWPT_ALLOC_FLAGS_IOPF and IOMMU_HWPT_ALLOC_FLAGS_USER_PASID_TABLE now. Signed-off-by: Lu Baolu --- include/uapi/linux/iommufd.h | 3 +++ drivers/iommu/iommufd/hw_pagetable.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 65bb856dd8fb..908d12219727 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -491,6 +491,9 @@ struct iommu_hwpt_alloc { __u32 flags; #define IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE (1 << 0) #define IOMMU_HWPT_ALLOC_FLAGS_USER_PASID_TABLE (1 << 1) +#define IOMMU_HWPT_ALLOC_FLAGS_ALL \ + (IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE | \ + IOMMU_HWPT_ALLOC_FLAGS_USER_PASID_TABLE) __u32 dev_id; __u32 pt_id; __u32 out_hwpt_id; diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 7f18e6bd76ec..7be6bf26290f 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -208,7 +208,7 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) int klen = 0; int rc = 0; - if (cmd->flags || cmd->__reserved) + if ((cmd->flags & ~IOMMU_HWPT_ALLOC_FLAGS_ALL) || cmd->__reserved) return -EOPNOTSUPP; idev = iommufd_get_device(ucmd, cmd->dev_id); From patchwork Tue May 30 05:37:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100563 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1964798vqr; Mon, 29 May 2023 23:10:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ym0j6HjIGL8TLbN2oG696O0g9gn5ZEdqVnqiqfIgFZY7qW9r4vwYaZwHNH7kjPAh8B69e X-Received: by 2002:a05:6a00:1a8b:b0:64d:277c:4ab2 with SMTP id e11-20020a056a001a8b00b0064d277c4ab2mr1332504pfv.24.1685427016993; Mon, 29 May 2023 23:10:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685427016; cv=none; d=google.com; s=arc-20160816; b=ONDxFX6a4Up+Bgd0krvcAbhoyYFGzs0Ly3G+Eo7dqxati0F25m2KMslho+oUrDCCE0 wGgnLUxWfe3eUMxCwnVb7o/dfq6Y/Za/pEYD2B96D7ZNXQ2QtmXAS4tuf6zHVH6coESQ lKJKXWK9UIMu1TUZxZ8qb+vp9+RoW/Te1dYeJjENM094dLwFE6zK8qG3D9ZKKc5TVg5P Is9Z3i2ZnyyuWgiLcVOU22Nw9LrM83bzpmdt4uTowL4z9VW0ax3UxVX/1UiMuER8hWQb OKV4fymn3bBOJ3L8n7dk3YD53SNbfeqsJ2zEty0X3T4NV4jCcEOmfcghw+RwbQmTKEHw pLTA== 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=1i5pBEsyB4oWMBS/837GN96OAM7rgwhR7DG43ZMFlDc=; b=QX5///M2tMaUWv8OJg+nj3xJaYeQ5uhTmPR8cykk44wvhZ+TiQuDJmjwuejwYGsANS vwmeK1mvlzNhD7OuHRGCcZCUTc8El6f7LTXIEGNGMNCEckBGzJif9zZGpFc1jL8+veeO qmutcEgQR17+vq4OAt48CaD7Y10GwhOuC2p6a6lrk5NnLCqHzb+XxhRXm3Z7jYFanL/t 3dQ/Fg/JpLn5twTQgMLtuiT9IoRA3jEZqOYRAzYgrunTSFOdJoZ0Yd9odRcGVsQwSnDN k72FcZ6fkwZHL3pJS06U6vFsZPGQ2ngdPaPVtRso1KXfi6jPvtHfowcqZ4b1aXMiBEZl D9Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jwX5106X; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q10-20020aa7982a000000b00643a695e976si1140343pfl.291.2023.05.29.23.10.02; Mon, 29 May 2023 23:10:16 -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=jwX5106X; 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 S229952AbjE3Fle (ORCPT + 99 others); Tue, 30 May 2023 01:41:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229728AbjE3Fkk (ORCPT ); Tue, 30 May 2023 01:40:40 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66D25107; Mon, 29 May 2023 22:40:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425221; x=1716961221; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ahpCrKl/gZhgbumlo1Sx9/qZq5FOZ/WQW6MhgCLqjXg=; b=jwX5106X4rYjuLF8pPUyMceVfDUkbBYPrM+2EcN2IIyqpZoDexOhQ01c fguQHSrcJPHCzDh4Tn55bk2Z1GsBqm8HfNso8XztgL2RcMXJ3i3lJUYBj s1dbtYMwlm1u2zUzpfjp5TJjbeNCcTx3HANu1jDAGeRM7LOKuR9OSZPzh HKOfuklZ+nIzKbEB6/WNa6jpo6nWBAFM5CcvTVeafvBEG5UDR9lwu+Btc 2Qa+Ol8ymW1fl48qemGaWhT2SPE9fVBt4Z50rmD0M8he783YmMuds3yGl s19hc76yFclh8aSlkK8/OccVksaXx2NLz+8jvrs0UEaLvlaMqhn/Vr2qv w==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579914" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579914" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:39:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369974" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369974" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:39:22 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 16/17] iommufd/selftest: Add IOPF feature for mock devices Date: Tue, 30 May 2023 13:37:23 +0800 Message-Id: <20230530053724.232765-17-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767298319798116707?= X-GMAIL-MSGID: =?utf-8?q?1767298319798116707?= So that we can test the delilvery of IO page faults through IOMMU with the selftest infrastructure. Signed-off-by: Lu Baolu --- drivers/iommu/iommufd/selftest.c | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index debf2d588990..d3d3342e95b6 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -14,6 +14,7 @@ #include "io_pagetable.h" #include "iommufd_private.h" #include "iommufd_test.h" +#include "../io-pgfault.h" static DECLARE_FAULT_ATTR(fail_iommufd); static struct dentry *dbgfs_root; @@ -96,6 +97,8 @@ enum selftest_obj_type { struct mock_dev { struct device dev; u32 dev_data; + unsigned char iopfq_name[16]; + struct iopf_queue *iopf_queue; }; struct selftest_obj { @@ -360,6 +363,64 @@ static int mock_domain_user_data_len(u32 hwpt_type) return sizeof(struct iommu_hwpt_selftest); }; +static int mock_dev_enable_feat(struct device *dev, enum iommu_dev_features feat) +{ + struct mock_dev *mdev = container_of(dev, struct mock_dev, dev); + struct iommu_group *group; + int rc; + + if (feat != IOMMU_DEV_FEAT_IOPF) + return -EOPNOTSUPP; + + group = iommu_group_get(dev); + if (!group) + return -ENODEV; + + /* Allocate the iopf queue: */ + snprintf(mdev->iopfq_name, sizeof(mdev->iopfq_name), + "mock%d-iopfq", iommu_group_id(group)); + mdev->iopf_queue = iopf_queue_alloc(mdev->iopfq_name); + if (!mdev->iopf_queue) { + rc = -ENOMEM; + goto err_put_group; + } + + /* Register I/O page fault: */ + rc = iopf_queue_add_device(mdev->iopf_queue, &mdev->dev); + if (rc) + goto err_free_queue; + rc = iommu_register_device_fault_handler(&mdev->dev, iommu_queue_iopf, + &mdev->dev); + if (rc) + goto err_remove_device; + + iommu_group_put(group); + + return 0; + +err_remove_device: + iopf_queue_remove_device(mdev->iopf_queue, &mdev->dev); +err_free_queue: + iopf_queue_free(mdev->iopf_queue); +err_put_group: + iommu_group_put(group); + return rc; +} + +static int mock_dev_disable_feat(struct device *dev, enum iommu_dev_features feat) +{ + struct mock_dev *mdev = container_of(dev, struct mock_dev, dev); + + if (feat != IOMMU_DEV_FEAT_IOPF) + return -EOPNOTSUPP; + + iommu_unregister_device_fault_handler(dev); + iopf_queue_remove_device(mdev->iopf_queue, dev); + iopf_queue_free(mdev->iopf_queue); + + return 0; +} + static const struct iommu_ops mock_ops = { .owner = THIS_MODULE, .pgsize_bitmap = MOCK_IO_PAGE_SIZE, @@ -373,6 +434,8 @@ static const struct iommu_ops mock_ops = { .set_dev_user_data = mock_domain_set_dev_user_data, .unset_dev_user_data = mock_domain_unset_dev_user_data, .dev_user_data_len = sizeof(struct iommu_test_device_data), + .dev_enable_feat = mock_dev_enable_feat, + .dev_disable_feat = mock_dev_disable_feat, .default_domain_ops = &(struct iommu_domain_ops){ .free = mock_domain_free, @@ -494,9 +557,16 @@ static struct mock_dev *mock_dev_create(void) rc = iommu_group_add_device(iommu_group, &mdev->dev); if (rc) goto err_del; + + rc = iommu_dev_enable_feature(&mdev->dev, IOMMU_DEV_FEAT_IOPF); + if (rc) + goto err_remove; + iommu_group_put(iommu_group); return mdev; +err_remove: + iommu_group_remove_device(&mdev->dev); err_del: device_del(&mdev->dev); err_dev_iommu: @@ -511,6 +581,7 @@ static struct mock_dev *mock_dev_create(void) static void mock_dev_destroy(struct mock_dev *mdev) { + iommu_dev_disable_feature(&mdev->dev, IOMMU_DEV_FEAT_IOPF); iommu_group_remove_device(&mdev->dev); device_del(&mdev->dev); kfree(mdev->dev.iommu); From patchwork Tue May 30 05:37:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolu Lu X-Patchwork-Id: 100541 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1956609vqr; Mon, 29 May 2023 22:52:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7a8An3hYqDiYePq24RY7vTqhA55xPlx2Q5K0q6uSNWxxI5/CnZtc1ftQxJ1ghhnBFynmLR X-Received: by 2002:a05:6a00:1a86:b0:647:d698:56d2 with SMTP id e6-20020a056a001a8600b00647d69856d2mr1204607pfv.27.1685425938090; Mon, 29 May 2023 22:52:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685425938; cv=none; d=google.com; s=arc-20160816; b=WXzduJzJJDeY52rx5B4mFYdWXIoBSfNIxmZWcGzlwv6BCn49a9a6Au3LkZ9hN+Lv9j hGmbIbNbj7Gma0N3CTvuth5lNWUw5RHpH/lsi4iqoUiAGqG34b7msB4mSiguuhRMYWkR fwhojOTajVgqL6JKzM43cEk6X4gvfZewjiFPVAfo5P0zhQWCrWlQYhUk+aYu5J9OR5hf s3wnTgwdASVB3LQI67eOaHoqyg7NMZkajRW++9uEGmGQDvjVm7tZDz5YXHQM9PCiZqMx OZyEjKFryBn/O7kc3LpJaJG3q3WhPa776mOFknhP3m2LmZ1i1n2ulCphNZh37mR3ZiCJ Xcag== 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=eueZZ9QA4YfhYet6SAfiUKgwh3mp4MgITjyXgFifMn8=; b=iH7Ge7X6UkPMOP+aarLEGVzHGWs8VsPHpBva9eYi+yq0TNNqFLh+k+qB3mcxuD/XXG lYysDnI1+qHwBq6L5hh8629BgTBbK/EX34bC1JlgOOanlnEiRCH98z07V330pfHW33rs L19IrAVHZVVMbhJEf1u8XhZ0xf4jaLSvkWsIsjUVX1ibGiMyJO4eSC6d55dUlXDLSMYC MxdBjL78Dx0+c3/PQhCX4iw7w/dT6Ap2oPpzJcUyV05LpbMvKlnaiYi3ajBhip5uoWt+ 1x+PmenRfWNtNcIlncU8G/xDmaB9stIdjqPzn5Lp0zjoFXimcozVnhrbOjH1WP+OV7bv F18g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=PTExL7r5; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y29-20020aa79e1d000000b006438e35303csi1181229pfq.260.2023.05.29.22.52.04; Mon, 29 May 2023 22:52:18 -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=PTExL7r5; 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 S230404AbjE3Fl3 (ORCPT + 99 others); Tue, 30 May 2023 01:41:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230304AbjE3Fkk (ORCPT ); Tue, 30 May 2023 01:40:40 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECD13123; Mon, 29 May 2023 22:40:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425222; x=1716961222; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=h3j7TOfIQuH7z3LxCDFxk2omlYmPQ3Xc+LrRNfedgCw=; b=PTExL7r5UpZQ7QZs1VS4fEp/bLDafLQvyVi+Kdyb+0OlA8xZ1Pxh5WOh /GQeJi4z5nbYd0eyIScx54ZYm7cxXadBexFnyiBluheGLtyyCbDqfPcLS vwuE8L/K2xMY4gI6HT8nR8IYJATEU9fMH+mOkY9llj9HSThCceTlm6UgQ Ni1G/FMzAzDIvbpdbpB+QGCoT6iHMFELHR3ylIkHEjUKbR3IdCYbzM5/Y V5gxQ3geMBYZJ64pgE1c8uOQ1V7oz+avMB4cuqnDggIZfOBIXjP5sMBL6 GDu78Y+RrPYuBnuHiR4GwjCs0qDCIP0dguGx5onbjoyiq1jnr25LrzrJT Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579939" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579939" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:39:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739370008" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739370008" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:39:25 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 17/17] iommufd/selftest: Cover iopf-capable nested hwpt Date: Tue, 30 May 2023 13:37:24 +0800 Message-Id: <20230530053724.232765-18-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767297188305829194?= X-GMAIL-MSGID: =?utf-8?q?1767297188305829194?= The coverage includes operations to allocate, destroy, and replace an iopf-capable nested HWPT. Signed-off-by: Lu Baolu --- tools/testing/selftests/iommu/iommufd_utils.h | 20 ++++++++++++++++--- tools/testing/selftests/iommu/iommufd.c | 17 +++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 487d45c29c6d..613ee7ef8af8 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -137,7 +137,8 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, }) static int _test_cmd_hwpt_alloc_nested(int fd, __u32 device_id, __u32 parent_id, - __u32 *hwpt_id) + __u32 event_fd, __u32 *hwpt_id, + __u32 *out_fault_fd) { struct iommu_hwpt_selftest data = { .flags = IOMMU_TEST_FLAG_NESTED, @@ -153,21 +154,34 @@ static int _test_cmd_hwpt_alloc_nested(int fd, __u32 device_id, __u32 parent_id, }; int ret; + if (out_fault_fd) { + cmd.event_fd = event_fd; + cmd.flags |= (IOMMU_HWPT_ALLOC_FLAGS_IOPF_CAPABLE | + IOMMU_HWPT_ALLOC_FLAGS_USER_PASID_TABLE); + } + ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd); if (ret) return ret; if (hwpt_id) *hwpt_id = cmd.out_hwpt_id; + if (out_fault_fd) + *out_fault_fd = cmd.out_fault_fd; return 0; } #define test_cmd_hwpt_alloc_nested(device_id, parent_id, hwpt_id) \ ASSERT_EQ(0, _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ - parent_id, hwpt_id)) + parent_id, 0, hwpt_id, NULL)) +#define test_cmd_hwpt_alloc_iopf(device_id, parent_id, event_fd, \ + hwpt_id, out_fault_fd) \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ + parent_id, event_fd, \ + hwpt_id, out_fault_fd)) #define test_err_cmd_hwpt_alloc_nested(_errno, device_id, parent_id, hwpt_id) \ EXPECT_ERRNO(_errno, \ _test_cmd_hwpt_alloc_nested(self->fd, device_id, \ - parent_id, hwpt_id)) + parent_id, 0, hwpt_id, NULL)) static int _test_cmd_hwpt_invalidate(int fd, __u32 hwpt_id) { diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 2987e8603418..6bf99172a8e9 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -294,7 +294,9 @@ TEST_F(iommufd_ioas, nested_hwpt_alloc) { uint32_t nested_hwpt_id[2] = {}; uint32_t parent_hwpt_id = 0; + uint32_t event_fd, fault_fd; uint32_t test_hwpt_id = 0; + uint32_t iopf_hwpt_id = 0; if (self->device_id) { /* Negative tests */ @@ -316,6 +318,12 @@ TEST_F(iommufd_ioas, nested_hwpt_alloc) test_cmd_hwpt_check_iotlb(nested_hwpt_id[1], IOMMU_TEST_IOTLB_DEFAULT); + /* Allocate and destroy iopf capable nested hwpt */ + event_fd = eventfd(0, EFD_CLOEXEC); + ASSERT_NE(-1, event_fd); + test_cmd_hwpt_alloc_iopf(self->device_id, parent_hwpt_id, + event_fd, &iopf_hwpt_id, &fault_fd); + /* Negative test: a nested hwpt on top of a nested hwpt */ test_err_cmd_hwpt_alloc_nested(EINVAL, self->device_id, nested_hwpt_id[0], @@ -344,9 +352,16 @@ TEST_F(iommufd_ioas, nested_hwpt_alloc) _test_ioctl_destroy(self->fd, nested_hwpt_id[1])); test_ioctl_destroy(nested_hwpt_id[0]); + /* Switch from nested_hwpt_id[1] to iopf hwpt */ + test_cmd_mock_domain_replace(self->stdev_id, iopf_hwpt_id); + EXPECT_ERRNO(EBUSY, + _test_ioctl_destroy(self->fd, iopf_hwpt_id)); + test_ioctl_destroy(nested_hwpt_id[1]); + /* Detach from nested_hwpt_id[1] and destroy it */ test_cmd_mock_domain_replace(self->stdev_id, parent_hwpt_id); - test_ioctl_destroy(nested_hwpt_id[1]); + test_ioctl_destroy(iopf_hwpt_id); + close(event_fd); /* Detach from the parent hw_pagetable and destroy it */ test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);