From patchwork Thu Jun 6 09:42:03 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: 13688201 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 DEB2CC25B75 for ; Thu, 6 Jun 2024 09:42:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BA8F6B00A1; Thu, 6 Jun 2024 05:42:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 76BDA6B00A2; Thu, 6 Jun 2024 05:42:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 633B26B00A3; Thu, 6 Jun 2024 05:42:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 444606B00A1 for ; Thu, 6 Jun 2024 05:42:15 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BD17781494 for ; Thu, 6 Jun 2024 09:42:14 +0000 (UTC) X-FDA: 82199972988.28.11436FE Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.35]) by imf30.hostedemail.com (Postfix) with ESMTP id E7A2680009 for ; Thu, 6 Jun 2024 09:42:11 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf30.hostedemail.com: domain of xu.xin16@zte.com.cn designates 63.216.63.35 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=1717666933; 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=vuIaxRIhb5hZGMnBYkACkHZr40Z2UBkRk4QEvo/xDyA=; b=kzY2LKACWSuHImEcUYb+QBYa80Jgx4Z9Gvp0s6iC7TQtDyDAtUwCPRiqswUWdQqIPC0c79 8LNAw5bBygy/hsMQWZ/j8XLCFYd03IEN//qJyG0RD3E4PeIw6WfwnHCUmLh8J4ZLLOkikC e/mRtleU4vMz8z1wtqOETzmb8xJ/kMs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf30.hostedemail.com: domain of xu.xin16@zte.com.cn designates 63.216.63.35 as permitted sender) smtp.mailfrom=xu.xin16@zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717666933; a=rsa-sha256; cv=none; b=cW3Yxr6c+1eVeaHKKZ8F4hmuTIKo6M9lk86t0O4RaklYV4+w9304hpZVzled3NT5QBdpGd PjhMBe+FdzypVuckfDxcNIXlNlTZ2l6xSXzd2nEDvYTzJH4/+qlK06IR/u91+rzB3AheJw /49ETbkMMmzyKNZsQistGF0xYV8p7Gs= 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 4VvzrG4pqkz4xPBc; Thu, 6 Jun 2024 17:42:06 +0800 (CST) Received: from xaxapp03.zte.com.cn ([10.88.97.17]) by mse-fl1.zte.com.cn with SMTP id 4569g0MO058198; Thu, 6 Jun 2024 17:42:00 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp02[null]) by mapi (Zmail) with MAPI id mid32; Thu, 6 Jun 2024 17:42:03 +0800 (CST) Date: Thu, 6 Jun 2024 17:42:03 +0800 (CST) X-Zmail-TransId: 2afa6661846bffffffff819-af71f X-Mailer: Zmail v1.0 Message-ID: <20240606174203124_OW-VQZ_ZLm4lGEimA-K9@zte.com.cn> Mime-Version: 1.0 From: To: , , , Cc: , , , , , Subject: =?utf-8?q?=5BPATCH_linux-next_v2=5D_mm=3A_huge=5Fmemory=3A_fix_misu?= =?utf-8?q?sed_mapping=5Flarge=5Ffolio=5Fsupport=28=29=C2=A0for_anon_folios?= X-MAIL: mse-fl1.zte.com.cn 4569g0MO058198 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6661846E.000/4VvzrG4pqkz4xPBc X-Rspamd-Queue-Id: E7A2680009 X-Stat-Signature: 1azc44ddrik8kbfhxoux5khpyct9d1ek X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1717666931-514785 X-HE-Meta: U2FsdGVkX1+QPXGVv4XYTyWy4yMVkguzSnAujT5Br96w9ewCktfxsiHshBuR/yB60X8nC4ntRi2fZNFfA/oXrrNezKQ3wxwuc5u4gKTdnGCVJOfpaPcXXE3j1/JBnZUhYI12knVCDOATPr371igDwOLmsAZw259nK3Wf0M9CV7RVFpDiq1x/KCZFt3B4vPaPN4Ahkdzt4FglEjnAtPjZ9sW9DUlymTe0Mcx3TUmf5KDD0vAVo1Ls3sO7sAnY7d/gl2B5Vc4mN7OVUYBQ5WDLB8wqvboxJbtau1gRgelQe/4nTKCbBjUJcZogKJBIOdHJjfYnh63Sxrq2yzh52rmkPxTmknoc59n7UKgozifchEREuIER07DiCApnM/g17zzIZIHl9qGxjbsoN9kXLUCnK3OtRaycr6BUWivq1Z+ejL35L7DBxEnwJlFihUb8soszlvfm0qH7NHYtmTpvccF//KsdfsJuPR5hOU4pu3fQHdP5RT6BoQz7DnJHJPG54d1inl4IXTlM7oIxUkt8C+raEsL3wrK2ddTSBr/zKqMjW7DuDA4hJ1oewMbaJwA9EfL1qwiTw+I9YKql0O/VW5ied4pvJCHElhXRjt6YxcM3sYap77/hvZbcvfniruBCDjZgIYbFwOanjOiAzL78RW87F7uFe6afPQ8ArI2F922mBMW0IOt9d0TxfujjCf1XPyfCYBZLLcC8XT4T0Pa48X+sm9MThOdd00+fF4VxJWmCxAbMPV7pJaX5ahYboqFFPqn5eFXF4tBl5zhM/6MuaQDUXFSd594ooDkUlNX315HZl+/p+EAghEx5SCqfPCcsjxdXyB31ZwNWpOfmobZZGcSNP73MRYP2HPOXat6+rcA8IPcL9LebSRVPMyZAs9Bc4PkjpMO40iLSb17ZFiH+7FxOAAwUXNXZEHM+LEvzJ6VD4l6wL1H0CNaqwesOFEar58aqN4C9D51hrlSpxERI4RP UlRI/IbO ihy+hvuq61OaJK+G3i9k8QF/edWr4vtcU9pFo+7gqYoQABWMv/3u0ZrQmqBW3aYHGBqCZcfqF5z0XcJrW38YdLKIWb92hhOZQC+17PktWTRDt74qn/ZUwhSXs/he4tVnrX1Y148R9BwjAiz9l733GBKN+D0b6oxQnUTeFJuST8vg2ESYVSGhqbx4vrqY3OjVmlI+7lRSbBj5SoGEcf329OAVlJqHF0jiHFQSL 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. Signed-off-by: Ran Xiaokai Reviewed-by: Barry Song Reviewed-by: Zi Yan Acked-by: David Hildenbrand --- include/linux/pagemap.h | 4 ++++ mm/huge_memory.c | 27 ++++++++++++++++----------- 2 files changed, 20 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..62d57270b08e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3009,30 +3009,35 @@ 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)) { + /* Cannot split anonymous THP to order-1 */ + 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) {