From patchwork Mon Jan 20 01:22:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13944631 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 68120C02188 for ; Mon, 20 Jan 2025 01:23:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6A6D280002; Sun, 19 Jan 2025 20:22:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C1A6A280001; Sun, 19 Jan 2025 20:22:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ABADD280002; Sun, 19 Jan 2025 20:22:59 -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 91146280001 for ; Sun, 19 Jan 2025 20:22:59 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 05EFE1C9DF9 for ; Mon, 20 Jan 2025 01:22:59 +0000 (UTC) X-FDA: 83026081278.18.A3ED6DD Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf05.hostedemail.com (Postfix) with ESMTP id 2118D100007 for ; Mon, 20 Jan 2025 01:22:56 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dYkyX+gi; spf=pass (imf05.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737336177; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zWHuTAWWoazPvHXkzMU7dW3hW1AXAy1GX7BhKkGbTX0=; b=KQ2SUn6OwbzikR1MXyFDcvU6PbXdrVaZo9aeWGxtpyZpQlqkU0th/hcBBOttL2pSYnbQzQ 96+BaDVkGRiJeE8bEt6c0nm+bs5qqioerohUwx3ZJ6RkD3rZu+LIk5asnbcomwpRR5QSBJ qj4zJT9bBgIwUnjTg4M83ksf9SHfnKk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737336177; a=rsa-sha256; cv=none; b=yCv3A/QCYvJ7uOUpzM7nLQa8VvxJeO9fFYjon4fkkvJeZfuxmho0Artt9NVnT8PT8w7M3n 8WnWG84DAaIfG92ww6GVmakR1+PPj1kdm63s1QeG4IhGRUxQn/AmNPblSXX/DfpkZTvQD8 zDEYbNf37m/6Y3wytSFJQB8BAaEatII= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dYkyX+gi; spf=pass (imf05.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2ef6c56032eso4998450a91.2 for ; Sun, 19 Jan 2025 17:22:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737336176; x=1737940976; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zWHuTAWWoazPvHXkzMU7dW3hW1AXAy1GX7BhKkGbTX0=; b=dYkyX+giv/9iPd7chCEyjZNnfPKYQsNjS1NZ55C484aycrlWnuQCmFqPN9+o2Xgy0c J0IXd7oubfwA41it90FVamDCIWQbIfJulngNufVACIcCFJG8KTGCiFtrNTcfvVzNUc10 0QyW6GOaz7rJDK6HM//DhrDLb+jDXr8M514nBUdtPy2O+fX2OjGdKNyStO5pTzUEu5oC LrdrOqEL9KH1MQZdSDGRh6LUvQshEDUbzLhblJQX1nD0KWuPQAU2bhIS/F/PeQTdHcWI ySLKQ5RMen5dWXiMLysES43DWH3xqXeTlHTBXd16atZgoRuB/eciDnREpiy3brspwVDn 6AGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737336176; x=1737940976; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zWHuTAWWoazPvHXkzMU7dW3hW1AXAy1GX7BhKkGbTX0=; b=dlBzeaofnzYyLxz72UbFXEVFBH87EbUIY4Ie1ozrJFG9TOvxocDr06Mcktvz/xjY8i C57jlbTpY0CDVd4kvkOrFuY6U20APTRm1Dy1ytQeuRAFRkn8xCpIagElv8GgFneZvxna DOb6/76+YLArXZmTohW2QSiAZHfhBsg0M5NbVWyQHVL+u9WsFtKRXz7gv3ME1bYabGgG mVx/9x0hUrmMZW6bFZBhsy4PSE02KQqW88Zxn1V+6KTdskaps0dkX2pdVQ/JX/QXui+N iuZ2Fr+aAp6EVUMpUTyf8+azrMqwrhJdm6PSQDbhPHDr0DNeKYRIDU7Am3f4+Y1qBOBn 6gZg== X-Forwarded-Encrypted: i=1; AJvYcCXVszWvIfHNFVGUCGZzM6TpXb1w62JxP+aRXoLArpzsov3JqzeHp3lG7R9bv+icjAFSZda1T9vAqw==@kvack.org X-Gm-Message-State: AOJu0YwoGRDtzVvwmmKBztIFGasvXvEt6ImrRvGx1PGHw7EDRI6Crkdq lS/hWHXB0UptKqdPqVxgMFw0R53Vk07zykgxPfV2gj5PWbPgI9K8 X-Gm-Gg: ASbGncsfFYJ4rgwrMXi4EQMtgExjHwjVCr94rDQbLkUriY0a8t1DrEB9JZulZSvsG+D yprrVL9F2E7sV2SwFQtiSw5w/3XjTMV3K5TQzlw5R0fDWfNFs3O3lIAHoYpMpoIQeaoARlqAWtF t3KxQO6r3wO5mq40XhUsE9VgzjccIvpOT49vZN489hZNLPDTCD1HVqetel4WJkFVUDiczQ9h0sv xu0gbhVUbvLzNK9kJ3lug0DODhFYELSpDhfRvfKIRNbgSkyGEPmIw0EuMkzM2hnIkJfLYc58T7f vw== X-Google-Smtp-Source: AGHT+IFNXuIjOpZfo/SccX3mXibk/Kq9SaUdfRtj6M+BjmKaE13u0uSI6XF6CkslRL76AcNd2Ep1XQ== X-Received: by 2002:a17:90b:1f8b:b0:2f6:539:3cd8 with SMTP id 98e67ed59e1d1-2f782ca2291mr17790924a91.18.1737336175844; Sun, 19 Jan 2025 17:22:55 -0800 (PST) Received: from EBJ9932692.tcent.cn ([43.134.20.116]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f77629acd5sm6945514a91.37.2025.01.19.17.22.49 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 19 Jan 2025 17:22:55 -0800 (PST) From: Lance Yang To: akpm@linux-foundation.org Cc: 21cnbao@gmail.com, ryan.roberts@arm.com, dev.jain@arm.com, david@redhat.com, shy828301@gmail.com, ziy@nvidia.com, libang.li@antgroup.com, baolin.wang@linux.alibaba.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Lance Yang , Mingzhe Yang Subject: [RFC 1/2] mm/mthp: add pte_range_none_or_zeropfn() helper Date: Mon, 20 Jan 2025 09:22:29 +0800 Message-ID: <20250120012230.3343-2-ioworker0@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250120012230.3343-1-ioworker0@gmail.com> References: <20250120012230.3343-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2118D100007 X-Stat-Signature: 4yppm7e3naiscqa3nbwt88bexqcs9g3r X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737336176-743055 X-HE-Meta: U2FsdGVkX1+ypZTaXAUZClamMSez+J6q53ggSRTjuBU2obwJv9+7K+yrE7Yqt3CGOozLfFs2h1jCQQVuGzaFDgOA/b3/HAb7LfLi4BWfi4V7SKyoiaDbJTjxaweDX4TWVlsr5w1/hXj2rK7YsHf2so4HRvkzHOEXkT7maO8qr4tfIqauuWoZekr79fAGTm8UuRhnfU91sZSxASxD0Ib6B3SnhqqGfIV78XsTdIybPsWuka1QXGqdM0xd/60BBjH/ctTOxQhAaE/3aqmE4WQE/nPjg1KJWyrWy0ZCjiQP7671CHcqKo9yB5Q5rVJjzzSpNJGTcvtkiZVV7lEEMCOfYIH3wvqn3siR/hWWNQsSYcADq2RvxLHbhta4S0af1xkmhl+U/phVAnXjS9eAkdHWzF46uVmE5kCuJnTw7GxtrjJj/RdKnN6JIb50FyJ5c4VhUYyJ2ZXJQZtGr5f4ywPJunW2ACDX7TyGbjD861BKeDeIGrVGoW/Clad0HSKezJxT6F60IgDs5NmFAw5qQADBNksV4R/FosdpJ61/plpZql6BnmTyqzVOhfd3rErfq2L1A2prbhlMikDyQuqtrBmQadOLBd52NbuJOOgAEInqGxu4hj7eew88e4/P9AXfnQzxLuU8jMyVHhboT5fCJipqa8YdBQuonZm4UGWLEKYsgpUFWgaqU6SGLbN8db376Dw6NQWlu0A7OTpHCGpeq3aD8JejpaM8WgZOGNcYZp8J2iK0r+HQ9XSnUgytk+/0q2oHbn6YAJERyp/dQ5g/sLtGr6yLIgUG5gaw2jfhkBcl3msPNnzc4Y6AubEY9Xzt7804m4dgynZREy1iJfRksPGMt0RG0lTgV7doTJ7fZKr6jND8DTdQSx1iaCrHTDpHh62WAND2y0dFIGubj9Eoyy0slUFFbKkTw9hmUw5cYcFIf0QdWw3RUHwhC69m+lzmKuzTk7ddmZM70mcQTadyOip fes3fl0m 2QW+ErF0gm94+9zi1P7ohrcF+R9HsPCLUB0klNJCBlu6kHscfGs7UDS67+xTltpV0J46y4euw28+8sXKZ11PEo9rWhCXdpGynvvCXag1IR4D0anNtFmsLl8MJc0pLAlEZWGqsDaz9f6x7loeUxODncoLUIBC3kygn9UxDJsvaoiec8ghUoO1aJAlObFA1LDPBk8J8z7GUVrRi6aHJFkYox7tflAiGRSCmPrDCZgxxXG+EnJfQyVicQXSZ4AQIUmF1AqPy7lKuslaChBkEk5PJ6Zt9HEGr+HVkS8x2D0jWybDhJc0VHd7K365HPk2DsQl83EBShgNE24rar1h/+61Hnp927tTsfjvFAnbiu0nILahhwP8ib9mGfDLCliY5eGEdvsExCEx6+alCU6hGdu1C7FgESR63ucmGABhgO+Xn5GrLZR5Zq0QTvz24ttYHg3p1uiHAViaYn3ApqKG7y9mtsJw4/KD3XwBFC8jB0rE11+mH+7s= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In preparation for relaxing the mTHP PTE mapping restriction on the demand zero page. We intend to provide more opportunities to take advantage of mTHP. Signed-off-by: Mingzhe Yang Signed-off-by: Lance Yang --- mm/memory.c | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 2a20e3810534..4e148309b3e0 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4724,18 +4724,55 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) return ret; } -static bool pte_range_none(pte_t *pte, int nr_pages) +/* Flags for __pte_range_check(). */ +typedef int __bitwise prc_t; + +/* Check if PTEs are not present. */ +#define PRC_CHECK_NONE ((__force prc_t)BIT(0)) + +/* Check if PTEs are mapped to the zero page. */ +#define PRC_CHECK_ZEROPFN ((__force prc_t)BIT(1)) + +static bool __pte_range_check(pte_t *pte, int nr_pages, prc_t flags, + bool *any_zeropfn) { int i; + pte_t ptent; + + if (any_zeropfn) + *any_zeropfn = false; for (i = 0; i < nr_pages; i++) { - if (!pte_none(ptep_get_lockless(pte + i))) - return false; + ptent = ptep_get_lockless(pte + i); + + if ((flags & PRC_CHECK_NONE) && pte_none(ptent)) + continue; + + if ((flags & PRC_CHECK_ZEROPFN) && + is_zero_pfn(pte_pfn(ptent))) { + if (any_zeropfn) + *any_zeropfn = true; + continue; + } + + return false; } return true; } +static inline bool pte_range_none(pte_t *pte, int nr_pages) +{ + return __pte_range_check(pte, nr_pages, PRC_CHECK_NONE, NULL); +} + +static inline bool pte_range_none_or_zeropfn(pte_t *pte, int nr_pages, + bool *any_zeropfn) +{ + return __pte_range_check( + pte, nr_pages, PRC_CHECK_NONE | PRC_CHECK_ZEROPFN, any_zeropfn); +} + static struct folio *alloc_anon_folio(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; From patchwork Mon Jan 20 01:22:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13944632 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 0DCACC02187 for ; Mon, 20 Jan 2025 01:23:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6ED4A280004; Sun, 19 Jan 2025 20:23:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 69BBD280001; Sun, 19 Jan 2025 20:23:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 517EC280004; Sun, 19 Jan 2025 20:23:06 -0500 (EST) 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 33024280001 for ; Sun, 19 Jan 2025 20:23:06 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D0F5E1A0EB4 for ; Mon, 20 Jan 2025 01:23:05 +0000 (UTC) X-FDA: 83026081530.18.6A3A62C Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf07.hostedemail.com (Postfix) with ESMTP id F221B40002 for ; Mon, 20 Jan 2025 01:23:03 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G1qBWwaj; spf=pass (imf07.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737336184; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Hgy3I40fUOIkhs3A7OczOazm565K5yUyg2x9qlHIL+c=; b=rfU/CJFLk5brLkr6TrBXfVS8Nkl20xEY1fZTmHmAMqGZU1r5oF6/J83PnvU5y8PZgQjj7r ipx/Iad6tXWZlbfiAuX0cwz6Y5jCI78uTA5u/IMwQ7LvMjUrfSXV4Nxg4DAH4Nbz4wESFS TgoFK08XxCF3CTJe68IpyjH/e0XNerQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=G1qBWwaj; spf=pass (imf07.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737336184; a=rsa-sha256; cv=none; b=8lJfmcPs5CiNDx3kAocVP0hjW/ZrQ1CHmPAr6lKPAykurutJ9hr5vqsS0aWhex+nY1WpcI ybdUQ+YR0SjVnsTKSWAUnkHJVcVc+zkJuBNqN9uF3or1oPhWH7VH5rthQ/KP5eKlsC15jf ZAYUwIs2sJ4bucogK5Z1RGeVTqfhJSY= Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2efe25558ddso5033370a91.2 for ; Sun, 19 Jan 2025 17:23:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737336183; x=1737940983; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Hgy3I40fUOIkhs3A7OczOazm565K5yUyg2x9qlHIL+c=; b=G1qBWwaj8Ng5ZDpSjTqT+4ulow8m6sQSnOa7cTqrBTnct4Lc/77nIf4LtP9imBOyOX B2Z/Cq80Eypa+eFZZjhSrGTtSr4sUzi1Q96/T0SfjHBKqoLCsJBHYxBvXvdRhLsxWMyO 8/7LWudrI71CigKnqGa5PM/v0S4SvJSGO69uzvWRoAwLtkWWgPLAk9l/GwO8q6pZvnfC yZ3IzB/AqOv6osGZ5PE9drvNB8PJNmVdCEUjBnzwmHzDlaVy5AZ0uE8bDH7cQirFf9XD vjJQsk6znAC2UJVbjCb0XpEsQNfw0/Bpwqleu6flVDKcc52GlQNB7aBW8HEqw4u4V7Jx syZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737336183; x=1737940983; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Hgy3I40fUOIkhs3A7OczOazm565K5yUyg2x9qlHIL+c=; b=aY7xoJt13b7X4pWMPPTrbW6OGXXCWqdm38BU8mG7V+4Lyeb+V5P1kuuLTxKy6Qw1AC HLlS/hkv87Rt2UWZSjqt6z7WrOYET/x9YQ/HJNnBQ2SB44BbbX8ckn5SUjqQlCLPlcU5 ibCmFSFRJiDl78XYTRh6uan3sd3uTVeYTWPR46wVObRoljLt1jAt1muTWHScwgUudq7C NZglSgZq0AuRib8dsWIoSVVBfWWtjbPWgxh2lvOSdwXPP7Q4xwqbGi6MPD2/R8NHAYlg gJ8LsQ+1ofeSgk+2kxNpJPQIRjaa4xmmL3EDGkja63yKZtnAwgHLIsu7WYVoHk/urEUl yZNA== X-Forwarded-Encrypted: i=1; AJvYcCUiodWm7Qu7a15maE83ZZ8WB6QwFAJHOetjBJ7NuHUmGA6yf3M79sB5JCqLGJ2+8sQ33ZgkFBKA2A==@kvack.org X-Gm-Message-State: AOJu0Yys++ELf5p0cLR62m2Cllqt2rjaSY8KnDshjmG4JIlebIgm1WgV NCsSESGfu2yyzc1FK3SyqoLmM1wLZO+k7dDLXiDctGHlWvJn0MEb X-Gm-Gg: ASbGncu3IkYr5G4PZyK77Lblbw4cOCSnCnXAC4fGgC8tiXMdicUBgWDj1zexxecyOmd oFb6GrhsjqUkrJPcU2BfcCkr4hPYnI0aEhc5m4RjI/+B1OK4EryncOOqVGjk7Be5ugQTAZ1yKZU WOVGmSDuj3BU9rojTUTfTrCajrT1Wuu3wlKwNbSfFnQHlNQLOulPtWpmptUzGl9WTE/zL/BVWVE GrwUBhy5rw2NsTD9Wslx30VBs6Mp6mipSGfJ1FaEA77FGMrzvF7e2afCUOGO4O1GtgeCeux4eWs Ng== X-Google-Smtp-Source: AGHT+IGilBEXOdf30oMeYJSnhHL+cLRO5bpvVan9EzDDik+E4juIxcZXxIWdMZEkaiPwt2xug5cbNg== X-Received: by 2002:a17:90b:4ed0:b0:2ee:c1d2:bc67 with SMTP id 98e67ed59e1d1-2f782c99704mr17459100a91.16.1737336182714; Sun, 19 Jan 2025 17:23:02 -0800 (PST) Received: from EBJ9932692.tcent.cn ([43.134.20.116]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f77629acd5sm6945514a91.37.2025.01.19.17.22.56 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 19 Jan 2025 17:23:02 -0800 (PST) From: Lance Yang To: akpm@linux-foundation.org Cc: 21cnbao@gmail.com, ryan.roberts@arm.com, dev.jain@arm.com, david@redhat.com, shy828301@gmail.com, ziy@nvidia.com, libang.li@antgroup.com, baolin.wang@linux.alibaba.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Lance Yang , Mingzhe Yang Subject: [RFC 2/2] mm/mthp: relax anon mTHP PTE Mapping restrictions Date: Mon, 20 Jan 2025 09:22:30 +0800 Message-ID: <20250120012230.3343-3-ioworker0@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250120012230.3343-1-ioworker0@gmail.com> References: <20250120012230.3343-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: F221B40002 X-Stat-Signature: rjcyc9pdffj3wfqxz15woirapqnjnbsn X-Rspam-User: X-HE-Tag: 1737336183-911410 X-HE-Meta: U2FsdGVkX18CoCqvyIad4+xBJvR8zCcz3NoYDwwdk2Axd2ef2v64zQKztLg71wtpmwyE5jEK3Rb93V+vy2sK0+S2YLzdlMPWUCkhfdTtTjmDSSigCnLkHDLTK1VUDtJoJQn8lBBAFEMiaKBurc6ADQ3y/ndC+JUahB6Y18IJZ3EcZmdbumufK/H/25oUIbct35zosvmQsXdQXtO/36yHnmoMLzYP6aBqWmgWxO/3fue1fa+b5GbEE0LbvW/7+VM1w+JWeE1S6kJdaovxcleu/ctKQQGVTTjr3gI14s0jo0ZUI2K1mm3A34w27sJxRZuURnt8E4IqRtdSkVkkR0UskWG6TcJruTrkDiTPhm7w24epaMmvVmsHLgsb1bP+EXDG9kdqNQxTDodyWef0DXKh1Y0gSIAFPQn97cDNWHpAFepGxlym2DLyI3lwQxAZlyHm7+G0P7GAQA+sy2hQMaoVM95TEjwkTkwD62fo+sAS+9VUCvn3BURRleK3zik0fHncG+nrGHaMlFLRhRaRHx7fod14W9y4WZNd127pktOuULYNd8+1G4YWWXKG2MPqNjm/W05Kt4a5Vpf6kv39PNH/qYgg9LTzgeShdFwZDv8nEn5g7Ey0GwyzJfQJ12flh5YjGkkryUh8UcqMxo0y8AmgwdpcVY6hMR0oOknllupF6EdpRPR68W3SzPqZcO8eMC3ageEcRQX05AFjiuyHN1JU8K2HiUgLMLk8L2IYBZwG8+14C2whB/gu4Cxasxs4m6XVyBQ+CnkOcJAlhBXleo1e/kpRGGxSHS48BYgw6xRmcr+KiVItUkzGuw72dWoaDNc0H+e4U1nxAEdg1UziMmM0MJwLbA45Be9praHDo6BMA0F3Zm8vE/rrSRXvqKxOL254oY3vM3JVl26gW1jnU4tH6WIcca1fuq5HRfL95kqItC5GY3EUk0V6pKu4y05cMK+7/Ykx0TUAzUZom+Lqbik opEsMYQ1 pPpGSwhUBsvYWEORCEjulVXPGk5diVN+kCGn0UaggSdU6FzKzD/wxX+7nLQufWcMviG/vWZqsMeAIzUhS8PpFC6ao3olFv0RTU5V3Bpwf3X3+qka5O6mltlXMJHExxRRNZp6XKbNQKmxZG5xNVn0YPODrqdfSeLjxuhlgzSdao5WgllQ3+ZPx9l3eB/en55gjCABAYtnYFJNQBZfN/4IfiM9J68iwaQxpV9hdSl+t1E4j0SnL74hqkMVArDu2qnazvyIlLLuwjNflGJT88rYAcRYEBZpGsGb9L4irTpwJdGp1mniYC5BxleYJWxHsWSNWJKIA9CIwcJCH/8HJ65YiDO7XcTdgL3vjanjy8qflAcY8SPt+5qgUOfIJseHguR6WTYB+rJmhTR3fqEGVnC0Tt8cjs/PY86RhOPmytxcySdvxjBa+21m/IrQQGQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000142, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Previously, mTHP could only be mapped to PTEs where all entries were none. With this change, PTEs within the range mapping the demand-zero page can now be treated as `pte_none` and remapped to a new mTHP, providing more opportunities to take advantage of mTHP. Signed-off-by: Mingzhe Yang Signed-off-by: Lance Yang --- mm/memory.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 4e148309b3e0..99ec75c6f0fe 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4815,7 +4815,8 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf) order = highest_order(orders); while (orders) { addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); - if (pte_range_none(pte + pte_index(addr), 1 << order)) + if (pte_range_none_or_zeropfn(pte + pte_index(addr), 1 << order, + NULL)) break; order = next_order(&orders, order); } @@ -4867,6 +4868,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; unsigned long addr = vmf->address; + bool any_zeropfn = false; struct folio *folio; vm_fault_t ret = 0; int nr_pages = 1; @@ -4939,7 +4941,8 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) if (nr_pages == 1 && vmf_pte_changed(vmf)) { update_mmu_tlb(vma, addr, vmf->pte); goto release; - } else if (nr_pages > 1 && !pte_range_none(vmf->pte, nr_pages)) { + } else if (nr_pages > 1 && !pte_range_none_or_zeropfn( + vmf->pte, nr_pages, &any_zeropfn)) { update_mmu_tlb_range(vma, addr, vmf->pte, nr_pages); goto release; } @@ -4965,6 +4968,10 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) entry = pte_mkuffd_wp(entry); set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr_pages); + /* At least one PTE was mapped to the zero page */ + if (nr_pages > 1 && any_zeropfn) + flush_tlb_range(vma, addr, addr + (nr_pages * PAGE_SIZE)); + /* No need to invalidate - it was non-present before */ update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr_pages); unlock: