Skip to contentSkip to conversation

Locking on vm-event operations (monitor)

DISCUSSION Corneliu ZUZU 3 participants 13 messages Jul 22, 2016

lore
Share & tools
lore
#0 lore raw

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

As for locking, I see that for example vm_event_init_domain(), vm_event_cleanup_domain() and monitor_domctl() are all protected by the domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_* resources, but it doesn't seem to take the domctl lock, so it seems possible for that to happen _while_ those resources are initialized/cleaned-up
* monitor_guest_request() also calls monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot() which attempts a vm_event_ring_lock(ved), which could also be called _while_ that's initialized (vm_event_enable()) or cleaned-up (vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX() there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Am I missing something with these conclusions?
As a resolution for this, I've been thinking of adding a 'subsys_lock' field in the vm_event_domain structure, either spinlock or rw-lock, which would be initialised/uninitialised when d.vm_event is allocated/freed (domain_create()/complete_domain_destroy()).

Let me know what you think,
Corneliu.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#1 lore raw
Quoted 3 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

Correct. Without the vcpu/domain pause, a vcpu could be midway through a vmexit path with the monitor configuration changing under its feet. OTOH, it could be running in guest context, and only receive the update one scheduling timeslice later.

Quoted 9 lines

As for locking, I see that for example vm_event_init_domain(), vm_event_cleanup_domain() and monitor_domctl() are all protected by the domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_* resources, but it doesn't seem to take the domctl lock, so it seems possible for that to happen _while_ those resources are initialized/cleaned-up
* monitor_guest_request() also calls monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot() which attempts a vm_event_ring_lock(ved), which could also be called _while_ that's initialized (vm_event_enable()) or cleaned-up (vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX() there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Am I missing something with these conclusions?
As a resolution for this, I've been thinking of adding a 'subsys_lock' field in the vm_event_domain structure, either spinlock or rw-lock, which would be initialised/uninitialised when d.vm_event is allocated/freed (domain_create()/complete_domain_destroy()).

I don't think you are missing anything. It seems like a monitor lock is needed.

FWIW, the domctl lock is only used at the moment because it was easy, and worked when everything was a domctl. Being a global spinlock, it is a severe bottlekneck for concurrent domain management, which I need to find some time to break apart, so the less reliance on it the better from my point of view.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 15 lines · Andrew Cooper
Quoted 3 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

Correct. Without the vcpu/domain pause, a vcpu could be midway through a vmexit path with the monitor configuration changing under its feet. OTOH, it could be running in guest context, and only receive the update one scheduling timeslice later.

Quoted 7 lines

As for locking, I see that for example vm_event_init_domain(), vm_event_cleanup_domain() and monitor_domctl() are all protected by the domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_* resources, but it doesn't seem to take the domctl lock, so it seems possible for that to happen _while_ those resources are initialized/cleaned-up
* monitor_guest_request() also calls monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot() which attempts a vm_event_ring_lock(ved), which could also be called _while_ that's initialized (vm_event_enable()) or cleaned-up (vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX() there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Quoted 9 lines
Quoted 2 lines

Am I missing something with these conclusions?
As a resolution for this, I've been thinking of adding a 'subsys_lock' field in the vm_event_domain structure, either spinlock or rw-lock, which would be initialised/uninitialised when d.vm_event is allocated/freed (domain_create()/complete_domain_destroy()).

I don't think you are missing anything. It seems like a monitor lock is needed.

FWIW, the domctl lock is only used at the moment because it was easy, and worked when everything was a domctl. Being a global spinlock, it is a severe bottlekneck for concurrent domain management, which I need to find some time to break apart, so the less reliance on it the better from my point of view.

~Andrew

I was thinking of not touching the way the domctl lock is acquired in these cases at all, but rather leave that as it is and introduce the rw-lock separately. Are you suggesting I should also change do_domctl to skip domctl_lock_acquire()-ing if op->cmd is XEN_DOMCTL_vm_event_op ? That would certainly be a first step towards amending that bottleneck you mentioned, but it would break consistency of locking-behavior for domctls. I think it would also add significant complexity to what I'll be doing so I hope it wouldn't be a problem if we leave that for a future patch.

Corneliu.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 19 lines · Corneliu ZUZU
Quoted 18 lines · Andrew Cooper
Quoted 3 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

