Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.201.27 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796378775; b=2iOCu1ZhgH67K1ZSoy1HbgmjhFR6l1PJP3zWbrl+VESFqK9yNd1PU3/+fUOqmBRO1pQR g1/1q6glGgYbL1UrJ4Bvsnl6+Fxrfe/1nl1QQtaLkd37g0uaZKe5xwAJDfKwaPi9aHgO8 2o4ffeGA1XMPHIADQmsn/Dah0r2e6pna2gvu4wr9jMMPRLM2OYzupsQ2DWTlV27MxXMWL ZVr5j3wI3KXyAeCwve3WWpmu60gFcMw0SxY7qqyM+kcRuB/z3euREV5Xat01PIaLtRool HxbVZ4At3X4a6vEPItcqDWCSzuHDcGfWCVR/z5GhaFbnb69yugmHLbPcwNd+D761UAw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796378775; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy:X-EOPAttributedMessage:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=oia9GVoL7MrBiPJaDIVsX3TfzMPjAl8aR6Vr83x4mgc=; b=pUwxSrALR+GOaVzIJ1U3Et6GKWuzp/oauZGkV2XMolTwgQ05i26p4Xax7QVmxvbzY+/k /wegiv4FOLIoJKT68e0zVnAyFARAyNko29mKV+AvTxv55RtHxkDcAO+cEDTYfpTB6ETku 4FTXHJ96YXlnujhsZZGwByEgFulsOuaPCsubJL20VJBr0p616PVXPsNHuJn3EynECU3hr x39hgcnY32pG1Xt7pV/ineaivR25Q6K+SO/MhuMnW9gPShyJ1vAyeFEea2SCrss/mLH3r z3AF3R7TBvHUfGp8qR1zX+HD4Nrxbme7SjOESipdhlp7Sqh7SR0Wpq/Z4Ev1V9Y93Ew== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=b8BIGZuz; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.201.27 Received: from PH7PR06CU001.outbound.protection.outlook.com (mail-westus3azon11010027.outbound.protection.outlook.com [52.101.201.27]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56DDD606A32C for ; Fri, 4 Dec 2026 10:06:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 56DDD606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=b8BIGZuz ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=tb1mUDBZ55QiWEkW1PDCk0aJ3jM46upeURTvxCSYuiWXtod6Bdh9KwOUVpjFcigpNX3YhrGhFgRwH2IFGou0eQh6sdw6n9ZYUZBDuO4T05QjiP8G3UQcw/eYYb6DvnQ7Lu5nl1kv0s+qKJ53LnRTw/DgfYcx9ba1eHGRLACMG5FLdRcGkwPBe19inBWLQScIKPNzGL4SXfi21G1MEKDdXgF5r0OJNP40h1hlX6aKgG14TDpVj/C8EWPs5rkqOCwxPfRQNxiAqJZa4mBqoURWHJNBBBB13mMAVr3Vxh9X3wP6JqcqoH9+mGYjndhRlkDZFh7IUmVJzeVKuzunm62EbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=oia9GVoL7MrBiPJaDIVsX3TfzMPjAl8aR6Vr83x4mgc=; b=kkH+Lx1aHUZHuoTYWG/eBaz8ZlZbTzBudjISRCzUy6x2De1Q0/gn4KnvB8qzc9PLjdXiswh6IZCDdQoqOQBAHj7Eqdmf1cxZV3BptQTeNd6brpPVe8RAJYKHoZY+gRURGyollHdKEKjeuwnEyt0FlGHhTU+/pIYMQZLAG4eX1nesx+Ybhh3dk8htVt0lQutE+ItxJFxkEkdgOEThw3irvlKnRgkqFBmTz+HWYd5m+wbjWpkjzCNarmBaymPP/L4bdgfy/+uXYi0aHZdYGuhjyhs/DwGzOrlzYQMVDSytQ4M5OttzSozJXvPaHi3BTYWqo7bvqG2oqF0jq3NEj9i3KQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=8bytes.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=oia9GVoL7MrBiPJaDIVsX3TfzMPjAl8aR6Vr83x4mgc=; b=b8BIGZuzQvJ24j1nWQMSv86x9LO34jxLcKx/foWmYbjxSlgg5YTVWvpCkxnuvFEuS4vib5AQXI6WicFVMXJn8/s7W5BGqWp0nZ4dLezvAs0/bNYIFW+nCtEYZt82rE0DC9w/XSIZPXn8jBJwOJV/oP25O2nV50XXcpm1LVKBCDQ= Received: from PH0PR07CA0018.namprd07.prod.outlook.com (2603:10b6:510:5::23) by LV2PR12MB5989.namprd12.prod.outlook.com (2603:10b6:408:171::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Fri, 4 Dec 2026 10:06:10 +0000 Received: from SJ5PEPF000001CF.namprd05.prod.outlook.com (2603:10b6:510:5:cafe::81) by PH0PR07CA0018.outlook.office365.com (2603:10b6:510:5::23) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.13 via Frontend Transport; Fri, 4 Dec 2026 10:06:10 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by SJ5PEPF000001CF.mail.protection.outlook.com (10.167.242.43) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.709.6 via Frontend Transport; Fri, 4 Dec 2026 10:06:09 +0000 Received: from kali.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 04:06:08 -0600 From: Vasant Hegde To: CC: Subject: [PATCH 35/38] iommu/amd: Drain the PPR log before detaching an SVA PASID Date: Fri, 4 Dec 2026 09:56:33 +0000 Message-ID: <20261204095636.335222-36-vasant.hegde@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261204095636.335222-1-vasant.hegde@amd.com> References: <20261204095636.335222-1-vasant.hegde@amd.com> X-Mailing-List: iommu@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ5PEPF000001CF:EE_|LV2PR12MB5989:EE_ X-MS-Office365-Filtering-Correlation-Id: afe2c2c9-5778-49ae-ca02-08df51ee386e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|23010399003|36860700016|376014|1800799024|11063799006|22082099003|18002099003|261102123227099003|56012099006|261026140227099003|261028144727099003|10067099003|261022105027099003|261026121527099003|261021123027099003|261009223027099003|261021121427099003; X-Microsoft-Antispam-Message-Info: R51jrcqsBML2sHmxJ8js6Jq48RKY/C/5KGfPq4pTQK89iP4DNrHhgLog2ofYdajaULBocN05zLUcS9rhBHQSzo1vckzp3drrwv+//Zr9Hbs3szU5EMcBfQeroahSbq0WvTnoBi2jipDe4+eokp+TzWENtetBmzSNb3e+3eP+JkS+itz7811zT2O3ebL/udarlpxjCXnrbKerjzA8eYf3WXkocpj6Cjzj0TGhhVV5AfIxi2xkjmsY55w/CqffdI1HKIg16JJTXUXaZIuNSN1BNSQoOfJ7PL27PM9djN1XzEWqbbgZ7kK6etvUUJoj04tGc3LF2NcYQeupAaghHd+QlcT+VGuGsogIYmyvNDgtCGHWAkFg2BzPfXxB6J8C1kRpp/yecUag5cqP7X4zlN9ImMe5Je1F7X8rc0Q1pqvhokQ0xThpTIC8OjpajE2ksm7I53eKrmMn9Ah+Q0s5ShAb4dDNnl05vL/8WM3GNiaorQ/h7HCdyC9/WoEpYMVQBBmYtc/ISM9b6OyLNktLNkKKpP48O018yGs8vFVhl9jV5IBx9smi1k89wkX8jMuFYEWY2R8OSHsrpKmWZfueR48oilz01XlpnSv/HZv/yqRA7hIjH2NlAprPOEEudauEjLOJM59QBRKfmFRxNHQHv9lXjALDIyjA/akPay+1rCm/VihZM35Q2VVJA0Nm7rVSilQ1wdOZxknTKZR2SExD4gW0KA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(82310400026)(23010399003)(36860700016)(376014)(1800799024)(11063799006)(22082099003)(18002099003)(261102123227099003)(56012099006)(261026140227099003)(261028144727099003)(10067099003)(261022105027099003)(261026121527099003)(261021123027099003)(261009223027099003)(261021121427099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LLIC1zvcmDugWxadtq6575dQRJdHuic/7ETWCv7E9GyLh7ULp+3yPSzU+zrxlKfQmM0rV/y1UnkvUHgNzyP6KCVolfmZEwuX/pI5vEkIOr9zcDMPyoE7VoAdchXGdtQKIlcA1PMtZBqm0074r5swyHyYd8PPD3t9nL0c/2IGFs8U83xDYw40OoiERyGRs7ypktxtcJhCcyyFaIaU5aGoAQy6H/KD8NXFx1+6TPcaNilsetAl3vpN+S+odXiyf1mTsz9B2Sd+LFdjKToYWbnon+JLMDdo0JFItBEcfh7ZD3NiU5bTiI6579PxHtlBtY7eV93sJHO43r6K7yu08E9jtl9QjQAgX/385409cbwipz+cft3f0yHXImScAloM2pw0igpD+Hyt/rPlLKDx3GZe1p0peodDAe6uDxPmdGR2EZYQ5cDhEE8fQGHnwMDorW/n X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 10:06:09.9740 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: afe2c2c9-5778-49ae-ca02-08df51ee386e X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: SJ5PEPF000001CF.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5989 amd_iommu_remove_dev_pasid() can return while a page request for that PASID is still sitting in the hardware PPR log. The PPR thread may later enqueue that request after the core has already freed the PASID attach handle, and the IOPF worker then dereferences the freed handle -- a use-after-free. The device driver issues a PASID stop before unbind, so no new request is generated for this PASID, but requests already posted remain in the log. Process those entries and then flush the IOPF workqueue so that queued work completes before the handle is freed. Serialize PPR log consumption with a new per-IOMMU mutex (ppr_lock): factor the existing poll into __amd_iommu_poll_ppr_log() under the lock, and have both the PPR thread and the drain path take it. Signed-off-by: Vasant Hegde --- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/amd_iommu_types.h | 2 ++ drivers/iommu/amd/init.c | 1 + drivers/iommu/amd/pasid.c | 10 +++++++++- drivers/iommu/amd/ppr.c | 29 ++++++++++++++++++++++++++++- 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/amd/amd_iommu.h b/drivers/iommu/amd/amd_iommu.h index b13eaa7620da..e0cb8cbc6f36 100644 --- a/drivers/iommu/amd/amd_iommu.h +++ b/drivers/iommu/amd/amd_iommu.h @@ -83,6 +83,7 @@ int __init amd_iommu_alloc_ppr_log(struct amd_iommu *iommu); void __init amd_iommu_free_ppr_log(struct amd_iommu *iommu); void amd_iommu_enable_ppr_log(struct amd_iommu *iommu); void amd_iommu_poll_ppr_log(struct amd_iommu *iommu); +void amd_iommu_drain_ppr_log(struct iommu_dev_data *dev_data); int amd_iommu_complete_ppr(struct amd_iommu *iommu, u16 devid, u32 pasid, int status, int tag, bool gn); diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h index bbfc561d6abf..ac34169b5913 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -692,6 +692,8 @@ struct amd_iommu { /* Base of the PPR log, if present */ u8 *ppr_log; + /* Serializes PPR log consumption */ + struct mutex ppr_lock; /* Name for PPR log interrupt */ unsigned char ppr_irq_name[16]; diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index e020459513d8..3a4624f5cc24 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -1975,6 +1975,7 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h, iommu->pci_seg = pci_seg; raw_spin_lock_init(&iommu->lock); + mutex_init(&iommu->ppr_lock); iommu->cmd_sem_val = 0; /* Add IOMMU to internal data structures */ diff --git a/drivers/iommu/amd/pasid.c b/drivers/iommu/amd/pasid.c index a5ca5c2b8241..d0e2bf7921bf 100644 --- a/drivers/iommu/amd/pasid.c +++ b/drivers/iommu/amd/pasid.c @@ -154,9 +154,10 @@ void amd_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid, struct iommu_domain *domain) { struct protection_domain *sva_pdom; + struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); unsigned long flags; - if (!is_pasid_valid(dev_iommu_priv_get(dev), pasid)) + if (!is_pasid_valid(dev_data, pasid)) return; sva_pdom = to_pdomain(domain); @@ -167,6 +168,13 @@ void amd_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid, remove_pdom_dev_pasid(sva_pdom, dev, pasid); spin_unlock_irqrestore(&sva_pdom->lock, flags); + + /* + * Queued faults reference the attach handle, which the caller + * frees once this returns. Consume requests already in the log + * and flush the work they queued. + */ + amd_iommu_drain_ppr_log(dev_data); } static void iommu_sva_domain_free(struct iommu_domain *domain) diff --git a/drivers/iommu/amd/ppr.c b/drivers/iommu/amd/ppr.c index 795e31735fea..6d4b29905d40 100644 --- a/drivers/iommu/amd/ppr.c +++ b/drivers/iommu/amd/ppr.c @@ -203,10 +203,12 @@ static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw) pci_dev_put(pdev); } -void amd_iommu_poll_ppr_log(struct amd_iommu *iommu) +static void __amd_iommu_poll_ppr_log(struct amd_iommu *iommu) { u32 head, tail; + lockdep_assert_held(&iommu->ppr_lock); + head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET); @@ -250,6 +252,31 @@ void amd_iommu_poll_ppr_log(struct amd_iommu *iommu) } } +void amd_iommu_poll_ppr_log(struct amd_iommu *iommu) +{ + mutex_lock(&iommu->ppr_lock); + __amd_iommu_poll_ppr_log(iommu); + mutex_unlock(&iommu->ppr_lock); +} + +/* + * Consume PPR requests already logged for this device, then flush the + * IOPF work they queued. The caller is releasing a PASID attach handle. + */ +void amd_iommu_drain_ppr_log(struct iommu_dev_data *dev_data) +{ + struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data); + + if (!iommu->ppr_log || !dev_data->ppr) + return; + + mutex_lock(&iommu->ppr_lock); + __amd_iommu_poll_ppr_log(iommu); + mutex_unlock(&iommu->ppr_lock); + + iopf_queue_flush_dev(dev_data->dev); +} + /************************************************************** * * IOPF handling stuff -- 2.31.1