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 101FE2FC030 for ; Tue, 2 Dec 2025 23:03:11 +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=1764716593; cv=none; b=CkOqPNfynnVBl/LyGcfMyGf6JCQOVrOfxDlw1+kWGXy7kiPIuRC1HJCPkm39UrN2HcSVqmhOzFbBmAIfDSdEjc41HbcOEqYMCa/3tAsLk9rOQGLtrjVqqy3nh2f5IUZG5CWrdr3wNGkODMDPGWnlruP6Umbyf0vDNk8Ich0uc4A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716593; c=relaxed/simple; bh=AFk5Ea55TAJa45eRFuyCiONTrwUNxTAU7i38Db4LRrk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DCk8t9InbszJvbaJqKYc1XHtcO+9aQzirM7LThk2EjypF0TGGPoBQUXLQ4HkKwPvB5Cb6VojIBJfoczKCR5floMq9yzt0xJevsWwdQVmF6E/2ZQUijYTn1zXPruTLX1LlYPiAapVHCGt8kycgUtxywOuwVwJCYe/0p9LD6Oh+Cs= 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=rsZ6DXJ2; 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--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="rsZ6DXJ2" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-34188ba5990so14530969a91.0 for ; Tue, 02 Dec 2025 15:03:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716591; x=1765321391; 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=9hD0KJCE1xNxvrhBxO7YABzs3soKeKORw0maD4NtSyI=; b=rsZ6DXJ2tc6gN5KzmNu/kKuzqg4Owj4TyAvXRpKB9SG6YlR4rYZ89ft4V7fYc9hW/2 2gYNvWAcWQ8Octm5sX6ji2viVh2V7YKuZkwlI+QpPtz0Z2ZSdaS+x9PRkS/i896aSrum BZPyvxnTJvtsEstCCxAlvcX6u8r6RT39HhLx4ZJ7QHHcJKP8AY5q7u/HuyV44G4aduDF 29DWOc4+1QT75hWOW9uQ0Dc0apXBNOk2pRhVe/H9nYxo4fJ+GdHrcw664qyOE1JEuEy4 tfBAGd6e9yZmtZQ8uO5XLUVagQmDCyjfyl9E1CpBdLF/5f9HmRrQsIzzq9MGxVKESTD6 vUMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716591; x=1765321391; 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=9hD0KJCE1xNxvrhBxO7YABzs3soKeKORw0maD4NtSyI=; b=m3s5tU+F6ExNbojUuQeEUsOuFc9L2oqBcmBU3z8PhosaY7R+DrTz/w4O2orwO3G7wJ ol/vulP6/XYw0D0Yw1146XxSf/C4quC03o6z98u4pmAdBmhMY9HoDxKu7WgzKgWEslup B5rRpe3WyT0Mpi+7Q2ujlOP8tM71L1TXC9xUq7++NJwMs0k/KaPhWFTE8RpNyZjhjJyJ SENezKDc1TolOv4U+RPW4dUHdPoip57sOGC9ZPi39+79i8RzeoPq5/x9Nb5W4FcC/XPL 1c+ja6fFQZRhjN0aAkhAPrEM5BFc7dUVY2uBt+RZS+GQQodE0/IqabxbLLQJFaPo6L7t jiuQ== X-Forwarded-Encrypted: i=1; AJvYcCXPmANFzjscXRRuMFDSOJPuPRnYCeUwBvlXV9s/qM7KCWch+c6Hzd6ms622LAwDh64WiGqiupYkt4zrPUw=@vger.kernel.org X-Gm-Message-State: AOJu0YxtdU7ww0auzwCOe/Yyp6sxNa4Bn99k0DS7eEqJvbYvItMpDpDJ 7kBozVovsQOhRk7t+EtIrclaUdOcbYYlGcdoJSh4vGyRky9z1C56h6dJYAL/nQXP3gYOhYIWVqP G6znHCwFUArqEEw== X-Google-Smtp-Source: AGHT+IG6VnJE2WmZozrveUHuXcYbn7N3t2981lMSNFJifrj+l9b4AV3NPIwPwvCWHKzsPSE9UhaWjjGLlPk0aw== X-Received: from pjz7.prod.google.com ([2002:a17:90b:56c7:b0:32d:69b3:b7b0]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:180d:b0:32b:c9c0:2a11 with SMTP id 98e67ed59e1d1-349126e21a5mr376280a91.4.1764716591295; Tue, 02 Dec 2025 15:03:11 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:34 +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-5-skhawaja@google.com> Subject: [RFC PATCH v2 04/32] iommufd-lu: Implement basic prepare/cancel/finish/retrieve using folios From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: YiFei Zhu , 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, Chris Li , praan@google.com Content-Type: text/plain; charset="UTF-8" From: YiFei Zhu The actual serialization and de-serialization is implemented in follow up commits. - On prepare, a single folio is created and preserved to store all the structs. - On cancel, the folio is unpreserved and freed. - On retrieve, the folio is restored, then an fd with anon_inode is created, with data pointing to the folio. - On finish, the folio is freed. Signed-off-by: YiFei Zhu Signed-off-by: Samiullah Khawaja --- MAINTAINERS | 1 + drivers/iommu/iommufd/iommufd_private.h | 7 ++ drivers/iommu/iommufd/liveupdate.c | 113 ++++++++++++++++++++++-- drivers/iommu/iommufd/main.c | 2 +- include/linux/kho/abi/iommufd.h | 31 +++++++ 5 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 include/linux/kho/abi/iommufd.h diff --git a/MAINTAINERS b/MAINTAINERS index 3bb9edf09f0e..bfe646d1c74f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13243,6 +13243,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/jgg/iommufd.git F: Documentation/userspace-api/iommufd.rst F: drivers/iommu/iommufd/ F: include/linux/iommufd.h +F: include/linux/kho/abi/iommufd.h F: include/uapi/linux/iommufd.h F: tools/testing/selftests/iommu/ diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index b6959ad55ad4..dfe9120aced0 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -21,6 +21,9 @@ struct iommu_option; struct iommufd_device; struct dma_buf_attachment; struct dma_buf_phys_vec; +struct iommufd_lu; + +extern const struct file_operations iommufd_fops; struct iommufd_sw_msi_map { struct list_head sw_msi_item; @@ -57,6 +60,10 @@ struct iommufd_ctx { /* Compatibility with VFIO no iommu */ u8 no_iommu_mode; struct iommufd_ioas *vfio_ioas; + +#ifdef CONFIG_LIVEUPDATE + struct iommufd_lu *lu; +#endif }; /* Entry for iommufd_ctx::mt_mmap */ diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liveupdate.c index d228157b6fed..a9b5956c0dee 100644 --- a/drivers/iommu/iommufd/liveupdate.c +++ b/drivers/iommu/iommufd/liveupdate.c @@ -2,15 +2,44 @@ #define pr_fmt(fmt) "iommufd: " fmt +#include #include #include +#include +#include #include +#include #include "iommufd_private.h" static int iommufd_liveupdate_preserve(struct liveupdate_file_op_args *args) { - return -EOPNOTSUPP; + struct iommufd_ctx *ictx = iommufd_ctx_from_file(args->file); + struct iommufd_lu *iommufd_lu; + size_t serial_size; + void *mem; + int rc; + + if (IS_ERR(ictx)) + return PTR_ERR(ictx); + + serial_size = sizeof(*iommufd_lu); + + mem = kho_alloc_preserve(serial_size); + if (!mem) { + rc = -ENOMEM; + goto err_ctx_put; + } + + iommufd_lu = mem; + + args->serialized_data = virt_to_phys(iommufd_lu); + iommufd_ctx_put(ictx); + return 0; + +err_ctx_put: + iommufd_ctx_put(ictx); + return rc; } static int iommufd_liveupdate_freeze(struct liveupdate_file_op_args *args) @@ -21,26 +50,100 @@ static int iommufd_liveupdate_freeze(struct liveupdate_file_op_args *args) static void iommufd_liveupdate_unpreserve(struct liveupdate_file_op_args *args) { + struct iommufd_ctx *ictx = iommufd_ctx_from_file(args->file); + + if (WARN_ON(IS_ERR(ictx))) + return; + + kho_unpreserve_free(phys_to_virt(args->serialized_data)); + iommufd_ctx_put(ictx); } static int iommufd_liveupdate_retrieve(struct liveupdate_file_op_args *args) { - return -EOPNOTSUPP; + struct iommufd_lu *iommufd_lu; + struct iommufd_ctx *ictx; + struct folio *folio_lu; + struct file *file; + int rc; + + folio_lu = kho_restore_folio(args->serialized_data); + if (IS_ERR_OR_NULL(folio_lu)) + return -EFAULT; + + iommufd_lu = folio_address(folio_lu); + + file = anon_inode_create_getfile("iommufd", &iommufd_fops, + NULL, O_RDWR, NULL); + if (IS_ERR(file)) { + rc = PTR_ERR(file); + goto err_folio_put; + } + + rc = iommufd_fops.open(file->f_inode, file); + if (rc) + goto err_fput; + + ictx = iommufd_ctx_from_file(file); + if (WARN_ON(IS_ERR(ictx))) { + rc = PTR_ERR(ictx); + goto err_fput; + } + + if (WARN_ON(ictx->lu)) { + rc = -EEXIST; + goto err_ctx_put; + } + ictx->lu = iommufd_lu; + + iommufd_ctx_put(ictx); + + args->file = file; + + return 0; + +err_ctx_put: + iommufd_ctx_put(ictx); +err_fput: + fput(file); +err_folio_put: + folio_put(folio_lu); + return rc; } static bool iommufd_liveupdate_can_finish(struct liveupdate_file_op_args *args) { - return false; + if (!args->retrieved || !args->file) { + pr_warn("%s: fd not reclaimed\n", __func__); + return false; + } + + return true; } static void iommufd_liveupdate_finish(struct liveupdate_file_op_args *args) { + struct iommufd_lu *iommufd_lu; + struct iommufd_ctx *ictx; + + ictx = iommufd_ctx_from_file(args->file); + iommufd_lu = ictx->lu; + ictx->lu = NULL; + iommufd_ctx_put(ictx); + + folio_put(virt_to_folio(iommufd_lu)); } static bool iommufd_liveupdate_can_preserve(struct liveupdate_file_handler *handler, struct file *file) { - return false; + struct iommufd_ctx *ictx = iommufd_ctx_from_file(file); + + if (IS_ERR(ictx)) + return false; + + iommufd_ctx_put(ictx); + return true; } static struct liveupdate_file_ops iommufd_lu_file_ops = { @@ -54,7 +157,7 @@ static struct liveupdate_file_ops iommufd_lu_file_ops = { }; static struct liveupdate_file_handler iommufd_lu_handler = { - .compatible = "iommufd-v1", + .compatible = IOMMUFD_LUO_COMPATIBLE, .ops = &iommufd_lu_file_ops, }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 18cc4af0a5c4..12601f9ad217 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -610,7 +610,7 @@ static int iommufd_fops_mmap(struct file *filp, struct vm_area_struct *vma) return rc; } -static const struct file_operations iommufd_fops = { +const struct file_operations iommufd_fops = { .owner = THIS_MODULE, .open = iommufd_fops_open, .release = iommufd_fops_release, diff --git a/include/linux/kho/abi/iommufd.h b/include/linux/kho/abi/iommufd.h new file mode 100644 index 000000000000..19d6b61ec3c3 --- /dev/null +++ b/include/linux/kho/abi/iommufd.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (C) 2025, Google LLC + * Author: Samiullah Khawaja + */ + +#ifndef _LINUX_KHO_ABI_IOMMUFD_H +#define _LINUX_KHO_ABI_IOMMUFD_H + +#include +#include +#include + +/** + * DOC: IOMMUFD Live Update ABI + * + * This header defines the ABI for preserving the state of an IOMMUFD file + * across a kexec reboot using LUO. + * + * This interface is a contract. Any modification to any of the serialization + * structs defined here constitutes a breaking change. Such changes require + * incrementing the version number in the IOMMUFD_LUO_COMPATIBLE string. + */ + +#define IOMMUFD_LUO_COMPATIBLE "iommufd-v1" + +struct iommufd_lu { +}; + +#endif /* _LINUX_KHO_ABI_IOMMUFD_H */ -- 2.52.0.158.g65b55ccf14-goog