Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795409397; b=QPjOHxol+hf1Q7CyBGWKoOKLh0IYP4eHxLha9KAqRCzF4d9IkrySkxMHnIR0TArKhgVC fv+nM5iS1Un0amTrEaDcXSH4Y/SULPzJ1Xktiw1TurNYuTmRJtYFf9CMY4kA8cDTFYKDY jybwiUhBrM+uIWKrORInAaywCSARALufnW+tkUNxire0bKMGfhi0kSVQR25mUjFvOs/d7 kSx6ramiDrsO3iur9mztnTPxx3g0eHp2n1WNCzT9k+s9JFDafcRspJs5j9fL0Tdy8T1lM rgXOTIG0lFI9ztbwRmBL9b0ssyOwy2kv+5k//TJfvgjMxAT09z9UnsncHwtcVHL2hkQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795409397; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=Z8BQO3KrgykwRtgGi0Q1dGoj+G3A1sp7JKxsrHaGsP4=; b=mQ2LUmYX6jJNrjgBVroBeCqWfToPsexrmD6/Px/VZJ19kDTUCG9DxEd30ACWxa+yyK4n A9Z/GLOeCCUiSXTy/AqdOD0URUgNumMYwU4Yeow65aiFyEfrr1iB3weQBZrfXYDKooylB LkkrUbmjvvOrFSqc/jPZOQdIcECLw5UMcaCBuIChAWIU46DZnULpFMj0LLHQYwoHnbFYj /wzI1fqXsw8H+VsBdfhyuGuebYaYcezoi3wkuhVODXlIHQlc76Ak1Lmax52Owd4vwfinv BWihuoPMASzR+51ttWZdfHRDMnngPNn7HSroKZgqrhiQ92WgTqaJkoybNsX/BfxjClQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=BFBYjWDy; arc=none smtp.remote-ip=209.85.214.200 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F26960A2605 for ; Mon, 23 Nov 2026 04:49:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9F26960A2605 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.a=rsa-sha256 header.s=20260707 header.b=BFBYjWDy Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2f033dc37cfso28224385ad.0 for ; Sun, 22 Nov 2026 20:49:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795409396; x=1796014196; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Z8BQO3KrgykwRtgGi0Q1dGoj+G3A1sp7JKxsrHaGsP4=; b=BFBYjWDyNCMAZ4eabi/KLvr2pVa5tUBLFFu4ZWnbnLFYC1ThzbtmzCvOp4xJDFpfSL JHMpVsZJDrA4tabv64d4VTY3TVNQyEW+DxWAL2VIlxIa6847glgodhiruAzOe6fCnKvH XbVl8R6XKrXiQdJ03TV2QgaKaxBrO+W17gJYAWWZMHsAWGVgmL/HbqjB5qtVaNxRIjYe 5EO8dLawtpqN+JFGMAoH2DMk+xqoQlQSGNvJoyZHIEF76E2Nucng2/ytvUniqNqZ/JYv Gl4/sT4kGrG0iKbgBimDVJnj9OQ1dEoe356z3Cfm1DLhneABjqEk6h+mWwxwZvcrTuA3 SDsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795409396; x=1796014196; h=content-type: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:content-type; bh=Z8BQO3KrgykwRtgGi0Q1dGoj+G3A1sp7JKxsrHaGsP4=; b=SZvAVucfO/+aq4gq9+dJ9vAoWE8aqJNsinQO1+XC5yllky+1HczPOHAm6bgbhCFqUl 4CEPrMPPYts/5vWdM/hSomvZ7d+eGnMHu/AMYOUH6eh6Vs7KPs+sK4yvDC8mIohyOXh6 LZIm5+ybz4eMHcbaq2rMAqXZefaEwzzLuBkUDpu6xtiBFjqwGf3jR0MsJkk8/10JKh5u dyHZoWiMvyDRv6N2f4GJbBbwbjSOCXxF4vLYwAy4Uj32KwcUKTArtG3O9NKvItxQ2YSp gtlBPa/fcWeeCcjW8tXTEmk8TV3jySubMF8ILsT9a77oXlLXuVXfT6ViJzlQ9sf2SlGn d/5Q== X-Forwarded-Encrypted: i=1; AL+hErHhk7ve94boa8V8Ft+JBoRxqtpXjZ1H98nQeOG76nQ+UfcDGT06R9DtEqPeAFIVe8QfyYo=@vger.kernel.org X-Gm-Message-State: APuU4Z9SDDcjmfvyVj9Ta4YzzLvNRJPVPQbCLqFppTHQVwf04cNHu3pO vHRxdrGWvJ0zZtCIKXfgCZteW+8nsfSlpWRQErUcD7tXe6hsOWb5XLlWkm2UvdzehlsmAzEUjAf 0oFbnh7snha2FbQ== X-Received: from pla14.prod.google.com ([2002:a17:902:ffce:b0:2e8:44b9:5c18]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:24b:b0:2e9:9bac:2e90 with SMTP id d9443c01a7336-2f03f1aa1d0mr89749225ad.65.1795409395769; Sun, 22 Nov 2026 20:49:55 -0800 (PST) Date: Mon, 23 Nov 2026 04:49:36 +0000 In-Reply-To: <20261123044949.3610702-1-skhawaja@google.com> X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261123044949.3610702-1-skhawaja@google.com> X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261123044949.3610702-6-skhawaja@google.com> Subject: [PATCH v8 05/18] iommu: Implement IOMMU domain preservation From: Samiullah Khawaja To: Joerg Roedel , Will Deacon , Lu Baolu , Jason Gunthorpe , David Woodhouse Cc: Samiullah Khawaja , Pranjal Shrivastava , Robin Murphy , Kevin Tian , Alex Williamson , Shuah Khan , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Pratyush Yadav , Pasha Tatashin , David Matlack , Andrew Morton , Vipin Sharma , Ankit Soni , Nicolin Chen , John Starks Content-Type: text/plain; charset="UTF-8" Add IOMMU domain preserve and unpreserve APIs that can be used to preserve an IOMMU domain across liveupdate. These APIs use the iommupt preserve and unpreserve ops of the domain to perform the respective live update operations. Signed-off-by: Samiullah Khawaja Reviewed-by: Pranjal Shrivastava Reviewed-by: Lu Baolu --- drivers/iommu/liveupdate.c | 119 +++++++++++++++++++++++++++++++ include/linux/iommu-liveupdate.h | 11 +++ include/linux/iommu.h | 5 ++ 3 files changed, 135 insertions(+) diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c index a1f40e242d93..a98f16d11727 100644 --- a/drivers/iommu/liveupdate.c +++ b/drivers/iommu/liveupdate.c @@ -37,6 +37,7 @@ #define pr_fmt(fmt) "iommu: liveupdate: " fmt #include +#include #include #include #include @@ -67,6 +68,35 @@ struct iommu_flb_obj { struct iommu_ser_set device_set; }; +/* + * Reserve a new serialized object at the end of the set. The deleted objects + * are not reused, so the objects never move and can be referenced by their + * physical address. + */ +static void *iommu_ser_set_alloc(struct iommu_ser_set *set) +{ + struct iommu_hdr_ser *hdr; + int ret; + + ret = kho_block_set_grow(&set->bs, set->count + 1); + if (ret) + return ERR_PTR(ret); + + if (!set->count) + kho_block_set_it_init(&set->it, &set->bs); + + hdr = kho_block_set_it_reserve_entry(&set->it); + if (WARN_ON_ONCE(!hdr)) + return ERR_PTR(-ENOSPC); + + set->count++; + *set->head_pa = kho_block_set_head_pa(&set->bs); + + hdr->ref_count = 1; + hdr->size = set->bs.entry_size; + return hdr; +} + /* * Restore a set of serialized objects from the previous kernel. The objects * are read using the size recorded by the previous kernel in their header. @@ -239,3 +269,92 @@ void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) liveupdate_unregister_flb(handler, &iommu_flb); } EXPORT_SYMBOL_GPL(iommu_liveupdate_unregister_flb); + +/** + * iommu_preserve_domain() - Preserve an IOMMU domain across live update + * @domain: Domain to preserve + * @ser: Pointer to receive the virtual serialized domain state handle + * + * Return: 0 on success, or negative error code. + */ +int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser) +{ + struct pt_iommu *pt = iommupt_from_domain(domain); + struct iommu_domain_ser *domain_ser; + struct iommu_flb_obj *flb_obj; + int ret; + + if (!pt || !pt->ops->preserve || !pt->ops->unpreserve) + return -EOPNOTSUPP; + + ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj); + if (ret) + return ret; + + mutex_lock(&flb_obj->lock); + if (domain->domain_ser) { + ret = -EBUSY; + goto out_unlock; + } + + domain_ser = iommu_ser_set_alloc(&flb_obj->domain_set); + if (IS_ERR(domain_ser)) { + ret = PTR_ERR(domain_ser); + goto out_unlock; + } + + ret = pt->ops->preserve(pt, domain_ser); + if (ret) { + domain_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED; + goto out_unlock; + } + + domain->domain_ser = domain_ser; + *ser = domain_ser; + ret = 0; +out_unlock: + mutex_unlock(&flb_obj->lock); + liveupdate_flb_put_outgoing(&iommu_flb); + return ret; +} +EXPORT_SYMBOL_GPL(iommu_preserve_domain); + +/** + * iommu_unpreserve_domain() - Unpreserve a preserved IOMMU domain + * @domain: Domain to unpreserve + */ +void iommu_unpreserve_domain(struct iommu_domain *domain) +{ + struct pt_iommu *pt = iommupt_from_domain(domain); + struct iommu_domain_ser *domain_ser; + struct iommu_flb_obj *flb_obj; + int ret; + + if (WARN_ON(!pt || !pt->ops->unpreserve)) + return; + + ret = liveupdate_flb_get_outgoing(&iommu_flb, (void **)&flb_obj); + if (WARN_ON(ret)) + return; + + mutex_lock(&flb_obj->lock); + if (!domain->domain_ser) + goto out_unlock; + + /* + * There is no check for attached devices here. The correctness relies + * on the Live Update Orchestrator's session lifecycle. All resources + * (iommufd, vfio devices) are preserved within a single session. If the + * session is torn down, the .unpreserve callbacks for all files will be + * invoked, ensuring a consistent cleanup without needing explicit + * refcounting for the serialized objects here. + */ + domain_ser = domain->domain_ser; + pt->ops->unpreserve(pt, domain_ser); + domain_ser->hdr.flags |= IOMMU_SER_FLAG_DELETED; + domain->domain_ser = NULL; +out_unlock: + mutex_unlock(&flb_obj->lock); + liveupdate_flb_put_outgoing(&iommu_flb); +} +EXPORT_SYMBOL_GPL(iommu_unpreserve_domain); diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h index 4755ab3cd67a..caa9778eee2d 100644 --- a/include/linux/iommu-liveupdate.h +++ b/include/linux/iommu-liveupdate.h @@ -15,6 +15,8 @@ #ifdef CONFIG_IOMMU_LIVEUPDATE int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler); void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler); +int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser); +void iommu_unpreserve_domain(struct iommu_domain *domain); #else static inline int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler) { @@ -24,5 +26,14 @@ static inline int iommu_liveupdate_register_flb(struct liveupdate_file_handler * static inline void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler) { } + +static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser) +{ + return -EOPNOTSUPP; +} + +static inline void iommu_unpreserve_domain(struct iommu_domain *domain) +{ +} #endif #endif /* _LINUX_IOMMU_LIVEUPDATE_H */ diff --git a/include/linux/iommu.h b/include/linux/iommu.h index ef5043752a36..77f78c88c627 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #define IOMMU_READ (1 << 0) @@ -249,6 +250,10 @@ struct iommu_domain { struct list_head next; }; }; + +#ifdef CONFIG_IOMMU_LIVEUPDATE + struct iommu_domain_ser *domain_ser; +#endif }; static inline bool iommu_is_dma_domain(struct iommu_domain *domain) -- 2.56.1.809.ga0b61035b5-goog