From patchwork Wed Jul 12 20:05:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adam Buchbinder X-Patchwork-Id: 9837351 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 BB10160393 for ; Wed, 12 Jul 2017 20:05:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ADC33286AA for ; Wed, 12 Jul 2017 20:05:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A27FF286AE; Wed, 12 Jul 2017 20:05:52 +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, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3D489286AA for ; Wed, 12 Jul 2017 20:05:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752337AbdGLUFu (ORCPT ); Wed, 12 Jul 2017 16:05:50 -0400 Received: from mail-pf0-f175.google.com ([209.85.192.175]:35140 "EHLO mail-pf0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751161AbdGLUFt (ORCPT ); Wed, 12 Jul 2017 16:05:49 -0400 Received: by mail-pf0-f175.google.com with SMTP id c73so17986945pfk.2 for ; Wed, 12 Jul 2017 13:05:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=9pAWouHASJCsKeckUKKUqrwlvx2N1R4Z1XjyXHnLayA=; b=nezQZ1M6QZD2BpofbEiv+ao2wlgS8624dBl0hu9HJ04ZFRdAxRa42sI6XwuU4P1pFc Spza33Aj+jM0YjgvlavvFuM6KOX8LQZdlNO1W+HJOsrzUCm0rY2C6qC9/rGfrQeUu/6j wOyhfI0eLUGdgGpEOd/CdxYJNAW+cHj8+N2PsKkl/PbdgwZXq0lz9eHkSUzvK/E5THLB EuNtYYwmxg1KQPjLPW6upB7GCNIZD37rzWbfBWllSGnMVTzOKWua4aRA2dhaL7ZBi2sx UrnMoZ9Rg4So1JlfRQOfEqh9KW5fJ9LOyHrrk+CvQZ+J0IbANpNMcdMHtRkWk3LbVnI5 hKhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=9pAWouHASJCsKeckUKKUqrwlvx2N1R4Z1XjyXHnLayA=; b=XKiIZ6mOoCczOrSPZ0UbSSyuGSj469JVboXONp6OjBHoAvqOXCSFv2Ba/euHbd/zRC g2iYgfcrdQTRzIUo8rBewHstT9zl8ElYkR5Swu/zZuA3oM48khp1R1iK1bczCgg13VMg 6dvH/UhaQOVFGmrreKJ+FfY1qmoe2MzdkIeGesvrYxG7bT7V6BAsBzsFl3ksMp3fM+uD jQpD9F2JraYQZTuUCU22FuY1zCjNhnpNfHI+ud6LKwOl9TTwwsW5yGg//btFPKk5sEn2 Mh7Kqj0RdjVMy4q4ATo2n7YNDwiVtVxXv1cJi00YcUzEyqNRubHjeNM+c/+cSRJzMV5e jFsA== X-Gm-Message-State: AIVw110oBSnwLeOTuutNix3BAhxHusaNTcteibXw/MUs+YRx+c8HJdxF +0xyEdXRAGd1TgqxFJYW0Q== X-Received: by 10.98.215.73 with SMTP id v9mr57751091pfl.103.1499889948001; Wed, 12 Jul 2017 13:05:48 -0700 (PDT) Received: from abuchbinder-glaptop.corp.google.com (c-73-223-36-121.hsd1.ca.comcast.net. [73.223.36.121]) by smtp.gmail.com with ESMTPSA id 8sm7027341pfs.23.2017.07.12.13.05.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 12 Jul 2017 13:05:47 -0700 (PDT) From: Adam Buchbinder To: linux-btrfs@vger.kernel.org Cc: Adam Buchbinder Subject: [PATCH] btrfs-progs: Fix data race in btrfs-convert. Date: Wed, 12 Jul 2017 13:05:38 -0700 Message-Id: <20170712200538.18881-1-abuchbinder@google.com> X-Mailer: git-send-email 2.13.2.932.g7449e964c-goog Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The status display was reading the state while the task was updating it. Use a mutex to prevent the race. This race was detected using ThreadSanitizer and misc-tests/005-convert-progress-thread-crash. ================== WARNING: ThreadSanitizer: data race Write of size 8 by main thread: #0 ext2_copy_inodes btrfs-progs/convert/source-ext2.c:853 #1 copy_inodes btrfs-progs/convert/main.c:145 #2 do_convert btrfs-progs/convert/main.c:1297 #3 main btrfs-progs/convert/main.c:1924 Previous read of size 8 by thread T1: #0 print_copied_inodes btrfs-progs/convert/main.c:124 Location is stack of main thread. Thread T1 (running) created by main thread at: #0 pthread_create #1 task_start btrfs-progs/task-utils.c:50 #2 do_convert btrfs-progs/convert/main.c:1295 #3 main btrfs-progs/convert/main.c:1924 SUMMARY: ThreadSanitizer: data race btrfs-progs/convert/source-ext2.c:853 in ext2_copy_inodes Signed-off-by: Adam Buchbinder --- convert/main.c | 12 ++++++++++-- convert/source-ext2.c | 3 +++ convert/source-fs.h | 3 +++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/convert/main.c b/convert/main.c index c56382e..c9c1fd4 100644 --- a/convert/main.c +++ b/convert/main.c @@ -88,6 +88,7 @@ #include #include #include +#include #include #include "ctree.h" @@ -119,10 +120,12 @@ static void *print_copied_inodes(void *p) task_period_start(priv->info, 1000 /* 1s */); while (1) { count++; + pthread_mutex_lock(&priv->mutex); printf("copy inodes [%c] [%10llu/%10llu]\r", work_indicator[count % 4], - (unsigned long long)priv->cur_copy_inodes, - (unsigned long long)priv->max_copy_inodes); + (u64)priv->cur_copy_inodes, + (u64)priv->max_copy_inodes); + pthread_mutex_unlock(&priv->mutex); fflush(stdout); task_period_wait(priv->info); } @@ -1286,6 +1289,11 @@ static int do_convert(const char *devname, u32 convert_flags, u32 nodesize, } printf("creating btrfs metadata"); + ret = pthread_mutex_init(&ctx.mutex, NULL); + if (ret) { + error("failed to init mutex: %d", ret); + goto fail; + } ctx.max_copy_inodes = (cctx.inodes_count - cctx.free_inodes_count); ctx.cur_copy_inodes = 0; diff --git a/convert/source-ext2.c b/convert/source-ext2.c index 38c3cd3..4bce4b3 100644 --- a/convert/source-ext2.c +++ b/convert/source-ext2.c @@ -18,6 +18,7 @@ #include "kerncompat.h" #include +#include #include "disk-io.h" #include "transaction.h" #include "utils.h" @@ -850,7 +851,9 @@ static int ext2_copy_inodes(struct btrfs_convert_context *cctx, ret = ext2_copy_single_inode(trans, root, objectid, ext2_fs, ext2_ino, &ext2_inode, convert_flags); + pthread_mutex_lock(&p->mutex); p->cur_copy_inodes++; + pthread_mutex_unlock(&p->mutex); if (ret) return ret; if (trans->blocks_used >= 4096) { diff --git a/convert/source-fs.h b/convert/source-fs.h index ca32d15..7ae6edd 100644 --- a/convert/source-fs.h +++ b/convert/source-fs.h @@ -17,6 +17,8 @@ #ifndef __BTRFS_CONVERT_SOURCE_FS_H__ #define __BTRFS_CONVERT_SOURCE_FS_H__ +#include + #include "kerncompat.h" #define CONV_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID @@ -37,6 +39,7 @@ extern const struct simple_range btrfs_reserved_ranges[3]; struct task_info; struct task_ctx { + pthread_mutex_t mutex; u64 max_copy_inodes; u64 cur_copy_inodes; struct task_info *info;