From patchwork Fri Sep 2 14:33:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pranith Kumar X-Patchwork-Id: 9311089 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 188C360756 for ; Fri, 2 Sep 2016 14:33:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0A6B1297B7 for ; Fri, 2 Sep 2016 14:33:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F3258297C0; Fri, 2 Sep 2016 14:33:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 41770297B7 for ; Fri, 2 Sep 2016 14:33:37 +0000 (UTC) Received: from localhost ([::1]:42526 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfpXE-00045E-HU for patchwork-qemu-devel@patchwork.kernel.org; Fri, 02 Sep 2016 10:33:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57022) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfpWz-00044D-ON for qemu-devel@nongnu.org; Fri, 02 Sep 2016 10:33:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bfpWv-0000AW-I4 for qemu-devel@nongnu.org; Fri, 02 Sep 2016 10:33:20 -0400 Received: from mail-yw0-x244.google.com ([2607:f8b0:4002:c05::244]:36549) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfpWv-0000AE-DW for qemu-devel@nongnu.org; Fri, 02 Sep 2016 10:33:17 -0400 Received: by mail-yw0-x244.google.com with SMTP id z124so2621539ywz.3 for ; Fri, 02 Sep 2016 07:33:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:mime-version; bh=LLDy7Kloq5/L4V/rR4eITALaGDxXLFYSx8lUtLKITGw=; b=UmuAyvPAgmLeKLPlmnP/pMCL59VpGfp3mJe+jSchOPIwukvIeGlOqdipCynePncaHN q2bDZkrvRDpx3VYedRnb7X4LxatTTBY7LTFRy0XKHtb8YUvL1jdQS+nDi2NA4cAoUIAL JTV4uP6i4hx0bxNi/Obn3suiI9IoJlhCthMSd8+1aET+1siIIE0rFdkg+3ssNF1wY8sl 4bNCVFP3+TNaXXwMZqnuTi0BQvHFw+S6/avfCbnH73LrX1yYNz5Yb1BcwUCWzXWS1e77 ezAQKiOeKsVhHaUe6F/2fi8yocXZFQMDA1wTeBz14Ruu3sVbjGt+luvsqDuPDrKQUbxD TzDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=LLDy7Kloq5/L4V/rR4eITALaGDxXLFYSx8lUtLKITGw=; b=MFFhJXUfXB3M+oa1g9AKuPTYcxAKcKdHUaOvRlfpExXcvu1t583iDbfovJNdse+wLJ hjZK7dnqii5KMpTr4NrXUf7JUqryphqXphxk4Kqb6VxBgLYEOhAZJvmfD6uRPxoePOF4 0PTmJQgSYVDKA4Rr+453tvLXjzf8urTgaZM4dVuzK1JoxmHdFkh3agwAHF6VTFGkVcc7 mWKz/XbxgbYQPcfXWNp2XtFPu9CimicIz1WmqRHJJYcdxTCziJI/9qCJS4xVCM439d5A K23pBy67IEcmsCq8npFKlLwuCjs0PKNM2k5a1II1OgVTvyv4v+TJu14bIHwdXs504nCq OUuw== X-Gm-Message-State: AE9vXwNluc6pSAflys3R1LFHoa4xo15HvFavhvuyhFVkpFeRb+g/+qYNSF+Y8q5jo4/Fig== X-Received: by 10.129.105.6 with SMTP id e6mr7766065ywc.288.1472826796972; Fri, 02 Sep 2016 07:33:16 -0700 (PDT) Received: from evgadesktop (108-232-152-155.lightspeed.tukrga.sbcglobal.net. [108.232.152.155]) by smtp.gmail.com with ESMTPSA id a68sm4109421ywh.44.2016.09.02.07.33.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Sep 2016 07:33:16 -0700 (PDT) From: Pranith Kumar To: pbonzini@redhat.com, qemu-devel@nongnu.org Date: Fri, 02 Sep 2016 10:33:15 -0400 Message-ID: <87shti74no.fsf@gmail.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4002:c05::244 Subject: [Qemu-devel] aio: reg. smp_read_barrier_depends() in aio_bh_poll() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Hi Paolo, This is in reference to the discussion we had yesterday on IRC. I am trying to understand the need for smp_read_barrier_depends() and how it prevents the following race condition. I think a regular barrier() should suffice instead of smp_read_barrier_depends(). Consider: P0 P1 ---------------------------------------- bh = ctx->first_bh; smp_read_barrier_depends(); // barrier() should be sufficient since bh // is local variable next = bh->next; lock(bh_lock); new_bh->next = ctx->first_bh; smp_wmb(); ctx->first_bh = new_bh; unlock(bh_lock); if (bh) { // do something } Why do you think smp_read_barrier_depends() is necessary here? If bh was a shared variable I would understand, but here bh is local and a regular barrier() would make sure that we are not optimizing the initial load into bh. A patch fixing this follows. Thanks, --- Pranith From: Pranith Kumar Date: Fri, 2 Sep 2016 10:30:23 -0400 Subject: [PATCH] aio: Remove spurious smp_read_barrier_depends() smp_read_barrier_depends() should be used only if you are reading dependent pointers which are shared. Here 'bh' is a local variable and dereferencing it will always be ordered after loading 'bh', i.e., bh->next will always be ordered after fetching bh. However the initial load into 'bh' should not be optimized away, hence we use atomic_read() to ensure this. Signed-off-by: Pranith Kumar --- async.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/async.c b/async.c index 3bca9b0..f4f8b17 100644 --- a/async.c +++ b/async.c @@ -76,9 +76,8 @@ int aio_bh_poll(AioContext *ctx) ctx->walking_bh++; ret = 0; - for (bh = ctx->first_bh; bh; bh = next) { - /* Make sure that fetching bh happens before accessing its members */ - smp_read_barrier_depends(); + for (bh = atomic_read(&ctx->first_bh); bh; bh = next) { + /* store bh->next since bh can be freed in aio_bh_call() */ next = bh->next; /* The atomic_xchg is paired with the one in qemu_bh_schedule. The * implicit memory barrier ensures that the callback sees all writes