Re: [Xen-devel] [PATCH v2 1/2] xenbus: req->body should be updated before req->state

From: dongli . zhang
Date: Tue Mar 03 2020 - 15:36:24 EST




On 3/3/20 11:37 AM, Julien Grall wrote:
> Hi,
>
> On 03/03/2020 18:47, Dongli Zhang wrote:
>> The req->body should be updated before req->state is updated and the
>> order should be guaranteed by a barrier.
>>
>> Otherwise, read_reply() might return req->body = NULL.
>>
>> Below is sample callstack when the issue is reproduced on purpose by
>> reordering the updates of req->body and req->state and adding delay in
>> code between updates of req->state and req->body.
>>
>> [ÂÂ 22.356105] general protection fault: 0000 [#1] SMP PTI
>> [ÂÂ 22.361185] CPU: 2 PID: 52 Comm: xenwatch Not tainted 5.5.0xen+ #6
>> [ÂÂ 22.366727] Hardware name: Xen HVM domU, BIOS ...
>> [ÂÂ 22.372245] RIP: 0010:_parse_integer_fixup_radix+0x6/0x60
>> ... ...
>> [ÂÂ 22.392163] RSP: 0018:ffffb2d64023fdf0 EFLAGS: 00010246
>> [ÂÂ 22.395933] RAX: 0000000000000000 RBX: 75746e7562755f6d RCX: 0000000000000000
>> [ÂÂ 22.400871] RDX: 0000000000000000 RSI: ffffb2d64023fdfc RDI: 75746e7562755f6d
>> [ÂÂ 22.405874] RBP: 0000000000000000 R08: 00000000000001e8 R09: 0000000000cdcdcd
>> [ÂÂ 22.410945] R10: ffffb2d6402ffe00 R11: ffff9d95395eaeb0 R12: ffff9d9535935000
>> [ÂÂ 22.417613] R13: ffff9d9526d4a000 R14: ffff9d9526f4f340 R15: ffff9d9537654000
>> [ÂÂ 22.423726] FS:Â 0000000000000000(0000) GS:ffff9d953bc80000(0000)
>> knlGS:0000000000000000
>> [ÂÂ 22.429898] CS:Â 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>> [ÂÂ 22.434342] CR2: 000000c4206a9000 CR3: 00000001ea3fc002 CR4: 00000000001606e0
>> [ÂÂ 22.439645] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
>> [ÂÂ 22.444941] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
>> [ÂÂ 22.450342] Call Trace:
>> [ÂÂ 22.452509]Â simple_strtoull+0x27/0x70
>> [ÂÂ 22.455572]Â xenbus_transaction_start+0x31/0x50
>> [ÂÂ 22.459104]Â netback_changed+0x76c/0xcc1 [xen_netfront]
>> [ÂÂ 22.463279]Â ? find_watch+0x40/0x40
>> [ÂÂ 22.466156]Â xenwatch_thread+0xb4/0x150
>> [ÂÂ 22.469309]Â ? wait_woken+0x80/0x80
>> [ÂÂ 22.472198]Â kthread+0x10e/0x130
>> [ÂÂ 22.474925]Â ? kthread_park+0x80/0x80
>> [ÂÂ 22.477946]Â ret_from_fork+0x35/0x40
>> [ÂÂ 22.480968] Modules linked in: xen_kbdfront xen_fbfront(+) xen_netfront
>> xen_blkfront
>> [ÂÂ 22.486783] ---[ end trace a9222030a747c3f7 ]---
>> [ÂÂ 22.490424] RIP: 0010:_parse_integer_fixup_radix+0x6/0x60
>>
>> The barrier() in test_reply() is changed to virt_rmb(). The "while" is
>> changed to "do while" so that test_reply() is used as a read memory
>> barrier.
>>
>> Signed-off-by: Dongli Zhang <dongli.zhang@xxxxxxxxxx>
>> ---
>> Changed since v1:
>> ÂÂ - change "barrier()" to "virt_rmb()" in test_reply()
>>
>> Â drivers/xen/xenbus/xenbus_comms.c |Â 2 ++
>> Â drivers/xen/xenbus/xenbus_xs.cÂÂÂ | 11 +++++++----
>> Â 2 files changed, 9 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/xen/xenbus/xenbus_comms.c
>> b/drivers/xen/xenbus/xenbus_comms.c
>> index d239fc3c5e3d..852ed161fc2a 100644
>> --- a/drivers/xen/xenbus/xenbus_comms.c
>> +++ b/drivers/xen/xenbus/xenbus_comms.c
>> @@ -313,6 +313,8 @@ static int process_msg(void)
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ req->msg.type = state.msg.type;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ req->msg.len = state.msg.len;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ req->body = state.body;
>> +ÂÂÂÂÂÂÂÂÂÂÂ /* write body, then update state */
>> +ÂÂÂÂÂÂÂÂÂÂÂ virt_wmb();
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ req->state = xb_req_state_got_reply;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ req->cb(req);
>> ÂÂÂÂÂÂÂÂÂ } else
>> diff --git a/drivers/xen/xenbus/xenbus_xs.c b/drivers/xen/xenbus/xenbus_xs.c
>> index ddc18da61834..1e14c2118861 100644
>> --- a/drivers/xen/xenbus/xenbus_xs.c
>> +++ b/drivers/xen/xenbus/xenbus_xs.c
>> @@ -194,15 +194,18 @@ static bool test_reply(struct xb_req_data *req)
>> ÂÂÂÂÂ if (req->state == xb_req_state_got_reply || !xenbus_ok())
>> ÂÂÂÂÂÂÂÂÂ return true;
>> Â -ÂÂÂ /* Make sure to reread req->state each time. */
>> -ÂÂÂ barrier();
>> +ÂÂÂ /*
>> +ÂÂÂÂ * read req->state before other fields of struct xb_req_data
>> +ÂÂÂÂ * in the caller of test_reply(), e.g., read_reply()
>> +ÂÂÂÂ */
>> +ÂÂÂ virt_rmb();
>
> Looking at the code again, I am afraid the barrier only happen in the false
> case. Should not the new barrier added in the 'true' case?

I would leave the original "barrier()" in the 'false' case and add the new
barrier only in the 'true' case.

Thank you very much!

Dongli Zhang