From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 4D64D204F93 for ; Tue, 2 Dec 2025 23:03:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716588; cv=none; b=lJMrAx5UyJapve1v9FhNnHpn6kqmtbaktwcBqaMjQeQD9XgM0wRYHrxHnIaHhlSJdECDgFneLnUKT3dk/kY4dx6fDkm6REh8xY3xfarK5vRMP7GgGSKSRt7tp37yLNpQcObxAKp6jl30n6wq31aCuNW+RNGUEsMwFAqTYPMOnbs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716588; c=relaxed/simple; bh=3dpv2T/KPouo6biL8naEZoBVqMmu70tshpBj6mjBqss=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AEUMgDt3+4CcVZxdEvOghSIeGYagPQz9594ssWdupFmu56WLYOOZQ2RE3wxfYDnewd+jx7cJ29zH26PvFSlvYHls3vdTXUgiJF6pHrg2b1HOa9v6n5u5wI6cO9WpyqF+VF0XUveblDuXrn8YBh64cgliuiKUlU21PVIpHagnxFM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tWGOkSMp; arc=none smtp.client-ip=209.85.210.201 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--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tWGOkSMp" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7b8a12f0cb4so5675064b3a.3 for ; Tue, 02 Dec 2025 15:03:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716586; x=1765321386; 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=fBZlFP/EB2P+WVJQqIikPs/rswuiFST1UO20sqZFKaw=; b=tWGOkSMp3palq5xTdsD8J3buw/MeNYJ/Yu5fwL9AWgxULI0KU5FvIitQ394VtoC8/6 0RaB46mXPmickh07t480co28WDV4P4/7UvBgpKzXcLW7yLp18W7DmF+tH4KVyOywjDAY CMdgeAEG3vB1yNZkcEYT4YS6qWv0h193o9aJKDrxrLxm27UWOAPHMABykIDN45u96G59 xro1wcVkTpw/DWY3MaleSf5NudiWlOGqNcmvj4HmlpnwiEG5ZIHZImssQute3/IKmWl4 0f9D6RK6XzKw5HhePzhD48ItCizBOfZPyy5fK+oMv3vhkHMXJVXYDPa1tS4HbXZSbqSB JW6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716586; x=1765321386; 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=fBZlFP/EB2P+WVJQqIikPs/rswuiFST1UO20sqZFKaw=; b=d0tBjVzCL0NFxpPF2vd8U/KAIeqhFNBuBa3Nqn+mXSEgKONJV2lcHNqpKhuVBYkI3J U1S2TX058NrtyS0QIxs25PUM+dME372bHsN6TnygootB878fDgRSoHIp9e9XY9YYkj9l wenWyeePU6nGc5HgdQuBVbOusBg2PxpARq8prFNU8UyZEGkE6pFSxDdorS5JwdqUEcxB IM2yLRsF0dx/b3R/fAwjaspkAOkMzSplU0L7ll4OTVgVVRP40r6tmIQis6RE2SJ0L4NV 0kAfmgDt8bAH1gfltW344fbL9wUwRVvEgYeyzAS6F0nOpBrhUY8D6PoKm7eJ94Iy4uct rFBA== X-Forwarded-Encrypted: i=1; AJvYcCW3FDy50PR7CERlZX69HF/SpahRJw3NHA6pxttGTAgEum0zrpfEQT2dMhYokXbqHwmStzRTNx2HgN+XpFw=@vger.kernel.org X-Gm-Message-State: AOJu0Yy0Dmg9H7UJz/fzAnotCJCSD1k3MMwhENzR9gDCe2hIo8OkMJzg N8jAOYR8zaWkHap4xEfDtQLGGsVD2bRVP5/Z40ldFGZfEeq+UwmtdnlmrKxhcydSgSrwz0opzPE GXS9m7xSaeUQr9g== X-Google-Smtp-Source: AGHT+IGwm2qNuoclKb/WRJ/9e/2T50mKMClCoHj5CFtiQtnpImqfROZN2V64QnmfFa0SmkASiofdI1wDFOJAOA== X-Received: from pfbg6.prod.google.com ([2002:a05:6a00:ae06:b0:7b8:fc17:3960]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3a24:b0:7ab:e007:deec with SMTP id d2e1a72fcca58-7e00eb6dc8cmr178184b3a.32.1764716586477; Tue, 02 Dec 2025 15:03:06 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:31 +0000 In-Reply-To: <20251202230303.1017519-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251202230303.1017519-1-skhawaja@google.com> X-Mailer: git-send-email 2.52.0.158.g65b55ccf14-goog Message-ID: <20251202230303.1017519-2-skhawaja@google.com> Subject: [RFC PATCH v2 01/32] iommufd: Allow HWPTs to have a NULL IOAS From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: YiFei Zhu , Robin Murphy , Pratyush Yadav , Samiullah Khawaja , Kevin Tian , Alex Williamson , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, Chris Li , praan@google.com Content-Type: text/plain; charset="UTF-8" From: YiFei Zhu Normally HWPTs are created with a parent IOAS to allow the mappings to be modified. For liveupdate we want an immutable HWPT upon restore, so no IOAS is needed. This patch prepares iommufd so it would not crash on a NULL hwpt_paging->ioas. Signed-off-by: YiFei Zhu --- drivers/iommu/iommufd/device.c | 11 ++++++++--- drivers/iommu/iommufd/hw_pagetable.c | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 4c842368289f..ba4d9c3cfa8b 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -418,6 +418,7 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev, lockdep_assert_held(&igroup->lock); + /* unreachable if !hwpt_paging->ioas */ rc = iopt_table_enforce_dev_resv_regions(&hwpt_paging->ioas->iopt, idev->dev, &igroup->sw_msi_start); @@ -603,7 +604,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev, ioasid_t pasid) { struct iommufd_hwpt_paging *hwpt_paging = find_hwpt_paging(hwpt); - bool attach_resv = hwpt_paging && pasid == IOMMU_NO_PASID; + bool attach_resv = hwpt_paging && pasid == IOMMU_NO_PASID && hwpt_paging->ioas; struct iommufd_group *igroup = idev->igroup; struct iommufd_hw_pagetable *old_hwpt; struct iommufd_attach *attach; @@ -707,7 +708,7 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev, ioasid_t pasid) xa_erase(&igroup->pasid_attach, pasid); kfree(attach); } - if (hwpt_paging && pasid == IOMMU_NO_PASID) + if (hwpt_paging && pasid == IOMMU_NO_PASID && hwpt_paging->ioas) iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, idev->dev); mutex_unlock(&igroup->lock); @@ -739,6 +740,9 @@ iommufd_group_remove_reserved_iova(struct iommufd_group *igroup, lockdep_assert_held(&igroup->lock); + if (!hwpt_paging->ioas) + return; + attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); xa_for_each(&attach->device_array, index, cur) iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt, cur->dev); @@ -756,6 +760,7 @@ iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup, lockdep_assert_held(&igroup->lock); + /* unreachable if !hwpt_paging->ioas */ attach = xa_load(&igroup->pasid_attach, IOMMU_NO_PASID); old_hwpt_paging = find_hwpt_paging(attach->hwpt); if (!old_hwpt_paging || hwpt_paging->ioas != old_hwpt_paging->ioas) { @@ -782,7 +787,7 @@ iommufd_device_do_replace(struct iommufd_device *idev, ioasid_t pasid, struct iommufd_hw_pagetable *hwpt) { struct iommufd_hwpt_paging *hwpt_paging = find_hwpt_paging(hwpt); - bool attach_resv = hwpt_paging && pasid == IOMMU_NO_PASID; + bool attach_resv = hwpt_paging && pasid == IOMMU_NO_PASID && hwpt_paging->ioas; struct iommufd_hwpt_paging *old_hwpt_paging; struct iommufd_group *igroup = idev->igroup; struct iommufd_hw_pagetable *old_hwpt; diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index fe789c2dc0c9..78d2130e0061 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -23,6 +23,7 @@ void iommufd_hwpt_paging_destroy(struct iommufd_object *obj) container_of(obj, struct iommufd_hwpt_paging, common.obj); if (!list_empty(&hwpt_paging->hwpt_item)) { + /* unreachable if !hwpt_paging->ioas */ mutex_lock(&hwpt_paging->ioas->mutex); list_del(&hwpt_paging->hwpt_item); mutex_unlock(&hwpt_paging->ioas->mutex); @@ -32,7 +33,9 @@ void iommufd_hwpt_paging_destroy(struct iommufd_object *obj) } __iommufd_hwpt_destroy(&hwpt_paging->common); - refcount_dec(&hwpt_paging->ioas->obj.users); + + if (hwpt_paging->ioas) + refcount_dec(&hwpt_paging->ioas->obj.users); } void iommufd_hwpt_paging_abort(struct iommufd_object *obj) @@ -41,9 +44,11 @@ void iommufd_hwpt_paging_abort(struct iommufd_object *obj) container_of(obj, struct iommufd_hwpt_paging, common.obj); /* The ioas->mutex must be held until finalize is called. */ - lockdep_assert_held(&hwpt_paging->ioas->mutex); + if (hwpt_paging->ioas) + lockdep_assert_held(&hwpt_paging->ioas->mutex); if (!list_empty(&hwpt_paging->hwpt_item)) { + /* unreachable if !hwpt_paging->ioas */ list_del_init(&hwpt_paging->hwpt_item); iopt_table_remove_domain(&hwpt_paging->ioas->iopt, hwpt_paging->common.domain); @@ -457,6 +462,9 @@ int iommufd_hwpt_set_dirty_tracking(struct iommufd_ucmd *ucmd) return PTR_ERR(hwpt_paging); ioas = hwpt_paging->ioas; + if (!ioas) + return -EINVAL; + enable = cmd->flags & IOMMU_HWPT_DIRTY_TRACKING_ENABLE; rc = iopt_set_dirty_tracking(&ioas->iopt, hwpt_paging->common.domain, @@ -482,6 +490,9 @@ int iommufd_hwpt_get_dirty_bitmap(struct iommufd_ucmd *ucmd) return PTR_ERR(hwpt_paging); ioas = hwpt_paging->ioas; + if (!ioas) + return -EINVAL; + rc = iopt_read_and_clear_dirty_data( &ioas->iopt, hwpt_paging->common.domain, cmd->flags, cmd); -- 2.52.0.158.g65b55ccf14-goog