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 6F3043019BD for ; Tue, 2 Dec 2025 23:03:25 +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=1764716608; cv=none; b=RG9+T5M2mIn0C7rCkwkZmealKhG15OSwKcrQza6h/O/o3FM5OKXVuS+ECEap6ySoTfGHsTBknnUebdECHiEacIf3+T5VJ5GgK+LPMwgnUp2tHNloqx2OOfugJTElQu+OkWa0cCdgFT5jp9+6dcyrfIGrv0nz83dZ2+2k3/+DluE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716608; c=relaxed/simple; bh=+wJGUPW+DGR0+eE6ainbStfGk+YSOiY3wmspBdRDkGc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UlaefLw34KuOeucVlwcUQMQAIv7Cs3nxZ5BEUlgB9LATN85cBAchWGTl5jJJo4CHzE1D2zwB2Uk23BvBNzT2kEtERr2uzdPUYTBs/bC5M8GYlz+N8FQ9riIt+mQT6Tgxyt7kWp67O0OZrasf/jDsJpB2LF253SrlqttWl9qZ3qY= 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=DlhKGk5b; 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="DlhKGk5b" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7b96038aa68so262551b3a.0 for ; Tue, 02 Dec 2025 15:03:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716605; x=1765321405; 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=DwNkBuTOYxcz9fcP4mex7+raXbrBc4r0h1CS3B8eZyw=; b=DlhKGk5bCue/vqgSb0vwFkNZ2sNhCMNHfN5E+ghbXHoq4RGRUoCu9SQ+yZur2k5P8y KlXfb7smg8wjK1un20JYieinLAfb3+o/q4wmwyAuNmCYc59XLE154FLXmtYKHM6urzL5 xLQfqRrohxjFAaC93LCqVf9Y+L9xsexQyhWLvpOf8dlJiwMSuosZbQm0Nku9uuUJYAZO 1TvUXJgzpulwtQwK8XgIDDYrbv5/Q18xshfidrQUAhjMF8LAdG/OlMgNgjhoJ0gRPC9C mWOIYpkVYrhALNQtJop9fO10JRusIVhvRO8yIYbahKaxvZoBJQol4IwIWG13y2ohCUjt oLew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716605; x=1765321405; 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=DwNkBuTOYxcz9fcP4mex7+raXbrBc4r0h1CS3B8eZyw=; b=cYreX6rxxn7p7dpzzN3seAbgeLnUVeF6PtwVMQDBzbJM1Dv/nfQr5f8kBE/3Yuku8g QqqjETntmQCqDKxmmzDGb9hmy67n39qhmhpv+Onkdv8PYLvnGXDmO6eaWa4SuNGU3WVN VZX4m4t63SV59jqsHvPijkGv3NPxb1dVYQKYlx5nswmKMhG67Q+LzNLposP9Tzi2sMW3 LyEY9CTRNmdxs5KUgPr4jmjpsgOAvJnZgfBarAPH3toF+VCKKUcArulmrnop2P8kab95 Dm2q6SIuzMyKj4fj7dwwsv3wOHzPg+spfFTamd4bpVOqW6/24pDq/4JxqfZwN0XTidvm k60w== X-Forwarded-Encrypted: i=1; AJvYcCW4rOEvlMp4w4Ofab9QQneYhptBYUNj/+F8aUPYWmhBcIEjFiZLUbDx8UvjxusI/XXllZfSRhE7aUPCZUU=@vger.kernel.org X-Gm-Message-State: AOJu0YxxXTnP59mHOu63J1gTg77mOeY43QFKjoW4MPnCy7S6eFS1H0ND FoHRf7pFakjKZSyyum/rkW2UrARpFIrwH7zmsd/rsLeGUaRmGBHWQk7z5UqlnojVzhlbgd5Yv6x Ufo0j1hdqFT6XYg== X-Google-Smtp-Source: AGHT+IEral7Kt58UysjsBZjUzARRPkNEdR/sgcAZVZv8DXV6nL3lw2H7AE/s2MPYUc46xQvCS+YJsoRQcXAieA== X-Received: from pfld4.prod.google.com ([2002:a05:6a00:1984:b0:7dd:8bba:6393]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1a8d:b0:7b0:1d84:8634 with SMTP id d2e1a72fcca58-7dff408dc23mr318972b3a.15.1764716604926; Tue, 02 Dec 2025 15:03:24 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:43 +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-14-skhawaja@google.com> Subject: [RFC PATCH v2 13/32] iommu: Add APIs to preserve/unpreserve iommu domains 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" These APIs will be used by iommufd LU handler to preserve and unpreserve a domain for live update. Signed-off-by: Samiullah Khawaja --- drivers/iommu/liveupdate.c | 82 ++++++++++++++++++++++++++++++++++++++ include/linux/iommu-lu.h | 2 + include/linux/iommu.h | 4 ++ 3 files changed, 88 insertions(+) diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c index 21ce7be9b87e..25a943e5e1e3 100644 --- a/drivers/iommu/liveupdate.c +++ b/drivers/iommu/liveupdate.c @@ -127,3 +127,85 @@ int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) return liveupdate_unregister_flb(handler, &iommu_flb); } EXPORT_SYMBOL(iommu_liveupdate_unregister_flb); + +static int reserve_obj_ser(struct iommu_objs_ser **objs_ptr, u64 max_objs) +{ + struct iommu_objs_ser *next_objs, *objs = *objs_ptr; + int idx; + + if (objs->nr_objs == max_objs) { + next_objs = kho_alloc_preserve(PAGE_SIZE); + if (!next_objs) + return -ENOMEM; + + objs->next_objs = virt_to_phys(next_objs); + objs = next_objs; + *objs_ptr = objs; + objs->nr_objs = 0; + } + + idx = objs->nr_objs++; + return idx; +} + +int iommu_domain_preserve(struct iommu_domain *domain, struct iommu_domain_ser **ser) +{ + struct iommu_domain_ser *domain_ser; + struct iommu_lu_flb_obj *flb_obj; + int idx, ret; + + if (!domain->ops->preserve) + return -EOPNOTSUPP; + + ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj); + if (ret) + return ret; + + guard(mutex)(&flb_obj->lock); + idx = reserve_obj_ser((struct iommu_objs_ser **)&flb_obj->iommu_domains, + MAX_IOMMU_DOMAIN_SERS); + if (idx < 0) + return idx; + + domain_ser = &flb_obj->iommu_domains->iommu_domains[idx]; + idx = flb_obj->ser->nr_domains++; + domain_ser->obj.idx = idx; + domain_ser->obj.ref_count = 1; + + ret = domain->ops->preserve(domain, domain_ser); + if (ret) { + domain_ser->obj.deleted = true; + return ret; + } + + domain->preserved_state = domain_ser; + *ser = domain_ser; + return 0; +} +EXPORT_SYMBOL_GPL(iommu_domain_preserve); + +int iommu_domain_unpreserve(struct iommu_domain *domain) +{ + struct iommu_domain_ser *domain_ser; + struct iommu_lu_flb_obj *flb_obj; + int ret; + + if (!domain->ops->unpreserve) + return -EOPNOTSUPP; + + ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj); + if (ret) + return ret; + + guard(mutex)(&flb_obj->lock); + domain_ser = domain->preserved_state; + if (domain_ser->attach_count) + ret = -EBUSY; + + domain->ops->unpreserve(domain, domain_ser); + domain_ser->obj.deleted = true; + domain->preserved_state = NULL; + + return 0; +} +EXPORT_SYMBOL_GPL(iommu_domain_unpreserve); diff --git a/include/linux/iommu-lu.h b/include/linux/iommu-lu.h index 59095d2f1bb2..2c8e5ac746ad 100644 --- a/include/linux/iommu-lu.h +++ b/include/linux/iommu-lu.h @@ -11,6 +11,8 @@ #include #include +int iommu_domain_preserve(struct iommu_domain *domain, struct iommu_domain_ser **ser); +int iommu_domain_unpreserve(struct iommu_domain *domain); int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler); int iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index f681d4d27d6e..17e1f3c29958 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -249,6 +249,10 @@ struct iommu_domain { struct list_head next; }; }; + +#ifdef CONFIG_LIVEUPDATE + struct iommu_domain_ser *preserved_state; +#endif }; static inline bool iommu_is_dma_domain(struct iommu_domain *domain) -- 2.52.0.158.g65b55ccf14-goog