xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Razvan Cojocaru <rcojocaru@bitdefender.com>
To: "Roger Pau Monné" <roger.pau@citrix.com>
Cc: "kevin.tian@intel.com" <kevin.tian@intel.com>,
	"tamas@tklengyel.com" <tamas@tklengyel.com>,
	"wei.liu2@citrix.com" <wei.liu2@citrix.com>,
	"jbeulich@suse.com" <jbeulich@suse.com>,
	"george.dunlap@eu.citrix.com" <george.dunlap@eu.citrix.com>,
	"andrew.cooper3@citrix.com" <andrew.cooper3@citrix.com>,
	"Mihai Donțu" <mdontu@bitdefender.com>,
	"Andrei Vlad LUTAS" <vlutas@bitdefender.com>,
	"jun.nakajima@intel.com" <jun.nakajima@intel.com>,
	"Alexandru Stefan ISAILA" <aisaila@bitdefender.com>,
	"xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>,
	"Anshul Makkar" <anshul.makkar@citrix.com>
Subject: Re: [PATCH v1] x86/hvm: Generic instruction re-execution mechanism for execute faults
Date: Thu, 22 Nov 2018 20:24:52 +0200	[thread overview]
Message-ID: <f3ec3175-1c2e-e3a2-ed8f-63bf4c235fca@bitdefender.com> (raw)
In-Reply-To: <20181122170801.pzdoif2g73aamnmu@mac>

On 11/22/18 7:08 PM, Roger Pau Monné wrote:
> On Thu, Nov 22, 2018 at 06:52:07PM +0200, Razvan Cojocaru wrote:
>> On 11/22/18 5:37 PM, Roger Pau Monné wrote:
>>> I don't think you are supposed to try to pause other vcpus while
>>> holding a lock, as you can see it's quite likely that you will end up
>>> deadlocking because the vCPU you are trying to pause is stuck waiting
>>> on the lock that you are holding.
>>>
>>> You should figure out whether you can get into vmx_start_reexecute
>>> without holding any locks, or alternatively drop the lock, pause the
>>> vCPUs and pick the lock again.
>>>
>>> See for example how hap_track_dirty_vram releases the lock before
>>> attempting to pause the domain for this same reason.
>>
>> Right, this will take more thinking.
>>
>> I've unlocked the p2m for testing and the initial hang is gone, however
>> the same problem now applies to rexec_lock: nothing prevents two or more
>> VCPUs from arriving in vmx_start_reexecute_instruction() simultaneously,
>> at which point one of them might take the lock and try to pause the
>> other, while the other is waiting to take the lock, with predictable
>> results.
>>
>> On the other hand, releasing rexec_lock as well will allow two VCPUs to
>> end up trying to pause each other (especially unpleasant in a 2 VCPU
>> guest). At any given moment, there should be only one VCPU alive and
>> trying to reexecute an instruction - and at least one VCPU alive on the
>> guest.
>>
>> We'll get more coffee, and of course suggestions are appreciated (as has
>> been all your help).
> 
> Hm, I don't think it's generally safe to try to pause domain vCPUs
> from the same domain context, as you say it's likely to deadlock since
> two vCPUs from the same domain might try to pause one another.
> 
> My knowledge of all this introspection logic is very vague, do you
> really need to stop the other vCPUs while performing this reexecution?
> 
> What are you trying to prevent by pausing other vCPUs?

Yes, that's unfortunately very necessary.

