iommufd live update
v4 cover iommufd live update
Live update is a technique wherein an application saves its state, launches an updated version of itself, and restores its state. Clients of the application experience a brief suspension of service, on the order of 100's of milliseconds, but are otherwise unaffected.
Define the IOMMU_IOAS_CHANGE_PROCESS ioctl to allow management and use of an iommufd device to be transferred from one process to another. The application is responsible for transferring the device descriptor to the new process, eg either by preservation across fork and exec or via SCM_RIGHTS. It atomically updates everything to the current process, grabbing the new mm, and transferring pinned page counts.
IOMMU_IOAS_CHANGE_PROCESS only supports DMA mappings created with IOMMU_IOAS_MAP_FILE, because the kernel metadata for such mappings does not depend on the userland VA of the pages (which is different in the new process). IOMMU_IOAS_CHANGE_PROCESS fails if other types of mappings are present.
Thanks to Jason Gunthorpe for code and ideas in this series.
This is implemented in QEMU by the series:
[PATCH] Live update: iommufd (V2 to be posted)
Changes in V2:
* deleted interface to update VA
* add selftest cases
Changes in V3:
* replaced lockdep_off with down_write_nest_lock
* cosmetic changes as requested
* check for zero length in iommufd_ioas_map_file
Changes in V4:
* added TEST_LENGTH for iommu_ioas_change_process
* fixed context vs patch "Selftest coverage for IOMMU_IOAS_MAP_FILE"
* changed IOMMUFD_CMD_IOAS_CHANGE_PROCESS to 0x92
* added RB's
Steve Sistare (4):
iommufd: Export do_update_pinned
iommufd: Lock all objects
iommufd: Add IOMMU_IOAS_CHANGE_PROCESS
iommufd: IOMMU_IOAS_CHANGE_PROCESS selftest
drivers/iommu/iommufd/io_pagetable.h | 6 + drivers/iommu/iommufd/ioas.c | 207 ++++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 2 + drivers/iommu/iommufd/main.c | 3 + drivers/iommu/iommufd/pages.c | 10 +- include/uapi/linux/iommufd.h | 23 +++ tools/testing/selftests/iommu/Makefile | 1 + tools/testing/selftests/iommu/iommufd.c | 141 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 6 + 9 files changed, 394 insertions(+), 5 deletions(-)
base-commit: c9d6ee6699fd6626b0974143a72c3406276e1c65
Signature · 1 line
1.8.3.1
PATCH v4 1/4 iommufd: Export do_update_pinned
Export do_update_pinned. No functional change.
--- drivers/iommu/iommufd/io_pagetable.h | 5 +++++ drivers/iommu/iommufd/pages.c | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-)
drivers/iommu/iommufd/io_pagetable.h +5 -0
@@ -252,4 +252,9 @@ struct iopt_pages_access { unsigned int users; }; +struct pfn_reader_user ; + +int iopt_update_pinned (struct iopt_pages *pages,unsigned long npages,bool inc, +struct pfn_reader_user *user); +#endif
drivers/iommu/iommufd/pages.c +5 -5
@@ -985,8 +985,8 @@ static int update_mm_locked_vm(struct iopt_pages *pages, unsigned long npages,return rc; } -static int do_update_pinned (struct iopt_pages *pages,unsigned long npages, -bool inc,struct pfn_reader_user *user) +int iopt_update_pinned (struct iopt_pages *pages,unsigned long npages,bool inc, +struct pfn_reader_user *user) {int rc =0 ;
@@ -1020,8 +1020,8 @@ static void update_unpinned(struct iopt_pages *pages)return ;if (pages->npinned == pages->last_npinned)return ; -do_update_pinned (pages, pages->last_npinned - pages->npinned, false, -NULL ); +iopt_update_pinned (pages, pages->last_npinned - pages->npinned, false, +NULL ); }/*
@@ -1051,7 +1051,7 @@ static int pfn_reader_user_update_pinned(struct pfn_reader_user *user, npages = pages->npinned - pages->last_npinned; inc = true; } -return do_update_pinned (pages, npages, inc, user); +return iopt_update_pinned (pages, npages, inc, user); }/*
Signature · 1 line
1.8.3.1
RE: [PATCH V4 1/4] iommufd: Export do_update_pinned
drivers/iommu/iommufd/pages.c static int update_mm_locked_vm(struct iopt_pages · line 991
Quoted 13 lines
From: Steve Sistare <[email protected]>
Sent: Friday, November 8, 2024 5:47 AMdrivers/iommu/iommufd/pages.c +2
-2@@ -985,8 +985,8 @@ static int update_mm_locked_vm(struct iopt_pages *pages, unsigned long npages, return rc; } -static int do_update_pinned(struct iopt_pages *pages, unsigned long npages, - bool inc, struct pfn_reader_user *user) +int iopt_update_pinned (struct iopt_pages *pages,unsigned long npages,bool inc, +struct pfn_reader_user *user)
iopt_pages_update_pinned()?
PATCH v4 2/4 iommufd: Lock all objects
In reply to Steve SistareDefine helpers to lock and unlock all iommufd_ctx objects. This will allow DMA mappings to be updated atomically during live update. This code is substantially the same as an initial version provided by Jason, plus fixes.
--- drivers/iommu/iommufd/ioas.c | 65 +++++++++++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 1 + 3 files changed, 67 insertions(+)
drivers/iommu/iommufd/ioas.c +65 -0
@@ -52,7 +52,10 @@ int iommufd_ioas_alloc_ioctl(struct iommufd_ucmd *ucmd) rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd));if (rc) goto out_table; + +down_read (&ucmd->ictx->ioas_creation_lock);iommufd_object_finalize (ucmd->ictx, &ioas->obj); +up_read (&ucmd->ictx->ioas_creation_lock);return 0 ; out_table:
@@ -374,6 +377,68 @@ int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd)return rc; } +static void iommufd_release_all_iova_rwsem (struct iommufd_ctx *ictx, +struct xarray *ioas_list) +{ +struct iommufd_ioas *ioas; +unsigned long index; + +xa_for_each (ioas_list, index, ioas) { +up_write (&ioas->iopt.iova_rwsem); +refcount_dec (&ioas->obj.users); + } +up_write (&ictx->ioas_creation_lock); +xa_destroy (ioas_list); +} + +static int iommufd_take_all_iova_rwsem (struct iommufd_ctx *ictx, +struct xarray *ioas_list) +{ +struct iommufd_object *obj; +unsigned long index; +int rc; + +/* +* This is very ugly, it is done instead of adding a lock around +* pages->source_mm, which is a performance path for mdev, we just +* obtain the write side of all the iova_rwsems which also protects the +* pages->source_*. Due to copies we can't know which IOAS could read +* from the pages, so we just lock everything. This is the only place +* locks are nested and they are uniformly taken in ID order. +* +* ioas_creation_lock prevents new IOAS from being installed in the +* xarray while we do this, and also prevents more than one thread from +* holding nested locks. +*/ +down_write (&ictx->ioas_creation_lock); +xa_lock (&ictx->objects); +xa_for_each (&ictx->objects, index, obj) { +struct iommufd_ioas *ioas; + +if (!obj || obj->type !=IOMMUFD_OBJ_IOAS ) +continue ; + +if (!refcount_inc_not_zero (&obj->users)) +continue ; + +xa_unlock (&ictx->objects); + + ioas = container_of(obj, struct iommufd_ioas, obj); +down_write_nest_lock (&ioas->iopt.iova_rwsem, + &ictx->ioas_creation_lock); + + rc =xa_err (xa_store (ioas_list, index, ioas,GFP_KERNEL )); +if (rc) { +iommufd_release_all_iova_rwsem (ictx, ioas_list); +return rc; + } + +xa_lock (&ictx->objects); + } +xa_unlock (&ictx->objects); +return 0 ; +} +int iommufd_option_rlimit_mode (struct iommu_option *cmd,struct iommufd_ctx *ictx) {
drivers/iommu/iommufd/iommufd_private.h +1 -0
@@ -24,6 +24,7 @@ struct iommufd_ctx {struct xarray objects;struct xarray groups;wait_queue_head_t destroy_wait; +struct rw_semaphore ioas_creation_lock;u8 account_mode;/* Compatibility with VFIO no iommu */
drivers/iommu/iommufd/main.c +1 -0
@@ -248,6 +248,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp)pr_info_once ("IOMMUFD is providing /dev/vfio/vfio, not VFIO.\n" ); } +init_rwsem (&ictx->ioas_creation_lock);xa_init_flags (&ictx->objects,XA_FLAGS_ALLOC1 |XA_FLAGS_ACCOUNT );xa_init (&ictx->groups); ictx->file = filp;
Signature · 1 line
1.8.3.1
RE: [PATCH V4 2/4] iommufd: Lock all objects
Quoted 7 lines
From: Steve Sistare <[email protected]>
Sent: Friday, November 8, 2024 5:47 AMDefine helpers to lock and unlock all
iommufd_ctxobjects. This will allow DMA mappings to be updated atomically during live update. This code is substantially the same as an initial version provided by Jason, plus fixes.
the subject and above words talk about 'all iommufd_ctx objects' but this patch actually tries to lock 'all IOAS objects'
Quoted 4 lines
Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Steve Sistare <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>
PATCH v4 3/4 iommufd: Add IOMMU_IOAS_CHANGE_PROCESS
In reply to Steve SistareAdd an ioctl that updates all DMA mappings to reflect the current process, Change the mm and transfer locked memory accounting from old to current mm. This will be used for live update, allowing an old process to hand the iommufd device descriptor to a new process. The new process calls the ioctl.
IOMMU_IOAS_CHANGE_PROCESS only supports DMA mappings created with IOMMU_IOAS_MAP_FILE, because the kernel metadata for such mappings does not depend on the userland VA of the pages (which is different in the new process).IOMMU_IOAS_CHANGE_PROCESS fails if other types of mappings are present.
This is a revised version of code originally provided by Jason.
--- drivers/iommu/iommufd/io_pagetable.h | 1 + drivers/iommu/iommufd/ioas.c | 142 ++++++++++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 2 + include/uapi/linux/iommufd.h | 23 ++++++ 5 files changed, 169 insertions(+)
drivers/iommu/iommufd/io_pagetable.h +1 -0
@@ -173,6 +173,7 @@ enum {IOPT_PAGES_ACCOUNT_NONE =0 ,IOPT_PAGES_ACCOUNT_USER =1 ,IOPT_PAGES_ACCOUNT_MM =2 , + IOPT_PAGES_ACCOUNT_MODE_NUM = 3, }; enum iopt_address_type {
drivers/iommu/iommufd/ioas.c +142 -0
148 lines not on this page · raw message
drivers/iommu/iommufd/iommufd_private.h +1 -0
@@ -282,6 +282,7 @@ static inline struct iommufd_ioas *iommufd_get_ioas(struct iommufd_ctx *ictx,int iommufd_ioas_allow_iovas (struct iommufd_ucmd *ucmd);int iommufd_ioas_map (struct iommufd_ucmd *ucmd);int iommufd_ioas_map_file (struct iommufd_ucmd *ucmd); +int iommufd_ioas_change_process (struct iommufd_ucmd *ucmd);int iommufd_ioas_copy (struct iommufd_ucmd *ucmd);int iommufd_ioas_unmap (struct iommufd_ucmd *ucmd);int iommufd_ioas_option (struct iommufd_ucmd *ucmd);
drivers/iommu/iommufd/main.c +2 -0
@@ -373,6 +373,8 @@ struct iommufd_ioctl_op { struct iommu_ioas_alloc, out_ioas_id),IOCTL_OP (IOMMU_IOAS_ALLOW_IOVAS ,iommufd_ioas_allow_iovas ,struct iommu_ioas_allow_iovas ,allowed_iovas ), +IOCTL_OP (IOMMU_IOAS_CHANGE_PROCESS ,iommufd_ioas_change_process , +struct iommu_ioas_change_process ,__reserved ),IOCTL_OP (IOMMU_IOAS_COPY ,iommufd_ioas_copy ,struct iommu_ioas_copy , src_iova), IOCTL_OP(IOMMU_IOAS_IOVA_RANGES, iommufd_ioas_iova_ranges,
include/uapi/linux/iommufd.h +23 -0
@@ -52,6 +52,7 @@ enum {IOMMUFD_CMD_HWPT_INVALIDATE =0x8d ,IOMMUFD_CMD_FAULT_QUEUE_ALLOC =0x8e ,IOMMUFD_CMD_IOAS_MAP_FILE =0x8f , + IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92, };/**
@@ -822,4 +823,26 @@ struct iommu_fault_alloc { __u32 out_fault_fd; };#define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_ALLOC) + +/** +* struct iommu_ioas_change_process - ioctl(VFIO_IOAS_CHANGE_PROCESS) +* @size: sizeof(struct iommu_ioas_change_process) +* @__reserved: Must be 0 +* +* This transfers pinned memory counts for every memory map in every IOAS +* in the context to the current process. This only supports maps created +* with IOMMU_IOAS_MAP_FILE, and returns EINVAL if other maps are present. +* If the ioctl returns a failure status, then nothing is changed. +* +* This API is useful for transferring operation of a device from one process +* to another, such as during userland live update. +*/ +struct iommu_ioas_change_process { +__u32 size; +__u32 __reserved; +}; + +#define IOMMU_IOAS_CHANGE_PROCESS \ + _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_CHANGE_PROCESS) +#endif
Signature · 1 line
1.8.3.1
RE: [PATCH V4 3/4] iommufd: Add IOMMU_IOAS_CHANGE_PROCESS
Quoted 19 lines
From: Steve Sistare <[email protected]>
Sent: Friday, November 8, 2024 5:47 AMdrivers/iommu/iommufd/ioas.c +16
-0+int iommufd_ioas_change_process (struct iommufd_ucmd *ucmd) +{ +struct iommu_ioas_change_process *cmd = ucmd->cmd; +struct iommufd_ctx *ictx = ucmd->ictx; +unsigned long all_npinned[IOPT_PAGES_ACCOUNT_MODE_NUM ] = {}; +struct iommufd_ioas *ioas; +struct iopt_area *area; +struct iopt_pages *pages; +struct xarray ioas_list; +unsigned long index; +int rc; + +if (cmd->__reserved) +return -EOPNOTSUPP ; + +xa_init (&ioas_list);
drivers/iommu/iommufd/ioas.c static int iommufd_take_all_iova_rwsem(struct iommufd_ctx *ictx, · line 543
Quoted 10 lines
drivers/iommu/iommufd/ioas.c +10
-0+ rc =iommufd_take_all_iova_rwsem (ictx, &ioas_list); +if (rc) +return rc; + +for_each_ioas_area (&ioas_list, index, ioas, area) { +if (area->pages->type !=IOPT_ADDRESS_FILE ) { + rc = -EINVAL ; + goto out; + } + }
this and next loop could be merged into one. It's not a bad idea to add cost to a wrong operation while making it slightly more efficient in a normal path.
Quoted 33 lines
drivers/iommu/iommufd/ioas.c +12
-0+ +/* +* Count last_pinned pages, then clear it to avoid double counting +* if the same iopt_pages is visited multiple times in this loop. +* Since we are under all the locks, npinned == last_npinned, so we +* can easily restore last_npinned before we return. +*/ +for_each_ioas_area (&ioas_list, index, ioas, area) { + pages = area->pages; + +if (need_charge_update (pages)) { + all_npinned[pages->account_mode] += pages-Quoted 1 line
last_npinned;drivers/iommu/iommufd/ioas.c +17
-0+ pages->last_npinned =0 ; + } + } + + rc =charge_current (all_npinned); + +if (rc) { +/* Charge failed. Fix last_npinned and bail. */ +for_each_ioas_area (&ioas_list, index, ioas, area) + area->pages->last_npinned = area->pages->npinned; + goto out; + } + +for_each_ioas_area (&ioas_list, index, ioas, area) { + pages = area->pages; + +/* Uncharge the old one (which also restores last_npinned) */
(quoted diff) +2
-0+ if (need_charge_update(pages)) + iopt_update_pinned(pages, pages->npinned, false,
Quoted 1 line
NULL);
suppose this should not fail. warn if it happens?
Jason Gunthorpe
maintainerRe: [PATCH V4 3/4] iommufd: Add IOMMU_IOAS_CHANGE_PROCESS
Quoted 25 lines · Kevin
Quoted 25 lines
From: Steve Sistare <[email protected]>
Sent: Friday, November 8, 2024 5:47 AMdrivers/iommu/iommufd/ioas.c +22
-0+int iommufd_ioas_change_process (struct iommufd_ucmd *ucmd) +{ +struct iommu_ioas_change_process *cmd = ucmd->cmd; +struct iommufd_ctx *ictx = ucmd->ictx; +unsigned long all_npinned[IOPT_PAGES_ACCOUNT_MODE_NUM ] = {}; +struct iommufd_ioas *ioas; +struct iopt_area *area; +struct iopt_pages *pages; +struct xarray ioas_list; +unsigned long index; +int rc; + +if (cmd->__reserved) +return -EOPNOTSUPP ; + +xa_init (&ioas_list); + rc =iommufd_take_all_iova_rwsem (ictx, &ioas_list); +if (rc) +return rc; + +for_each_ioas_area (&ioas_list, index, ioas, area) { +if (area->pages->type !=IOPT_ADDRESS_FILE ) {
Quoted 8 lines
drivers/iommu/iommufd/ioas.c static int iommufd_take_all_iova_rwsem(struct iommufd_ctx *ictx, · line 543
Quoted 4 lines
drivers/iommu/iommufd/ioas.c +4
-0+ rc = -EINVAL ; + goto out; + } + }this and next loop could be merged into one. It's not a bad idea to add cost to a wrong operation while making it slightly more efficient in a normal path.
The next loop messes up pages->last_npinned, so if you merge them then you also have to do a partial restoration of last_npinned and when I wrote this I thought that restoration was complex enough to be not worth the optimization..
Jason
PATCH v4 4/4 iommufd: IOMMU_IOAS_CHANGE_PROCESS selftest
In reply to Steve SistareAdd selftest cases for IOMMU_IOAS_CHANGE_PROCESS.
--- tools/testing/selftests/iommu/Makefile | 1 + tools/testing/selftests/iommu/iommufd.c | 141 ++++++++++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 6 ++ 3 files changed, 148 insertions(+)
tools/testing/selftests/iommu/Makefile +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only CFLAGS += -Wall -O2 -Wno-unused-function CFLAGS += $(KHDR_INCLUDES) +CFLAGS += -lcap TEST_GEN_PROGS := TEST_GEN_PROGS += iommufd
tools/testing/selftests/iommu/iommufd.c +141 -0
159 lines not on this page · raw message
tools/testing/selftests/iommu/iommufd_utils.h +6 -0
@@ -23,6 +23,12 @@#define BIT_MASK (nr) (1UL << ((nr) % __BITS_PER_LONG))#define BIT_WORD (nr) ((nr) / __BITS_PER_LONG) +enum { +IOPT_PAGES_ACCOUNT_NONE =0 , +IOPT_PAGES_ACCOUNT_USER =1 , +IOPT_PAGES_ACCOUNT_MM =2 , +}; +#define DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d))static inline void set_bit (unsigned int nr,unsigned long *addr)
Signature · 1 line
1.8.3.1