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: 4yppm7e3naiscqa3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CLUB0klNJCBlu6kHscfGs7UDS67+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;