From patchwork Thu Aug 25 10:10:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 12954471 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 5BDF0C28D13 for ; Thu, 25 Aug 2022 10:12:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F211294000C; Thu, 25 Aug 2022 06:12:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EAABF940007; Thu, 25 Aug 2022 06:12:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D24C194000C; Thu, 25 Aug 2022 06:12:01 -0400 (EDT) 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 BC8B8940007 for ; Thu, 25 Aug 2022 06:12:01 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 989D3C199D for ; Thu, 25 Aug 2022 10:12:01 +0000 (UTC) X-FDA: 79837699242.06.2050CA6 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) by imf06.hostedemail.com (Postfix) with ESMTP id 53DE218000C for ; Thu, 25 Aug 2022 10:12:01 +0000 (UTC) Received: by mail-pg1-f179.google.com with SMTP id w13so12378058pgq.7 for ; Thu, 25 Aug 2022 03:12:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=88xUUyVXHxDMCZmOYvZcYjojlwDW8mxu4+gwmvT3Uwg=; b=gCJc7t/irCxh/NsMtY1y28pn7+kS/+rCgGiEHuh81J6ucr5EX2vhMnTKm6bgUjyY0A FSH1ktN+iUPsTs7yW0vMLvS8VlTwmoaCjW/xDnuFk754nlVaaV5Sknqz7pgkUiF2NyNw kFNyD315ZQdYYefBhtxUO3hwVbrJh72K9xlvUHcJJytF8Hm+UucnXUn/bJV6GXT3KUbO qC74CEILcwNgX2/ksJAFI1VKjObcAa5bb61duB1Unbfwa4/HOAZINmkbhLrg/n2UTAM9 Y1LJVRSnkqYB4JH0aUKiKOhE31G/bmczwx7n5ekrUGCJhmRUyWj6K+A5mw3aTIe+aRlY 6TNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=88xUUyVXHxDMCZmOYvZcYjojlwDW8mxu4+gwmvT3Uwg=; b=SQBXlchEF9erhupyJYUf80lpYpnDMAVSsSgD3tz1GN8lUnRh4hsSex5x0CYvnWcgbI MhjMUbXrreNpy9DNOinrYxiaifZo1qlGYVsJiXCj9cwr632MKgTaUXQ+1m/VhBY0qyIB aaPhhpHLfqo5LdgzPWLP/cgrmdD6Vd0o9A0dMN4XzFkDDU5GaVw8KEmozcFchi3/UEsk 1JC8Perlx/GSePIWgtncr2azL0AY2yo1HdhVc5zGIMu3EQOnzaPzE07J6JqfstDdJ3OG c2kwk0qxMWV/2z7/PBknRFvS+Xp0j9jEgZCbr0Z8ePlYf2MwHZJIOwXyv9J36vEM8KeA wXRg== X-Gm-Message-State: ACgBeo26gCRo/eTCC65GK5CpStjoI1M/SeuHBRtX35eUfj9IANfoUsB8 RpBIxx8J0RsvpKqX63ja83897g== X-Google-Smtp-Source: AA6agR7wR0oMHsFEiShRabwQxkM0xbplFH+VDCYoO8SCvwJvs/eBFuYQvXDb00xbfLmujhgctEuB0w== X-Received: by 2002:a05:6a00:8c8:b0:52c:887d:fa25 with SMTP id s8-20020a056a0008c800b0052c887dfa25mr3524872pfu.86.1661422320411; Thu, 25 Aug 2022 03:12:00 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id b10-20020a170903228a00b001714c36a6e7sm8477581plh.284.2022.08.25.03.11.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 03:11:59 -0700 (PDT) From: Qi Zheng To: akpm@linux-foundation.org, david@redhat.com, kirill.shutemov@linux.intel.com, mika.penttila@nextfour.com, jgg@nvidia.com, tglx@linutronix.de, willy@infradead.org Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, muchun.song@linux.dev, Qi Zheng Subject: [RFC PATCH 5/7] pte_ref: add track_pte_{set, clear}() helper Date: Thu, 25 Aug 2022 18:10:35 +0800 Message-Id: <20220825101037.96517-6-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20220825101037.96517-1-zhengqi.arch@bytedance.com> References: <20220825101037.96517-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661422321; a=rsa-sha256; cv=none; b=XQKDU5LrGuGvE5Z3nrmalfGMLFEhKs8Q2CewxsX63l6B3PISTcJa4cf51x8p9kWLJl8gx+ 4HWfLuzYJdC9SAsrt62QBUaRLs9eMj23fJw5t5kNm57xuI13AYDWz8+gHi6DPkk7i8Vnie sYAJaQdn2gqDoh0DGejgjkOHBeKCyw0= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b="gCJc7t/i"; spf=pass (imf06.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661422321; 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=88xUUyVXHxDMCZmOYvZcYjojlwDW8mxu4+gwmvT3Uwg=; b=8ewYyNPDlh8/8XhX7xosDO9tXsUGlKm7RCR9fYdI3u/qofRB8y6zUqtZcWoWpBUau7545K 3/TnJmJQcQLr3Sv4e21pdM0uvrnBgWA8exfW7G7F0rK8JI66ORQB2qQMSCtadpibZBHbK+ RZPmVMyM7SK2yA+8hO3RoFWBZLb6lyo= X-Stat-Signature: w1ta635pat8hrj7mgkioehjhftteq5ea X-Rspamd-Queue-Id: 53DE218000C X-Rspam-User: X-Rspamd-Server: rspam06 Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b="gCJc7t/i"; spf=pass (imf06.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.179 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-HE-Tag: 1661422321-586658 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: The track_pte_set() is used to track the setting of the PTE page table entry, and the track_pte_clear() is used to track the clearing of the PTE page table entry, we update the pte_refcount of the PTE page in these two functions. In this way, the usage of the PTE page table page can be tracked by its pte_refcount. Signed-off-by: Qi Zheng --- include/linux/pte_ref.h | 13 +++++++++++++ mm/pte_ref.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/pte_ref.h b/include/linux/pte_ref.h index db14e03e1dff..ab49c7fac120 100644 --- a/include/linux/pte_ref.h +++ b/include/linux/pte_ref.h @@ -12,12 +12,25 @@ void pte_ref_init(pgtable_t pte); +void track_pte_set(struct mm_struct *mm, unsigned long addr, pte_t *ptep, + pte_t pte); +void track_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, + pte_t pte); #else /* !CONFIG_FREE_USER_PTE */ static inline void pte_ref_init(pgtable_t pte) { } +static inline void track_pte_set(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte) +{ +} + +static inline void track_pte_clear(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte) +{ +} #endif /* CONFIG_FREE_USER_PTE */ #endif /* _LINUX_PTE_REF_H */ diff --git a/mm/pte_ref.c b/mm/pte_ref.c index 12b27646e88c..818821d068af 100644 --- a/mm/pte_ref.c +++ b/mm/pte_ref.c @@ -69,4 +69,40 @@ void pte_ref_init(pgtable_t pte) pte->pte_refcount = 0; } +void track_pte_set(struct mm_struct *mm, unsigned long addr, pte_t *ptep, + pte_t pte) +{ + pgtable_t page; + + if (&init_mm == mm || pte_huge(pte)) + return; + + page = pte_to_page(ptep); + if (pte_none(*ptep) && !pte_none(pte)) { + pte_refcount_add(mm, page, PTE_MAPPED_OFFSET); + if (is_zero_pfn(pte_pfn(pte))) + pte_refcount_add(mm, page, PTE_ZERO_OFFSET); + } else if (is_zero_pfn(pte_pfn(*ptep)) && !is_zero_pfn(pte_pfn(pte))) { + pte_refcount_sub(mm, page, PTE_ZERO_OFFSET); + } +} +EXPORT_SYMBOL(track_pte_set); + +void track_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, + pte_t pte) +{ + pgtable_t page; + + if (&init_mm == mm || pte_huge(pte)) + return; + + page = pte_to_page(ptep); + if (!pte_none(pte)) { + pte_refcount_sub(mm, page, PTE_MAPPED_OFFSET); + if (is_zero_pfn(pte_pfn(pte))) + pte_refcount_sub(mm, page, PTE_ZERO_OFFSET); + } +} +EXPORT_SYMBOL(track_pte_clear); + #endif /* CONFIG_FREE_USER_PTE */