[libvirt] [PATCH] qemu: Resolve agent deadlock

John Ferlan jferlan at redhat.com
Tue Oct 27 14:46:58 UTC 2015



On 10/27/2015 01:00 AM, Peter Krempa wrote:
> On Mon, Oct 26, 2015 at 17:28:57 -0400, John Ferlan wrote:
>>
>>
>> On 10/26/2015 11:37 AM, Peter Krempa wrote:
>>> On Mon, Oct 26, 2015 at 11:06:21 -0400, John Ferlan wrote:
>>>> If we have a shutdown of a VM by a qemu agent while an agent EOF occurs
>>>> at relatively the same time, it's possible that a deadlock occurs depending
>>>> on what happens first.
>>>>
>>>> Assume qemuProcessHandleAgentEOF runs first, with the vm->lock it clears
>>>> priv->agent, then clears the vm->lock.
>>>
>>> Couldn't we make sure, that qemuProcessHandleAgentEOF clears priv->agent
>>> if and only if it removed the last reference?
>>
>> reference to?  agent?  vm?  via?
> 
> Since all of this is refering to the agent I was refering to it too.
> 

Difficult to be 100% certain from just your comment - no context and no
suggestion for a way to handle this. There is a reference on the vm once
an agent is connected...

>> qemuConnectAgent/qemuAgentOpen takes out a reference to the agent. That
>> is un-referenced by qemuAgentClose. The EnterAgent takes out a reference
>> which is un-referenced during ExitAgent. Adding a new reference just for
>> EOF processing doesn't solve the problem.
>>
>> EOF doesn't do an agent-unlock, it does a vm-lock/unlock, and calls
>> qemuAgentClose which will attempt an agent-lock. The agent may be locked
>> by some other thread and it needs to wait until that reference is cleared.
>>
>> Adding some sort of unref-agent check logic in EOF similar to ExitAgent
>> feels like it'll cause other issues. It seems "backwards" to remove the
>> last reference and avoid everything else that qemuCloseAgent does.
>>
>> I think logically if some sort of unref-agent check logic was added,
>> then qemuCloseAgent could not be called from either EOF or ExitAgent.
>> >From EOF, if the current thread was the last one, then the agent is
>> free'd so qemuCloseAgent shouldn't be called. If the current thread
>> wasn't the last one, then we'd have to wait for the last reference check
>> in ExitAgent, but once that is done, the agent is freed and thus
>> qemuCloseAgent shouldn't be called.
> 
> I wanted to point out that since we do have the 'EnterAgent' and
> 'ExitAgent' helpers, they should be used to do any kind of logic
> required to do the locking and it should not be necessary at any point
> to copy the pointer to priv->agent. Otherwise it creates a really ugly
> usage pattern which requires a separate variable and basically defeats
> the Enter/Exit pattern we use anywhere else.
> 
> Doing so probably will increase the complexity of either the helpers or
> the closing function, but the complexity will not be exposed in a
> repeated pattern through the code. Since we already have the helpers, we
> should use them and not clutter the rest of the code.
> 

Fair enough... I guess I find it "odd" to access a structure after we've
removed the lock on it, while other code/threads can modify that field.
Since the only access is the one thing that was locked/reffed (eg
priv->agent or in the Enter/Exit Monitor cases priv->mon), then for
99.9% of the time it's fine. The 0.1% is a timing window where
priv->agent (or priv->mon) could be set to NULL by a EOF some time after
EnterAgent has increased the ref on priv->agent.

Looking at the ExitMonitor code compared to the ExitAgent code - it
would seem logically ExitMonitor could have the same issue - that is
priv->mon could be set to NULL in it's EOF path during qemuProcessStop,
but that code is headed towards qemuProcessKill and there's many more
instructions that need to be executed before perhaps an ExitMonitor
would run in a separate thread. NB: I had only a brief look at the
ExitMonitor/EOFMonitor logic. If somehow the EOF thread gets all the way
to the "if (priv->mon) qemuMonitorClose(priv->mon); priv->mon = NULL;"
code before the ExitMonitor thread can ObjectUnref(priv->mon), then it
seems we'd have a similar scenario (unlikely, but possible).

> One other option worth checking is moving the stuff happening in
> qemuAgentClose into the destructor for the agent object
> (qemuAgentDispose) which would then auto-call it in the case where
> you remove the last reference. I didn't check thoroughly enough though
> to see whether it's possible.
> 

That seems risky, IMO... It also doesn't solve the issue where the vm's
priv->agent gets set to NULL by the EOF code and the ExitAgent code
cannot Unlock.

So if the problem is that EOF clears priv->agent before calling
qemuAgentClose(), then what happens if we remove (or move) that
clearing? Well, qemuAgentClose will attempt to get the agent-lock, but
needs to wait for the unlock. When ExitAgent is active it executes it's
Unref and Unlock, but will never have the case of !hasRefs. Once it
unlocks the agent, qemuAgentClose code does it's thing and upon return
could set priv->agent = NULL (inside a vm-lock/unlock). However, that
leaves a window where the agent is free'd, but priv->agent isn't NULL.
That's a problem during qemuDomainSetVcpusFlags where the agent is
accessed twice without checking the second if the agent was still
available. Although that's resolvable with another AgentAvailable check
and perhaps some logic to ascertain during EOF if we were in an inAgent
Enter/Exit window.

John




More information about the libvir-list mailing list