All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jan Beulich" <JBeulich@suse.com>
To: Alexandru Isaila <aisaila@bitdefender.com>, andrew.cooper3@citrix.com
Cc: kevin.tian@intel.com, sstabellini@kernel.org,
	wei.liu2@citrix.com, jun.nakajima@intel.com,
	"Razvan Cojocaru" <rcojocaru@bitdefender.com>,
	george.dunlap@eu.citrix.com, tim@xen.org,
	"Mihai Donțu" <mdontu@bitdefender.com>,
	ian.jackson@eu.citrix.com, xen-devel@lists.xen.org,
	paul.durrant@citrix.com, suravee.suthikulpanit@amd.com,
	boris.ostrovsky@oracle.com
Subject: Re: [PATCH v2 4/4] x86/hvm: Implement hvmemul_write() using real mappings
Date: Mon, 18 Sep 2017 07:43:41 -0600	[thread overview]
Message-ID: <59BFE9AD020000780017C4D8@prv-mh.provo.novell.com> (raw)
In-Reply-To: <1504886736-1823-5-git-send-email-aisaila@bitdefender.com>

>>> On 08.09.17 at 18:05, <aisaila@bitdefender.com> wrote:
> Changes since V1:
> 	- Moved ASSERT to the begining of the loop
> 	- Corrected the decrement on mfn int the while statement
> 	- Modified the comment to PAGE_SIZE+1

While several of my v1 comments were taken care of verbally, some
haven't been addressed here or during the discussion.

> While the maximum size of linear mapping is capped at 1 page, the logic
> in the helpers is written to work properly as hvmemul_ctxt->mfn[] gets 
> longer,
> specifically with XSAVE instruction emulation in mind.
> 
> This has only had light testing so far.

Has this changed in the meantime?

> +static void *hvmemul_map_linear_addr(
> +    unsigned long linear, unsigned int bytes, uint32_t pfec,
> +    struct hvm_emulate_ctxt *hvmemul_ctxt)
> +{
> +    struct vcpu *curr = current;
> +    void *err, *mapping;
> +
> +    /* First and final gfns which need mapping. */
> +    unsigned long frame = linear >> PAGE_SHIFT, first = frame;
> +    unsigned long final = (linear + bytes - !!bytes) >> PAGE_SHIFT;
> +
> +    /*
> +     * mfn points to the next free slot.  All used slots have a page reference
> +     * held on them.
> +     */
> +    mfn_t *mfn = &hvmemul_ctxt->mfn[0];
> +
> +    /*
> +     * The caller has no legitimate reason for trying a zero-byte write, but
> +     * final is calculate to fail safe in release builds.
> +     *
> +     * The maximum write size depends on the number of adjacent mfns[] which
> +     * can be vmap()'d, accouting for possible misalignment within the region.
> +     * The higher level emulation callers are responsible for ensuring that
> +     * mfns[] is large enough for the requested write size.
> +     */
> +    if ( bytes == 0 ||
> +         final - first > ARRAY_SIZE(hvmemul_ctxt->mfn) - 1 )
> +    {
> +        ASSERT_UNREACHABLE();
> +        goto unhandleable;
> +    }
> +
> +    do {
> +        enum hvm_translation_result res;
> +        struct page_info *page;
> +        pagefault_info_t pfinfo;
> +        p2m_type_t p2mt;
> +
> +        /* Error checking.  Confirm that the current slot is clean. */
> +        ASSERT(mfn_x(*mfn) == 0);
> +
> +        res = hvm_translate_get_page(curr, frame << PAGE_SHIFT, true, pfec,
> +                                     &pfinfo, &page, NULL, &p2mt);
> +
> +        switch ( res )
> +        {
> +        case HVMTRANS_okay:
> +            break;
> +
> +        case HVMTRANS_bad_linear_to_gfn:
> +            x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
> +            err = ERR_PTR(~(long)X86EMUL_EXCEPTION);

Why the casts to long here and further down?

> +            goto out;
> +
> +        case HVMTRANS_bad_gfn_to_mfn:
> +            err = NULL;
> +            goto out;
> +
> +        case HVMTRANS_gfn_paged_out:
> +        case HVMTRANS_gfn_shared:
> +            err = ERR_PTR(~(long)X86EMUL_RETRY);
> +            goto out;
> +
> +        default:
> +            goto unhandleable;
> +        }
> +
> +        *mfn++ = _mfn(page_to_mfn(page));
> +        frame++;
> +
> +        if ( p2m_is_discard_write(p2mt) )
> +        {
> +            err = ERR_PTR(~(long)X86EMUL_OKAY);
> +            goto out;
> +        }
> +
> +    } while ( frame < final );
> +
> +    /* Entire access within a single frame? */
> +    if ( first == final )
> +        mapping = map_domain_page(hvmemul_ctxt->mfn[0]) + (linear & ~PAGE_MASK);
> +    /* Multiple frames? Need to vmap(). */
> +    else if ( (mapping = vmap(hvmemul_ctxt->mfn,
> +                              mfn - hvmemul_ctxt->mfn)) == NULL )

v1 comment was "final - first + 1 would likely yield better code."

> +        goto unhandleable;
> +
> +#ifndef NDEBUG /* Poision unused mfn[]s with INVALID_MFN. */
> +    while ( mfn < hvmemul_ctxt->mfn + ARRAY_SIZE(hvmemul_ctxt->mfn) )
> +    {
> +        ASSERT(mfn_x(*mfn) == 0);
> +        *mfn++ = INVALID_MFN;
> +    }
> +#endif
> +
> +    return mapping;
> +
> + unhandleable:
> +    err = ERR_PTR(~(long)X86EMUL_UNHANDLEABLE);
> +
> + out:
> +    /* Drop all held references. */
> +    while ( mfn-- > hvmemul_ctxt->mfn )
> +        put_page(mfn_to_page(mfn_x(*mfn)));
> +
> +    return err;
> +}
> +
> +static void hvmemul_unmap_linear_addr(
> +    void *mapping, unsigned long linear, unsigned int bytes,

While this was discussed in response to v1, I still think "mapping"
should be const void *, or a prereq patch (which I would object
to) should be submitted to drop the const from vunmap() and
unmap_domain_page().

> @@ -1007,23 +1160,15 @@ static int hvmemul_write(
>           (vio->mmio_gla == (addr & PAGE_MASK)) )
>          return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 1);
>  
> -    rc = hvm_copy_to_guest_linear(addr, p_data, bytes, pfec, &pfinfo);
> -
> -    switch ( rc )
> -    {
> -    case HVMTRANS_okay:
> -        break;
> -    case HVMTRANS_bad_linear_to_gfn:
> -        x86_emul_pagefault(pfinfo.ec, pfinfo.linear, &hvmemul_ctxt->ctxt);
> -        return X86EMUL_EXCEPTION;
> -    case HVMTRANS_bad_gfn_to_mfn:
> +    mapping = hvmemul_map_linear_addr(addr, bytes, pfec, hvmemul_ctxt);
> +    if ( IS_ERR(mapping) )
> +        return ~PTR_ERR(mapping);
> +    else if ( !mapping )

v1 comment: 'Pointless "else".'

Jan

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  parent reply	other threads:[~2017-09-18 13:43 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-08 16:05 [PATCH v2 0/4] Various XSA followups Alexandru Isaila
2017-09-08 16:05 ` [PATCH v2 1/4] x86/shadow: Use ERR_PTR infrastructure for sh_emulate_map_dest() Alexandru Isaila
2017-09-12  9:37   ` Wei Liu
2017-09-08 16:05 ` [PATCH v2 2/4] x86/hvm: Rename enum hvm_copy_result to hvm_translation_result Alexandru Isaila
2017-09-11 13:27   ` George Dunlap
2017-09-11 13:32     ` Wei Liu
2017-09-11 13:39     ` Andrew Cooper
2017-09-12 10:00       ` George Dunlap
2017-09-18  8:22   ` Tian, Kevin
2017-09-18 13:00   ` Jan Beulich
2017-09-08 16:05 ` [PATCH v2 3/4] x86/hvm: Break out __hvm_copy()'s translation logic Alexandru Isaila
2017-09-18 13:18   ` Jan Beulich
2017-09-08 16:05 ` [PATCH v2 4/4] x86/hvm: Implement hvmemul_write() using real mappings Alexandru Isaila
2017-09-12 14:32   ` Paul Durrant
2017-09-12 14:37     ` Andrew Cooper
2017-09-12 15:05       ` Jan Beulich
2017-09-12 15:06       ` Paul Durrant
2017-09-12 15:09   ` Andrew Cooper
2017-09-12 15:12     ` Paul Durrant
2017-09-18 13:43   ` Jan Beulich [this message]
2017-09-18 14:16     ` Alexandru Stefan ISAILA
2017-09-08 20:03 ` [PATCH v2 0/4] Various XSA followups Tim Deegan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=59BFE9AD020000780017C4D8@prv-mh.provo.novell.com \
    --to=jbeulich@suse.com \
    --cc=aisaila@bitdefender.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=george.dunlap@eu.citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=jun.nakajima@intel.com \
    --cc=kevin.tian@intel.com \
    --cc=mdontu@bitdefender.com \
    --cc=paul.durrant@citrix.com \
    --cc=rcojocaru@bitdefender.com \
    --cc=sstabellini@kernel.org \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tim@xen.org \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.