The scenario is this: for introspection purposes, a bunch of pages are
marked read-only in the EPT (or no-execute, but for the purposes of this
example let's stick to read-only).

Now, we'll get vm_events whenever an instruction will try to write into
one of those. Vm_events are expensive, so we _really_ want to get as few
of those as possible while still keeping the guest protected. So we want
to filter out irrelevant ones.

The main category of irrelevant ones are faults caused by walking the
guest's page table. We only want events caused by an actual write into a
protected page by an actual instruction running at RIP in the guest.

So, we don't want to get those vm_events where npfec.kind !=
npfec_kind_with_gla in p2m_mem_access_check(), hence this patch:

https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=c5387c4d75602dbb2f0d3d961a5c4b8faf3873db

_However_, please picture an instruction that both writes into a page P1
we're interested in, _and_ causes a write into a read-only page-walk
related page P2. Emulating the current instruction, as the upstream
patch does, does eliminate the vm_event caused by writing into P2, but
with the unfortunate side-effect of losing a potentially critical event
for the write into P1.

What this patch attempts to do is to mark P1 rwx (so allow the write),
then put the faulting VCPU into singlestep mode, then restore the
restrictions after it has finished single stepping. By now it's obvious
why all the other VCPUs need to be paused: one of them might do a
malicious write into P1 that silently succeeds (since the EPT is shared
among all VCPUs - putting altp2m aside for a moment). We don't want that.

Alternatively, we'd be happy with simply being able to set the relevant
A/D bits in the pages touched by the page walk, but after lenghty
negotiations that can be found in the xen-devel archives we were unable
to find a safe, architecturally correct way of doing that.

I hope this sheds some light on it.


Thanks,
Razvan

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

  reply	other threads:[~2018-11-22 18:25 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-16 10:06 [PATCH v1] x86/hvm: Generic instruction re-execution mechanism for execute faults Alexandru Stefan ISAILA
2018-11-16 17:04 ` Roger Pau Monné
2018-11-19 13:30   ` Alexandru Stefan ISAILA
2018-11-19 14:26     ` Jan Beulich
2018-11-19 15:08     ` Roger Pau Monné
2018-11-19 15:56       ` Alexandru Stefan ISAILA
2018-11-21  9:56         ` Roger Pau Monné
2018-11-21 10:28           ` Alexandru Stefan ISAILA
2018-11-21 11:41             ` Roger Pau Monné
2018-11-21 12:00               ` Alexandru Stefan ISAILA
2018-11-19 13:33   ` Jan Beulich
2018-11-21 18:55   ` Razvan Cojocaru
2018-11-22  9:50     ` Alexandru Stefan ISAILA
2018-11-22 10:00       ` Jan Beulich
2018-11-22 10:07       ` Roger Pau Monné
2018-11-22 10:05     ` Roger Pau Monné
2018-11-22 10:14       ` Razvan Cojocaru
2018-11-22 10:58         ` Roger Pau Monné
2018-11-22 12:48           ` Razvan Cojocaru
2018-11-22 14:49             ` Roger Pau Monné
2018-11-22 15:25               ` Razvan Cojocaru
2018-11-22 15:37                 ` Roger Pau Monné
2018-11-22 16:52                   ` Razvan Cojocaru
2018-11-22 17:08                     ` Roger Pau Monné
2018-11-22 18:24                       ` Razvan Cojocaru [this message]
2018-11-23  8:54                         ` Roger Pau Monné
     [not found]                           ` <59739FBC020000C234861ACF@prv1-mh.provo.novell.com>
     [not found]                             ` <F553A58C020000AB0063616D@prv1-mh.provo.novell.com>
     [not found]                               ` <4D445A680200003E34861ACF@prv1-mh.provo.novell.com>
     [not found]                                 ` <DAD49D5A020000780063616D@prv1-mh.provo.novell.com>
     [not found]                                   ` <5400A6CB0200003634861ACF@prv1-mh.provo.novell.com>
     [not found]                                     ` <203C1A92020000400063616D@prv1-mh.provo.novell.com>
     [not found]                                       ` <0DF3BC62020000E934861ACF@prv1-mh.provo.novell.com>
     [not found]                                         ` <C6A2E442020000640063616D@prv1-mh.provo.novell.com>
     [not found]                                           ` <6EEA58AB020000EA34861ACF@prv1-mh.provo.novell.com>
2018-11-27 10:31                           ` Razvan Cojocaru
2018-11-27 11:32                             ` Roger Pau Monné
2018-11-27 11:45                               ` Razvan Cojocaru
2018-11-27 11:59                                 ` Andrew Cooper
2018-11-27 12:12                                   ` Razvan Cojocaru
2018-12-19 16:49                               ` Alexandru Stefan ISAILA
2018-12-19 17:40                                 ` Roger Pau Monné
2018-12-20 14:37                                   ` Alexandru Stefan ISAILA
     [not found]                         ` <838191050200006B34861ACF@prv1-mh.provo.novell.com>
2018-11-23  9:07                           ` Jan Beulich
2018-11-27 10:49                             ` Razvan Cojocaru
2018-11-27 11:28                               ` Jan Beulich
2018-11-27 11:44                                 ` Razvan Cojocaru
2019-05-13 13:58                               ` Razvan Cojocaru
2019-05-13 13:58                                 ` [Xen-devel] " Razvan Cojocaru
2019-05-13 14:06                                 ` Jan Beulich
2019-05-13 14:06                                   ` [Xen-devel] " Jan Beulich
2019-05-13 14:15                                   ` Razvan Cojocaru
2019-05-13 14:15                                     ` [Xen-devel] " Razvan Cojocaru
2019-05-14 13:47                                     ` Razvan Cojocaru
2019-05-14 13:47                                       ` [Xen-devel] " Razvan Cojocaru
2019-05-14 14:16                                       ` Jan Beulich
2019-05-14 14:16                                         ` [Xen-devel] " Jan Beulich
2019-05-14 14:20                                         ` Razvan Cojocaru
2019-05-14 14:20                                           ` [Xen-devel] " Razvan Cojocaru
     [not found]                           ` <A31948D30200007D0063616D@prv1-mh.provo.novell.com>
2018-11-23  9:10                             ` Jan Beulich
     [not found]                             ` <9B05ED9E020000C434861ACF@prv1-mh.provo.novell.com>
     [not found]                               ` <626A217B020000C50063616D@prv1-mh.provo.novell.com>
     [not found]                                 ` <0D3C56BA0200004834861ACF@prv1-mh.provo.novell.com>
2018-12-20  9:07                                   ` Jan Beulich

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=f3ec3175-1c2e-e3a2-ed8f-63bf4c235fca@bitdefender.com \
    --to=rcojocaru@bitdefender.com \
    --cc=aisaila@bitdefender.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=anshul.makkar@citrix.com \
    --cc=george.dunlap@eu.citrix.com \
    --cc=jbeulich@suse.com \
    --cc=jun.nakajima@intel.com \
    --cc=kevin.tian@intel.com \
    --cc=mdontu@bitdefender.com \
    --cc=roger.pau@citrix.com \
    --cc=tamas@tklengyel.com \
    --cc=vlutas@bitdefender.com \
    --cc=wei.liu2@citrix.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).