|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [XEN][PATCH v5 7/8] xen/memory: add XENMEM_remove_from_physmap_batch
On 22.11.2026 10:54, Haseeb Ashraf wrote:
> 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.
Just that -ENOSYS isn't quite right then. It would better be -EOPNOTSUPP.
> --- 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,
As I can't spot any write through xrfpb - pointer-to-const?
> + 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.
You touch on locking, yet then the code still doesn't look quite right:
Without continuously holding the lock (at least for reading), how do
you ensure ...
> 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);
... this operates on what you did obtain references for? Other changes
could have occurred while e.g. the loop above is still running. Or is
guest_physmap_remove_page_batch() supposed to be checking that? Yet
even then this doesn't feel quite right: With multiple operations
having gone on in between, such a check may succeed but you may still
not remove exactly what the caller intended.
> + for ( i = 0; i < n; i++ )
> + if ( pages[i] != NULL )
Nit (also further up): We prefer to omit such " != 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);
All four variables are about to go out of scope, hence the simpler
xvfree() will do here-
> @@ -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;
> + }
To avoid the separate return and cleanup, maybe better ...
> + rc = xenmem_remove_from_physmap_batch(d, &xrfpb, start_extent);
if ( !rc )
rc = xenmem_remove_from_physmap_batch(d, &xrfpb, start_extent);
? (Possibly with ASSERT(rc < 0) in an else.)
> + 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;
> --- 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.
How does contiguous or not matter here?
Jan
> + * 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 |