From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795409405; b=33D8vhbX/2IGcKKtC8llT5ExY249TsJO+WdWpjL+38KlON/ugItliepOwK+1w8+mA40v i7OboAF1axo/dyNJqTHJgUuVipSgg6j+L+LsW4HKzG533Nh9HSVm8s3GpSFty8HSAL2gY Ay5PjxZL9iiZgu3TGBmiTSs4ZxKkR7NCoLZldrlS5g1cDukPICv519XEdui/cDOJdV8TL 7VDgzFxwgf7hXxaHfsC3rTeKftUr9eBVwUBl3/vmOqSVVm2U7lI28S0m05sz+S2V1YbLZ YjSZwB0QYmGmMDsoxT5qJ7BeQskMWuDDibiOa3WmJ/vrYIc4xLYMU0X4EywZUFeKcNA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795409405; 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=39/so1FjaFdi6WcGoZaDeWQdPor+TxvpgEJoYfxHlTU=; b=pQH0dh/MMbQNRGmd+H1moR/FTxK7OFIggHCjUBmKtSXaYp/sY6wirFNj7KhzoBhyLjpG W6c7/grIQK1EVQswcNi7N8YXlWvUVeloEssWHam1iIpuUKLa7lZYCdDmy9pe3YU6tCaBS 1UF0pFjuN/nLvZLN/+t6QyPqhVOjNSC3M1F5IatNZLppzZ322SVPgkaRXkEpOLpv6U1a+ skNHBA83yriNxRBBl8r3xO3vwCd7udZnAMk2TPdHAo9nj57uavfTJtfmhpWtQmJCddJ0k CHgbkajo5Hwgmi0xMlBGJ35IZoFzOZIgD1BM1b+KhtRsOagVsW3KL4WOtHVFsE0JSAQ== 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=Id2lfava; arc=none smtp.remote-ip=209.85.214.198 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (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 B1DFF60A6F74 for ; Mon, 23 Nov 2026 04:50:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B1DFF60A6F74 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=Id2lfava Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2f0717e54b0so10947685ad.0 for ; Sun, 22 Nov 2026 20:50:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795409404; x=1796014204; 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=39/so1FjaFdi6WcGoZaDeWQdPor+TxvpgEJoYfxHlTU=; b=Id2lfavaJq6FoSq7O1jj211cQUmv6o2m6/C/u0HiKiYJTWU/qasdgVqQJhEMI99Ukb JqMjGZAxUPfAVbmwAkfbJDvGzE/soUgI8EzHS0tydpMHu+CLfEWAX7Jd8DwFP1CReFaD N7p2zTugAQ1zhIeut0BW/H49YD8MitWLDYEhdrqw6UDCJA0AACe9cSIIuSi0gHfOitYv ZSzHFrk6B2yK4tZnZeAPzVHkbUN65UlwNayCstIywd4uV7YJZxL20GTTCFj7C8T0v94v MM5Jl99UkQY2YLsjTmft7C8NKvqSzm1PO65Z2+GujwJ++mjkvgMDkTv7Fist/IHKI66L 8jww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795409404; x=1796014204; 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=39/so1FjaFdi6WcGoZaDeWQdPor+TxvpgEJoYfxHlTU=; b=ipwao1Go+VLzZO2tp6iit0d87qlQP//PW3xcoJk/sk2soweaqZ/BqxDKd+BC5LPM+f 647rh2IhraKPF2pOWnkM84/qTufd02Ffd0qoOEeOCex1JAnXueZqAVDemTjehO35/R6O 0sCiJTZnOUw8JDn1B8RtHqwTbFIXIyChxWsaKCdUY+vebwgqQm3isZ2I9oDAWv+8xz8B REG2DBmCX9QRAcNp/6V9uJ4PzE9wf+d/Tx0WjaWtTlM9vzjBPTdEYxfvmIIc/t3+e3Pe b0kBMZtZeltIT1MApjNirXWQAT3MUHE2UvDCZip3Hnhu0X9e/e9D5AmQc7CSphDE8cBl GdFQ== X-Forwarded-Encrypted: i=1; AL+hErE31oVpPjCsloUsJxBm4IgWG+PZHBeIa5SI1Cuq1j7Pj8yLFs25y0RKqxY4lXjml6sdkfxDsr0MQ2JDbxs=@vger.kernel.org X-Gm-Message-State: APuU4Z/dkKG6MQWBN1lBZ6Tyww4Yrukgyb0TTmPdxL4dsBjOZRLlqTdT cdwj6B79paKkbIWxI9RejI1B2i9WfVUzra6mV1JQ8HXWNjr7wUiAN4fCQpcVcLDDyuasXmLyqLV f1ICMw0XD7F4SUA== X-Received: from plbkc3.prod.google.com ([2002:a17:903:33c3:b0:2f0:424a:82c6]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1ac8:b0:2f0:a586:81d9 with SMTP id d9443c01a7336-2f0a58682a8mr7145295ad.50.1795409404019; Sun, 22 Nov 2026 20:50:04 -0800 (PST) Date: Mon, 23 Nov 2026 04:49:46 +0000 In-Reply-To: <20261123044949.3610702-1-skhawaja@google.com> X-Mailing-List: linux-kernel@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-16-skhawaja@google.com> Subject: [PATCH v8 15/18] iommufd: Implement ioctl to mark HWPT for preservation From: Samiullah Khawaja To: Joerg Roedel , Will Deacon , Lu Baolu , Jason Gunthorpe , David Woodhouse Cc: YiFei Zhu , 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" From: YiFei Zhu Userspace provides a token to mark the HWPT for preservation. Note that this token is not the LUO token that is used to preserve the iommufd. Once all the required HWPT are marked for preservation, the user can preserve the iommufd into LUO. The iommufd will preserve the HWPTs that are marked for preservation. The marked HWPTs are tracked using a new XArray mark protected by a new liveupdate mutex. This mutex will also be used during iommufd preservation to protect against any race with the mark preserve ioctl. The HWPT token will be used during restore to identify this HWPT. The restoration logic is not implemented and will be added later. Signed-off-by: YiFei Zhu Signed-off-by: Samiullah Khawaja Reviewed-by: Pranjal Shrivastava Reviewed-by: Lu Baolu --- MAINTAINERS | 1 + drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/iommufd_private.h | 17 ++++++ drivers/iommu/iommufd/liveupdate.c | 75 +++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 9 +++ include/uapi/linux/iommufd.h | 31 ++++++++++ 6 files changed, 134 insertions(+) create mode 100644 drivers/iommu/iommufd/liveupdate.c diff --git a/MAINTAINERS b/MAINTAINERS index 0c66ccfd9c8f..81fb1fed6a6a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14152,6 +14152,7 @@ M: Samiullah Khawaja R: Pranjal Shrivastava L: iommu@lists.linux.dev S: Maintained +F: drivers/iommu/iommufd/liveupdate.c F: drivers/iommu/liveupdate.c F: include/linux/iommu-liveupdate.h F: include/linux/kho/abi/iommu.h diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile index 67207914bb6e..cfd5b4b14ccb 100644 --- a/drivers/iommu/iommufd/Makefile +++ b/drivers/iommu/iommufd/Makefile @@ -12,6 +12,7 @@ iommufd-y := \ iommufd-$(CONFIG_IOMMUFD_NOIOMMU) += hwpt_noiommu.o iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o +iommufd-$(CONFIG_IOMMU_LIVEUPDATE) += liveupdate.o obj-$(CONFIG_IOMMUFD) += iommufd.o obj-$(CONFIG_IOMMUFD_DRIVER) += iova_bitmap.o diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index eb2e85b27e42..a33b32708afa 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -44,6 +44,11 @@ struct iommufd_ctx { struct file *file; struct xarray objects; struct xarray groups; +#ifdef CONFIG_IOMMU_LIVEUPDATE +#define IOMMUFD_OBJ_LIVEUPDATE_MARK XA_MARK_1 + /* @liveupdate_mutex: Protects the preservation of HWPTs. */ + struct mutex liveupdate_mutex; +#endif wait_queue_head_t destroy_wait; struct rw_semaphore ioas_creation_lock; struct maple_tree mt_mmap; @@ -392,6 +397,9 @@ struct iommufd_hwpt_paging { bool auto_domain : 1; bool enforce_cache_coherency : 1; bool nest_parent : 1; +#ifdef CONFIG_IOMMU_LIVEUPDATE + u64 liveupdate_token; +#endif /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; struct iommufd_sw_msi_maps present_sw_msi; @@ -729,6 +737,15 @@ void iommufd_vdevice_abort(struct iommufd_object *obj); int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd); void iommufd_hw_queue_destroy(struct iommufd_object *obj); +#ifdef CONFIG_IOMMU_LIVEUPDATE +int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd); +#else +static inline int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) +{ + return -ENOTTY; +} +#endif + #ifdef CONFIG_IOMMUFD_TEST int iommufd_test(struct iommufd_ucmd *ucmd); void iommufd_selftest_destroy(struct iommufd_object *obj); diff --git a/drivers/iommu/iommufd/liveupdate.c b/drivers/iommu/iommufd/liveupdate.c new file mode 100644 index 000000000000..9d73917157cd --- /dev/null +++ b/drivers/iommu/iommufd/liveupdate.c @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright (C) 2026, Google LLC + * Author: Samiullah Khawaja + */ + +#define pr_fmt(fmt) "iommufd: " fmt + +#include +#include +#include + +#include "iommufd_private.h" + +int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_liveupdate_mark_preserve *cmd = ucmd->cmd; + struct iommufd_ctx *ictx = ucmd->ictx; + struct iommufd_hwpt_paging *hwpt; + struct iommufd_object *obj; + unsigned long index; + bool marked = false; + int rc = 0; + + if (cmd->flags || cmd->__reserved) + return -EOPNOTSUPP; + + hwpt = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id); + if (IS_ERR(hwpt)) + return PTR_ERR(hwpt); + + mutex_lock(&ictx->liveupdate_mutex); + + xa_lock(&ictx->objects); + + /* PRI use cases are not supported. */ + if (hwpt->common.fault) { + rc = -EOPNOTSUPP; + goto out_unlock; + } + + xa_for_each_marked(&ictx->objects, index, obj, IOMMUFD_OBJ_LIVEUPDATE_MARK) { + struct iommufd_hwpt_paging *hwpt_marked; + + if (WARN_ON_ONCE(obj->type != IOMMUFD_OBJ_HWPT_PAGING)) + continue; + + hwpt_marked = to_hwpt_paging(container_of(obj, struct iommufd_hw_pagetable, obj)); + + if (hwpt_marked == hwpt) + marked = true; + + if (hwpt_marked->liveupdate_token == cmd->hwpt_token) { + if (hwpt_marked == hwpt) + goto out_unlock; + + rc = -EADDRINUSE; + goto out_unlock; + } + } + + __xa_set_mark(&ictx->objects, hwpt->common.obj.id, IOMMUFD_OBJ_LIVEUPDATE_MARK); + + if (marked) + pr_warn_ratelimited("Overwriting HWPT liveupdate token from: %llu to %llu\n", + hwpt->liveupdate_token, cmd->hwpt_token); + hwpt->liveupdate_token = cmd->hwpt_token; + +out_unlock: + xa_unlock(&ictx->objects); + mutex_unlock(&ictx->liveupdate_mutex); + iommufd_put_object(ictx, &hwpt->common.obj); + return rc; +} diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 9a921b153162..fe8610fd58e3 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -333,6 +333,9 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) init_rwsem(&ictx->ioas_creation_lock); xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); xa_init(&ictx->groups); +#ifdef CONFIG_IOMMU_LIVEUPDATE + mutex_init(&ictx->liveupdate_mutex); +#endif ictx->file = filp; mt_init_flags(&ictx->mt_mmap, MT_FLAGS_ALLOC_RANGE); init_waitqueue_head(&ictx->destroy_wait); @@ -395,6 +398,9 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp) * iommufd_object_tombstone_user() */ xa_destroy(&ictx->objects); +#ifdef CONFIG_IOMMU_LIVEUPDATE + mutex_destroy(&ictx->liveupdate_mutex); +#endif WARN_ON(!xa_empty(&ictx->groups)); @@ -440,6 +446,7 @@ union ucmd_buffer { struct iommu_hwpt_alloc hwpt; struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap; struct iommu_hwpt_invalidate cache; + struct iommu_hwpt_liveupdate_mark_preserve mark_preserve; struct iommu_hwpt_set_dirty_tracking set_dirty_tracking; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; @@ -516,6 +523,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { __reserved), IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl, struct iommu_viommu_alloc, out_viommu_id), + IOCTL_OP(IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE, iommufd_hwpt_liveupdate_mark_preserve, + struct iommu_hwpt_liveupdate_mark_preserve, hwpt_token), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 010f47f7d3d2..6ab890e041c0 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -58,6 +58,7 @@ enum { IOMMUFD_CMD_VEVENTQ_ALLOC = 0x93, IOMMUFD_CMD_HW_QUEUE_ALLOC = 0x94, IOMMUFD_CMD_IOAS_NOIOMMU_GET_PA = 0x95, + IOMMUFD_CMD_HWPT_LIVEUPDATE_MARK_PRESERVE = 0x96, }; /** @@ -1398,4 +1399,34 @@ struct iommu_hw_queue_alloc { __aligned_u64 length; }; #define IOMMU_HW_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HW_QUEUE_ALLOC) + +/** + * struct iommu_hwpt_liveupdate_mark_preserve - ioctl(IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE) + * @size: sizeof(struct iommu_hwpt_liveupdate_mark_preserve) + * @flags: Must be 0 + * @hwpt_id: HW pagetable ID to preserve for liveupdate + * @__reserved: Must be 0 + * @hwpt_token: Token to identify this hwpt upon restore + * + * The target HWPT will be preserved during iommufd preservation. + * Only file-based memory mappings (e.g. memfd) are supported for HWPTs marked + * for preservation. Mapping anonymous memory into a preserved HWPT will result + * in a failure during the preservation phase. + * + * The hwpt_token is provided by userspace. If userspace enters a token + * already in use within this iommufd, -EADDRINUSE is returned from this ioctl. + * + * Note: There is no 'unmark' operation, so any HWPTs pooled in userspace that + * are marked for preservation must be destroyed after use. + */ +struct iommu_hwpt_liveupdate_mark_preserve { + __u32 size; + __u32 flags; + __u32 hwpt_id; + __u32 __reserved; + __aligned_u64 hwpt_token; +}; +#define IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE \ + _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_LIVEUPDATE_MARK_PRESERVE) + #endif -- 2.56.1.809.ga0b61035b5-goog