From patchwork Mon May 19 05:40:42 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adam Buchbinder X-Patchwork-Id: 4199451 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 95FF29F1CD for ; Mon, 19 May 2014 05:40:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B8B2C20270 for ; Mon, 19 May 2014 05:40:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D8A0F20279 for ; Mon, 19 May 2014 05:40:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752215AbaESFkr (ORCPT ); Mon, 19 May 2014 01:40:47 -0400 Received: from mail-pb0-f41.google.com ([209.85.160.41]:64411 "EHLO mail-pb0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751269AbaESFkp (ORCPT ); Mon, 19 May 2014 01:40:45 -0400 Received: by mail-pb0-f41.google.com with SMTP id uo5so5340509pbc.14 for ; Sun, 18 May 2014 22:40:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=GUuGaVulNLBChzwflkqj0FyYSIwacc77Vc3TskV9Si8=; b=Xf55MH488GxOiLWKus60lQKCPepgEIPZMgaUUnxHcbQPb0s1j9w5bVkROrSgsKo8sN svL+0N2O2ZktduP0ChMhLDCGXnXYAMU/q28GAsoGYiqSlN0HIYI1SAtHU+8MMXO+CxoC zeYMnGcGO8aE23RZpPQV51x/L7Dc2UpZpCiE6utD7l9tHxsoUFuItKz2mKkvNIZGqiG2 J7bgTP2LomW2nGe47o6KdI2zZ4W48Xr7g6nJGYFaRhhmonT/FOdDaRk7oJWTRAWZ9Gpk jxTqmz8IXG6NiNOkV1K3I2Xw9r+bteOGBfOQbeRurtiMKvVR3cQYtiGOkJhSLG/U/l/8 sO2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=GUuGaVulNLBChzwflkqj0FyYSIwacc77Vc3TskV9Si8=; b=AzrCEYHOIMO6/jwTzy7Mn2HLnCaABG5sf9Db805jHlqcTztYkqXJNyQEMS5V0y0baj IHbMpaNbYi4WWvyA/LMW+wb/ZXmFgMY9ksNUntSawlINCkxKhJ2cR64mZdEln2y2uLKP +TDQDCdby9Tv2GNDqiLq/+UQs9VzUqEzIteQ67JKiCF8JjDSIhSFmNMvFtbJDnCZCrK+ At2yPT+sUCJoockkY1EVyTL1SClMeolkWvb2jxk5H1GBAiIDiy6rh3kIgJib7fKlDXBz F4Za/RWg7zDE4DxXgCHEMhQ+vQ17wyt5fNfdVRm80R5yEEWl67871/jOC2cDlnU5Tnbu IaMA== X-Gm-Message-State: ALoCoQkAz2DKK4LfaPt8To6azJ8uTE6vNyuH5nF9v2y28YjPY/54lYQX2ENyn5JiEri1P9zYhGgayLr2QhYbJzk/IHlZmsuvV5C236yiClN8WW5IjTL6cJSoLChWH4FoO7nRi48AobXbQFe576mSA/QvlkvTbPWAXA== X-Received: by 10.66.157.200 with SMTP id wo8mr39804715pab.92.1400478045040; Sun, 18 May 2014 22:40:45 -0700 (PDT) Received: from abuchbinder-glaptop.corp.google.com ([172.19.248.233]) by mx.google.com with ESMTPSA id pb7sm70687538pac.10.2014.05.18.22.40.43 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 18 May 2014 22:40:44 -0700 (PDT) From: Adam Buchbinder To: linux-btrfs@vger.kernel.org Cc: dave@jikos.cz, Adam Buchbinder Subject: [PATCH] btrfs-image: Fix a data race in build_chunk_tree. Date: Sun, 18 May 2014 22:40:42 -0700 Message-Id: <1400478042-19837-1-git-send-email-abuchbinder@google.com> X-Mailer: git-send-email 1.9.1.423.g4596e3a Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP A mdrestore_struct was being written to without its mutex being held. This race was found with ThreadSanitizer; the relevant part of the report looks like this: WARNING: ThreadSanitizer: data race (pid=18828) Write of size 8 at 0x7fffffc3d088 by main thread: #0 build_chunk_tree .../btrfs-progs/btrfs-image.c:2233 #1 __restore_metadump .../btrfs-progs/btrfs-image.c:2294 #2 restore_metadump .../btrfs-progs/btrfs-image.c:2345 #3 main .../btrfs-progs/btrfs-image.c:2545 Previous read of size 8 at 0x7fffffc3d088 by thread T1 (mutexes: write M0): #0 restore_worker .../btrfs-progs/btrfs-image.c:1636 Location is stack of main thread. Mutex M0 created at: #0 pthread_mutex_init ??:0 #1 mdrestore_init .../btrfs-progs/btrfs-image.c:1766 #2 __restore_metadump .../btrfs-progs/btrfs-image.c:2286 #3 restore_metadump .../btrfs-progs/btrfs-image.c:2345 #4 main .../btrfs-progs/btrfs-image.c:2545 Thread T1 (tid=18830, running) created by main thread at: #0 pthread_create ??:0 #1 mdrestore_init .../btrfs-progs/btrfs-image.c:1784 #2 __restore_metadump .../btrfs-progs/btrfs-image.c:2286 #3 restore_metadump .../btrfs-progs/btrfs-image.c:2345 #4 main .../btrfs-progs/btrfs-image.c:2545 --- btrfs-image.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/btrfs-image.c b/btrfs-image.c index cc8627c..017ab1d 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -2228,6 +2228,7 @@ static int build_chunk_tree(struct mdrestore_struct *mdres, buffer = tmp; } + pthread_mutex_lock(&mdres->mutex); super = (struct btrfs_super_block *)buffer; chunk_root_bytenr = btrfs_super_chunk_root(super); mdres->leafsize = btrfs_super_leafsize(super); @@ -2236,6 +2237,7 @@ static int build_chunk_tree(struct mdrestore_struct *mdres, BTRFS_UUID_SIZE); mdres->devid = le64_to_cpu(super->dev_item.devid); free(buffer); + pthread_mutex_unlock(&mdres->mutex); return search_for_chunk_blocks(mdres, chunk_root_bytenr, 0); }