[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


  • To: Haseeb Ashraf <haseebashraf091@xxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Tue, 1 Dec 2026 16:59:19 +0100
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=google header.d=suse.com header.i="@suse.com" header.h="Content-Transfer-Encoding:Content-Type:In-Reply-To:Autocrypt:From:Content-Language:References:Cc:To:Subject:User-Agent:MIME-Version:Date:Message-ID"
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Haseeb Ashraf <haseeb.ashraf@xxxxxxxxxxx>, oleksii.kurochko@xxxxxxxxx, community.manager@xxxxxxxxxxxxxx, andrew.cooper3@xxxxxxxxxx, anthony.perard@xxxxxxxxxx, michal.orzel@xxxxxxx, julien@xxxxxxx, roger@xxxxxxxxxxxxxx, sstabellini@xxxxxxxxxx, dpsmith@xxxxxxxxxxxxxxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Tue, 01 Dec 2026 15:59:33 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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*/
>  




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.