Correct. Without the vcpu/domain pause, a vcpu could be midway through a vmexit path with the monitor configuration changing under its feet. OTOH, it could be running in guest context, and only receive the update one scheduling timeslice later.

Quoted 9 lines

As for locking, I see that for example vm_event_init_domain(), vm_event_cleanup_domain() and monitor_domctl() are all protected by the domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_* resources, but it doesn't seem to take the domctl lock, so it seems possible for that to happen _while_ those resources are initialized/cleaned-up
* monitor_guest_request() also calls monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot() which attempts a vm_event_ring_lock(ved), which could also be called _while_ that's initialized (vm_event_enable()) or cleaned-up (vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX() there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Am I missing something with these conclusions?
As a resolution for this, I've been thinking of adding a 'subsys_lock' field in the vm_event_domain structure, either spinlock or rw-lock, which would be initialised/uninitialised when d.vm_event is allocated/freed (domain_create()/complete_domain_destroy()).

Quoted 8 lines
Quoted 6 lines

I don't think you are missing anything. It seems like a monitor lock is needed.

FWIW, the domctl lock is only used at the moment because it was easy, and worked when everything was a domctl. Being a global spinlock, it is a severe bottlekneck for concurrent domain management, which I need to find some time to break apart, so the less reliance on it the better from my point of view.

~Andrew

I was thinking of not touching the way the domctl lock is acquired in these cases at all, but rather leave that as it is and introduce the rw-lock separately. Are you suggesting I should also change do_domctl to skip domctl_lock_acquire()-ing if op->cmd is XEN_DOMCTL_vm_event_op ? That would certainly be a first step towards amending that bottleneck you mentioned, but it would break consistency of locking-behavior for domctls. I think it would also add significant complexity to what I'll be doing so I hope it wouldn't be a problem if we leave that for a future patch.

Sorry - I didn't intend to suggest altering the domctl lock. Just that adding in a new monitor lock and using it everywhere where appropriate would be a good step.

Breaking the domctl lock is a very large can of worms, which you definitely won't want to open while attempting to do something else.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
Quoted 29 lines · Andrew Cooper
Quoted 26 lines · Corneliu ZUZU
Quoted 24 lines · Andrew Cooper
Quoted 3 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

Correct. Without the vcpu/domain pause, a vcpu could be midway through a vmexit path with the monitor configuration changing under its feet. OTOH, it could be running in guest context, and only receive the update one scheduling timeslice later.

Quoted 9 lines

As for locking, I see that for example vm_event_init_domain(), vm_event_cleanup_domain() and monitor_domctl() are all protected by the domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_* resources, but it doesn't seem to take the domctl lock, so it seems possible for that to happen _while_ those resources are initialized/cleaned-up
* monitor_guest_request() also calls monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot() which attempts a vm_event_ring_lock(ved), which could also be called _while_ that's initialized (vm_event_enable()) or cleaned-up (vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX() there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Am I missing something with these conclusions?
As a resolution for this, I've been thinking of adding a 'subsys_lock' field in the vm_event_domain structure, either spinlock or rw-lock, which would be initialised/uninitialised when d.vm_event is allocated/freed (domain_create()/complete_domain_destroy()).

I don't think you are missing anything. It seems like a monitor lock is needed.

FWIW, the domctl lock is only used at the moment because it was easy, and worked when everything was a domctl. Being a global spinlock, it is a severe bottlekneck for concurrent domain management, which I need to find some time to break apart, so the less reliance on it the better from my point of view.

~Andrew

Quoted 8 lines
Quoted 1 line

I was thinking of not touching the way the domctl lock is acquired in these cases at all, but rather leave that as it is and introduce the rw-lock separately. Are you suggesting I should also change do_domctl to skip domctl_lock_acquire()-ing if op->cmd is XEN_DOMCTL_vm_event_op ? That would certainly be a first step towards amending that bottleneck you mentioned, but it would break consistency of locking-behavior for domctls. I think it would also add significant complexity to what I'll be doing so I hope it wouldn't be a problem if we leave that for a future patch.

Sorry - I didn't intend to suggest altering the domctl lock. Just that adding in a new monitor lock and using it everywhere where appropriate would be a good step.

Breaking the domctl lock is a very large can of worms, which you definitely won't want to open while attempting to do something else.

~Andrew

Yep, agreed.

Corneliu.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw
In reply to Corneliu ZUZU
Quoted 28 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

As for locking, I see that for example vm_event_init_domain(),
vm_event_cleanup_domain() and monitor_domctl() are all protected by the
domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_*
resources, but it doesn't seem to take the domctl lock, so it seems
possible for that to happen _while_ those resources are
initialized/cleaned-up
* monitor_guest_request() also calls
monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot()
which attempts a vm_event_ring_lock(ved), which could also be called
_while_ that's initialized (vm_event_enable()) or cleaned-up
(vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX() there doesn't seem to be taken into account the possibility of a
concurrent monitor_init_domain()/monitor_cleanup_domain()

Quoted 1 line

Am I missing something with these conclusions?

Your conclusions look correct, but I assume that the reason why this has not been addressed in the past is that introspection applications are expected to be well-behaved. Specifically, in a codebase where the choice between uint64_t and long int matters speed-wise, and where unlikely()s also matter, an extra lock may be an issue.

The typical flow of an introspection application is:

1. Initialize everything.
2. Subscribe to relevant events.
3. Event processing loop.
4. Unsubscribe from events.
5. Do a last-run of event processing (already queued in the ring buffer).
6. Uninitialize everything (no events are possible here because of steps 4-5).

Quoted 4 lines

As a resolution for this, I've been thinking of adding a 'subsys_lock' field in the vm_event_domain structure, either spinlock or rw-lock, which would be initialised/uninitialised when d.vm_event is allocated/freed (domain_create()/complete_domain_destroy()).

I have nothing against this. Having as many assurances as possible that things will work is definitely a plus in my book - with the comment that I would prefer a rwlock to an ordinary spinlock, and that "subsys_lock" sounds obscure to me, although I admit that I can't think of a good name at the moment.

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw
Quoted 29 lines · Razvan Cojocaru
Quoted 28 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

As for locking, I see that for example vm_event_init_domain(),
vm_event_cleanup_domain() and monitor_domctl() are all protected by the
domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_*
resources, but it doesn't seem to take the domctl lock, so it seems
possible for that to happen _while_ those resources are
initialized/cleaned-up
* monitor_guest_request() also calls
monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot()
which attempts a vm_event_ring_lock(ved), which could also be called
_while_ that's initialized (vm_event_enable()) or cleaned-up
(vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX() there doesn't seem to be taken into account the possibility of a
concurrent monitor_init_domain()/monitor_cleanup_domain()

Quoted 4 lines
Quoted 1 line

Am I missing something with these conclusions?

Your conclusions look correct, but I assume that the reason why this has not been addressed in the past is that introspection applications are expected to be well-behaved.

I wouldn't think that was the rationale (considering that the risk is crashing the hypervisor as a whole; also see below), I'd rather think this simply went unnoticed.

Quoted 6 lines

Specifically, in a codebase where the
choice between uint64_t and long int matters speed-wise, and where unlikely()s also matter, an extra lock may be an issue.

The typical flow of an introspection application is:

Quoted 7 lines

1. Initialize everything.
2. Subscribe to relevant events.
3. Event processing loop.
4. Unsubscribe from events.
5. Do a last-run of event processing (already queued in the ring buffer).
6. Uninitialize everything (no events are possible here because of steps 4-5).

And even if an introspection application behaves as sanely as you say, the current layout of the code still doesn't guarantee bug-free behavior. That's because for one, there are in-guest events (that trigger access of vm-events resources) that are completely asynchronous in relation to 2-6, for example a HVMOP_guest_request_vm_event (subsequently calling monitor_guest_request()).

Quoted 7 lines
Quoted 4 lines

As a resolution for this, I've been thinking of adding a 'subsys_lock' field in the vm_event_domain structure, either spinlock or rw-lock, which would be initialised/uninitialised when d.vm_event is allocated/freed (domain_create()/complete_domain_destroy()).

I have nothing against this. Having as many assurances as possible that things will work is definitely a plus in my book - with the comment that I would prefer a rwlock to an ordinary spinlock

Yep, I'd prefer that too.

Quoted 3 lines

, and that "subsys_lock"
sounds obscure to me, although I admit that I can't think of a good name at the moment.

I'm referring to monitor, share & paging as "subsystems" of the whole "vm-events" subsystem (in that sense the 3 aforementioned are "sub-subsystems"). To me it's acceptable, but I'm open to better names.

Quoted 2 lines

Thanks,
Razvan

Thanks,
Corneliu.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#7 lore raw
Quoted 55 lines · Corneliu ZUZU
Quoted 28 lines · Razvan Cojocaru
Quoted 27 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

As for locking, I see that for example vm_event_init_domain(),
vm_event_cleanup_domain() and monitor_domctl() are all protected by the
domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_*
resources, but it doesn't seem to take the domctl lock, so it seems
possible for that to happen _while_ those resources are
initialized/cleaned-up
* monitor_guest_request() also calls
monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot()
which attempts a vm_event_ring_lock(ved), which could also be called
_while_ that's initialized (vm_event_enable()) or cleaned-up
(vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX()

Quoted 7 lines
Quoted 4 lines

there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Am I missing something with these conclusions?

Your conclusions look correct, but I assume that the reason why this has not been addressed in the past is that introspection applications are expected to be well-behaved.

I wouldn't think that was the rationale (considering that the risk is crashing the hypervisor as a whole; also see below), I'd rather think this simply went unnoticed.

Quoted 6 lines

Specifically, in a codebase where the
choice between uint64_t and long int matters speed-wise, and where unlikely()s also matter, an extra lock may be an issue.

The typical flow of an introspection application is:

Quoted 7 lines

1. Initialize everything.
2. Subscribe to relevant events.
3. Event processing loop.
4. Unsubscribe from events.
5. Do a last-run of event processing (already queued in the ring buffer).
6. Uninitialize everything (no events are possible here because of steps 4-5).

Quoted 6 lines

And even if an introspection application behaves as sanely as you say, the current layout of the code still doesn't guarantee bug-free behavior. That's because for one, there are in-guest events (that trigger access of vm-events resources) that are completely asynchronous in relation to 2-6, for example a HVMOP_guest_request_vm_event (subsequently calling monitor_guest_request()).

Not true. As part of step 4 d->monitor.guest_request_enabled will become 0, so no sensitive code will run after that:

129 void monitor_guest_request(void)
130 {
131 struct vcpu *curr = current;
132 struct domain *d = curr->domain;
133
134 if ( d->monitor.guest_request_enabled )
135 {
136 vm_event_request_t req = {
137 .reason = VM_EVENT_REASON_GUEST_REQUEST,
138 .vcpu_id = curr->vcpu_id,
139 };
140
141 monitor_traps(curr, d->monitor.guest_request_sync, &req);
142 }
143 }

The guest _may_ try to request an event after that, but the request will be ignored.

Quoted 8 lines
Quoted 3 lines

, and that "subsys_lock"
sounds obscure to me, although I admit that I can't think of a good name at the moment.

I'm referring to monitor, share & paging as "subsystems" of the whole "vm-events" subsystem (in that sense the 3 aforementioned are "sub-subsystems").
To me it's acceptable, but I'm open to better names.

If Tamas agrees with you I'll consider myself outvoted.

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#8 lore raw
Quoted 54 lines · Razvan Cojocaru
Quoted 53 lines · Corneliu ZUZU
Quoted 28 lines · Razvan Cojocaru
Quoted 27 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting for all vCPUs), not to synchronize concurrent operations (lock-behavior).

As for locking, I see that for example vm_event_init_domain(),
vm_event_cleanup_domain() and monitor_domctl() are all protected by the
domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_*
resources, but it doesn't seem to take the domctl lock, so it seems
possible for that to happen _while_ those resources are
initialized/cleaned-up
* monitor_guest_request() also calls
monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot()
which attempts a vm_event_ring_lock(ved), which could also be called
_while_ that's initialized (vm_event_enable()) or cleaned-up
(vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX()

Quoted 7 lines
Quoted 4 lines

there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Am I missing something with these conclusions?

Your conclusions look correct, but I assume that the reason why this has not been addressed in the past is that introspection applications are expected to be well-behaved.

I wouldn't think that was the rationale (considering that the risk is crashing the hypervisor as a whole; also see below), I'd rather think this simply went unnoticed.

Quoted 13 lines

Specifically, in a codebase where the
choice between uint64_t and long int matters speed-wise, and where unlikely()s also matter, an extra lock may be an issue.

The typical flow of an introspection application is:

1. Initialize everything.
2. Subscribe to relevant events.
3. Event processing loop.
4. Unsubscribe from events.
5. Do a last-run of event processing (already queued in the ring buffer).
6. Uninitialize everything (no events are possible here because of steps 4-5).

Quoted 8 lines
Quoted 6 lines

And even if an introspection application behaves as sanely as you say, the current layout of the code still doesn't guarantee bug-free behavior. That's because for one, there are in-guest events (that trigger access of vm-events resources) that are completely asynchronous in relation to 2-6, for example a HVMOP_guest_request_vm_event (subsequently calling monitor_guest_request()).

Not true. As part of step 4 d->monitor.guest_request_enabled will become 0, so no sensitive code will run after that:

I wasn't being rigorous with 2-6 above (with that I only meant to say 'there can't be any vm-events before everything is initialized') and it seems true that at least in the monitor_guest_request() case it might not be able to produce a hypervisor crash (although there are a number of ugly inconsistencies on that code path). But nonetheless a hypervisor crash can _still_ happen even with a _sane_ toolstack user.

Look @ hvm_do_resume():

1. If you, as a toolstack user, get at sane step no. 6: "Uninitialize everything (no events are possible here because of steps 4-5)."
2. But just before you do that, a hvm_do_resume() happens on an arbitrary vCPU of the domain and gets to this point:

    if ( unlikely(v->arch.vm_event) )
    {
        // ---> let's say you are now at this point, just after the 
above non-NULL check
        struct monitor_write_data *w = &v->arch.vm_event->write_data;

3. and before proceeding further, the uninitialization sequence _completes_, i.e. this was done in vm_event_cleanup_domain():

    for_each_vcpu ( d, v )
    {
        xfree(v->arch.vm_event);
        v->arch.vm_event = NULL;
    }

4. and then in hvm_do_resume() this gets to be done:

        struct monitor_write_data *w = &v->arch.vm_event->write_data;

        [...]

        if ( w->do_write.msr )
        {
            hvm_msr_write_intercept(w->msr, w->value, 0);
            w->do_write.msr = 0;
        }

then you'll have a beautiful NULL-pointer access hypervisor crash.

Quoted 27 lines

129 void monitor_guest_request(void)
130 {
131 struct vcpu *curr = current;
132 struct domain *d = curr->domain;
133
134 if ( d->monitor.guest_request_enabled )
135 {
136 vm_event_request_t req = {
137 .reason = VM_EVENT_REASON_GUEST_REQUEST,
138 .vcpu_id = curr->vcpu_id,
139 };
140
141 monitor_traps(curr, d->monitor.guest_request_sync, &req);
142 }
143 }

The guest _may_ try to request an event after that, but the request will be ignored.

Quoted 7 lines
Quoted 3 lines

, and that "subsys_lock"
sounds obscure to me, although I admit that I can't think of a good name at the moment.

I'm referring to monitor, share & paging as "subsystems" of the whole "vm-events" subsystem (in that sense the 3 aforementioned are "sub-subsystems").
To me it's acceptable, but I'm open to better names.

Quoted 5 lines

If Tamas agrees with you I'll consider myself outvoted.

Thanks,
Razvan

Corneliu.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#9 lore raw
Quoted 112 lines · Corneliu ZUZU
Quoted 61 lines · Razvan Cojocaru
Quoted 60 lines · Corneliu ZUZU
Quoted 32 lines · Razvan Cojocaru
Quoted 31 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and
why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting
for all vCPUs), not to synchronize concurrent operations (lock-behavior).

As for locking, I see that for example vm_event_init_domain(),
vm_event_cleanup_domain() and monitor_domctl() are all protected by
the
domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_*
resources, but it doesn't seem to take the domctl lock, so it seems
possible for that to happen _while_ those resources are
initialized/cleaned-up
* monitor_guest_request() also calls
monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot()
which attempts a vm_event_ring_lock(ved), which could also be called
_while_ that's initialized (vm_event_enable()) or cleaned-up
(vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX()

Quoted 8 lines
Quoted 4 lines

there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Am I missing something with these conclusions?

Your conclusions look correct, but I assume that the reason why this has
not been addressed in the past is that introspection applications are expected to be well-behaved.

I wouldn't think that was the rationale (considering that the risk is crashing the hypervisor as a whole; also see below), I'd rather think this simply went unnoticed.

Quoted 15 lines

Specifically, in a codebase where the
choice between uint64_t and long int matters speed-wise, and where unlikely()s also matter, an extra lock may be an issue.

The typical flow of an introspection application is:

1. Initialize everything.
2. Subscribe to relevant events.
3. Event processing loop.
4. Unsubscribe from events.
5. Do a last-run of event processing (already queued in the ring buffer).
6. Uninitialize everything (no events are possible here because of steps
4-5).

Quoted 8 lines
Quoted 6 lines

And even if an introspection application behaves as sanely as you say, the current layout of the code still doesn't guarantee bug-free behavior. That's because for one, there are in-guest events (that trigger access of vm-events resources) that are completely asynchronous in relation to 2-6, for example a HVMOP_guest_request_vm_event (subsequently calling monitor_guest_request()).

Not true. As part of step 4 d->monitor.guest_request_enabled will become 0, so no sensitive code will run after that:

I wasn't being rigorous with 2-6 above (with that I only meant to say 'there can't be any vm-events before everything is initialized') and it seems true that at least in the monitor_guest_request() case it might not be able to produce a hypervisor crash (although there are a number of ugly inconsistencies on that code path). But nonetheless a hypervisor crash can _still_ happen even with a _sane_ toolstack user.

Look @ hvm_do_resume():

1. If you, as a toolstack user, get at sane step no. 6: "Uninitialize everything (no events are possible here because of steps 4-5)."
2. But just before you do that, a hvm_do_resume() happens on an arbitrary vCPU of the domain and gets to this point:

    if ( unlikely(v->arch.vm_event) )
    {
        // ---> let's say you are now at this point, just after the
above non-NULL check
        struct monitor_write_data *w = &v->arch.vm_event->write_data;

3. and before proceeding further, the uninitialization sequence _completes_, i.e. this was done in vm_event_cleanup_domain():

    for_each_vcpu ( d, v )
    {
        xfree(v->arch.vm_event);
        v->arch.vm_event = NULL;
    }

4. and then in hvm_do_resume() this gets to be done:

        struct monitor_write_data *w = &v->arch.vm_event->write_data;

        [...]

        if ( w->do_write.msr )
        {
            hvm_msr_write_intercept(w->msr, w->value, 0);
            w->do_write.msr = 0;
        }
Quoted 1 line

then you'll have a beautiful NULL-pointer access hypervisor crash.

As I've said before, of course I agree with that - isn't that what a previous patch you've submitted addressed? I was merely replying to your concerns assuming that that issue is addressed (as discussed before), and assuming that the introspection application behaves gracefully.

Again, I'm all for your proposed changes with the additional locking. I personally do prefer as many safeguards as possible.

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#10 lore raw
Quoted 106 lines · Razvan Cojocaru
Quoted 105 lines · Corneliu ZUZU
Quoted 61 lines · Razvan Cojocaru
Quoted 60 lines · Corneliu ZUZU
Quoted 32 lines · Razvan Cojocaru
Quoted 31 lines · Corneliu ZUZU

Hi,

I've been inspecting vm-event code parts to try and understand when and
why domain pausing/locking is done. If I understood correctly, domain pausing is done solely to force all the vCPUs of that domain to see a configuration update and act upon it (e.g. in the case of a XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG that enables CR3 monitoring, domain pausing/unpausing ensures immediate enabling of CR3 load-exiting
for all vCPUs), not to synchronize concurrent operations (lock-behavior).

As for locking, I see that for example vm_event_init_domain(),
vm_event_cleanup_domain() and monitor_domctl() are all protected by
the
domctl-lock, but I don't think that's enough.
Here are a few code-paths that led me to believe that:
* do_hvm_op()->monitor_guest_request() reads d.monitor.guest_request_*
resources, but it doesn't seem to take the domctl lock, so it seems
possible for that to happen _while_ those resources are
initialized/cleaned-up
* monitor_guest_request() also calls
monitor_traps()->...->vm_event_wait_slot()->...->vm_event_grab_slot()
which attempts a vm_event_ring_lock(ved), which could also be called
_while_ that's initialized (vm_event_enable()) or cleaned-up
(vm_event_disable())
* hvm_monitor_cr() - e.g. on the code-path vmx_vmexit_handler(EXIT_REASON_CR_ACCESS)->vmx_cr_access(VMX_CONTROL_REG_ACCESS_TYPE_MOV_TO_CR)->hvm_mov_to_cr()->hvm_set_cr0()->hvm_monitor_crX()

Quoted 8 lines
Quoted 4 lines

there doesn't seem to be taken into account the possibility of a concurrent monitor_init_domain()/monitor_cleanup_domain()

Am I missing something with these conclusions?

Your conclusions look correct, but I assume that the reason why this has
not been addressed in the past is that introspection applications are expected to be well-behaved.

I wouldn't think that was the rationale (considering that the risk is crashing the hypervisor as a whole; also see below), I'd rather think this simply went unnoticed.

Quoted 15 lines

Specifically, in a codebase where the
choice between uint64_t and long int matters speed-wise, and where unlikely()s also matter, an extra lock may be an issue.

The typical flow of an introspection application is:

1. Initialize everything.
2. Subscribe to relevant events.
3. Event processing loop.
4. Unsubscribe from events.
5. Do a last-run of event processing (already queued in the ring buffer).
6. Uninitialize everything (no events are possible here because of steps
4-5).

Quoted 8 lines
Quoted 6 lines

And even if an introspection application behaves as sanely as you say, the current layout of the code still doesn't guarantee bug-free behavior. That's because for one, there are in-guest events (that trigger access of vm-events resources) that are completely asynchronous in relation to 2-6, for example a HVMOP_guest_request_vm_event (subsequently calling monitor_guest_request()).

Not true. As part of step 4 d->monitor.guest_request_enabled will become 0, so no sensitive code will run after that:

I wasn't being rigorous with 2-6 above (with that I only meant to say 'there can't be any vm-events before everything is initialized') and it seems true that at least in the monitor_guest_request() case it might not be able to produce a hypervisor crash (although there are a number of ugly inconsistencies on that code path). But nonetheless a hypervisor crash can _still_ happen even with a _sane_ toolstack user.

Look @ hvm_do_resume():

1. If you, as a toolstack user, get at sane step no. 6: "Uninitialize everything (no events are possible here because of steps 4-5)."
2. But just before you do that, a hvm_do_resume() happens on an arbitrary vCPU of the domain and gets to this point:

     if ( unlikely(v->arch.vm_event) )
     {
         // ---> let's say you are now at this point, just after the
above non-NULL check
         struct monitor_write_data *w = &v->arch.vm_event->write_data;

3. and before proceeding further, the uninitialization sequence _completes_, i.e. this was done in vm_event_cleanup_domain():

     for_each_vcpu ( d, v )
     {
         xfree(v->arch.vm_event);
         v->arch.vm_event = NULL;
     }

4. and then in hvm_do_resume() this gets to be done:

         struct monitor_write_data *w = &v->arch.vm_event->write_data;

         [...]
Quoted 8 lines
Quoted 7 lines
         if ( w->do_write.msr )
         {
             hvm_msr_write_intercept(w->msr, w->value, 0);
             w->do_write.msr = 0;
         }

then you'll have a beautiful NULL-pointer access hypervisor crash.

As I've said before, of course I agree with that

Sorry, I thought by your previous statement "I assume that the reason why this has not been addressed in the past is that introspection applications are expected to be well-behaved" you were inferring that with a 'sane' toolstack user, as you defined it, it won't be possible to have a hypervisor crash.

Quoted 2 lines

- isn't that what a
previous patch you've submitted addressed?

I'm not sure what patch you're referring to, I don't remember ever addressing these issues before..

Quoted 6 lines

I was merely replying to your
concerns assuming that that issue is addressed (as discussed before), and assuming that the introspection application behaves gracefully.

Again, I'm all for your proposed changes with the additional locking. I personally do prefer as many safeguards as possible.

I understand that, and I was saying that without these safe-guards significant system compromise is possible, so I wouldn't call them 'additional', but rather 'missing'. :-)

Quoted 2 lines

Thanks,
Razvan

Anyway, I think we might've misunderstood each-other here, so better leave the details for when I send the patch-series. ;-)

Thanks too,
Corneliu.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#11 lore raw
Quoted 34 lines · Corneliu ZUZU
Quoted 34 lines
Quoted 34 lines

Look @ hvm_do_resume():

1. If you, as a toolstack user, get at sane step no. 6: "Uninitialize everything (no events are possible here because of steps 4-5)."
2. But just before you do that, a hvm_do_resume() happens on an arbitrary vCPU of the domain and gets to this point:

     if ( unlikely(v->arch.vm_event) )
     {
         // ---> let's say you are now at this point, just after the
above non-NULL check
         struct monitor_write_data *w = &v->arch.vm_event->write_data;

3. and before proceeding further, the uninitialization sequence _completes_, i.e. this was done in vm_event_cleanup_domain():

     for_each_vcpu ( d, v )
     {
         xfree(v->arch.vm_event);
         v->arch.vm_event = NULL;
     }

4. and then in hvm_do_resume() this gets to be done:

         struct monitor_write_data *w = &v->arch.vm_event->write_data;

         [...]

         if ( w->do_write.msr )
         {
             hvm_msr_write_intercept(w->msr, w->value, 0);
             w->do_write.msr = 0;
         }
Quoted 8 lines
Quoted 2 lines
Quoted 1 line

then you'll have a beautiful NULL-pointer access hypervisor crash.

As I've said before, of course I agree with that

Sorry, I thought by your previous statement "I assume that the reason why this has not been addressed in the past is that introspection applications are expected to be well-behaved" you were inferring that with a 'sane' toolstack user, as you defined it, it won't be possible to have a hypervisor crash.

I did (please see below).

Quoted 5 lines
Quoted 2 lines

- isn't that what a
previous patch you've submitted addressed?

I'm not sure what patch you're referring to, I don't remember ever addressing these issues before..

I am talking about your patch "x86/monitor: fix: don't compromise a monitor_write_data with pending CR writes", which, if I'm not mistaken, addresses the above problem (since we won't xfree(v->arch.vm_event) on monitor uninit anymore).

With the above problem fixed, the workflow I suggested should be fine.

Again, I would prefer things to be rock solid, so personally I encourage your patch and hope we get it in.

Thanks,
Razvan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#12 lore raw
Quoted 35 lines · Razvan Cojocaru
Quoted 34 lines · Corneliu ZUZU
Quoted 34 lines
Quoted 34 lines

Look @ hvm_do_resume():

1. If you, as a toolstack user, get at sane step no. 6: "Uninitialize everything (no events are possible here because of steps 4-5)."
2. But just before you do that, a hvm_do_resume() happens on an arbitrary vCPU of the domain and gets to this point:

      if ( unlikely(v->arch.vm_event) )
      {
          // ---> let's say you are now at this point, just after the
above non-NULL check
          struct monitor_write_data *w = &v->arch.vm_event->write_data;

3. and before proceeding further, the uninitialization sequence _completes_, i.e. this was done in vm_event_cleanup_domain():

      for_each_vcpu ( d, v )
      {
          xfree(v->arch.vm_event);
          v->arch.vm_event = NULL;
      }

4. and then in hvm_do_resume() this gets to be done:

          struct monitor_write_data *w = &v->arch.vm_event->write_data;

          [...]

          if ( w->do_write.msr )
          {
              hvm_msr_write_intercept(w->msr, w->value, 0);
              w->do_write.msr = 0;
          }
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
Quoted 1 line

then you'll have a beautiful NULL-pointer access hypervisor crash.

As I've said before, of course I agree with that

Sorry, I thought by your previous statement "I assume that the reason why this has not been addressed in the past is that introspection applications are expected to be well-behaved" you were inferring that with a 'sane' toolstack user, as you defined it, it won't be possible to have a hypervisor crash.

I did (please see below).

Then, again, you'd be wrong, the hvm_do_resume() was just one example, in a similar fashion this can happen in a number of other places. Details will be given in the patch-series.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

- isn't that what a
previous patch you've submitted addressed?

I'm not sure what patch you're referring to, I don't remember ever addressing these issues before..

I am talking about your patch "x86/monitor: fix: don't compromise a monitor_write_data with pending CR writes", which, if I'm not mistaken, addresses the above problem (since we won't xfree(v->arch.vm_event) on monitor uninit anymore).

Heh, I wouldn't call fixing problem A and by -coincidence- with that fix also having -totally unrelated- problem B go away "addressing problem B". I didn't even mention these locking issues in that patch, so how could I have addressed them there?

Quoted 1 line

With the above problem fixed, the workflow I suggested should be fine.

The fact of the matter remains that the current state of staging (which is what this thread applies to) still has the hvm_do_resume() issue as well as the others.

Quoted 5 lines

Again, I would prefer things to be rock solid, so personally I encourage your patch and hope we get it in.

Thanks,
Razvan

Corneliu.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel