From patchwork Thu Feb 1 10:08:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13540822 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 D6EA0C47DDB for ; Thu, 1 Feb 2024 09:13:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 062B96B0081; Thu, 1 Feb 2024 04:13:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F050E6B0082; Thu, 1 Feb 2024 04:13:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DCCDB6B0083; Thu, 1 Feb 2024 04:13:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C56AA6B0082 for ; Thu, 1 Feb 2024 04:13:18 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6B71E40614 for ; Thu, 1 Feb 2024 09:13:18 +0000 (UTC) X-FDA: 81742671276.15.0754011 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf27.hostedemail.com (Postfix) with ESMTP id 9318E40005 for ; Thu, 1 Feb 2024 09:13:15 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf27.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706778796; 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=VLe+pmmDVDfQp16VtG7VOulZ/4B50muaiac6o0Il8VA=; b=xI3KUPp662whYBNDf7LFHThHv+hbVQX0IlVzm1EFTch6zWMSbwPWTk7h+wNlPJMPj7i599 /jQyyrGDrrVA3+DR48IZvO81PKo8F+iDqZszCJ7iY9eAHfAnEz0TWvp1rEf50xg1jOmWb0 DGEhDGxtvq+hOTxiiiX+OAKbydNwE90= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf27.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=liushixin2@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706778796; a=rsa-sha256; cv=none; b=eCqOkJqkKWbLf1KFWv7sw9ksGcx50BeCagvWQ3eABrmnsIfXTLonU5/VFTUx8kfTL51xOg rtPacxmxzakDun1YxYyo2tN3RQGFVStakDnaR2xuCcZg3HIdoLPWuJ/tiMD+u1D3s91ufD IR6MstmsjEDzR/HBQ8/38QUCB62fmAQ= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4TQY7c3fSJz1Q8hG; Thu, 1 Feb 2024 17:11:56 +0800 (CST) Received: from dggpemd200004.china.huawei.com (unknown [7.185.36.141]) by mail.maildlp.com (Postfix) with ESMTPS id 971BA1401E0; Thu, 1 Feb 2024 17:13:09 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpemd200004.china.huawei.com (7.185.36.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1258.28; Thu, 1 Feb 2024 17:13:09 +0800 From: Liu Shixin To: Alexander Viro , Christian Brauner , Jan Kara , Matthew Wilcox , Andrew Morton CC: , , , Liu Shixin Subject: [PATCH 1/2] mm/readahead: stop readahead loop if memcg charge fails Date: Thu, 1 Feb 2024 18:08:34 +0800 Message-ID: <20240201100835.1626685-2-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240201100835.1626685-1-liushixin2@huawei.com> References: <20240201100835.1626685-1-liushixin2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemd200004.china.huawei.com (7.185.36.141) X-Rspamd-Queue-Id: 9318E40005 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: aump89jbtd5z736dx1oyocgbunos39bh X-HE-Tag: 1706778795-973447 X-HE-Meta: U2FsdGVkX1+MKkWIiRw6pyFam44REkLMRobsEtz1AEzRRMJPzTxj8E2RWBljHA/4n18RafBMjbDLmlRzOLHrSR0Z4POD+4IYygSasdAJ7aDhhgstq4L3y7w6tpk3ctr84cBJTF2w+XvP9Hqk7vsdi3XI42nN69x0wkZZ1uTp+mX2p0BnPi0O5+UNw78GL9IQYvKpzQXklhM4QemCN/95eL8EmimE4jI9LYQLtLQZaXMNx/th9apEQ50MMYpmmqLAMnl0F6ASrooBSAh/YlnJbMuKNaF2mD9BNUNt2Vg8sfO9xdjWh2fwVwhPlE7+pkXp4QXTrjkIt1cprAE0Q/p06w4VUVnj7bp+a7ZQwOORqFitjUz7S2nagZKP5qCZ8OXCFfGhStFkEFPBccxFJAUVkU6j4ynkbWNGu2f2g3lPCuosLsMoxgaaIdRmN4etEaXNSEVHLwznXtIEkLSrucPw2YSvTd5L7xnkb5nqnM+G6OmHx7i2kaxycvY79qaAOfKP5ZnBsLGTG8zODajJ9mDXzGZyiN0Ss5a2Ga+DJTMG2+tSl7670HWhVXMSeR7AisFsTnFfeP/ssR3QQzg7YnEIxgl9BmCKtRJLHmYnmdOn+23rCpTvRcAsr4xLqVPqedNoGtMs3TEipT0A846g7pwVMsWH5fZ5oMW2GgqCV7KA1UVr6v38OYMWUhJ/xm0dyq/EgzpRiyadGN2sFX++rH22MlfXlg4ycps1t0y/4a2v//gSEnqbc+91N7TqwLFgP+idJVP8pA0eDryimmAM89FsP2J8NsOaXsRFKe+I+UB9shUh5fKJpXK+fZGfGQf/CgHLC0zvHA0XQj8XM/sPhP/Y6bystk230oTzVDIfk328bP38TT4hwDuoKLGxzeKiFrz9mQGhgCEy0b1xAU+Dk45pslUjokHuALP9AKaMN83U+d8ay1tMUECkz+E92Dnk5H1swmo/aZSZmPRsmAwCX72 tOJeIkE4 87JL81Ysv8qM3kyP82B6KPu/awy4nPBgbi+qC3mAjXs53L04PgZW2s8WK3yO4QYOP5Vf5A82sS0J41YQIk/lbff4AShfNDICaF3nbCtll7sjQuGyxDO944IltDkPs44zLkK5xtj9Qe++k+Jw= 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: List-Subscribe: List-Unsubscribe: When a task in memcg readaheads file pages, page_cache_ra_unbounded() will try to readahead nr_to_read pages. Even if the new allocated page fails to charge, page_cache_ra_unbounded() still tries to readahead next page. This leads to too much memory reclaim. Stop readahead if mem_cgroup_charge() fails, i.e. add_to_page_cache_lru() returns -ENOMEM. Signed-off-by: Liu Shixin Signed-off-by: Jinjiang Tu Reviewed-by: Jan Kara --- mm/readahead.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mm/readahead.c b/mm/readahead.c index 23620c57c1225..cc4abb67eb223 100644 --- a/mm/readahead.c +++ b/mm/readahead.c @@ -228,6 +228,7 @@ void page_cache_ra_unbounded(struct readahead_control *ractl, */ for (i = 0; i < nr_to_read; i++) { struct folio *folio = xa_load(&mapping->i_pages, index + i); + int ret; if (folio && !xa_is_value(folio)) { /* @@ -247,9 +248,12 @@ void page_cache_ra_unbounded(struct readahead_control *ractl, folio = filemap_alloc_folio(gfp_mask, 0); if (!folio) break; - if (filemap_add_folio(mapping, folio, index + i, - gfp_mask) < 0) { + + ret = filemap_add_folio(mapping, folio, index + i, gfp_mask); + if (ret < 0) { folio_put(folio); + if (ret == -ENOMEM) + break; read_pages(ractl); ractl->_index++; i = ractl->_index + ractl->_nr_pages - index - 1; From patchwork Thu Feb 1 10:08:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Shixin X-Patchwork-Id: 13540821 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 6AFA8C47DDF for ; Thu, 1 Feb 2024 09:13:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5E3B6B0080; Thu, 1 Feb 2024 04:13:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B35D16B0081; Thu, 1 Feb 2024 04:13:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FD516B0082; Thu, 1 Feb 2024 04:13:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 8D4886B0080 for ; Thu, 1 Feb 2024 04:13:18 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 528CE160976 for ; Thu, 1 Feb 2024 09:13:18 +0000 (UTC) X-FDA: 81742671276.15.26E1A6E Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf25.hostedemail.com (Postfix) with ESMTP id 52F85A0013 for ; Thu, 1 Feb 2024 09:13:14 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706778796; 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=wvmhl+5gUmTJi6IqK1s9zuIPvkJzdu1e4AjqE6mpLa8=; b=chyJSjJoY5JrljoQml7pcocaW5NJ802NW064esEZn8mgY+9FH2OQyapFp+Q48PEOt8vuaX lcxre4Rp2p9tZWCOeRIBnTvouZ+8fCOo8imJjMpMvJ9EtkOF1QQluwzQzUOvE9dkfgjcHr GS2FSSoy76pGWSHrYTTHso3eEmhfxoQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706778796; a=rsa-sha256; cv=none; b=d5HCr+xHAyvf5AFqT+dU/cencDQHdkzTq77gjHahTr+A3Vtws7MDRifotnxnt3Hkw6b32A PzSANyfYoDl4q7pX/v2ovtS8XGECSfhlARRCSzw8dTM+MH/rhdwAY1d4GyGzpMgRjX1j66 6pLG5m6JP6usqBnkdMXs7T4j4vAQhGA= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of liushixin2@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liushixin2@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4TQY7c6YpPzsWkK; Thu, 1 Feb 2024 17:11:56 +0800 (CST) Received: from dggpemd200004.china.huawei.com (unknown [7.185.36.141]) by mail.maildlp.com (Postfix) with ESMTPS id 2C21818005E; Thu, 1 Feb 2024 17:13:10 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpemd200004.china.huawei.com (7.185.36.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1258.28; Thu, 1 Feb 2024 17:13:09 +0800 From: Liu Shixin To: Alexander Viro , Christian Brauner , Jan Kara , Matthew Wilcox , Andrew Morton CC: , , , Liu Shixin Subject: [PATCH 2/2] mm/readahead: limit sync readahead while too many active refault Date: Thu, 1 Feb 2024 18:08:35 +0800 Message-ID: <20240201100835.1626685-3-liushixin2@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240201100835.1626685-1-liushixin2@huawei.com> References: <20240201100835.1626685-1-liushixin2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemd200004.china.huawei.com (7.185.36.141) X-Rspamd-Queue-Id: 52F85A0013 X-Rspam-User: X-Stat-Signature: g8y1iiire6sedkqqnjtj1qrsrtn4qhou X-Rspamd-Server: rspam03 X-HE-Tag: 1706778794-659888 X-HE-Meta: U2FsdGVkX1+BGtqJUpDggrP5YwXXJB0N9IZwvcGQeP6o02F7Jh+lybhOB6KIb6ZgBkre8vy4sQvThYjJ1kmqFinBseB7xd0Xml9qB45Bsy2rlSXeO5lg2GRrro8O/U+HiCiPoJuOHlvUzFypBJt39bdQyJXvp1FQN8T8kj+12yI1XiM+8koh3zslNaejjju3D1z1hjTDkZsSE3T9jTcm3xk43cug/DeD2wbmMaEUeMJ2VGWPPzGC3xnlxQU7HlFsvFkiAsx2BWX86vpVMUQfyxdU2BSmfiALjKDV16F47WLXYNSJSu4JAME4OMevGFSjOaexrW4u6SPUwioRV3nzZoW9tsYgwnGDqNaT/ezP/yw3S+KH/9TyMJK+7vNvL9okDphPHIBti3v5gKdMDl+agWgLzrv9nVx+SsG00yYwp8E4zzFFw9fvsonWyQgDBtmtkqd+hWG7goYujTgToPTBxYFTsygMfnm8K5aiT3IJFT/hXbjDQ7WTfowmybCDgLSlK0dAakRw4G49QlQlPluv6nV02bj0+k+tAQlvKN+OAY/H1PxXput2OU6x0FFGEGENLNNjYB9cV4SrYKROui+3k9HoRjy3s2RNHrN4uWSFKFXJyOJsioWZQZI073MrohRLf0onFWr6Ycwm8PLeBCCYDQcO4G5R/2Iwy0ygzcu21gq+6R3IAzIu1Jy9BYAm7HaySvY1412UE2i5H/bqxJ9v93TJP4IKeGFXH1Ic6ROkCBFQu03UYQpxSRhM8h3MTH19xXz7O9vQS25w2lNq+AkS1XiluJd3wi/M3YNlwQzgk6KQr4RvAUhf//cgVXk8Qf55NtiuWOZCO74jLIuSW+8Ykep6llHV2eWkAMCG/yZDWQ1SzZLHDssc7sKGygmowb7C6w5IvDyDPdpf9uuFbTKPIJ00vDUItD5mvwod3TU4uSaDSiga9GDxYCzDSEYjYyjMpo1CzTjEhbK7GYGkzy5 UkrKcBML bE76J7btSbVSykj1Cadv7uKcth5ycIRu4kALnp2NnzuJJPBkg/i4EFS6fqrzjQyza/QQVgtbm1o8z1beMo0pz6QbAa9yL/zujBk2CjvD5crnw1g1fsIrr67qw0ld6T43i8PgEzHJottF0XSBSNOCwh9Lz2g== 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: List-Subscribe: List-Unsubscribe: When the pagefault is not for write and the refault distance is close, the page will be activated directly. If there are too many such pages in a file, that means the pages may be reclaimed immediately. In such situation, there is no positive effect to read-ahead since it will only waste IO. So collect the number of such pages and when the number is too large, stop bothering with read-ahead for a while until it decreased automatically. Define 'too large' as 10000 experientially, which can solves the problem and does not affect by the occasional active refault. Signed-off-by: Liu Shixin --- include/linux/fs.h | 2 ++ include/linux/pagemap.h | 1 + mm/filemap.c | 16 ++++++++++++++++ mm/readahead.c | 4 ++++ 4 files changed, 23 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index ed5966a704951..f2a1825442f5a 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -960,6 +960,7 @@ struct fown_struct { * the first of these pages is accessed. * @ra_pages: Maximum size of a readahead request, copied from the bdi. * @mmap_miss: How many mmap accesses missed in the page cache. + * @active_refault: Number of active page refault. * @prev_pos: The last byte in the most recent read request. * * When this structure is passed to ->readahead(), the "most recent" @@ -971,6 +972,7 @@ struct file_ra_state { unsigned int async_size; unsigned int ra_pages; unsigned int mmap_miss; + unsigned int active_refault; loff_t prev_pos; }; diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 2df35e65557d2..da9eaf985dec4 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -1256,6 +1256,7 @@ struct readahead_control { pgoff_t _index; unsigned int _nr_pages; unsigned int _batch_count; + unsigned int _active_refault; bool _workingset; unsigned long _pflags; }; diff --git a/mm/filemap.c b/mm/filemap.c index 750e779c23db7..4de80592ab270 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3037,6 +3037,7 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, #ifdef CONFIG_MMU #define MMAP_LOTSAMISS (100) +#define ACTIVE_REFAULT_LIMIT (10000) /* * lock_folio_maybe_drop_mmap - lock the page, possibly dropping the mmap_lock * @vmf - the vm_fault for this fault. @@ -3142,6 +3143,18 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) if (mmap_miss > MMAP_LOTSAMISS) return fpin; + ractl._active_refault = READ_ONCE(ra->active_refault); + if (ractl._active_refault) + WRITE_ONCE(ra->active_refault, --ractl._active_refault); + + /* + * If there are a lot of refault of active pages in this file, + * that means the memory reclaim is ongoing. Stop bothering with + * read-ahead since it will only waste IO. + */ + if (ractl._active_refault >= ACTIVE_REFAULT_LIMIT) + return fpin; + /* * mmap read-around */ @@ -3151,6 +3164,9 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf) ra->async_size = ra->ra_pages / 4; ractl._index = ra->start; page_cache_ra_order(&ractl, ra, 0); + + WRITE_ONCE(ra->active_refault, ractl._active_refault); + return fpin; } diff --git a/mm/readahead.c b/mm/readahead.c index cc4abb67eb223..d79bb70a232c4 100644 --- a/mm/readahead.c +++ b/mm/readahead.c @@ -263,6 +263,10 @@ void page_cache_ra_unbounded(struct readahead_control *ractl, folio_set_readahead(folio); ractl->_workingset |= folio_test_workingset(folio); ractl->_nr_pages++; + if (unlikely(folio_test_workingset(folio))) + ractl->_active_refault++; + else if (unlikely(ractl->_active_refault)) + ractl->_active_refault--; } /*