From patchwork Thu Aug 25 10:10:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 12954473 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 23D9CC04AA5 for ; Thu, 25 Aug 2022 10:12:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B02D5940009; Thu, 25 Aug 2022 06:12:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A8B95940007; Thu, 25 Aug 2022 06:12:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 906B6940009; Thu, 25 Aug 2022 06:12:13 -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 7BAAC940007 for ; Thu, 25 Aug 2022 06:12:13 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5FC8941AD3 for ; Thu, 25 Aug 2022 10:12:13 +0000 (UTC) X-FDA: 79837699746.23.15B1CEB Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf08.hostedemail.com (Postfix) with ESMTP id 0D54D160006 for ; Thu, 25 Aug 2022 10:12:12 +0000 (UTC) Received: by mail-pg1-f178.google.com with SMTP id s206so17470461pgs.3 for ; Thu, 25 Aug 2022 03:12:12 -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=4+N7HYlfplazgpIqzrok71tJ5+HL03OgoPpZtaJJ/JA=; b=Bu7SsRr4s/1augCft2+vJK75ZrctEK6Iyfqu3Pqt47wpkqwjMjUrUC1JkgguNrwL13 2hwv5VLcCfH+1TdJctlmV4tEW0D5p2uuFLXfW83lWXUXrRqn7wVkKO6VfJ+TG0znQv03 iVFjsT+BlKjd/AFLzEoJHLCxK2LgbnhjLHjQ2I0kzLOzbt/5nN04MawaWeD4I0Qm64w2 E9MSbNuPJ3pLKOYKGmuvPUahHRz+8UnEMIUSOM24Pd3OQIg9OBXQ7WM9H2O2mD3CUq/L a6iPYt/Xvmok3ln2e30SBo3/3tTpT4vmSws68CmrOOrJk1agvp1haB8koBrCgA4j8OJl 16qg== 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=4+N7HYlfplazgpIqzrok71tJ5+HL03OgoPpZtaJJ/JA=; b=0WHpr7DBlhhQwiMqbY05F39XWxuyr3QAnBsTWSkgfL5TqivjZ0tATyjy9I4vTTnk7H 9y0L42J4o13liwMThaiiLFZDHEGq6Y4OsXjyLyi+oWimHlR+2xrf4J1hGGMafYGM5Uhs Fjy2aIMLemzKn97KKSRnq7wUj5/YfDFsMtDmhfqwSkBs4todv82eKrucH5ZdaVppEssO g82puwDV0DwTNyUcqooU1HYWAI/ERqaTDhkpw6Ey+l+GxicrtwznBA0HMGPiM2lOL2+O Ph8HngnJnvM0I8PrMNGTgq1UqVEvhJgmvF5Z0F/RcJiDhhPe1AsyEEj/YGW+xzR+Mj2h u6dg== X-Gm-Message-State: ACgBeo3f91Y8Sn/PU/TBlmtK8snkyYhDMPTpU5VhVvZPKW+cQ9h3F+sU EFbzbqsafy9M2NerEb9xS8t0MA== X-Google-Smtp-Source: AA6agR4E8A9z3wJppoQNhY57GIv03JlBORZW3VAf7AyAGpWyEGHzTY8vAaMpe8BdYq7iY2OzpCD70Q== X-Received: by 2002:a63:e851:0:b0:42a:3bc0:9ad1 with SMTP id a17-20020a63e851000000b0042a3bc09ad1mr2612576pgk.543.1661422332147; Thu, 25 Aug 2022 03:12:12 -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.12.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 03:12:11 -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 7/7] mm: add proc interface to free user PTE page table pages Date: Thu, 25 Aug 2022 18:10:37 +0800 Message-Id: <20220825101037.96517-8-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=1661422333; a=rsa-sha256; cv=none; b=nDk247WR8Iw6akIZvQvYnQFicHii/5jbtxRAxB1R52wWbnVp3bvZOpU2gCqJe3L/hRdaI/ 3qHJc/eCWtt+X3YoeYTXLyano/E2i3bm+v7bE9MzhgnFdvh7SePOVTSH729Tl01bD6jRG6 vr/jeaEgmEIaJnoLEaavrIuIYlTy7xk= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=Bu7SsRr4; spf=pass (imf08.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.178 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=1661422333; 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=4+N7HYlfplazgpIqzrok71tJ5+HL03OgoPpZtaJJ/JA=; b=DLFqjquVnu/pSfjRE7bm95+cAn7RIDHjf1rubCSXb+gb/ix0mqbFLmjoTSxy9aBSePG5T4 KI1VfIVeXCNlDjeijDEqedgJOUQ+StAsrYTut8dCeR4pDAKLI/cshrd3DKHLQdLKdABiYP TIP95OtMBSsQtDIhVxeF7ExTPHR4xCo= X-Stat-Signature: 74zanx11gidw8uwapbtqy4j8gooa81it X-Rspamd-Queue-Id: 0D54D160006 X-Rspam-User: X-Rspamd-Server: rspam06 Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=Bu7SsRr4; spf=pass (imf08.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-HE-Tag: 1661422332-908826 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: Add /proc/sys/vm/free_ptes file to procfs, when pid is written to the file, we will traverse its process address space, find and free empty PTE pages or zero PTE pages. Signed-off-by: Qi Zheng --- include/linux/pte_ref.h | 5 ++ kernel/sysctl.c | 12 ++++ mm/pte_ref.c | 126 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) diff --git a/include/linux/pte_ref.h b/include/linux/pte_ref.h index ab49c7fac120..f7e244129291 100644 --- a/include/linux/pte_ref.h +++ b/include/linux/pte_ref.h @@ -16,6 +16,11 @@ 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); + +int free_ptes_sysctl_handler(struct ctl_table *table, int write, + void *buffer, size_t *length, loff_t *ppos); +extern int sysctl_free_ptes_pid; + #else /* !CONFIG_FREE_USER_PTE */ static inline void pte_ref_init(pgtable_t pte) diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 35d034219513..14e1a9841cb8 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -64,6 +64,7 @@ #include #include #include +#include #include "../lib/kstrtox.h" @@ -2153,6 +2154,17 @@ static struct ctl_table vm_table[] = { .extra1 = SYSCTL_ONE, .extra2 = SYSCTL_FOUR, }, +#ifdef CONFIG_FREE_USER_PTE + { + .procname = "free_ptes", + .data = &sysctl_free_ptes_pid, + .maxlen = sizeof(int), + .mode = 0200, + .proc_handler = free_ptes_sysctl_handler, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_INT_MAX, + }, +#endif #ifdef CONFIG_COMPACTION { .procname = "compact_memory", diff --git a/mm/pte_ref.c b/mm/pte_ref.c index 818821d068af..e7080a3100a6 100644 --- a/mm/pte_ref.c +++ b/mm/pte_ref.c @@ -6,6 +6,14 @@ */ #include #include +#include +#include +#include +#include +#include +#include + +#include "internal.h" #ifdef CONFIG_FREE_USER_PTE @@ -105,4 +113,122 @@ void track_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, } EXPORT_SYMBOL(track_pte_clear); +#ifdef CONFIG_DEBUG_VM +void pte_free_debug(pmd_t pmd) +{ + pte_t *ptep = (pte_t *)pmd_page_vaddr(pmd); + int i = 0; + + for (i = 0; i < PTRS_PER_PTE; i++, ptep++) { + pte_t pte = *ptep; + BUG_ON(!(pte_none(pte) || is_zero_pfn(pte_pfn(pte)))); + } +} +#else +static inline void pte_free_debug(pmd_t pmd) +{ +} +#endif + + +static int kfreeptd_pmd_entry(pmd_t *pmd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pmd_t pmdval; + pgtable_t page; + struct mm_struct *mm = walk->mm; + struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0); + spinlock_t *ptl; + bool free = false; + unsigned long haddr = addr & PMD_MASK; + + if (pmd_trans_unstable(pmd)) + goto out; + + mmap_read_unlock(mm); + mmap_write_lock(mm); + + if (mm_find_pmd(mm, addr) != pmd) + goto unlock_out; + + ptl = pmd_lock(mm, pmd); + pmdval = *pmd; + if (pmd_none(pmdval) || pmd_leaf(pmdval)) { + spin_unlock(ptl); + goto unlock_out; + } + page = pmd_pgtable(pmdval); + if (!pte_mapped_count(page) || pte_zero_count(page) == PTRS_PER_PTE) { + pmd_clear(pmd); + flush_tlb_range(&vma, haddr, haddr + PMD_SIZE); + free = true; + } + spin_unlock(ptl); + +unlock_out: + mmap_write_unlock(mm); + mmap_read_lock(mm); + + if (free) { + pte_free_debug(pmdval); + mm_dec_nr_ptes(mm); + pgtable_pte_page_dtor(page); + __free_page(page); + } + +out: + cond_resched(); + return 0; +} + +static const struct mm_walk_ops kfreeptd_walk_ops = { + .pmd_entry = kfreeptd_pmd_entry, +}; + +int sysctl_free_ptes_pid; +int free_ptes_sysctl_handler(struct ctl_table *table, int write, + void *buffer, size_t *length, loff_t *ppos) +{ + int ret; + + ret = proc_dointvec_minmax(table, write, buffer, length, ppos); + if (ret) + return ret; + if (write) { + struct task_struct *task; + struct mm_struct *mm; + + rcu_read_lock(); + task = find_task_by_vpid(sysctl_free_ptes_pid); + if (!task) { + rcu_read_unlock(); + return -ESRCH; + } + mm = get_task_mm(task); + rcu_read_unlock(); + + if (!mm) { + mmput(mm); + return -ESRCH; + } + + do { + ret = -EBUSY; + + if (mmap_read_trylock(mm)) { + ret = walk_page_range(mm, FIRST_USER_ADDRESS, + ULONG_MAX, + &kfreeptd_walk_ops, NULL); + + mmap_read_unlock(mm); + } + + cond_resched(); + } while (ret == -EAGAIN); + + mmput(mm); + } + return ret; +} + #endif /* CONFIG_FREE_USER_PTE */