From: Qi Zheng <zhengqi.arch@bytedance.com>
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 <zhengqi.arch@bytedance.com>
Subject: [RFC PATCH 5/7] pte_ref: add track_pte_{set, clear}() helper
Date: Thu, 25 Aug 2022 18:10:35 +0800 [thread overview]
Message-ID: <20220825101037.96517-6-zhengqi.arch@bytedance.com> (raw)
In-Reply-To: <20220825101037.96517-1-zhengqi.arch@bytedance.com>
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 <zhengqi.arch@bytedance.com>
---
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 */
--
2.20.1
next prev parent reply other threads:[~2022-08-25 10:12 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-25 10:10 [RFC PATCH 0/7] Try to free empty and zero user PTE page table pages Qi Zheng
2022-08-25 10:10 ` [RFC PATCH 1/7] mm: use ptep_clear() in non-present cases Qi Zheng
2022-08-25 10:10 ` [RFC PATCH 2/7] mm: introduce CONFIG_FREE_USER_PTE Qi Zheng
2022-08-25 10:10 ` [RFC PATCH 3/7] mm: add pte_to_page() helper Qi Zheng
2022-08-25 10:10 ` [RFC PATCH 4/7] mm: introduce pte_refcount for user PTE page table page Qi Zheng
2022-08-25 10:10 ` Qi Zheng [this message]
2022-08-25 10:10 ` [RFC PATCH 6/7] x86/mm: add x86_64 support for pte_ref Qi Zheng
2022-08-25 10:10 ` [RFC PATCH 7/7] mm: add proc interface to free user PTE page table pages Qi Zheng
2022-08-29 10:09 ` [RFC PATCH 0/7] Try to free empty and zero " David Hildenbrand
2022-08-29 14:00 ` Qi Zheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220825101037.96517-6-zhengqi.arch@bytedance.com \
--to=zhengqi.arch@bytedance.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=jgg@nvidia.com \
--cc=kirill.shutemov@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mika.penttila@nextfour.com \
--cc=muchun.song@linux.dev \
--cc=tglx@linutronix.de \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).