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 E35D6322C6D for ; Tue, 2 Dec 2025 23:03:47 +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=1764716630; cv=none; b=DRs2qP3W3KpiTJtc9K/Y5Gi4qXmr9JmlCcEbCXWHh0qqNrcmE6CVZXSnPcTk90JvBgUpQoJ28+0MkL41+eFKCwpj17tntZXWnUzucjsbvwE2ZK/sYplQ4L3MWHGrp/KTqCR48w+ZAkmQlVJ3cHjdNiJDvdbdCuoHo40L2BSCvHs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716630; c=relaxed/simple; bh=N3EqjwtTe9su2bIBiTmXGew4mmqSpg+pDjvGW6S4hCo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ie01p+33lArWSFsu+AS7royVTYy1jEE54XKRTg3HOF2bFHmefPkEbrzUmm2odezGoA77RbncMs/razr4IPszjGJb+lS7CG6P2uZdLCbTMUe+IBlz3CQBK1FCdTiGDLL6u/2vVHPMMyc2+mtmgfoD7znDLpg6zXWaXvzT271JJ/A= 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=3NpzVmU2; 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="3NpzVmU2" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7b873532cc8so207860b3a.1 for ; Tue, 02 Dec 2025 15:03:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716627; x=1765321427; 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=DNSA26/0+alws+cJX6nlD69kg695LZYJa/y+ifDRVVk=; b=3NpzVmU2jS76o1tIuwPO+gjK53UNEt3sclKSScjeFFsWfAmmvo1RDitGm7VAoQ/WQa qEf9u2NtTbqUzrU636rU2I4CHcEx+nNDwliVl23lTN/BOwvKkOzoYATlEzF6PzSKT1pl VukiIzzcolC+c9hv6et+o7RsWe7ITeVUiWaMqAP9l93bAzXJFoWQwJ5Ab3cW3/lUbWmk BS3vctWm4IlQ385JKtVVMovUaYDPdOp4COEQcE4OXpI60RYL2iFp1lKtRxECxDST644V PsM8tLYwzqgZ1RUQ4+P1kUkOZzOMJR6k6myvjfTwc8ZIYknvnNMRiDP/8kEQk6Iy99Au WmaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716627; x=1765321427; 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=DNSA26/0+alws+cJX6nlD69kg695LZYJa/y+ifDRVVk=; b=ZcRzsKFpKcIIV3GpWqlIPudV8xZ7s6cOiLwpCnYpUKCCbfo/DlJfzLJA3/SpHG8DVy TDdAPTFIzNgkr3tYLXSTPyb0Bjaq2xUXdmOO7A6+1S+iqTikVNhuBISKbMlHWhet8udl 160UPJpS5dyQaZMWF0J0g8hk4mSJ/mBY4864iEK+569Pp1EY5infrCt8Pn00YODJyl9X DWny9rIu93PIvqEcz72k1u11Vudfg8ZjyVbF/heYQWacirkcrG8Bt2kIqqXBiYPVHVN6 pbmUT4zZjXmvioUb6NzaUy8kQ56Cqc67/cjPcOuzJCnKrsXQSkthQNAdKc1eFtHJSxBB d/sQ== X-Forwarded-Encrypted: i=1; AJvYcCVwFSIUcQLZbw4tFIF1lVv6fJ99m26ZQerz+BrXCLIWpPdR/XZLTBi4TkGeRiQEjXE6wqpcIM3KztNwhno=@vger.kernel.org X-Gm-Message-State: AOJu0Yyu7ZnVZ3Y0Y4Vj+YzFr6oWXSAxZHIWf1raBUQGrzLgKPuCU9BS R4aACGcOtyq/ra2STcreXw7sw1PkHBePNYI6iLVuevOt1goG4efNtEaG91vqOwAc1EtUgMnjZhd rLiTV4KsJulR7tg== X-Google-Smtp-Source: AGHT+IEQcTdBsG+lezU0a0aTxC6nQMJIRJmCEjaChT/fOWKOUkp2R6PTeSxb+1Ke6yUnV01KNpqjsxktw4Uf7w== X-Received: from pfbfh23.prod.google.com ([2002:a05:6a00:3917:b0:7a5:9e0b:aed9]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3d43:b0:7aa:d1d4:bb7b with SMTP id d2e1a72fcca58-7dff4758789mr341995b3a.16.1764716627187; Tue, 02 Dec 2025 15:03:47 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:58 +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-29-skhawaja@google.com> Subject: [RFC PATCH v2 28/32] iommu/vt-d: reuse the preserved domain id for preserved devices From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , Alex Williamson , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, YiFei Zhu , Chris Li , praan@google.com Content-Type: text/plain; charset="UTF-8" Preserved devices have their domain ids preserved by the previous kernel. During restore and reattach in the next kernel the domain ids are already reclaimed so reuse the presered domain id. Signed-off-by: Samiullah Khawaja --- drivers/iommu/intel/iommu.c | 42 +++++++++++++++++++++++------------- drivers/iommu/intel/iommu.h | 3 ++- drivers/iommu/intel/nested.c | 2 +- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 888351f91918..177bf1b2715f 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1033,7 +1033,8 @@ static bool first_level_by_default(struct intel_iommu *iommu) return true; } -int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) +int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu, + int restore_did) { struct iommu_domain_info *info, *curr; int num, ret = -ENOSPC; @@ -1053,8 +1054,11 @@ int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) return 0; } - num = ida_alloc_range(&iommu->domain_ida, IDA_START_DID, - cap_ndoms(iommu->cap) - 1, GFP_KERNEL); + if (restore_did >= 0) + num = restore_did; + else + num = ida_alloc_range(&iommu->domain_ida, IDA_START_DID, + cap_ndoms(iommu->cap) - 1, GFP_KERNEL); if (num < 0) { pr_err("%s: No free domain ids\n", iommu->name); goto err_unlock; @@ -1325,10 +1329,16 @@ static int dmar_domain_attach_device(struct dmar_domain *domain, { struct device_domain_info *info = dev_iommu_priv_get(dev); struct intel_iommu *iommu = info->iommu; + struct device_ser *device_ser = NULL; unsigned long flags; int ret; - ret = domain_attach_iommu(domain, iommu); +#ifdef CONFIG_LIVEUPDATE + device_ser = dev_iommu_restored_state(dev); +#endif + + ret = domain_attach_iommu(domain, iommu, + dev_iommu_restore_did(dev, &domain->domain)); if (ret) return ret; @@ -1341,16 +1351,18 @@ static int dmar_domain_attach_device(struct dmar_domain *domain, if (dev_is_real_dma_subdevice(dev)) return 0; - if (!sm_supported(iommu)) - ret = domain_context_mapping(domain, dev); - else if (intel_domain_is_fs_paging(domain)) - ret = domain_setup_first_level(iommu, domain, dev, - IOMMU_NO_PASID, NULL); - else if (intel_domain_is_ss_paging(domain)) - ret = domain_setup_second_level(iommu, domain, dev, - IOMMU_NO_PASID, NULL); - else if (WARN_ON(true)) - ret = -EINVAL; + if (!device_ser) { + if (!sm_supported(iommu)) + ret = domain_context_mapping(domain, dev); + else if (intel_domain_is_fs_paging(domain)) + ret = domain_setup_first_level(iommu, domain, dev, + IOMMU_NO_PASID, NULL); + else if (intel_domain_is_ss_paging(domain)) + ret = domain_setup_second_level(iommu, domain, dev, + IOMMU_NO_PASID, NULL); + else if (WARN_ON(true)) + ret = -EINVAL; + } if (ret) goto out_block_translation; @@ -3612,7 +3624,7 @@ domain_add_dev_pasid(struct iommu_domain *domain, if (!dev_pasid) return ERR_PTR(-ENOMEM); - ret = domain_attach_iommu(dmar_domain, iommu); + ret = domain_attach_iommu(dmar_domain, iommu, -1); if (ret) goto out_free; diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index b0c56e27f167..aa336050015e 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -1174,7 +1174,8 @@ void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, u64 addr, */ #define QI_OPT_WAIT_DRAIN BIT(0) -int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu); +int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu, + int restore_did); void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu); void device_block_translation(struct device *dev); int paging_domain_compatible(struct iommu_domain *domain, struct device *dev); diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c index a3fb8c193ca6..4fed9f5981e5 100644 --- a/drivers/iommu/intel/nested.c +++ b/drivers/iommu/intel/nested.c @@ -40,7 +40,7 @@ static int intel_nested_attach_dev(struct iommu_domain *domain, return ret; } - ret = domain_attach_iommu(dmar_domain, iommu); + ret = domain_attach_iommu(dmar_domain, iommu, -1); if (ret) { dev_err_ratelimited(dev, "Failed to attach domain to iommu\n"); return ret; -- 2.52.0.158.g65b55ccf14-goog