|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch
From: Haseeb Ashraf <haseeb.ashraf@xxxxxxxxxxx> Add a batched hypercall to remove pages from a domain's physical address space, like XENMEM_add_to_physmap_batch does for adding. Each entry gets the result of XENMEM_remove_from_physmap. The p2m helper invalidates contiguous runs once per chunk of 256 entries, using the TLBI method supported by the CPU and range size. Stage guest handles into per-hypercall buffers because they cannot be accessed while holding the p2m lock and the target may be the caller. Take a reference on each page before removal so it remains allocated until TLB invalidation completes. Copy each chunk's results back, and allow the hypercall to be preempted between chunks. The staging buffers are allocated and not placed on the stack: 256 entries need 28 bytes each (7 KiB), a large part of the 32 KiB Arm stack. Each buffer stays within PAGE_SIZE, so xvmalloc_array() uses the xmalloc pool. A vmalloc area would be unmapped when it is freed, which flushes Xen's own TLB. The chunk also bounds the p2m lock hold time and the invalidation work between preemption points. This is a resource bound, not a measured throughput threshold. Only architectures that select HAS_PHYSMAP_REMOVE_BATCH implement this hypercall; other architectures return -ENOSYS and callers can fall back to XENMEM_remove_from_physmap. The XSM check is the same as for the single-page operation. No in-tree caller is added; the hypercall is meant for a Dom0 kernel or toolstack that unmaps many guest pages. Signed-off-by: Haseeb Ashraf <haseeb.ashraf@xxxxxxxxxxx> --- New in v5. CHANGELOG.md | 5 ++ xen/common/memory.c | 152 ++++++++++++++++++++++++++++++++++++ xen/include/public/memory.h | 35 +++++++++ 3 files changed, 192 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 225397f128..39b3d16928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) afterwards. ### Added + - On Arm: + - XENMEM_remove_from_physmap_batch, which removes several pages from the + physical address space of a domain with one hypercall and a TLB + invalidation per run of pages, instead of one hypercall and one + invalidation per page. Other architectures return -ENOSYS for it. ### Removed - On Arm: diff --git a/xen/common/memory.c b/xen/common/memory.c index 4afa2271b7..b5256bf6ec 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1147,6 +1147,117 @@ static int xenmem_add_to_physmap_batch(struct domain *d, return 0; } +#ifdef CONFIG_HAS_PHYSMAP_REMOVE_BATCH +/* + * Number of entries removed under one p2m write lock and TLB invalidation. + * Entries are staged in per hypercall buffers because the guest handles + * cannot be accessed with the p2m lock held: the target may be the caller. + * The buffers stay below PAGE_SIZE so that they come from the xmalloc pool, + * a vmalloc area is unmapped, and flushed from the TLBs, when it is freed. + */ +#define XENMEM_RFPB_CHUNK 256U + +static int xenmem_remove_from_physmap_batch( + struct domain *d, struct xen_remove_from_physmap_batch *xrfpb, + unsigned int extent) +{ + struct page_info **pages = NULL; + xen_pfn_t *gpfns = NULL; + mfn_t *mfns = NULL; + int *errs = NULL; + unsigned int chunk, i; + int rc = 0; + + BUILD_BUG_ON(XENMEM_RFPB_CHUNK * sizeof(*pages) > PAGE_SIZE); + BUILD_BUG_ON(XENMEM_RFPB_CHUNK * sizeof(*gpfns) > PAGE_SIZE); + BUILD_BUG_ON(XENMEM_RFPB_CHUNK * sizeof(*mfns) > PAGE_SIZE); + + if ( unlikely(xrfpb->size < extent) ) + return -EILSEQ; + + if ( unlikely(xrfpb->size == extent) ) + return extent ? -EILSEQ : 0; + + if ( !guest_handle_subrange_okay(xrfpb->gpfns, extent, xrfpb->size - 1) || + !guest_handle_subrange_okay(xrfpb->errs, extent, xrfpb->size - 1) ) + return -EFAULT; + + chunk = min_t(unsigned int, xrfpb->size - extent, XENMEM_RFPB_CHUNK); + + pages = xvmalloc_array(struct page_info *, chunk); + gpfns = xvmalloc_array(xen_pfn_t, chunk); + mfns = xvmalloc_array(mfn_t, chunk); + errs = xvmalloc_array(int, chunk); + if ( !pages || !gpfns || !mfns || !errs ) + { + rc = -ENOMEM; + goto out; + } + + while ( xrfpb->size > extent ) + { + unsigned int n = min_t(unsigned int, xrfpb->size - extent, chunk); + + if ( __copy_from_guest_offset(gpfns, xrfpb->gpfns, extent, n) ) + { + rc = -EFAULT; + goto out; + } + + /* + * Take a reference on each page first: this needs the p2m read + * lock, which the removal below holds for writing. The references + * also keep the pages from being freed before the TLBs are flushed. + * A frame without a page is reported as XENMEM_remove_from_physmap + * does. + */ + for ( i = 0; i < n; i++ ) + { + pages[i] = get_page_from_gfn(d, gpfns[i], NULL, P2M_ALLOC); + if ( pages[i] != NULL ) + { + mfns[i] = page_to_mfn(pages[i]); + errs[i] = 0; + } + else + { + mfns[i] = INVALID_MFN; + errs[i] = -ENOENT; + } + } + + guest_physmap_remove_page_batch(d, gpfns, mfns, errs, n); + + for ( i = 0; i < n; i++ ) + if ( pages[i] != NULL ) + put_page(pages[i]); + + if ( __copy_to_guest_offset(xrfpb->errs, extent, errs, n) ) + { + rc = -EFAULT; + goto out; + } + + extent += n; + + /* Ask for a continuation if there is more work to do. */ + if ( xrfpb->size > extent && hypercall_preempt_check() ) + { + rc = extent; + goto out; + } + } + + out: + XVFREE(errs); + XVFREE(mfns); + XVFREE(gpfns); + XVFREE(pages); + + return rc; +} +#endif /* CONFIG_HAS_PHYSMAP_REMOVE_BATCH */ + static int construct_memop_from_reservation( const struct xen_memory_reservation *r, struct memop_args *a) @@ -1787,6 +1898,47 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) break; } +#ifdef CONFIG_HAS_PHYSMAP_REMOVE_BATCH + case XENMEM_remove_from_physmap_batch: + { + struct xen_remove_from_physmap_batch xrfpb; + + BUILD_BUG_ON((typeof(xrfpb.size))-1 > + (UINT_MAX >> MEMOP_EXTENT_SHIFT)); + + /* Check for malicious or buggy input. */ + if ( start_extent != (typeof(xrfpb.size))start_extent ) + return -EDOM; + + if ( copy_from_guest(&xrfpb, arg, 1) ) + return -EFAULT; + + d = rcu_lock_domain_by_any_id(xrfpb.domid); + if ( d == NULL ) + return -ESRCH; + + rc = paging_mode_translate(d) + ? xsm_remove_from_physmap(XSM_TARGET, curr_d, d) + : -EACCES; + if ( rc != 0 ) + { + rcu_unlock_domain(d); + return rc; + } + + rc = xenmem_remove_from_physmap_batch(d, &xrfpb, start_extent); + + rcu_unlock_domain(d); + + if ( rc > 0 ) + rc = hypercall_create_continuation( + __HYPERVISOR_memory_op, "lh", + op | (rc << MEMOP_EXTENT_SHIFT), arg); + + return rc; + } +#endif /* CONFIG_HAS_PHYSMAP_REMOVE_BATCH */ + case XENMEM_access_op: rc = mem_access_memop(cmd, guest_handle_cast(arg, xen_mem_access_op_t)); break; diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index bd9fc37b52..4de16cd59c 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -302,6 +302,41 @@ struct xen_remove_from_physmap { typedef struct xen_remove_from_physmap xen_remove_from_physmap_t; DEFINE_XEN_GUEST_HANDLE(xen_remove_from_physmap_t); +/* + * A batched version of XENMEM_remove_from_physmap: remove several pages from + * the guest physical address space of a domain with a single hypercall. + * Each entry gets the result XENMEM_remove_from_physmap would have returned. + * The TLBs are invalidated once per run of consecutive pages in a chunk of + * entries the hypervisor processes at a time, so this is much cheaper than + * one XENMEM_remove_from_physmap per page, especially for a contiguous range. + * + * The return value is 0 if the list was processed, and then errs[] holds the + * per entry results, or a negative error. The hypercall returns -ENOSYS where + * it is not implemented, callers must fall back to XENMEM_remove_from_physmap. + * Processing is not transactional: a negative return may follow completed + * removals, including a chunk whose result copy failed. + * + * arg == addr of xen_remove_from_physmap_batch_t. + */ +#define XENMEM_remove_from_physmap_batch 29 +struct xen_remove_from_physmap_batch { + /* IN */ + /* Which domain to change the mapping for. */ + domid_t domid; + + /* Number of pages to unmap. */ + uint16_t size; + + /* GPFNs of the current mappings of the pages. */ + XEN_GUEST_HANDLE(xen_pfn_t) gpfns; + + /* OUT */ + /* Per entry error code, 0 on success. */ + XEN_GUEST_HANDLE(int) errs; +}; +typedef struct xen_remove_from_physmap_batch xen_remove_from_physmap_batch_t; +DEFINE_XEN_GUEST_HANDLE(xen_remove_from_physmap_batch_t); + /*** REMOVED ***/ /*#define XENMEM_translate_gpfn_list 8*/
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |