Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp592951pxb; Wed, 13 Apr 2022 08:22:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzAphI6Ruu/q2WF0+hkKNHbt3qtwOBzzQ2eoqM2opaPVZxC1LRJk0ZliF3bfyE8bUF6pfy X-Received: by 2002:a17:903:94:b0:158:a14f:f117 with SMTP id o20-20020a170903009400b00158a14ff117mr3399539pld.18.1649863339560; Wed, 13 Apr 2022 08:22:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649863339; cv=none; d=google.com; s=arc-20160816; b=d3mFiIsT8EyyRMZpPUyETTT4nctib3h+JSmYdwpwagrodqmPP/PauCF5OdJcTJoGDO 9pJSNj78l3eo8mwjcMt98DtQZefc8JRujmj6Np8EIQMLup3byXJ2mvMnyeTX8nzJcO3w EXVkg3SjgY7wBW5w9WVj+ZvZjMRu47l8lTqy7hk9p7mGnIu5m7M9KksmzPaW5qmMwh7h 9pH+x6UDfRpPZN1YIeM3rm+ci7ZAiLSIPbIn8YS6VZdhv/rQxDN8xzYPuTAfB2+fFXDE vmrYe/O8/OhnqwdA++mSQA6PxX4WiGlibVelhlb7MzjJxTWc8k1ngRSvjgSOass6VfrO 0vRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:abuse-reports-to:tuid:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from; bh=VKtk9cCEDC5JDIsaBlfZmwe423s06Vmyll/fYBhcEGg=; b=SN27uO0aAn1n1dmAjTYUm7IQyoR1lR0kTFzze1PZ+gZNd6B1qGXjfAdTlIyv1THzkg mKLe62qlE5aeOQV2BMXqxYJoKzFI8O3wGTyplM9a09h+4PXI8M5zbGjZqL66BJuPt0me xvKOF9no98HORPCvKOBoqwJDZiaQpBm/aP0SYOs8VKy0z0mBlks74gOUJObJNS9FiSJt lVXMLhU/+5Hu5zw6zz00bc8kpkwfY9Nz++hMfv88P0xNHcsUADtutvdE8RfAQ5Gm20Ko QnZ+8FeBY0YtjLOi+a97y/z1Yn4H08KzXiqUcgYvaEn1img15X1LsR6dQCA4LYVQSIYf Wbxg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r29-20020a63fc5d000000b0038259e42e7csi5725398pgk.845.2022.04.13.08.22.04; Wed, 13 Apr 2022 08:22:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232893AbiDMIsE (ORCPT + 99 others); Wed, 13 Apr 2022 04:48:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234075AbiDMIrH (ORCPT ); Wed, 13 Apr 2022 04:47:07 -0400 Received: from support.corp-email.com (support.corp-email.com [222.73.234.235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 793584D616 for ; Wed, 13 Apr 2022 01:44:46 -0700 (PDT) Received: from ([114.119.32.142]) by support.corp-email.com ((D)) with ASMTP (SSL) id IEG00038; Wed, 13 Apr 2022 16:44:38 +0800 Received: from localhost.localdomain (172.16.35.4) by GCY-MBS-28.TCL.com (10.136.3.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.18; Wed, 13 Apr 2022 16:44:38 +0800 From: Rokudo Yan To: CC: , , , Subject: [PATCH] f2fs: avoid deadlock in gc thread under low memory Date: Wed, 13 Apr 2022 16:44:32 +0800 Message-ID: <20220413084432.1312900-1-wu-yan@tcl.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [172.16.35.4] X-ClientProxiedBy: GCY-EXS-27.TCL.com (10.74.128.67) To GCY-MBS-28.TCL.com (10.136.3.28) tUid: 2022413164438f383f1451cf81c4bc895e3d2271acda2 X-Abuse-Reports-To: service@corp-email.com Abuse-Reports-To: service@corp-email.com X-Complaints-To: service@corp-email.com X-Report-Abuse-To: service@corp-email.com X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There is a potential deadlock in gc thread may happen under low memory as below: gc_thread_func -f2fs_gc -do_garbage_collect -gc_data_segment -move_data_block -set_page_writeback(fio.encrypted_page); -f2fs_submit_page_write as f2fs_submit_page_write try to do io merge when possible, so the encrypted_page is marked PG_writeback but may not submit to block layer immediately, if system enter low memory when gc thread try to move next data block, it may do direct reclaim and enter fs layer as below: -move_data_block -f2fs_grab_cache_page(index=?, for_write=false) -grab_cache_page -find_or_create_page -pagecache_get_page -__page_cache_alloc -- __GFP_FS is set -alloc_pages_node -__alloc_pages -__alloc_pages_slowpath -__alloc_pages_direct_reclaim -__perform_reclaim -try_to_free_pages -do_try_to_free_pages -shrink_zones -mem_cgroup_soft_limit_reclaim -mem_cgroup_soft_reclaim -mem_cgroup_shrink_node -shrink_node_memcg -shrink_list -shrink_inactive_list -shrink_page_list -wait_on_page_writeback -- the page is marked writeback during previous move_data_block call the gc thread wait for the encrypted_page writeback complete, but as gc thread held sbi->gc_lock, the writeback & sync thread may blocked waiting for sbi->gc_lock, so the bio contain the encrypted_page may nerver submit to block layer and complete the writeback, which cause deadlock. To avoid this deadlock condition, we mark the gc thread with PF_MEMALLOC_NOFS flag, then it will nerver enter fs layer when try to alloc cache page during move_data_block. Signed-off-by: Rokudo Yan --- fs/f2fs/gc.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index e020804f7b07..cc71f77b98c8 100644 --- a/fs/f2fs/gc.c +++ b/fs/f2fs/gc.c @@ -38,6 +38,12 @@ static int gc_thread_func(void *data) wait_ms = gc_th->min_sleep_time; + /* + * Make sure that no allocations from gc thread will ever + * recurse to the fs layer to avoid deadlock as it will + * hold sbi->gc_lock during garbage collection + */ + memalloc_nofs_save(); set_freezable(); do { bool sync_mode, foreground = false; -- 2.25.1