Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2767426imj; Mon, 11 Feb 2019 08:10:22 -0800 (PST) X-Google-Smtp-Source: AHgI3IZEWnbQ7m2QiEU97c0xbQmdoK5+phFrsWzVAY9XypLvJxWOcJdr6eDm1KO+2poMphD/MBI3 X-Received: by 2002:a63:b447:: with SMTP id n7mr7279318pgu.401.1549901422829; Mon, 11 Feb 2019 08:10:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549901422; cv=none; d=google.com; s=arc-20160816; b=LPKGq0DTvN/pJFKNQLYFO/VF6HMN6uRG/OqZ3tjLTqpqG9mnhTfPUlXvJi+zNsIXM6 Uto3hh/o9ltKWyd/+Uuf6pbSql1xGBGDWmcjoJN5Ha1UggJsPQdFKVeLeW57ob13UryJ XEVQD32ue61ZwEGIvgDdYrJkFntdTEM10ecaOGQbHFlYgUFuL+TTIdes3Yb3/D1PB5V0 5rT1sRCW+jFst2U4XEMa6WZaWbTCJB+UFhpqYTHKFGZbACkOmJF3qYzrKpYcCRHdjtcv Ei/OlTmakJpqpSywKtv3r1acpNGsMzviW5Mlnk0kaHQMf/LHtSt7xH/L+RCDUXTWHKpE KoIQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tr8Y9o2McmzIWUyXWALVmE1yD8zDKuGLJ+K/sF3wQ78=; b=Dn6BFcpq6PdQiHBKkPYOsg4fUR50elVdZmpulRCjiRIB5VTgfmZl7pE4xE0FL5vyGO n2gUk20P+a2UzBfukEasgZLMxCUAJpQVbiIsPUCnUS4xVlqn3BwGvC4JoatokoKY0RBH Qcu8MfD9NLViQ6SyUHDFtRb63A7gA03krPfcbydklm9RMJzw6IuaIfMMehwJuJOuaU5n UwOKm0ngD2DI9Cga8xqvF8hCzOondYbgNGoHZxEeIjzWS2Ym5hQwDt5dP8YZpamXARV2 c98TZdoDbidqTKeCIi4W8pxk0hHwhwOcBKydVwkfFKfV7tYatAYMWayC+Vif1wtgGYMJ MRbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uLedZvss; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c26si9318831pfd.2.2019.02.11.08.10.07; Mon, 11 Feb 2019 08:10:22 -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=uLedZvss; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729742AbfBKQIS (ORCPT + 99 others); Mon, 11 Feb 2019 11:08:18 -0500 Received: from mail.kernel.org ([198.145.29.99]:59660 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729481AbfBKO0A (ORCPT ); Mon, 11 Feb 2019 09:26:00 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 251E8214DA; Mon, 11 Feb 2019 14:25:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549895158; bh=9L1/I5857NC5Pk1w13t6oS/yyEsSsJa5rUmH7ky0bO8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uLedZvss8ST+biHqJgkpK0thlwNV2ig7yBMuRikKOGWIQjOwApSG9fNyhIIsmjIdD CsnzehAkK34HMWIxsOUgqeouiwBHC9pFG2zIAeJA5Sp6KPNtUSW0jIXcbnnwVguFye 87Zx0CRenTmr8SZxrxX7oC+5zCcRrS11ibhPizCE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sheng Yong , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 4.20 082/352] f2fs: fix race between write_checkpoint and write_begin Date: Mon, 11 Feb 2019 15:15:09 +0100 Message-Id: <20190211141851.224902611@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141846.543045703@linuxfoundation.org> References: <20190211141846.543045703@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ [ 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 17049b030b6c..e90ca6aa3a00 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -2326,6 +2326,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 @@ -2335,9 +2336,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: @@ -2375,6 +2382,7 @@ restart: 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; } @@ -2388,7 +2396,7 @@ out: 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