Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3626229imu; Mon, 28 Jan 2019 08:04:06 -0800 (PST) X-Google-Smtp-Source: ALg8bN6lHrXtKQyyQghd7eZ1C1Mqc9vw2ZUB4PtQfgdfC4k6Zm2mlpsRETPqmbyjmS1w+Avf0YH5 X-Received: by 2002:a63:8441:: with SMTP id k62mr20204680pgd.392.1548691446684; Mon, 28 Jan 2019 08:04:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548691446; cv=none; d=google.com; s=arc-20160816; b=OCslaRcIZeS68SShWMMi//gdv4LUlxsLEVZGse1ZnMWsCEq6iPtD61+30exgKEPXQZ seioPK08OvKk93Cswi0NpK1TcEJp5jfABiclrXX7BNkbxfH+13uorgBZ8swk35ceJgKT 9tbia5NKN4Z1RoAj9E0Pssia7M5qzwaneq5W/hr/Yvk3HT8U8gD1jylptLziUtGWGkM5 /SU5FmLlClu09+2fdrjzWO+I6zRL8d1K4fzSMRJjtOJ9N0RwMNTgBp2umNtxjBh4hgEL QLyLjzicsCHB9NQpaqLi1ENfDzOo8Tl6e/tmqhOa0yP08E0ys0yVoKc5Ftr7h4cj0a+s V4NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lLthohCwLm9ZO3lAhqlrPnqGRHMB09z0WZpfmtREarY=; b=UI9Twkn7OOfL3tmf7qAS4GAb6/6l9A4fr8dYSGf5mhFuWY0hNEs+MMSYiawC9AYnWF FAVjnElglGQ7AtGr1UlvWHV0I+wgjQYbDPb0rBwHqbNEvDo2uIAnapJa4rwRgPWrevFR kNErTEV9NA4LNdXmMWl+fTESAmmrn78IkbhZKoqeznml/2Ztaj2Bcm3t9o21ukUgwYKO LA6dWw223WhLOvBX9OvCZrp2X6cO8/OhF3GxLb3dJ3dQyQ5+V4KM9rd3QFAlSZGvnGfi +H2CglPX9TnIHDPRU8xQJ+9mCeWO8Con+botMr8YUUqfENBrXBfrEjJCiGLKp6n7yxrB t0RA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=QHGB+Wwt; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u123si15544254pgb.516.2019.01.28.08.03.50; Mon, 28 Jan 2019 08:04:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=QHGB+Wwt; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730204AbfA1QDF (ORCPT + 99 others); Mon, 28 Jan 2019 11:03:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:48342 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731309AbfA1QDC (ORCPT ); Mon, 28 Jan 2019 11:03:02 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 41C5621852; Mon, 28 Jan 2019 16:03:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548691381; bh=spUt8smuldx0tduaXNgijvQ2OkKzs9lQLvzP5nQRfns=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QHGB+Wwty4tEV5lGog2HY2TnvVgFv5xauAfq/AvlK6TUClc9D48GF1a77JZw4JXEu LNrNDmGV5ZPesvKUWzE6DdRgGzbDu5K29B6FTClO2N5/o8jTTFXMHbR/JG+uaas17X HmnjISy/t3NMnEQ5LzmKf5vdwdEdWSYpQZccIDGU= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Sheng Yong , Jaegeuk Kim , Sasha Levin , linux-f2fs-devel@lists.sourceforge.net Subject: [PATCH AUTOSEL 4.19 074/258] f2fs: fix race between write_checkpoint and write_begin Date: Mon, 28 Jan 2019 10:56:20 -0500 Message-Id: <20190128155924.51521-74-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190128155924.51521-1-sashal@kernel.org> References: <20190128155924.51521-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sheng Yong [ Upstream commit 2866fb16d67992195b0526d19e65acb6640fb87f ] The following race could lead to inconsistent SIT bitmap: Task A Task B ====== ====== f2fs_write_checkpoint block_operations f2fs_lock_all down_write(node_change) down_write(node_write) ... sync ... up_write(node_change) f2fs_file_write_iter set_inode_flag(FI_NO_PREALLOC) ...... f2fs_write_begin(index=0, has inline data) prepare_write_begin __do_map_lock(AIO) => down_read(node_change) f2fs_convert_inline_page => update SIT __do_map_lock(AIO) => up_read(node_change) f2fs_flush_sit_entries <= inconsistent SIT finish write checkpoint sudden-power-off If SPO occurs after checkpoint is finished, SIT bitmap will be set incorrectly. Signed-off-by: Sheng Yong Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/data.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index 11f28342f641..08314fb42652 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -2259,6 +2259,7 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi, bool locked = false; struct extent_info ei = {0,0,0}; int err = 0; + int flag; /* * we already allocated all the blocks, so we don't need to get @@ -2268,9 +2269,15 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi, !is_inode_flag_set(inode, FI_NO_PREALLOC)) return 0; + /* f2fs_lock_op avoids race between write CP and convert_inline_page */ + if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode)) + flag = F2FS_GET_BLOCK_DEFAULT; + else + flag = F2FS_GET_BLOCK_PRE_AIO; + if (f2fs_has_inline_data(inode) || (pos & PAGE_MASK) >= i_size_read(inode)) { - __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); + __do_map_lock(sbi, flag, true); locked = true; } restart: @@ -2308,6 +2315,7 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi, f2fs_put_dnode(&dn); __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true); + WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); locked = true; goto restart; } @@ -2321,7 +2329,7 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi, f2fs_put_dnode(&dn); unlock_out: if (locked) - __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false); + __do_map_lock(sbi, flag, false); return err; } -- 2.19.1