All of lore.kernel.org
 help / color / mirror / Atom feed
From: Julien Grall <julien@xen.org>
To: "Roger Pau Monné" <roger.pau@citrix.com>
Cc: Wei Liu <wl@xen.org>, George Dunlap <george.dunlap@eu.citrix.com>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	Julien Grall <jgrall@amazon.com>, Jan Beulich <jbeulich@suse.com>,
	xen-devel@lists.xenproject.org
Subject: Re: [Xen-devel] [PATCH 2/2] xen/x86: hap: Clean-up and harden hap_enable()
Date: Tue, 4 Feb 2020 11:11:11 +0000	[thread overview]
Message-ID: <58bc6c70-89f5-cdcf-552c-1ea5a3fa5508@xen.org> (raw)
In-Reply-To: <20200204105156.GH4679@Air-de-Roger>



On 04/02/2020 10:51, Roger Pau Monné wrote:
> On Tue, Feb 04, 2020 at 09:34:11AM +0000, Julien Grall wrote:
>> From: Julien Grall <jgrall@amazon.com>
>>
>> Unlike shadow_enable(), hap_enable() can only be called once during
>> domain creation and with the mode equal to mode equal to
>                                      ^ equals to

Will fix it.

>> PG_external | PG_translate | PG_refcounts.
>>
>> If it were called twice, then we might have something interesting
>                                                ^ a problem
>> problem as the p2m tables would be re-allocated (and therefore all the
>> mappings would be lost).
>>
>> Add code to sanity check the mode and that the function is only called
>> once. Take the opportunity to an if checking that PG_translate is set.
>                                  ^ add an if

Will fix it.

>>
>> Signed-off-by: Julien Grall <jgrall@amazon.com>
>>
>> ---
>>
>> It is not entirely clear when PG_translate was enforced.
>> ---
>>   xen/arch/x86/mm/hap/hap.c | 18 +++++++++++-------
>>   1 file changed, 11 insertions(+), 7 deletions(-)
>>
>> diff --git a/xen/arch/x86/mm/hap/hap.c b/xen/arch/x86/mm/hap/hap.c
>> index 31362a31b6..b734e2e6d3 100644
>> --- a/xen/arch/x86/mm/hap/hap.c
>> +++ b/xen/arch/x86/mm/hap/hap.c
>> @@ -445,6 +445,13 @@ int hap_enable(struct domain *d, u32 mode)
>>       unsigned int i;
>>       int rv = 0;
>>   
>> +    if ( mode != (PG_external | PG_translate | PG_refcounts) )
>> +        return -EINVAL;
>> +
>> +    /* The function can only be called once */
>> +    if ( d->arch.paging.mode != 0 )
>> +        return -EINVAL;
> 
> If you want to return EINVAL for both they can be merged into a single
> if. Also note that this would usually be written as
> if ( d->arch.paging.mode ) to keep it shorter.

To be honest, this is a matter of taste. There is also an argument that 
for MISRA, your suggestion is not compliant (see Rule 14.4).

> 
> Albeit I think you might want to return EEXIST instead of EINVAL if
> mode is already set.

I am happy with that.

> 
>> +
>>       domain_pause(d);
>>   
>>       old_pages = d->arch.paging.hap.total_pages;
>> @@ -465,13 +472,10 @@ int hap_enable(struct domain *d, u32 mode)
>>       d->arch.paging.alloc_page = hap_alloc_p2m_page;
>>       d->arch.paging.free_page = hap_free_p2m_page;
>>   
>> -    /* allocate P2m table */
>> -    if ( mode & PG_translate )
>> -    {
>> -        rv = p2m_alloc_table(p2m_get_hostp2m(d));
>> -        if ( rv != 0 )
>> -            goto out;
>> -    }
>> +    /* allocate P2M table */
>> +    rv = p2m_alloc_table(p2m_get_hostp2m(d));
>> +    if ( rv != 0 )
> 
> I would also avoid comparing against 0 here.

See above.

Cheers,

-- 
Julien Grall

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

  reply	other threads:[~2020-02-04 11:11 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-04  9:34 [Xen-devel] [PATCH 0/2] xen/x86: hap: Small clean-up/hardening in hap_enable() Julien Grall
2020-02-04  9:34 ` [Xen-devel] [PATCH 1/2] xen/x86: hap: Fix coding style " Julien Grall
2020-02-04 10:07   ` Roger Pau Monné
2020-02-04  9:34 ` [Xen-devel] [PATCH 2/2] xen/x86: hap: Clean-up and harden hap_enable() Julien Grall
2020-02-04 10:51   ` Roger Pau Monné
2020-02-04 11:11     ` Julien Grall [this message]
2020-02-04 11:28       ` Roger Pau Monné
2020-02-04 11:33         ` George Dunlap
2020-02-04 11:44         ` Julien Grall
2020-02-04 12:36           ` Jan Beulich
2020-02-04 12:51             ` Julien Grall

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=58bc6c70-89f5-cdcf-552c-1ea5a3fa5508@xen.org \
    --to=julien@xen.org \
    --cc=andrew.cooper3@citrix.com \
    --cc=george.dunlap@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=jgrall@amazon.com \
    --cc=roger.pau@citrix.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.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.