From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7CF135E1AD for ; Thu, 28 May 2026 20:24:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779999852; cv=none; b=UEdI1MhowLbOtMzSkoW60SgC4hQCwg28zSqFYodReMIHuRVHprw7xYP87M5UrIAVSnZU3QzZtpr6V+9zKihJOVJ1ZI49qiZpl8tcj6k2wd030ZPXW8gafix0kFimmk4sm0VJtItZKk/HuijaQFDkTOax50tmcdb1VpXbtPPZFbg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779999852; c=relaxed/simple; bh=rCPBs6VpzFnSeGV1IQONO9sqLS2eG7RrmNxYtsUNeyw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=c9KaQ2mwoFeyIVu52OqNvoH4v1cRxocIQzkF9YBNxuy3PZS8WYzHBRNaslRLrG5/LJqnWYoX2gaLtC1E64YaG55KNC94Uwd2EJyqXTx3xp+Gn0ZCu+SShPwSbHaDuYdjHw4iT9aGvizRkSEm1Tugc+ffarS6gTU8GHQtGopAE90= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--praan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mHWlUVgn; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--praan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mHWlUVgn" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-368f2d76b04so12392350a91.3 for ; Thu, 28 May 2026 13:24:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779999850; x=1780604650; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=junrNZ+TOSSnGGCt6IWg1rMtIZuzEOzNu3RMYtfyWkA=; b=mHWlUVgnf+i6tgAJL5mZDPBOwkgpBVSahNHXwBM7D5WLWK9/gZEmPiHt4nV2+S3cSR WM2826vfIgQpNCITrjF+4IPF8G+lbSeJnqCsyyG5m+UQEMRoY9s2xog5Vw2gWhizxqNP hgl+Qw0+D0T6D2eRfDyrC7tPfwUiWH7cvr5fa08ORpC+q6kS6rgL4bkcxZcEqyVNcV1U EbGWZuDWGnylPqBULLuCOIJsv+iIxjlj/8THlmfxvhf7IP23CH5vu38EmN3H7Gvi5IlX eg36xh+7azQkKWCfROcvF8bZ2NvzvTtDfuabDdKTO6fdvO5yaXnEZqH0mEHxBwyaY5az DMSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779999850; x=1780604650; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=junrNZ+TOSSnGGCt6IWg1rMtIZuzEOzNu3RMYtfyWkA=; b=B0LLVKK5wBtQ0X/ZZQuR3ejmMQOSlQ8Qc5L5lFEcGMN1wGzNdKB6bFuOBO284RtX1A TEf2o5gOL8KJPf5vVI70fPQASOb3WchjxUsG5Xzq0apiMZPtETEFCMcvk7ueNGxKl3QK C9YYza+DwmFolRrmWvG4if+ABlRToPBryrVYBLquAbgpxFPViVMIsZpa+oQQp2zuKyUZ RhpDPaS2OfHspK3yG70L39pSr3B+3Y+Vp6BN70r5Z9ChfiNzndK1JxYAAExj9YgH+9mO PUGoTr5sIcpkNmd6zoNHa6vk2V5Fcm9AILmNq9h8oQ3yF+tpfVtT4wujptYDd0FPucas t0fw== X-Forwarded-Encrypted: i=1; AFNElJ/gCjp7UN2++aDkhgT2SzU166ely7B5/vt0f7T+S0UcxtzuniDzQRGyYPup0lcdFYL63EgTjIaUyKW3t4o=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5Ky0OVkvEno16vGMVyhz9G4Jq3f1fx7DSaxuAyovTJ7m6ImL9 3y1g8gPJ1J2Zyi9DsPsOTsIKBIraKZuHJlYcubv4JOHuy0qxpmlYCEsoCFnDQqIbjT7i4uT79sF k/g== X-Received: from pjbbb14.prod.google.com ([2002:a17:90b:8e:b0:36b:bdb2:1426]) (user=praan job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:586b:b0:36b:abe2:bc82 with SMTP id 98e67ed59e1d1-36bbcc16bb4mr121972a91.1.1779999850016; Thu, 28 May 2026 13:24:10 -0700 (PDT) Date: Thu, 28 May 2026 20:23:53 +0000 In-Reply-To: <20260528202353.3422206-1-praan@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260528202353.3422206-1-praan@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260528202353.3422206-8-praan@google.com> Subject: [PATCH v5 7/7] iommu/amd: Fail probe on ATS configuration failure From: Pranjal Shrivastava To: iommu@lists.linux.dev, linux-pci@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Joerg Roedel , Will Deacon , Bjorn Helgaas , David Woodhouse , Lu Baolu , Robin Murphy , Suravee Suthikulpanit , Jason Gunthorpe , Nicolin Chen , David Matlack , Samiullah Khawaja , Daniel Mentz , Pasha Tatashin , Mostafa Saleh , Pranjal Shrivastava Content-Type: text/plain; charset="UTF-8" Update the AMD IOMMU driver to handle ATS configuration and enablement more strictly. Specifically, update the device probe to fail if pci_prepare_ats() returns an error. This ensures that any ATS-capable master reaching the attach phase is guaranteed to have a valid config. Additionally, update pdev_enable_cap_ats() to WARN_ON() if pci_enable_ats fails. Since earlier checks in the probe phase preclude config-related failures, any failure during hardware enablement is considered a kernel bug. Fix a pre-existing Use-After-Free risk by ensuring iommu_ignore_device() is called on all probe failures after iommu_init_device(). Signed-off-by: Pranjal Shrivastava --- drivers/iommu/amd/iommu.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 84cad43dc188..b74c4504bee3 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -570,10 +570,16 @@ static inline int pdev_enable_cap_ats(struct pci_dev *pdev) if (amd_iommu_iotlb_sup && (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP)) { ret = pci_enable_ats(pdev, PAGE_SHIFT); - if (!ret) { - dev_data->ats_enabled = 1; - dev_data->ats_qdep = pci_ats_queue_depth(pdev); - } + /* + * pci_enable_ats() should not fail here because earlier checks + * have already verified support and configuration. + */ + if (WARN_ON(ret)) + return ret; + + dev_data->ats_enabled = 1; + dev_data->ats_qdep = pci_ats_queue_depth(pdev); + ret = 0; } return ret; @@ -2502,10 +2508,22 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev) else dev_data->max_irqs = MAX_IRQS_PER_TABLE_512; - if (dev_is_pci(dev)) - pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT); + if (dev_is_pci(dev)) { + struct pci_dev *pdev = to_pci_dev(dev); + + if (pci_ats_supported(pdev)) { + ret = pci_prepare_ats(pdev, PAGE_SHIFT); + if (ret) { + iommu_dev = ERR_PTR(ret); + goto out_err; + } + } + } out_err: + if (IS_ERR(iommu_dev)) + iommu_ignore_device(iommu, dev); + return iommu_dev; } -- 2.54.0.823.g6e5bcc1fc9-goog