diff mbox series

[RFC,bpf-next,1/9] mm: Introduce active vm item

Message ID 20221212003711.24977-2-laoar.shao@gmail.com (mailing list archive)
State New
Headers show
Series mm, bpf: Add BPF into /proc/meminfo | expand

Commit Message

Yafang Shao Dec. 12, 2022, 12:37 a.m. UTC
A new page extension active_vm is introduced in this patch. It can be
enabled and disabled by setting CONFIG_ACTIVE_VM at compile time or a boot
parameter 'active_vm' at run time.

In the followup patches, we will use it to account specific memory
allocation for page, slab and percpu memory.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 include/linux/active_vm.h | 23 +++++++++++++++++++++++
 mm/Kconfig                |  8 ++++++++
 mm/Makefile               |  1 +
 mm/active_vm.c            | 33 +++++++++++++++++++++++++++++++++
 mm/active_vm.h            |  8 ++++++++
 mm/page_ext.c             |  4 ++++
 6 files changed, 77 insertions(+)
 create mode 100644 include/linux/active_vm.h
 create mode 100644 mm/active_vm.c
 create mode 100644 mm/active_vm.h
diff mbox series

Patch

diff --git a/include/linux/active_vm.h b/include/linux/active_vm.h
new file mode 100644
index 000000000000..899e578e94fa
--- /dev/null
+++ b/include/linux/active_vm.h
@@ -0,0 +1,23 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __INCLUDE_ACTIVE_VM_H
+#define __INCLUDE_ACTIVE_VM_H
+
+#ifdef CONFIG_ACTIVE_VM
+#include <linux/jump_label.h>
+
+extern struct static_key_true active_vm_disabled;
+
+static inline bool active_vm_enabled(void)
+{
+	if (static_branch_likely(&active_vm_disabled))
+		return false;
+
+	return true;
+}
+#else
+static inline bool active_vm_enabled(void)
+{
+	return false;
+}
+#endif /* CONFIG_ACTIVE_VM */
+#endif /* __INCLUDE_ACTIVE_VM_H */
diff --git a/mm/Kconfig b/mm/Kconfig
index 57e1d8c5b505..ba1087e4afff 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -1150,6 +1150,14 @@  config LRU_GEN_STATS
 	  This option has a per-memcg and per-node memory overhead.
 # }
 
+config ACTIVE_VM
+	bool "To track memory size of active VM item"
+	default y
+	depends on PAGE_EXTENSION
+	help
+		Allow scope-based memory accouting for specific memory, e.g. the
+		system-wide BPF memory usage.
+
 source "mm/damon/Kconfig"
 
 endmenu
diff --git a/mm/Makefile b/mm/Makefile
index 8e105e5b3e29..347dcff061d5 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -138,3 +138,4 @@  obj-$(CONFIG_IO_MAPPING) += io-mapping.o
 obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
 obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
 obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
+obj-$(CONFIG_ACTIVE_VM) += active_vm.o
diff --git a/mm/active_vm.c b/mm/active_vm.c
new file mode 100644
index 000000000000..60849930a7d3
--- /dev/null
+++ b/mm/active_vm.c
@@ -0,0 +1,33 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/page_ext.h>
+
+static bool __active_vm_enabled __initdata =
+				IS_ENABLED(CONFIG_ACTIVE_VM);
+
+DEFINE_STATIC_KEY_TRUE(active_vm_disabled);
+EXPORT_SYMBOL(active_vm_disabled);
+
+static int __init early_active_vm_param(char *buf)
+{
+	return strtobool(buf, &__active_vm_enabled);
+}
+
+early_param("active_vm", early_active_vm_param);
+
+static bool __init need_active_vm(void)
+{
+	return __active_vm_enabled;
+}
+
+static void __init init_active_vm(void)
+{
+	if (!__active_vm_enabled)
+		return;
+
+	static_branch_disable(&active_vm_disabled);
+}
+
+struct page_ext_operations active_vm_ops = {
+	.need = need_active_vm,
+	.init = init_active_vm,
+};
diff --git a/mm/active_vm.h b/mm/active_vm.h
new file mode 100644
index 000000000000..72978955833e
--- /dev/null
+++ b/mm/active_vm.h
@@ -0,0 +1,8 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __MM_ACTIVE_VM_H
+#define __MM_ACTIVE_VM_H
+
+#ifdef CONFIG_ACTIVE_VM
+extern struct page_ext_operations active_vm_ops;
+#endif /* CONFIG_ACTIVE_VM */
+#endif /* __MM_ACTIVE_VM_H */
diff --git a/mm/page_ext.c b/mm/page_ext.c
index ddf1968560f0..3a3a91bc9e06 100644
--- a/mm/page_ext.c
+++ b/mm/page_ext.c
@@ -10,6 +10,7 @@ 
 #include <linux/page_idle.h>
 #include <linux/page_table_check.h>
 #include <linux/rcupdate.h>
+#include "active_vm.h"
 
 /*
  * struct page extension
@@ -84,6 +85,9 @@  static struct page_ext_operations *page_ext_ops[] __initdata = {
 #ifdef CONFIG_PAGE_TABLE_CHECK
 	&page_table_check_ops,
 #endif
+#ifdef CONFIG_ACTIVE_VM
+	&active_vm_ops,
+#endif
 };
 
 unsigned long page_ext_size = sizeof(struct page_ext);