Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp414099pxb; Wed, 18 Nov 2020 07:42:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJwTuWGOC3pwhNPrUgT0ZsxY6ZlCJY+ZdYoBS2FLoVWRbvHKyiySSLOR5nOLbcxfObuSIomd X-Received: by 2002:a17:906:9391:: with SMTP id l17mr2269387ejx.22.1605714166973; Wed, 18 Nov 2020 07:42:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605714166; cv=none; d=google.com; s=arc-20160816; b=WbypJzYogtpvOgi9HPRqUHr2k6YMda6RoOJDvsAN5OEEMebaao7QHpHPKSHCdEAsqi m1cMvWg5tJ7CdjSpGkc0zXpcMrGOx9DQ3D7Mal1VOUR3OlB0mwhtGBFrvaV2L0zVRpNk ZpwJcs5Em4phPucUKg2Rt+UrdbjhiAZo475v4s0RE060Nb4DqoSlTXX9WxDg597HwgX2 ZDAnEyu/3I/v4hU+tWLhnd80Govs9/p71nQ6AgZF7iYeRk/F2J7pgCNULvd+zw9UImzm nD+6pJ1DMAaCpdIXs2WV4FSIg/8lnPH2+yLko33KbYRIV0z46YAnTS4JwsB0lDE1RC+A 4yZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=aTfARycfPe5TMTx5lZg7EVRgT2qvCg6B5B2Q6/C3RLw=; b=X3F1in/eH6wwCRaIM86YhLWDb5SdVygjzj3G7mOvfJdBHImoFT5OanbKREDxK7TvYH DxXnx7vcPwP9RnRT5ezoBDAFcO40WXv6KlRmVjN5dIFaNucvJAAdq/IwnSFO2tgDUjaD mofvTytTXgIsX9aPmmXhsWhthOdtwHXQcJeLXRhCr4WtNFxwYk6Gn/TyH00FLapwIBLF oBzmnrZQe4dT7dR2JsaGuYYG6y6557h4gkmUPHVq29XzLkDgx3MeNTPdr3zJpqrKRya3 G/kpY+EV1LsmRVORYlYz4a0w94RWhRYO9y5dacQFaj2O0VGiFf09rm1po2K689IEv8UU Csmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ThZjbXk5; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qn24si15413723ejb.680.2020.11.18.07.42.23; Wed, 18 Nov 2020 07:42:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ThZjbXk5; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726311AbgKRPmN (ORCPT + 99 others); Wed, 18 Nov 2020 10:42:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727460AbgKRPmJ (ORCPT ); Wed, 18 Nov 2020 10:42:09 -0500 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA231C061A48 for ; Wed, 18 Nov 2020 07:42:08 -0800 (PST) Received: by mail-qk1-x749.google.com with SMTP id b191so1759924qkc.10 for ; Wed, 18 Nov 2020 07:42:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=aTfARycfPe5TMTx5lZg7EVRgT2qvCg6B5B2Q6/C3RLw=; b=ThZjbXk50/HkJw0Dg8BJQRrRhAjwDm8m7vW/Bl9MifeXkHxoGoPZb6v48Ldpfo2lL6 tr9DImth9kDbSYpEaTPgAnQ4HmUdHYMneefd0DUlsd/3FJ75wiaLSSRYqeh0df2TtOY4 15x4VqmFSAWj3wtDKcnzvUVrA/ksH/b+xGafQuK9maq3DCuS2nydh1abQl4/l4e5MUZ3 q5M4PJH50cvISlxvI/3gQaO93O3X/tzPogwunG7j9AyIUJsfNLWTmZk8b0nlDd0M1+3B sPTnTP2qAfQIpIf/T1lIBvbJNZOrGsM4N56NDGkIkG6JBYdyqJZ6a2vgwwmKAehYFphB n3Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=aTfARycfPe5TMTx5lZg7EVRgT2qvCg6B5B2Q6/C3RLw=; b=BIpcH77oLrRxiXJvSGHrO4mIZJ+lgqFzyxJWEDA1pExmIuhNxEd0O9+oAOJSewHa9M w7t0DV4ateQRX6Zujxqd+8kKyMT/sfjvYWs6FceuHrTHxNRqRrvmQrpVPQK0xdu1ZhMG kY5rMvQn51ceELHri5WyU7UEw3VolgL/nlb/TwWueA+uRYxzDM2rnTfw0GZAJOOaAvEQ cfAax+zmXev/iaXXURxHKM4ccVSXLYxWAaeaxotfpznD3vKM7ef0vJ7gZfFYUlpuSmYN d1C5ppAfrVhG32kDSfOBEX0XJB1HJdvAwOgj+ZBrOgLQ2uXIQkv/cU56/nkblt1N4xg2 E+ew== X-Gm-Message-State: AOAM533NnRlQNy2UrFuEoWn9+0hUYsUd+yPkffez8h0o0u8NSSFmH34a B0ixr+DEpVo6RmQAYmiL/9jYzG6q5F/sKAuXHPN/u/ZZDuRurX5+uzeroOmRJn3O+eANaR1cuc1 YCB0VkD18eJtgmsx3PgzUwXBGl62KJg26Q1ApzuFXsdL2eZsj4kWbPB7x9XB+1NXQfPoFu3yKxY 8iUhJpcSo= Sender: "saranyamohan via sendgmr" X-Received: from saranyamohan.svl.corp.google.com ([100.116.76.178]) (user=saranyamohan job=sendgmr) by 2002:a0c:f9c8:: with SMTP id j8mr5220496qvo.17.1605714127986; Wed, 18 Nov 2020 07:42:07 -0800 (PST) Date: Wed, 18 Nov 2020 07:39:40 -0800 In-Reply-To: <20201118153947.3394530-1-saranyamohan@google.com> Message-Id: <20201118153947.3394530-55-saranyamohan@google.com> Mime-Version: 1.0 References: <20201118153947.3394530-1-saranyamohan@google.com> X-Mailer: git-send-email 2.29.2.299.gdc1121823c-goog Subject: [RFC PATCH v3 54/61] e2fsck: fix race in ext2fs_read_bitmaps() From: Saranya Muruganandam To: linux-ext4@vger.kernel.org, tytso@mit.edu Cc: adilger.kernel@dilger.ca, Wang Shilong , Saranya Muruganandam Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Wang Shilong During corruption testing hiting following segfault: Multiple threads triggered to read bitmaps Signal (11) SIGSEGV si_code=SEGV_MAPERR fault addr=0x200 ./e2fsck[0x4382ae] /lib64/libpthread.so.0(+0x14b20)[0x7f5854d2fb20] ./e2fsck(ext2fs_rb_insert_color+0xc)[0x46ac0c] ./e2fsck[0x467bb4] ./e2fsck[0x467e6d] ./e2fsck[0x45ba95] ./e2fsck[0x45c124] /lib64/libpthread.so.0(+0x94e2)[0x7f5854d244e2] /lib64/libc.so.6(clone+0x43)[0x7f5854beb6c3] Problem is @block_map might be set NULL if one of thread exit, move such kind of cleanup operation to main thread after all threads exit. Another potential problem is e2fsck_read_bitmap() could be called during pass1, this need be serialized, serialize it in the pass1. Signed-off-by: Wang Shilong Signed-off-by: Saranya Muruganandam --- e2fsck/pass1.c | 2 +- lib/ext2fs/rw_bitmaps.c | 25 ++++++++----------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/e2fsck/pass1.c b/e2fsck/pass1.c index e98cda9f..3899d710 100644 --- a/e2fsck/pass1.c +++ b/e2fsck/pass1.c @@ -4183,9 +4183,9 @@ report_problem: } continue; } + e2fsck_pass1_fix_lock(ctx); e2fsck_read_bitmaps(ctx); pb->inode_modified = 1; - e2fsck_pass1_fix_lock(ctx); pctx->errcode = ext2fs_extent_delete(ehandle, 0); e2fsck_pass1_fix_unlock(ctx); diff --git a/lib/ext2fs/rw_bitmaps.c b/lib/ext2fs/rw_bitmaps.c index 95de9b1c..eb791202 100644 --- a/lib/ext2fs/rw_bitmaps.c +++ b/lib/ext2fs/rw_bitmaps.c @@ -445,14 +445,6 @@ success_cleanup: return 0; cleanup: - if (do_block) { - ext2fs_free_block_bitmap(fs->block_map); - fs->block_map = 0; - } - if (do_inode) { - ext2fs_free_inode_bitmap(fs->inode_map); - fs->inode_map = 0; - } if (inode_bitmap) ext2fs_free_mem(&inode_bitmap); if (block_bitmap) @@ -463,9 +455,12 @@ cleanup: } -static errcode_t read_bitmaps_range_end(ext2_filsys fs, int do_inode, int do_block) +static errcode_t read_bitmaps_range_end(ext2_filsys fs, int do_inode, int do_block, + errcode_t retval) { - errcode_t retval = 0; + + if (retval) + goto cleanup; /* Mark group blocks for any BLOCK_UNINIT groups */ if (do_block) { @@ -474,7 +469,7 @@ static errcode_t read_bitmaps_range_end(ext2_filsys fs, int do_inode, int do_blo goto cleanup; } - return retval; + return 0; cleanup: if (do_block) { ext2fs_free_block_bitmap(fs->block_map); @@ -497,10 +492,8 @@ static errcode_t read_bitmaps_range(ext2_filsys fs, int do_inode, int do_block, return retval; retval = read_bitmaps_range_start(fs, do_inode, do_block, start, end, NULL, NULL); - if (retval) - return retval; - return read_bitmaps_range_end(fs, do_inode, do_block); + return read_bitmaps_range_end(fs, do_inode, do_block, retval); } #ifdef CONFIG_PFSCK @@ -636,9 +629,7 @@ out: free(thread_infos); free(thread_ids); - if (!retval) - retval = read_bitmaps_range_end(fs, do_inode, do_block); - + retval = read_bitmaps_range_end(fs, do_inode, do_block, retval); if (!retval) { if (do_inode) fs->flags &= ~EXT2_FLAG_IBITMAP_TAIL_PROBLEM; -- 2.29.2.299.gdc1121823c-goog