Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7788AC7618E for ; Wed, 26 Apr 2023 17:38:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E22796B010E; Wed, 26 Apr 2023 13:38:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD30F6B010F; Wed, 26 Apr 2023 13:38:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C99F26B0110; Wed, 26 Apr 2023 13:38:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id B871F6B010E for ; Wed, 26 Apr 2023 13:38:58 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 875011402E5 for ; Wed, 26 Apr 2023 17:38:58 +0000 (UTC) X-FDA: 80724252756.30.535DB49 Received: from p3plwbeout15-03.prod.phx3.secureserver.net (p3plsmtp15-03-2.prod.phx3.secureserver.net [173.201.193.38]) by imf01.hostedemail.com (Postfix) with ESMTP id 3328940011 for ; Wed, 26 Apr 2023 17:38:54 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=none; spf=none (imf01.hostedemail.com: domain of phillip@squashfs.org.uk has no SPF policy when checking 173.201.193.38) smtp.mailfrom=phillip@squashfs.org.uk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682530735; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rcGC1zp7OFx7SttHD6fQ3ANHWYb90to6Vj0REm2Xtic=; b=yVNuE6Vk2EvVmx6dJalGylu9uSeMYHJN81/RGF7LF5QGAKSlr9Z7CVdGJmZCiWceMH5z+M c2pvdMViLWV2cEJ7BG3Dob8YXPggvtREOh14mlGsFHk5FL1AtHZK9tSZ1rUU010JZbHzoM 3Zy/z8fsuGBvHgX5/NrhrbljcSHC74c= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=none; spf=none (imf01.hostedemail.com: domain of phillip@squashfs.org.uk has no SPF policy when checking 173.201.193.38) smtp.mailfrom=phillip@squashfs.org.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682530735; a=rsa-sha256; cv=none; b=uBp7fqbcttC6DueSPV5oULb1QVlomxoUggOj3S46ElT39GwYRfnlsDWkPyD10En8hwOvbC noBUk9PJ6Ge1+cEtDhe+hj0CSlqj3F/iY9n9SXpB261t7ULCjXfVF31ZFas8I50wHRTGQr lj+NRr+PI6ZKlKKNdewFou0oJZCys5g= Received: from mailex.mailcore.me ([94.136.40.145]) by :WBEOUT: with ESMTP id rj6Tpls8R5WDhrj6Tpfp9I; Wed, 26 Apr 2023 10:38:54 -0700 X-SECURESERVER-ACCT: phillip@squashfs.org.uk X-SID: rj6Tpls8R5WDh Received: from 82-69-79-175.dsl.in-addr.zen.co.uk ([82.69.79.175] helo=[192.168.178.87]) by smtp07.mailcore.me with esmtpa (Exim 4.94.2) (envelope-from ) id 1prj6V-0002PA-5w; Wed, 26 Apr 2023 18:38:56 +0100 Message-ID: <72cf1f14-c02b-033e-6fa9-8558e628ffb6@squashfs.org.uk> Date: Wed, 26 Apr 2023 18:38:51 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH 1/1] mm/oom_kill: trigger the oom killer if oom occurs without __GFP_FS Content-Language: en-GB From: Phillip Lougher To: Hui Wang , Michal Hocko Cc: linux-mm@kvack.org, akpm@linux-foundation.org, surenb@google.com, colin.i.king@gmail.com, shy828301@gmail.com, hannes@cmpxchg.org, vbabka@suse.cz, hch@infradead.org, mgorman@suse.de, dan.carpenter@oracle.com References: <20230426051030.112007-1-hui.wang@canonical.com> <20230426051030.112007-2-hui.wang@canonical.com> <9f827ae2-eaec-8660-35fa-71e218d5a2c5@squashfs.org.uk> In-Reply-To: <9f827ae2-eaec-8660-35fa-71e218d5a2c5@squashfs.org.uk> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Mailcore-Auth: 439999529 X-Mailcore-Domain: 1394945 X-123-reg-Authenticated: phillip@squashfs.org.uk X-Originating-IP: 82.69.79.175 X-CMAE-Envelope: MS4xfM/rt8BekA7utr1TAE0XoBrf7UkdOzlEHz/TBX27luVBSi2s540nnBYCtI4VzebJyJgIgsabS6xfCkq21f/3agOnwmbhom1QfpqX6miVB1/iPyjCZpmp jdv0TFVvquIieA3eZaxuilX89Qb21CN5LVTzc+cxKYJhAVrAAFoX4105Z9VbE1mxXXSxXVbJu5/r6TEIXaoNMw1XYWOyvLZCOnw= X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3328940011 X-Stat-Signature: xdqug3k3skb3nd8jcdos43z85q13symb X-Rspam-User: X-CMAE-Analysis: v=2.4 cv=estnjKlX c=1 sm=1 tr=0 ts=644961b0 a=U6dgvN6OMzNKPQwS3+yPjw==:117 a=84ok6UeoqCVsigPHarzEiQ==:17 a=ggZhUymU-5wA:10 a=IkcTkHD0fZMA:10 a=dKHAf1wccvYA:10 a=ZemOHMvfgc8gN60WYXwA:9 a=QEXdDO2ut3YA:10 X-HE-Tag: 1682530734-387776 X-HE-Meta: U2FsdGVkX195VRb8l9JY6causQr6WtL/IkDzpLDC2NFwHwp7Nj4PKDxgsVXqApS/sIMzGiEJo2ldn/vekZy0uD4EDlyzwFNSxg56HbKpCIi1sara0o1TQ/YIAfBEO5G0YYmBjQffBwNNbcqN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bw5LgxTxC+02kHmia+3BraYxrEOu+5EtooJQqAEHW8PkiIhm51HxjlVvCAf4v5y2eY/75F60mSxW7lhLc2u9k92BLGVmbk0gJENubASWBTms1mvTjJuV6yZD/Lijb2giIa+JuLYaYXeekQonZI0SUJxbK3il03BXKGjFIwkDCwzoSNkULii1DEb7iIc3uo4qImx+2GogBK6oT3WRdIJmwEx82cdHb9YLAwcen/0CZKJKtQL8P7Y749jDAMR2fGPbkmT9HEPUTZ5Hl9kgY7HT8e5paBjdDiktB/llo5Y5il1vrXX32xgJOzhu9fXDA7dITPQCNxCR41eVCdSXw X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 26/04/2023 17:44, Phillip Lougher wrote: > > On 26/04/2023 12:07, Hui Wang wrote: >> >> On 4/26/23 16:33, Michal Hocko wrote: >>> [CC squashfs maintainer] >>> >>> On Wed 26-04-23 13:10:30, Hui Wang wrote: >>>> If we run the stress-ng in the filesystem of squashfs, the system >>>> will be in a state something like hang, the stress-ng couldn't >>>> finish running and the console couldn't react to users' input. >>>> >>>> This issue happens on all arm/arm64 platforms we are working on, >>>> through debugging, we found this issue is introduced by oom handling >>>> in the kernel. >>>> >>>> The fs->readahead() is called between memalloc_nofs_save() and >>>> memalloc_nofs_restore(), and the squashfs_readahead() calls >>>> alloc_page(), in this case, if there is no memory left, the >>>> out_of_memory() will be called without __GFP_FS, then the oom killer >>>> will not be triggered and this process will loop endlessly and wait >>>> for others to trigger oom killer to release some memory. But for a >>>> system with the whole root filesystem constructed by squashfs, >>>> nearly all userspace processes will call out_of_memory() without >>>> __GFP_FS, so we will see that the system enters a state something like >>>> hang when running stress-ng. >>>> >>>> To fix it, we could trigger a kthread to call page_alloc() with >>>> __GFP_FS before returning from out_of_memory() due to without >>>> __GFP_FS. >>> I do not think this is an appropriate way to deal with this issue. >>> Does it even make sense to trigger OOM killer for something like >>> readahead? Would it be more mindful to fail the allocation instead? >>> That being said should allocations from squashfs_readahead use >>> __GFP_RETRY_MAYFAIL instead? >> >> Thanks for your comment, and this issue could hardly be reproduced on >> ext4 filesystem, that is because the ext4->readahead() doesn't call >> alloc_page(). If changing the ext4->readahead() as below, it will be >> easy to reproduce this issue with the ext4 filesystem (repeatedly >> run: $stress-ng --bigheap ${num_of_cpu_threads} --sequential 0 >> --timeout 30s --skip-silent --verbose) >> >> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c >> index ffbbd9626bd8..8b9db0b9d0b8 100644 >> --- a/fs/ext4/inode.c >> +++ b/fs/ext4/inode.c >> @@ -3114,12 +3114,18 @@ static int ext4_read_folio(struct file *file, >> struct folio *folio) >>  static void ext4_readahead(struct readahead_control *rac) >>  { >>         struct inode *inode = rac->mapping->host; >> +       struct page *tmp_page; >> >>         /* If the file has inline data, no need to do readahead. */ >>         if (ext4_has_inline_data(inode)) >>                 return; >> >> +       tmp_page = alloc_page(GFP_KERNEL); >> + >>         ext4_mpage_readpages(inode, rac, NULL); >> + >> +       if (tmp_page) >> +               __free_page(tmp_page); >>  } >> >> >> BTW, I applied my patch to the linux-next and ran the oom stress-ng >> testcases overnight, there is no hang, oops or crash, looks like >> there is no big problem to use a kthread to trigger the oom killer in >> this case. >> >> And Hi squashfs maintainer, I checked the code of filesystem, looks >> like most filesystems will not call alloc_page() in the readahead(), >> could you please help take a look at this issue, thanks. > > > This will be because most filesystems don't need to do so. Squashfs is > a compressed filesystem with large blocks covering much more than one > page, and it decompresses these blocks in squashfs_readahead().   If > __readahead_batch() does not return the full set of pages covering the > Squashfs block, it allocates a temporary page for the decompressors to > decompress into to "fill in the hole". > > What can be done here as far as Squashfs is concerned ....  I could > move the page allocation out of the readahead path (e.g. do it at > mount time). You could try this patch which does that.  Compile tested only. fs/squashfs/page_actor.c | 10 +--------- fs/squashfs/page_actor.h | 1 - fs/squashfs/squashfs_fs_sb.h | 1 + fs/squashfs/super.c | 10 ++++++++++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/fs/squashfs/page_actor.c b/fs/squashfs/page_actor.c index 81af6c4ca115..6cce239eca66 100644 --- a/fs/squashfs/page_actor.c +++ b/fs/squashfs/page_actor.c @@ -110,15 +110,7 @@ struct squashfs_page_actor *squashfs_page_actor_init_special(struct squashfs_sb_ if (actor == NULL) return NULL; - if (msblk->decompressor->alloc_buffer) { - actor->tmp_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); - - if (actor->tmp_buffer == NULL) { - kfree(actor); - return NULL; - } - } else - actor->tmp_buffer = NULL; + actor->tmp_buffer = msblk->actor_page; actor->length = length ? : pages * PAGE_SIZE; actor->page = page; diff --git a/fs/squashfs/page_actor.h b/fs/squashfs/page_actor.h index 97d4983559b1..df5e999afa42 100644 --- a/fs/squashfs/page_actor.h +++ b/fs/squashfs/page_actor.h @@ -34,7 +34,6 @@ static inline struct page *squashfs_page_actor_free(struct squashfs_page_actor * { struct page *last_page = actor->last_page; - kfree(actor->tmp_buffer); kfree(actor); return last_page; } diff --git a/fs/squashfs/squashfs_fs_sb.h b/fs/squashfs/squashfs_fs_sb.h index 72f6f4b37863..8feddc9e6cce 100644 --- a/fs/squashfs/squashfs_fs_sb.h +++ b/fs/squashfs/squashfs_fs_sb.h @@ -47,6 +47,7 @@ struct squashfs_sb_info { struct squashfs_cache *block_cache; struct squashfs_cache *fragment_cache; struct squashfs_cache *read_page; + void *actor_page; int next_meta_index; __le64 *id_table; __le64 *fragment_index; diff --git a/fs/squashfs/super.c b/fs/squashfs/super.c index e090fae48e68..674dc187d961 100644 --- a/fs/squashfs/super.c +++ b/fs/squashfs/super.c @@ -329,6 +329,15 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc) goto failed_mount; } + + /* Allocate page for squashfs_readahead()/squashfs_read_folio() */ + if (msblk->decompressor->alloc_buffer) { + msblk->actor_page = kmalloc(PAGE_SIZE, GFP_KERNEL); + + if(msblk->actor_page == NULL) + goto failed_mount; + } + msblk->stream = squashfs_decompressor_setup(sb, flags); if (IS_ERR(msblk->stream)) { err = PTR_ERR(msblk->stream); @@ -454,6 +463,7 @@ static int squashfs_fill_super(struct super_block *sb, struct fs_context *fc) squashfs_cache_delete(msblk->block_cache); squashfs_cache_delete(msblk->fragment_cache); squashfs_cache_delete(msblk->read_page); + kfree(msblk->actor_page); msblk->thread_ops->destroy(msblk); kfree(msblk->inode_lookup_table); kfree(msblk->fragment_index); -- 2.35.1 > > Adding __GFP_RETRY_MAYFAIL so the alloc() can fail will mean Squashfs > returning I/O failures due to no memory.  That will cause a lot of > applications to crash in a low memory situation.  So a crash rather > than a hang. > > Phillip > > > >