From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C4DABC433F5 for ; Wed, 1 Jun 2022 13:33:25 +0000 (UTC) Received: from localhost ([::1]:58780 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nwOTU-0002hn-Kj for qemu-devel@archiver.kernel.org; Wed, 01 Jun 2022 09:33:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34166) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nwOPS-0000Q2-Nz for qemu-devel@nongnu.org; Wed, 01 Jun 2022 09:29:14 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:47510) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nwOPO-0000ec-VC for qemu-devel@nongnu.org; Wed, 01 Jun 2022 09:29:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654090149; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8Z+b3FAmn8oPv1ij7VRlLlO2ybOemGY/YduHNSU7g7I=; b=SmuszuZhI8aai2aIBNyfOjjE92yTG7hdCTtGC7nivASp/n26YkSzLpl7t/hyXN13JjQIQs Enw89XRF3zDi8+wMucRlu/bxeEZQcUTr7wa57ygdjHjyZ1jYDgsR6eB0PggrjYA0aS8LKh bG3iDQKZDFVfinLAyjcYslOVvjfNK4A= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-617-nzIV7WwiNp6rsxDERcLM6g-1; Wed, 01 Jun 2022 09:29:08 -0400 X-MC-Unique: nzIV7WwiNp6rsxDERcLM6g-1 Received: by mail-wm1-f70.google.com with SMTP id i30-20020a1c541e000000b0039c1bf9739fso905786wmb.6 for ; Wed, 01 Jun 2022 06:29:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=8Z+b3FAmn8oPv1ij7VRlLlO2ybOemGY/YduHNSU7g7I=; b=p81wPo0Lyx823tYEqFe8Gn/HtCC+YDAmW6VZvUhs/8hWfTmVnsRGSHa80BZFnWCtdI 2WxmODXMAWTysI7rYNjDRG1biR42inVsqA96eBb/DmWUP065bj+uPz+4D+EdlWRl8AoR 57fpKZgTvk0r1q96YH9+xqa4X/PQG8BciKQPNy/UmCV5qYziBWz3ARpmMQQbQSYihWZm 3Ww3utPEf7l+Fd3KZ72ECoznEpgNWAsiS326D07+fEcxTiLT1MEGvJzdgve6EfUhu5Ms Biypt31GMCYMQl+j0s0TdBE20yY4OTYb0Nd6YzYVavQaxkBkeiHCWE6kOIjK5qZtooY0 1tIw== X-Gm-Message-State: AOAM533ZWTEFbBlABY5ssvm1+4jEUI7pMn4l29HpoSu1ro4hS7NLr3CX 6ElYVARuHgN5/6Yk8yVWyVs5l/ZpraBGUuZte/P20dP7xs8en+HAR/NG1Dp8CsndJ6eGPw5Ffy5 XKebnkOTqeY1Jc80= X-Received: by 2002:adf:fc01:0:b0:20c:ff9a:2c53 with SMTP id i1-20020adffc01000000b0020cff9a2c53mr53790062wrr.142.1654090147286; Wed, 01 Jun 2022 06:29:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6C0vJsg6TmMOLzeL6V6hYJZdLVt5LVckkkZtOF8UWFHKL/RnixSdmvuq2poLuwpXN4q9PBw== X-Received: by 2002:adf:fc01:0:b0:20c:ff9a:2c53 with SMTP id i1-20020adffc01000000b0020cff9a2c53mr53790043wrr.142.1654090146996; Wed, 01 Jun 2022 06:29:06 -0700 (PDT) Received: from ?IPV6:2003:cb:c705:2600:951d:63df:c091:3b45? (p200300cbc7052600951d63dfc0913b45.dip0.t-ipconnect.de. [2003:cb:c705:2600:951d:63df:c091:3b45]) by smtp.gmail.com with ESMTPSA id 184-20020a1c02c1000000b0039482d95ab7sm2129791wmc.24.2022.06.01.06.29.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Jun 2022 06:29:06 -0700 (PDT) Message-ID: Date: Wed, 1 Jun 2022 15:29:05 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [RESEND PATCH] hw/dma: fix crash caused by race condition Content-Language: en-US To: Stefan Hajnoczi Cc: Tong Zhang , Tong Zhang , Paolo Bonzini , Peter Xu , =?UTF-8?Q?Philippe_Mathieu-Daud=c3=a9?= , "qemu-devel@nongnu.org" , Francisco Londono References: <20220427205056.2522-1-t.zhang2@samsung.com> <0b54d6c7-f56d-1ad2-80b7-d75d1033d67e@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=170.10.129.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" On 01.06.22 15:24, Stefan Hajnoczi wrote: > On Wed, Jun 01, 2022 at 10:00:50AM +0200, David Hildenbrand wrote: >> On 01.06.22 02:20, Tong Zhang wrote: >>> Hi David, >>> >>> On Mon, May 30, 2022 at 9:19 AM David Hildenbrand wrote: >>>> >>>> On 27.04.22 22:51, Tong Zhang wrote: >>>>> assert(dbs->acb) is meant to check the return value of io_func per >>>>> documented in commit 6bee44ea34 ("dma: the passed io_func does not >>>>> return NULL"). However, there is a chance that after calling >>>>> aio_context_release(dbs->ctx); the dma_blk_cb function is called before >>>>> the assertion and dbs->acb is set to NULL again at line 121. Thus when >>>>> we run assert at line 181 it will fail. >>>>> >>>>> softmmu/dma-helpers.c:181: dma_blk_cb: Assertion `dbs->acb' failed. >>>>> >>>>> Reported-by: Francisco Londono >>>>> Signed-off-by: Tong Zhang >>>>> --- >>>>> softmmu/dma-helpers.c | 2 +- >>>>> 1 file changed, 1 insertion(+), 1 deletion(-) >>>>> >>>>> diff --git a/softmmu/dma-helpers.c b/softmmu/dma-helpers.c >>>>> index 7820fec54c..cb81017928 100644 >>>>> --- a/softmmu/dma-helpers.c >>>>> +++ b/softmmu/dma-helpers.c >>>>> @@ -177,8 +177,8 @@ static void dma_blk_cb(void *opaque, int ret) >>>>> aio_context_acquire(dbs->ctx); >>>>> dbs->acb = dbs->io_func(dbs->offset, &dbs->iov, >>>>> dma_blk_cb, dbs, dbs->io_func_opaque); >>>>> - aio_context_release(dbs->ctx); >>>>> assert(dbs->acb); >>>>> + aio_context_release(dbs->ctx); >>>>> } >>>>> >>>>> static void dma_aio_cancel(BlockAIOCB *acb) >>>> >>>> I'm fairly new to that code, but I wonder what prevents dma_blk_cb() to >>>> run after you reshuffled the code? >>>> >>> >>> IMO if the assert is to test whether io_func returns a non-NULL value >>> shouldn't it be immediately after calling io_func. >>> Also... as suggested by commit 6bee44ea346aed24e12d525daf10542d695508db >>> > dma: the passed io_func does not return NULL >> >> Yes, but I just don't see how it would fix the assertion you document in >> the patch description. The locking change to fix the assertion doesn't >> make any sense to me, and most probably I am missing something important :) > > The other thread will invoke dma_blk_cb(), which modifies dbs->acb, when > it can take the lock. Therefore dbs->acb may contain a value different > from our io_func()'s return value by the time we perform the assertion > check (that's the race). > > This patch makes sense to me. Can you rephrase your concern? The locking is around dbs->io_func(). aio_context_acquire(dbs->ctx); dbs->acb = dbs->io_func() aio_context_release(dbs->ctx); So where exactly would the lock that's now still held stop someone from modifying dbs->acb = NULL at the beginning of the function, which seems to be not protected by that lock? Maybe I'm missing some locking magic due to the lock being a recursive lock. -- Thanks, David / dhildenb