From patchwork Fri Jun 7 09:40:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xu.xin16@zte.com.cn X-Patchwork-Id: 13689559 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 042A0C27C53 for ; Fri, 7 Jun 2024 09:41:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 88F046B00AD; Fri, 7 Jun 2024 05:41:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 818206B00AF; Fri, 7 Jun 2024 05:41:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B8606B00B4; Fri, 7 Jun 2024 05:41:01 -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 4A7B36B00AD for ; Fri, 7 Jun 2024 05:41:01 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 08D25A101B for ; Fri, 7 Jun 2024 09:41:01 +0000 (UTC) X-FDA: 82203598722.08.BB36720 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by imf11.hostedemail.com (Postfix) with ESMTP id 2E02B40009 for ; Fri, 7 Jun 2024 09:40:56 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf11.hostedemail.com: domain of xu.xin16@zte.com.cn designates 63.216.63.40 as permitted sender) smtp.mailfrom=xu.xin16@zte.com.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717753259; 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:in-reply-to: references; bh=7JzbTqE/LVmrMaTm3BN5+MORuOZjuvRPiKJY5Uz5n1o=; b=ii73jj1X64+sjJIP1LPLBYPLpRdeFhJQLPppG0sgaLVNhtbhkI2DzgE357RQF7+4WVVUNG F0CtpVlA4aLMA45xhThOg/7PVPPhTmAmnoR5Wfr9dno7S69t+8K+O4sUKc/TFtdbK2PKZ+ gM6/rWmJ1nv8YfRBM5Gq2ndxZcqQtZA= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf11.hostedemail.com: domain of xu.xin16@zte.com.cn designates 63.216.63.40 as permitted sender) smtp.mailfrom=xu.xin16@zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717753259; a=rsa-sha256; cv=none; b=NvQ3j9JOVr7LrHo8i2/8UUaAFSzq9KZ5b7ehCcBGlTEx3khG9qSuLjLQif66/BnKaVRzXK AUAeY/F7gaJFENlXeg90qYjlde/GHlzcPbzDDM4ylmex4wHLqIs30flPrp8Lhy2RFg7DzC hvnOSwy21eYqs3kn18A/r1SeHc80J5Y= Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4VwbmJ0Sy0z8XrS1; Fri, 7 Jun 2024 17:40:48 +0800 (CST) Received: from xaxapp01.zte.com.cn ([10.88.99.176]) by mse-fl1.zte.com.cn with SMTP id 4579ejtn002111; Fri, 7 Jun 2024 17:40:45 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp02[null]) by mapi (Zmail) with MAPI id mid32; Fri, 7 Jun 2024 17:40:48 +0800 (CST) Date: Fri, 7 Jun 2024 17:40:48 +0800 (CST) X-Zmail-TransId: 2afa6662d5a0ffffffffa1b-000d9 X-Mailer: Zmail v1.0 Message-ID: <202406071740485174hcFl7jRxncsHDtI-Pz-o@zte.com.cn> Mime-Version: 1.0 From: To: , , , Cc: , , , , , Subject: =?utf-8?q?=5BPATCH_linux-next_v3=5D_mm=3A_huge=5Fmemory=3A_fix_misu?= =?utf-8?q?sed_mapping=5Flarge=5Ffolio=5Fsupport=28=29=C2=A0_for_anon_folios?= X-MAIL: mse-fl1.zte.com.cn 4579ejtn002111 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6662D5A0.000/4VwbmJ0Sy0z8XrS1 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2E02B40009 X-Stat-Signature: pi6uxrnwwtn4cdp7n7m4m963b356xedb X-Rspam-User: X-HE-Tag: 1717753256-96824 X-HE-Meta: U2FsdGVkX19++EJa2vrGj76P3pgLOATl6p2tspE2UzzMRWEKQS5wLIKxKufX2bMmZNjC4m2kwmxJ4SUHBBBASic1jOgjK/gNRVLhIpyCR2t1PptF6cfU30NZBusLaWeShmXKu157MBCNFSdFDkz81BwfWhDW0oy2NFwTImPWpY2Dn+OK4leG0mc3SHn3tBDJnRvcRAFVCUG/QKoFjQQdw7DV5+v3/iQBUUiP1wSY9AL/4UiWFT4EW4ifHV4UfyO2YprlzKOMNiw9tYXboyg6pz6goU7hgFO9AOwrvvkSb9QyS/wLK0Ckrhv6Xi//SPJhAT72jDzguyt1jGGIeUp8dx/UlHsc8bBa1Htw5ud7JzoQMoghV42a2iSeRk1rdVHP+vC+uGrtSDxqMHuXot11OfR5/0iFGv34y35yda02VsX+wtFVQNVPE2S9d2FepILvIXIcHH5h3GefzoOtprW+yUMf35Uyd9nszO9lZIU+HQ8b/ZZlycEbyiNZhV8Hxzx9E+jWv2IoQPO99syk0u5QyTkbUV620FvkV/zBOee+ob6+5d8heaM7z4xRtjZKLMKUtiyvLq5e+mfeppOxXi+FC8wB5DDYznr9P5ESuUhjH/4so5CCvTNLnT1hjg8NDMgEmUz7R60OqhadeL8FslMOr5tYp+LEDIRv7tn9PvzGWNOSilRBJOrxZB/F8H1leBT+UpR5pvEtsbvkHNvfpu8KtfDFZtvkGHp/E0SlWFcnoJ3WjojxkRp+VcGcoZlCz7ehX2yL9/1y+JsuC4sXt38qAtP/A6K7nthLEBXLf/XqG0IJA7OBmt5nNiz3hLflKzodQF0c9K/nYBcJP5rcbAeMZFrjHCfjZ2p2EvVTJKEpkBv8XXxzJKm1n28hAIQVALDK3BZU9qgUf5DOOi8aM5cjpiyGJXmKXm2TcYLY+z7KMaNQyqotC3i9J38cfSHLyM59swav0fdqNE8HnU82XzA SVdlB3dT T1txYu9m6+KCuXx1XXo7JcqZX5kzGns+5c8JZy3AlLYuU6x5j5FgdYxtO686Z8TQOYiu37j6365l4+dcQ1Ir8q+WpUXKbZWULd/67Lqclny/Qz7WgxVCXQ7CSj/MUlaerwZFz53Y3VWonLZc+Fv/MTLM+kgqG8Dpcl5VTZEkgmkRa61m44bfh0DTQ/e7m9kzGtqECvH4Fipr69oMfmatwjiXKcNm9pK9Pk1JmlRhnpjQkz6974/Sql53+UDIMzr4FBP/8pZzw8onxf6YRk5KFHtFy4cr/4ltZlLP3jSF3/Mg/vJnahHPSQQvkAUOIXGvEnXWtmSBr3Isg36w= 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: From: Ran Xiaokai When I did a large folios split test, a WARNING "[ 5059.122759][ T166] Cannot split file folio to non-0 order" was triggered. But the test cases are only for anonmous folios. while mapping_large_folio_support() is only reasonable for page cache folios. In split_huge_page_to_list_to_order(), the folio passed to mapping_large_folio_support() maybe anonmous folio. The folio_test_anon() check is missing. So the split of the anonmous THP is failed. This is also the same for shmem_mapping(). We'd better add a check for both. But the shmem_mapping() in __split_huge_page() is not involved, as for anonmous folios, the end parameter is set to -1, so (head[i].index >= end) is always false. shmem_mapping() is not called. Also add a VM_WARN_ON_ONCE() in mapping_large_folio_support() for anon mapping, So we can detect the wrong use more easily. THP folios maybe exist in the pagecache even the file system doesn't support large folio, it is because when CONFIG_TRANSPARENT_HUGEPAGE is enabled, khugepaged will try to collapse read-only file-backed pages to THP. But the mapping does not actually support multi order large folios properly. Using /sys/kernel/debug/split_huge_pages to verify this, with this patch, large anon THP is successfully split and the warning is ceased. Changes since v2: - fix two coding style problems suggested by David - update the comments of the order-1 case suggested by Barry Reviewed-by: Barry Song Reviewed-by: Zi Yan Acked-by: David Hildenbrand Signed-off-by: Ran Xiaokai --- include/linux/pagemap.h | 4 ++++ mm/huge_memory.c | 28 +++++++++++++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index ee633712bba0..59f1df0cde5a 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -381,6 +381,10 @@ static inline void mapping_set_large_folios(struct address_space *mapping) */ static inline bool mapping_large_folio_support(struct address_space *mapping) { + /* AS_LARGE_FOLIO_SUPPORT is only reasonable for pagecache folios */ + VM_WARN_ONCE((unsigned long)mapping & PAGE_MAPPING_ANON, + "Anonymous mapping always supports large folio"); + return IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && test_bit(AS_LARGE_FOLIO_SUPPORT, &mapping->flags); } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 317de2afd371..155d6a9f73be 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3009,30 +3009,36 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, if (new_order >= folio_order(folio)) return -EINVAL; - /* Cannot split anonymous THP to order-1 */ - if (new_order == 1 && folio_test_anon(folio)) { - VM_WARN_ONCE(1, "Cannot split to order-1 folio"); - return -EINVAL; - } - - if (new_order) { - /* Only swapping a whole PMD-mapped folio is supported */ - if (folio_test_swapcache(folio)) + if (folio_test_anon(folio)) { + /* order-1 is not supported for anonymous THP. */ + if (new_order == 1) { + VM_WARN_ONCE(1, "Cannot split to order-1 folio"); return -EINVAL; + } + } else if (new_order) { /* Split shmem folio to non-zero order not supported */ if (shmem_mapping(folio->mapping)) { VM_WARN_ONCE(1, "Cannot split shmem folio to non-0 order"); return -EINVAL; } - /* No split if the file system does not support large folio */ - if (!mapping_large_folio_support(folio->mapping)) { + /* + * No split if the file system does not support large folio. + * Note that we might still have THPs in such mappings due to + * CONFIG_READ_ONLY_THP_FOR_FS. But in that case, the mapping + * does not actually support large folios properly. + */ + if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && + !mapping_large_folio_support(folio->mapping)) { VM_WARN_ONCE(1, "Cannot split file folio to non-0 order"); return -EINVAL; } } + /* Only swapping a whole PMD-mapped folio is supported */ + if (folio_test_swapcache(folio) && new_order) + return -EINVAL; is_hzp = is_huge_zero_folio(folio); if (is_hzp) {