Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp462034pxj; Tue, 18 May 2021 07:16:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxGYWrtZm+YjCPKZKPBjnqb+Abz47r0EGY/E863gHUeUJRMhBAo5n0sdzMws5hPJ81hURnT X-Received: by 2002:a17:906:22c6:: with SMTP id q6mr6257510eja.275.1621347418142; Tue, 18 May 2021 07:16:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621347418; cv=none; d=google.com; s=arc-20160816; b=hQV0aPnbY1ljFIeDakw61q6nzkHP6Guyh4vWHpvQEayWmSOIvjjXehPx+pmBdHIpwI AIHHp+d0oXQznBhsoR5fMhQ0iejSko2TWxuHDVcdQ9Qv4DRmsCLveO/wXP+L8uPDp7Py OenOV+hSR/LqTZ+hqGAF2+m8H+cuQj7h28VMxXTKNCV9CM2ZJytEUwRVkmet/l8Nv+Oz rZoYjmVscGEVBKBcq9VgN2Vk1dKhBnFGRyYdLY7nRfy6ROYvwH/q2U7vqVmkZ8tW1yVY g3WvwHTbD769x7mguiMQ8Qfqt4VIsLLG29QSis3uf267GZLSvVQnCcU56Xd9y/YdUvhG bMTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=53P9whymssHQ863VxDPuXIPduc4deFcpQz9KM2dv7dE=; b=BDD3lawlLmJgt2IEgEWFRsNnubP54uH1u3gSWv2XGx9exNM3Eq0NowLD2uzh+PBy9W 750P/GnLiNzGmJiObjKC3eqcHxzmPgOm09cz7Wm1jOWU3qcDSAq5/it4o4uaLMi+y7BT PoAAaQEaUigdSAH18Zt5U6Z/WLm72HBtym4N2naSc5fLRRQrHanTB8v07eBs6ikPICXf OG/+GhxGpLOOVfQgwaBsHap/7RuI7rQnKIkVAtkGRJj2wsjubOcZTRNfMoU9TElACfdJ AyzzR3G6guxyUcFr2s/u2GpmvhZZFPGp4xFENwm7yLpIN2E7wcyeeubF4YO/pBO0JoCQ 20ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RNChhgWn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d3si14531570ede.313.2021.05.18.07.16.35; Tue, 18 May 2021 07:16:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=RNChhgWn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344390AbhEQPog (ORCPT + 99 others); Mon, 17 May 2021 11:44:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:55328 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244803AbhEQP2j (ORCPT ); Mon, 17 May 2021 11:28:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 064C461CAD; Mon, 17 May 2021 14:37:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621262234; bh=DcO3+0Pl6VbfrAtsgA9uT+SOD7nA8ur+05IvMxODMaI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RNChhgWnHXCOhHTQBw8FpnUVBOCRNL/XAd2aCC7RyEm1yWL0DvWK9sm8YsX3r+u6i E3jR8I1FwWuVA4CCANLellt3ML6FQ/Mx1gZnwn/zjrMOKy1w87hv9j/+3Yxg0vvniB 7iHMX1E/t4SsXIY8GBmnyXegZ9GTCUQmvLOGXKII= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yi Zhuang , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.10 113/289] f2fs: Fix a hungtask problem in atomic write Date: Mon, 17 May 2021 16:00:38 +0200 Message-Id: <20210517140308.975893246@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140305.140529752@linuxfoundation.org> References: <20210517140305.140529752@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yi Zhuang [ Upstream commit be1ee45d51384161681ecf21085a42d316ae25f7 ] In the cache writing process, if it is an atomic file, increase the page count of F2FS_WB_CP_DATA, otherwise increase the page count of F2FS_WB_DATA. When you step into the hook branch due to insufficient memory in f2fs_write_begin, f2fs_drop_inmem_pages_all will be called to traverse all atomic inodes and clear the FI_ATOMIC_FILE mark of all atomic files. In f2fs_drop_inmem_pages,first acquire the inmem_lock , revoke all the inmem_pages, and then clear the FI_ATOMIC_FILE mark. Before this mark is cleared, other threads may hold inmem_lock to add inmem_pages to the inode that has just been emptied inmem_pages, and increase the page count of F2FS_WB_CP_DATA. When the IO returns, it is found that the FI_ATOMIC_FILE flag is cleared by f2fs_drop_inmem_pages_all, and f2fs_is_atomic_file returns false,which causes the page count of F2FS_WB_DATA to be decremented. The page count of F2FS_WB_CP_DATA cannot be cleared. Finally, hungtask is triggered in f2fs_wait_on_all_pages because get_pages will never return zero. process A: process B: f2fs_drop_inmem_pages_all ->f2fs_drop_inmem_pages of inode#1 ->mutex_lock(&fi->inmem_lock) ->__revoke_inmem_pages of inode#1 f2fs_ioc_commit_atomic_write ->mutex_unlock(&fi->inmem_lock) ->f2fs_commit_inmem_pages of inode#1 ->mutex_lock(&fi->inmem_lock) ->__f2fs_commit_inmem_pages ->f2fs_do_write_data_page ->f2fs_outplace_write_data ->do_write_page ->f2fs_submit_page_write ->inc_page_count(sbi, F2FS_WB_CP_DATA ) ->mutex_unlock(&fi->inmem_lock) ->spin_lock(&sbi->inode_lock[ATOMIC_FILE]); ->clear_inode_flag(inode, FI_ATOMIC_FILE) ->spin_unlock(&sbi->inode_lock[ATOMIC_FILE]) f2fs_write_end_io ->dec_page_count(sbi, F2FS_WB_DATA ); We can fix the problem by putting the action of clearing the FI_ATOMIC_FILE mark into the inmem_lock lock. This operation can ensure that no one will submit the inmem pages before the FI_ATOMIC_FILE mark is cleared, so that there will be no atomic writes waiting for writeback. Fixes: 57864ae5ce3a ("f2fs: limit # of inmemory pages") Signed-off-by: Yi Zhuang Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/segment.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index ddfc3daebe9b..1c264fd5a0dd 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -327,23 +327,27 @@ void f2fs_drop_inmem_pages(struct inode *inode) struct f2fs_sb_info *sbi = F2FS_I_SB(inode); struct f2fs_inode_info *fi = F2FS_I(inode); - while (!list_empty(&fi->inmem_pages)) { + do { mutex_lock(&fi->inmem_lock); + if (list_empty(&fi->inmem_pages)) { + fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0; + + spin_lock(&sbi->inode_lock[ATOMIC_FILE]); + if (!list_empty(&fi->inmem_ilist)) + list_del_init(&fi->inmem_ilist); + if (f2fs_is_atomic_file(inode)) { + clear_inode_flag(inode, FI_ATOMIC_FILE); + sbi->atomic_files--; + } + spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); + + mutex_unlock(&fi->inmem_lock); + break; + } __revoke_inmem_pages(inode, &fi->inmem_pages, true, false, true); mutex_unlock(&fi->inmem_lock); - } - - fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0; - - spin_lock(&sbi->inode_lock[ATOMIC_FILE]); - if (!list_empty(&fi->inmem_ilist)) - list_del_init(&fi->inmem_ilist); - if (f2fs_is_atomic_file(inode)) { - clear_inode_flag(inode, FI_ATOMIC_FILE); - sbi->atomic_files--; - } - spin_unlock(&sbi->inode_lock[ATOMIC_FILE]); + } while (1); } void f2fs_drop_inmem_page(struct inode *inode, struct page *page) -- 2.30.2