Skip to contentSkip to conversation

Page counters optimizations

PATCH In review Roman Gushchin 4 participants 10 messages May 3, 2024 – May 10, 2024

Subsystems (5)
lore
Share & tools
lore

Roman Gushchin

author maintainer
#0 lore raw

cover Page counters optimizations

From: Roman Gushchin <[email protected]>

This patchset reorganizes page_counter structures which helps to make memory cgroup and hugetlb cgroup structures smaller (20%-35%) and more cache-effective. It also eliminates useless tracking of protected memory usage when it's not needed.

include/linux/hugetlb.h | 4 +-

 include/linux/hugetlb_cgroup.h |   9 +---
 include/linux/memcontrol.h     |  15 ++----
 include/linux/page_counter.h   |  88 +++++++++++++++++++++++++-------
 mm/hugetlb.c                   |  14 +++--
 mm/hugetlb_cgroup.c            | 150 ++++++++++++++++++++----------------------------------
 mm/memcontrol.c                | 351 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------------------------------------------------
 mm/page_counter.c              |  76 ++++++++++++++++++----------
 8 files changed, 340 insertions(+), 367 deletions(-)

Roman Gushchin (4):
mm: memcg: convert enum res_type to mem_counter_type
mm: memcg: merge multiple page_counters into a single structure
mm: memcg: don't call propagate_protected_usage() needlessly
mm: page_counters: initialize usage using ATOMIC_LONG_INIT() macro

 include/linux/hugetlb.h        |   4 +-
 include/linux/hugetlb_cgroup.h |   9 +-
 include/linux/memcontrol.h     |  15 +-
 include/linux/page_counter.h   |  88 +++++++--
 mm/hugetlb.c                   |  14 +-
 mm/hugetlb_cgroup.c            | 150 +++++---------
 mm/memcontrol.c                | 351 ++++++++++++++-------------------
 mm/page_counter.c              |  76 ++++---
 8 files changed, 340 insertions(+), 367 deletions(-)
Signature · 1 line
2.43.2
author maintainer
#1 lore raw

PATCH 1/4 mm: memcg: convert enum res_type to mem_counter_type

The res_type enum is listing all types of memory tracked by memory
cgroups: generic memory, swap, kernel memory, tcp etc; and it's currently used only for dealing with corresponding sysfs files.

To prepare for tracking of various types of memory by a single page_counter structure, a similar enumeration is needed. Instead of introducing a completely new enumeration, let's re-purpose the existing one: rename it into mem_counter_type, change items names to be more meaningful and move to page_counter.h. The latter is needed to have the total number of different memory types available.

This change doesn't bring any functional difference, it's a pure refactoring.

---
 include/linux/page_counter.h |  8 ++++
 mm/memcontrol.c              | 91 +++++++++++++++++-------------------
 2 files changed, 50 insertions(+), 49 deletions(-)
include/linux/page_counter.h +8 -0
index 8cd858d912c4..2486f98a0c71 100644
@@ -7,6 +7,14 @@
 #include <linux/limits.h>
 #include <asm/page.h>
 
+enum mem_counter_type {
+	MCT_MEMORY,		/* cgroup v1 and v2 */
+	MCT_SWAP,		/* cgroup v2 only */
+	MCT_MEMSW = MCT_SWAP,	/* cgroup v1 only */
+	MCT_KMEM,		/* cgroup v1 only */
+	MCT_TCPMEM,		/* cgroup v1 only */
+};
+
 struct page_counter {
 	/*
 	 * Make sure 'usage' does not share cacheline with any other field. The
mm/memcontrol.c +42 -49

281 lines not on this page · raw message

Signature · 1 line
2.43.2
author maintainer
#2 lore raw

PATCH 2/4 mm: memcg: merge multiple page_counters into a single structure

In reply to Roman Gushchin

Struct page_counter is used by memory and hugetlb cgroups to track the resource usage. Memory cgroups are using up to four different counters (on cgroup v1). Hugetlb cgroups are using 2 * HUGE_MAX_HSTATE counters.

Every page_counter structure (except the one belonging to the root cgroup) contains a pointer to the corresponding parent structure. It's obviously wasteful because for each cgroup there are several similar chains of page_counter structures. It's not particularly cache-effective too.

Also every page_counter structure contains fields required for tracking the usage of the memory protection, while it's used only for the main ("memory") counter and only by memory cgroups.

To address these issues let's make page_counter structures to support N counters and use a single page_counter structure per memory or hugetlb cgroup. N is determined at the compile time depending on the configuration and HUGE_MAX_HSTATE value. HUGE_MAX_HSTATE is relatively small on all existing platforms (max is 5 on sparc), so it's not too far from 4 memcg memory types.

Memory min/low functionality is supported only for the first counter.

Memory savings (on x86_64):
original:
memcg:

    /* size: 2112, cachelines: 33, members: 46 */
    /* sum members: 1987, holes: 8, sum holes: 117 */
    /* padding: 8 */
    /* paddings: 3, sum paddings: 72 */
    /* forced alignments: 7, forced holes: 2, sum forced holes: 96 */
  hugetlb:
    /* size: 1280, cachelines: 20, members: 8 */
    /* sum members: 1224, holes: 1, sum holes: 56 */
    /* forced alignments: 3, forced holes: 1, sum forced holes: 56 */

patched:
memcg:

    /* size: 1664, cachelines: 26, members: 43 */
    /* sum members: 1539, holes: 8, sum holes: 117 */
    /* padding: 8 */
    /* paddings: 1, sum paddings: 40 */
    /* forced alignments: 4, forced holes: 2, sum forced holes: 96 */
  hugetlb:
    /* size: 832, cachelines: 13, members: 7 */
    /* sum members: 776, holes: 1, sum holes: 56 */
    /* paddings: 1, sum paddings: 40 */
    /* forced alignments: 2, forced holes: 1, sum forced holes: 56 */

So it's 20-35% reduction in the number of cache lines.

---
 include/linux/hugetlb.h        |   4 +-
 include/linux/hugetlb_cgroup.h |   9 +-
 include/linux/memcontrol.h     |  15 +-
 include/linux/page_counter.h   |  72 +++++++--
 mm/hugetlb.c                   |  14 +-
 mm/hugetlb_cgroup.c            | 150 +++++++-----------
 mm/memcontrol.c                | 272 ++++++++++++++-------------------
 mm/page_counter.c              |  61 +++++---
 8 files changed, 277 insertions(+), 320 deletions(-)
include/linux/hugetlb.h +2 -2
index 68244bb3637a..32aeb2b53bc7 100644
@@ -67,9 +67,9 @@ struct resv_map {
 	 * here. If these fields are 0, then either the mapping is shared, or
 	 * cgroup accounting is disabled for this resv_map.
 	 */
-	struct page_counter *reservation_counter;
 	unsigned long pages_per_hpage;
 	struct cgroup_subsys_state *css;
+	int counter_idx;
 #endif
 };
 
@@ -102,8 +102,8 @@ struct file_region {
 	 * file_region in resv_map. These fields hold the info needed to
 	 * uncharge each reservation.
 	 */
-	struct page_counter *reservation_counter;
 	struct cgroup_subsys_state *css;
+	int counter_idx;
 #endif
 };
 
include/linux/hugetlb_cgroup.h +2 -7
index e5d64b8b59c2..7f9ec65494ec 100644
@@ -36,14 +36,9 @@ struct hugetlb_cgroup {
 	struct cgroup_subsys_state css;
 
 	/*
-	 * the counter to account for hugepages from hugetlb.
+	 * resource counters for hugepages and hugepage reservations
 	 */
-	struct page_counter hugepage[HUGE_MAX_HSTATE];
-
-	/*
-	 * the counter to account for hugepage reservations from hugetlb.
-	 */
-	struct page_counter rsvd_hugepage[HUGE_MAX_HSTATE];
+	struct page_counter res;
 
 	atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
 	atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS];
include/linux/memcontrol.h +3 -12
index 030d34e9d117..bbb99f029bc1 100644
@@ -187,16 +187,7 @@ struct mem_cgroup {
 	struct mem_cgroup_id id;
 
 	/* Accounted resources */
-	struct page_counter memory;		/* Both v1 & v2 */
-
-	union {
-		struct page_counter swap;	/* v2 only */
-		struct page_counter memsw;	/* v1 only */
-	};
-
-	/* Legacy consumer-oriented counters */
-	struct page_counter kmem;		/* v1 only */
-	struct page_counter tcpmem;		/* v1 only */
+	struct page_counter memory;
 
 	/* Range enforcement for interrupt charges */
 	struct work_struct high_work;
@@ -652,7 +643,7 @@ static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
 		return false;
 
 	return READ_ONCE(memcg->memory.elow) >=
-		page_counter_read(&memcg->memory);
+		page_counter_read(&memcg->memory, MCT_MEMORY);
 }
 
 static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
@@ -662,7 +653,7 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
 		return false;
 
 	return READ_ONCE(memcg->memory.emin) >=
-		page_counter_read(&memcg->memory);
+		page_counter_read(&memcg->memory, MCT_MEMORY);
 }
 
 void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg);
include/linux/page_counter.h +56 -16

129 lines not on this page · raw message

mm/hugetlb.c +6 -8
index 33d175add044..45dccf0df5ab 100644
@@ -455,7 +455,7 @@ static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg,
 					      struct file_region *rg)
 {
 #ifdef CONFIG_CGROUP_HUGETLB
-	nrg->reservation_counter = rg->reservation_counter;
+	nrg->counter_idx = rg->counter_idx;
 	nrg->css = rg->css;
 	if (rg->css)
 		css_get(rg->css);
@@ -470,8 +470,6 @@ static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
 {
 #ifdef CONFIG_CGROUP_HUGETLB
 	if (h_cg) {
-		nrg->reservation_counter =
-			&h_cg->rsvd_hugepage[hstate_index(h)];
 		nrg->css = &h_cg->css;
 		/*
 		 * The caller will hold exactly one h_cg->css reference for the
@@ -484,6 +482,7 @@ static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
 		 * untouched.
 		 */
 		css_get(&h_cg->css);
+		nrg->counter_idx = hstate_index(h);
 		if (!resv->pages_per_hpage)
 			resv->pages_per_hpage = pages_per_huge_page(h);
 		/* pages_per_hpage should be the same for all entries in
@@ -491,8 +490,8 @@ static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
 		 */
 		VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h));
 	} else {
-		nrg->reservation_counter = NULL;
 		nrg->css = NULL;
+		nrg->counter_idx = 0;
 	}
 #endif
 }
@@ -509,7 +508,7 @@ static bool has_same_uncharge_info(struct file_region *rg,
 				   struct file_region *org)
 {
 #ifdef CONFIG_CGROUP_HUGETLB
-	return rg->reservation_counter == org->reservation_counter &&
+	return rg->counter_idx == org->counter_idx &&
 	       rg->css == org->css;
 
 #else
@@ -1068,14 +1067,13 @@ resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map,
 {
 #ifdef CONFIG_CGROUP_HUGETLB
 	if (!h_cg || !h) {
-		resv_map->reservation_counter = NULL;
 		resv_map->pages_per_hpage = 0;
 		resv_map->css = NULL;
+		resv_map->counter_idx = 0;
 	} else {
-		resv_map->reservation_counter =
-			&h_cg->rsvd_hugepage[hstate_index(h)];
 		resv_map->pages_per_hpage = pages_per_huge_page(h);
 		resv_map->css = &h_cg->css;
+		resv_map->counter_idx = hstate_index(h);
 	}
 #endif
 }
mm/hugetlb_cgroup.c +54 -96

305 lines not on this page · raw message

mm/memcontrol.c +116 -156

649 lines not on this page · raw message

mm/page_counter.c +38 -23

179 lines not on this page · raw message

Signature · 1 line
2.43.2
maintainer
#3 lore raw

Re: [PATCH v1 2/4] mm: memcg: merge multiple page_counters into a single structure

On Fri, May 03, 2024 at 01:18:33PM -0700, Roman Gushchin wrote:
[...]

Quoted 4 lines

enum mem_counter_type {

include/linux/page_counter.h +1 -0
+#ifdef CONFIG_MEMCG
 	MCT_MEMORY,		/* cgroup v1 and v2 */

include/linux/page_counter.h line 39

Quoted 10 lines
include/linux/page_counter.h +5 -0
 	MCT_SWAP,		/* cgroup v2 only */
 	MCT_MEMSW = MCT_SWAP,	/* cgroup v1 only */
 	MCT_KMEM,		/* cgroup v1 only */
 	MCT_TCPMEM,		/* cgroup v1 only */
+#endif
+#ifdef CONFIG_CGROUP_HUGETLB
+	MCT_HUGETLB_MAX = __MCT_HUGETLB_MAX,
+#endif
+	__MCT_NR_ITEMS,
 };

Thanks for the awesome work. I haven't gone through all the patches yet but wanted to ask a quick question. In the above enum are you trying to do a union between memcg and hugetlb? It gave me a big pause to understand what you are trying to do.

author maintainer
#4 lore raw

Re: [PATCH v1 2/4] mm: memcg: merge multiple page_counters into a single structure

Quoted 13 lines · Shakeel Butt

On Fri, May 03, 2024 at 01:18:33PM -0700, Roman Gushchin wrote:
[...]

Quoted 11 lines

enum mem_counter_type {

include/linux/page_counter.h +5 -0
+#ifdef CONFIG_MEMCG
 	MCT_MEMORY,		/* cgroup v1 and v2 */
 	MCT_SWAP,		/* cgroup v2 only */
 	MCT_MEMSW = MCT_SWAP,	/* cgroup v1 only */
 	MCT_KMEM,		/* cgroup v1 only */
 	MCT_TCPMEM,		/* cgroup v1 only */
+#endif
+#ifdef CONFIG_CGROUP_HUGETLB
+	MCT_HUGETLB_MAX = __MCT_HUGETLB_MAX,
+#endif
Quoted 8 lines

include/linux/page_counter.h line 39

Quoted 3 lines
include/linux/page_counter.h +1 -0
+	__MCT_NR_ITEMS,
 };

Thanks for the awesome work. I haven't gone through all the patches yet but wanted to ask a quick question. In the above enum are you trying to do a union between memcg and hugetlb? It gave me a big pause to understand what you are trying to do.

Yep, sort of. So the page_counter structure supports N independent counters, where N is sufficient enough for both memcg and hugetlb cases.

MCT_MEMORY, MCT_SWAP etc are used directly in the memcontrol.c code, while hugetlb code just indexes. MCT_HUGETLB_MAX magic is needed to define N at the compile time.

Thanks!

#5 lore raw

Re: [PATCH v1 2/4] mm: memcg: merge multiple page_counters into a single structure

Quoted 24 lines · Roman Gushchin
Quoted 13 lines · Shakeel Butt

On Fri, May 03, 2024 at 01:18:33PM -0700, Roman Gushchin wrote:
[...]

Quoted 11 lines

enum mem_counter_type {

include/linux/page_counter.h +5 -0
+#ifdef CONFIG_MEMCG
    MCT_MEMORY,             /* cgroup v1 and v2 */
    MCT_SWAP,               /* cgroup v2 only */
    MCT_MEMSW = MCT_SWAP,   /* cgroup v1 only */
    MCT_KMEM,               /* cgroup v1 only */
    MCT_TCPMEM,             /* cgroup v1 only */
+#endif
+#ifdef CONFIG_CGROUP_HUGETLB
+   MCT_HUGETLB_MAX = __MCT_HUGETLB_MAX,
+#endif
Quoted 8 lines
Quoted 3 lines

+ __MCT_NR_ITEMS,
};

Thanks for the awesome work. I haven't gone through all the patches yet but wanted to ask a quick question. In the above enum are you trying to do a union between memcg and hugetlb? It gave me a big pause to understand what you are trying to do.

Quoted 6 lines

Yep, sort of. So the page_counter structure supports N independent counters, where N is sufficient enough for both memcg and hugetlb cases.

MCT_MEMORY, MCT_SWAP etc are used directly in the memcontrol.c code, while hugetlb code just indexes. MCT_HUGETLB_MAX magic is needed to define N at the compile time.

Where N is __MCT_NR_ITEMS for all the counter array lengths? That doesn't look like it works if MCT_HUGETLB_MAX is small... i.e. there is both CONFIG_MEMCG and CONFIG_CGROUP_HUGETLB and (__MCT_HUGETLB_MAX = 1 or 3) since MCT_HUGETLB_MAX would be < MCT_TCPMEM and then __MCT_NR_ITEMS would be wrong?

If so, what about:

enum mem_counter_type {
#ifdef CONFIG_MEMCG
MCT_MEMORY, /* cgroup v1 and v2 */
MCT_SWAP, /* cgroup v2 only */
MCT_MEMSW = MCT_SWAP, /* cgroup v1 only */ MCT_KMEM, /* cgroup v1 only */
MCT_TCPMEM, /* cgroup v1 only */
#endif
MCT_MEMCG_NR_ITEMS,
#ifdef CONFIG_CGROUP_HUGETLB
MCT_HUGETLB_MAX = MCT_MEMCG_NR_ITEMS + __MCT_HUGETLB_MAX,
#else
MCT_HUGETLB_MAX = 0,
#endif
__MCT_NR_ITEMS = MAX(MCT_MEMCG_NR_ITEMS, MCT_HUGETLB_MAX)
};

author maintainer
#6 lore raw

Re: [PATCH v1 2/4] mm: memcg: merge multiple page_counters into a single structure

Quoted 43 lines · T.J. Mercier
Quoted 24 lines · Roman Gushchin
Quoted 15 lines · Shakeel Butt
Quoted 1 line · Roman Gushchin

[...]

Quoted 11 lines

enum mem_counter_type {

include/linux/page_counter.h +5 -0
+#ifdef CONFIG_MEMCG
   MCT_MEMORY,             /* cgroup v1 and v2 */
   MCT_SWAP,               /* cgroup v2 only */
   MCT_MEMSW = MCT_SWAP,   /* cgroup v1 only */
   MCT_KMEM,               /* cgroup v1 only */
   MCT_TCPMEM,             /* cgroup v1 only */
+#endif
+#ifdef CONFIG_CGROUP_HUGETLB
+   MCT_HUGETLB_MAX = __MCT_HUGETLB_MAX,
+#endif
Quoted 8 lines
Quoted 3 lines

+ __MCT_NR_ITEMS,
};

Thanks for the awesome work. I haven't gone through all the patches yet but wanted to ask a quick question. In the above enum are you trying to do a union between memcg and hugetlb? It gave me a big pause to understand what you are trying to do.

Quoted 6 lines

Yep, sort of. So the page_counter structure supports N independent counters, where N is sufficient enough for both memcg and hugetlb cases.

MCT_MEMORY, MCT_SWAP etc are used directly in the memcontrol.c code, while hugetlb code just indexes. MCT_HUGETLB_MAX magic is needed to define N at the compile time.

Where N is __MCT_NR_ITEMS for all the counter array lengths? That doesn't look like it works if MCT_HUGETLB_MAX is small... i.e. there is both CONFIG_MEMCG and CONFIG_CGROUP_HUGETLB and (__MCT_HUGETLB_MAX = 1 or 3) since MCT_HUGETLB_MAX would be < MCT_TCPMEM and then __MCT_NR_ITEMS would be wrong?

If so, what about:

Quoted 16 lines

enum mem_counter_type {
#ifdef CONFIG_MEMCG
MCT_MEMORY, /* cgroup v1 and v2 */
MCT_SWAP, /* cgroup v2 only */
MCT_MEMSW = MCT_SWAP, /* cgroup v1 only */ MCT_KMEM, /* cgroup v1 only */
MCT_TCPMEM, /* cgroup v1 only */
#endif
MCT_MEMCG_NR_ITEMS,
#ifdef CONFIG_CGROUP_HUGETLB
MCT_HUGETLB_MAX = MCT_MEMCG_NR_ITEMS + __MCT_HUGETLB_MAX,
#else
MCT_HUGETLB_MAX = 0,
#endif
__MCT_NR_ITEMS = MAX(MCT_MEMCG_NR_ITEMS, MCT_HUGETLB_MAX)
};

The page_counter structure is not shared between memory and hugetlb cgroups, so N should be big enough to accommodate 4 memcg counters __or__ 2 * HUGE_MAX_STATE hugetlb counters. Your version has enough space for both.

Thanks!

#7 lore raw

Re: [PATCH v1 2/4] mm: memcg: merge multiple page_counters into a single structure

In reply to Roman Gushchin
kernel test robot noticed "WARNING:at_mm/page_counter.c:#page_counter_cancel" on:

Hello,

kernel test robot noticed "WARNING:at_mm/page_counter.c:#page_counter_cancel" on:

commit: 214583b2262ef6157ee9834fa23a7da8f2292dd2 ("[PATCH v1 2/4] mm: memcg: merge multiple page_counters into a single structure")
url: https://github.com/intel-lab-lkp/linux/commits/Roman-Gushchin/mm-memcg-convert-enum-res_type-to-mem_counter_type/20240504-042046
base: https://git.kernel.org/cgit/linux/kernel/git/akpm/mm.git mm-everything
patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH v1 2/4] mm: memcg: merge multiple page_counters into a single structure

in testcase: ltp
version: ltp-x86_64-14c1f76-1_20240504
with following parameters:

disk: 1HDD
fs: xfs
test: syscalls-03

compiler: gcc-13
test machine: 4 threads 1 sockets Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz (Ivy Bridge) with 8G memory

(please refer to attached dmesg/kmsg for entire log/backtrace)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]

kern  :warn  : [  551.565920] ------------[ cut here ]------------
kern  :warn  : [  551.573137] page_counter underflow: -512 nr_pages=512
kern :warn : [  551.585841] WARNING: CPU: 0 PID: 6724 at mm/page_counter.c:58 page_counter_cancel (mm/page_counter.c:58 (discriminator 1)) 
kern  :warn  : [  551.810031] CPU: 0 PID: 6724 Comm: memfd_create03 Tainted: G S                 6.9.0-rc4-00574-g214583b2262e #1
kern  :warn  : [  551.820871] Hardware name: Hewlett-Packard HP Pro 3340 MT/17A1, BIOS 8.07 01/24/2013

kern :warn : [ 551.829368] RIP: 0010:page_counter_cancel (mm/page_counter.c:58 (discriminator 1))
kern :warn : [ 551.835103] Code: 3c 02 00 75 4f 49 c7 04 24 00 00 00 00 31 f6 e9 71 ff ff ff 48 89 ea 48 c7 c7 a0 88 f6 83 c6 05 06 21 d6 03 01 e8 84 d9 72 ff <0f> 0b eb ad 48 89 34 24 e8 d7 94 fb ff 48 8b 34 24 e9 67 ff ff ff All code
========

   0:	3c 02                	cmp    $0x2,%al
   2:	00 75 4f             	add    %dh,0x4f(%rbp)
   5:	49 c7 04 24 00 00 00 	movq   $0x0,(%r12)
   c:	00 
   d:	31 f6                	xor    %esi,%esi
   f:	e9 71 ff ff ff       	jmp    0xffffffffffffff85
  14:	48 89 ea             	mov    %rbp,%rdx
  17:	48 c7 c7 a0 88 f6 83 	mov    $0xffffffff83f688a0,%rdi
  1e:	c6 05 06 21 d6 03 01 	movb   $0x1,0x3d62106(%rip)        # 0x3d6212b
  25:	e8 84 d9 72 ff       	call   0xffffffffff72d9ae
  2a:*	0f 0b                	ud2		<-- trapping instruction
  2c:	eb ad                	jmp    0xffffffffffffffdb
  2e:	48 89 34 24          	mov    %rsi,(%rsp)
  32:	e8 d7 94 fb ff       	call   0xfffffffffffb950e
  37:	48 8b 34 24          	mov    (%rsp),%rsi
  3b:	e9 67 ff ff ff       	jmp    0xffffffffffffffa7

Code starting with the faulting instruction
===========================================

   0:	0f 0b                	ud2
   2:	eb ad                	jmp    0xffffffffffffffb1
   4:	48 89 34 24          	mov    %rsi,(%rsp)
   8:	e8 d7 94 fb ff       	call   0xfffffffffffb94e4
   d:	48 8b 34 24          	mov    (%rsp),%rsi
  11:	e9 67 ff ff ff       	jmp    0xffffffffffffff7d
kern  :warn  : [  551.854617] RSP: 0018:ffffc9000817fb58 EFLAGS: 00010286
kern  :warn  : [  551.860610] RAX: 0000000000000000 RBX: ffff8881001c4100 RCX: ffffffff8239a90e
kern  :warn  : [  551.868499] RDX: 1ffff11030706a6c RSI: 0000000000000008 RDI: ffff888183835360
kern  :warn  : [  551.876394] RBP: 0000000000000200 R08: 0000000000000001 R09: fffff5200102ff23
kern  :warn  : [  551.884295] R10: ffffc9000817f91f R11: 205d363233542020 R12: ffff8881001c4100
kern  :warn  : [  551.892184] R13: 0000000000000000 R14: 0000000000000000 R15: ffffffff869a1de8
kern  :warn  : [  551.900067] FS:  00007f45c0bc1740(0000) GS:ffff888183800000(0000) knlGS:0000000000000000
kern  :warn  : [  551.908910] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
kern  :warn  : [  551.915420] CR2: 00007f45c0c73900 CR3: 0000000206448002 CR4: 00000000001706f0
kern  :warn  : [  551.923304] Call Trace:
kern  :warn  : [  551.926508]  <TASK>
kern :warn : [  551.929366] ? __warn (kernel/panic.c:694) 
kern :warn : [  551.933354] ? page_counter_cancel (mm/page_counter.c:58 (discriminator 1)) 
kern :warn : [  551.938467] ? report_bug (lib/bug.c:180 lib/bug.c:219) 
kern :warn : [  551.942892] ? handle_bug (arch/x86/kernel/traps.c:239 (discriminator 1)) 
kern :warn : [  551.947142] ? exc_invalid_op (arch/x86/kernel/traps.c:260 (discriminator 1)) 
kern :warn : [  551.951741] ? asm_exc_invalid_op (arch/x86/include/asm/idtentry.h:621) 
kern :warn : [  551.956684] ? llist_add_batch (lib/llist.c:33 (discriminator 14)) 
kern :warn : [  551.961451] ? page_counter_cancel (mm/page_counter.c:58 (discriminator 1)) 
kern :warn : [  551.966564] ? page_counter_cancel (mm/page_counter.c:58 (discriminator 1)) 
kern :warn : [  551.971674] page_counter_uncharge (mm/page_counter.c:168 (discriminator 3)) 
kern :warn : [  551.976706] hugetlb_cgroup_uncharge_counter (mm/hugetlb_cgroup.c:392) 
kern :warn : [  551.982684] hugetlb_vm_op_close (mm/hugetlb.c:5222) 
kern :warn : [  551.987713] remove_vma (mm/mmap.c:142) 
kern :warn : [  551.991870] do_vmi_align_munmap (mm/mmap.c:2336 mm/mmap.c:2685) 
kern :warn : [  551.996897] ? __pfx_do_vmi_align_munmap (mm/mmap.c:2561) 
kern :warn : [  552.002446] do_vmi_munmap (mm/mmap.c:2757) 
kern :warn : [  552.006948] __vm_munmap (mm/mmap.c:3036) 
kern :warn : [  552.011288] ? __pfx___vm_munmap (mm/mmap.c:3027) 
kern :warn : [  552.016138] ? __pfx_ksys_write (fs/read_write.c:633) 
kern :warn : [  552.020914] __x64_sys_munmap (mm/mmap.c:3050) 
kern :warn : [  552.025509] do_syscall_64 (arch/x86/entry/common.c:52 (discriminator 1) arch/x86/entry/common.c:83 (discriminator 1)) 
kern :warn : [  552.029924] entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:130) 

kern :warn : [ 552.035733] RIP: 0033:0x7f45c0cc58f7
kern :warn : [ 552.040067] Code: 00 00 00 48 8b 15 09 05 0d 00 f7 d8 64 89 02 48 c7 c0 ff ff ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 66 90 b8 0b 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d d9 04 0d 00 f7 d8 64 89 01 48 All code
========

   0:	00 00                	add    %al,(%rax)
   2:	00 48 8b             	add    %cl,-0x75(%rax)
   5:	15 09 05 0d 00       	adc    $0xd0509,%eax
   a:	f7 d8                	neg    %eax
   c:	64 89 02             	mov    %eax,%fs:(%rdx)
   f:	48 c7 c0 ff ff ff ff 	mov    $0xffffffffffffffff,%rax
  16:	c3                   	ret
  17:	66 2e 0f 1f 84 00 00 	cs nopw 0x0(%rax,%rax,1)
  1e:	00 00 00 
  21:	66 90                	xchg   %ax,%ax
  23:	b8 0b 00 00 00       	mov    $0xb,%eax
  28:	0f 05                	syscall
  2a:*	48 3d 01 f0 ff ff    	cmp    $0xfffffffffffff001,%rax		<-- trapping instruction
  30:	73 01                	jae    0x33
  32:	c3                   	ret
  33:	48 8b 0d d9 04 0d 00 	mov    0xd04d9(%rip),%rcx        # 0xd0513
  3a:	f7 d8                	neg    %eax
  3c:	64 89 01             	mov    %eax,%fs:(%rcx)

3f: 48 rex.W

Code starting with the faulting instruction
===========================================

   0:	48 3d 01 f0 ff ff    	cmp    $0xfffffffffffff001,%rax
   6:	73 01                	jae    0x9
   8:	c3                   	ret
   9:	48 8b 0d d9 04 0d 00 	mov    0xd04d9(%rip),%rcx        # 0xd04e9
  10:	f7 d8                	neg    %eax
  12:	64 89 01             	mov    %eax,%fs:(%rcx)

15: 48 rex.W

The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20240510/[email protected]

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#8 lore raw

PATCH 3/4 mm: memcg: don't call propagate_protected_usage() needlessly

In reply to Roman Gushchin

Memory protection (min/low) requires a constant tracking of the usage of protected memory. propagate_protected_usage() is called each time and does a number of operations even in cases when the actual memory protection functionality is not supported (e.g. hugetlb cgroups or memcg swap counters). It's an obvious inefficiency, which can be addressed by calling propagate_protected_usage() optionally and only for the right counter type. It eliminates a number of operations from hot paths.

---
 include/linux/page_counter.h |  8 +++++++-
 mm/hugetlb_cgroup.c          |  2 +-
 mm/memcontrol.c              |  4 ++--
 mm/page_counter.c            | 15 ++++++++++++---
 4 files changed, 22 insertions(+), 7 deletions(-)
include/linux/page_counter.h +7 -1
index ae6cc080b78b..5d963f54fcb8 100644
@@ -62,6 +62,7 @@ struct page_counter {
 	/* Keep all the read most fields in a separete cacheline. */
 	CACHELINE_PADDING(_pad2_);
 
+	bool protection_support;
 	unsigned long min;
 	unsigned long low;
 	unsigned long high[__MCT_NR_ITEMS];
@@ -75,8 +76,12 @@ struct page_counter {
 #define PAGE_COUNTER_MAX (LONG_MAX / PAGE_SIZE)
 #endif
 
+/*
+ * Protection is supported only for the first counter (with id 0).
+ */
 static inline void page_counter_init(struct page_counter *counter,
-				     struct page_counter *parent)
+				     struct page_counter *parent,
+				     bool protection_support)
 {
 	int i;
 
@@ -86,6 +91,7 @@ static inline void page_counter_init(struct page_counter *counter,
 	}
 
 	counter->parent = parent;
+	counter->protection_support = protection_support;
 }
 
 static inline unsigned long page_counter_read(struct page_counter *counter,
mm/hugetlb_cgroup.c +1 -1
index 069c7f164dc5..81cb78d0714f 100644
@@ -79,7 +79,7 @@ static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup,
 	int idx;
 
 	page_counter_init(&h_cgroup->res,
-			  parent_h_cgroup ? &parent_h_cgroup->res : NULL);
+			  parent_h_cgroup ? &parent_h_cgroup->res : NULL, false);
 
 	for (idx = 0; idx < HUGE_MAX_HSTATE; idx++) {
 		unsigned long limit;
mm/memcontrol.c +2 -2
index f4511506ef1f..46becae5ff99 100644
@@ -5761,11 +5761,11 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
 	if (parent) {
 		WRITE_ONCE(memcg->swappiness, mem_cgroup_swappiness(parent));
 		WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable));
-		page_counter_init(&memcg->memory, &parent->memory);
+		page_counter_init(&memcg->memory, &parent->memory, true);
 	} else {
 		init_memcg_stats();
 		init_memcg_events();
-		page_counter_init(&memcg->memory, NULL);
+		page_counter_init(&memcg->memory, NULL, true);
 		root_mem_cgroup = memcg;
 		return &memcg->css;
 	}
mm/page_counter.c +12 -3
index b6ca3adbc226..5a27e3141ff3 100644
@@ -60,7 +60,8 @@ void page_counter_cancel(struct page_counter *counter,
 		new = 0;
 		atomic_long_set(&counter->usage[id], new);
 	}
-	propagate_protected_usage(counter, new);
+	if (counter->protection_support && id == 0)
+		propagate_protected_usage(counter, new);
 }
 
 /**
@@ -76,12 +77,14 @@ void page_counter_charge(struct page_counter *counter,
 			 unsigned long nr_pages)
 {
 	struct page_counter *c;
+	bool track_protection = counter->protection_support && (id == 0);
 
 	for (c = counter; c; c = c->parent) {
 		long new;
 
 		new = atomic_long_add_return(nr_pages, &c->usage[id]);
-		propagate_protected_usage(c, new);
+		if (track_protection)
+			propagate_protected_usage(c, new);
 		/*
 		 * This is indeed racy, but we can live with some
 		 * inaccuracy in the watermark.
@@ -107,6 +110,7 @@ bool page_counter_try_charge(struct page_counter *counter,
 			     struct page_counter **fail)
 {
 	struct page_counter *c;
+	bool track_protection = counter->protection_support && (id == 0);
 
 	for (c = counter; c; c = c->parent) {
 		long new;
@@ -136,7 +140,8 @@ bool page_counter_try_charge(struct page_counter *counter,
 			*fail = c;
 			goto failed;
 		}
-		propagate_protected_usage(c, new);
+		if (track_protection)
+			propagate_protected_usage(c, new);
 		/*
 		 * Just like with failcnt, we can live with some
 		 * inaccuracy in the watermark.
@@ -226,6 +231,8 @@ void page_counter_set_min(struct page_counter *counter, unsigned long nr_pages)
 {
 	struct page_counter *c;
 
+	WARN_ON_ONCE(!counter->protection_support);
+
 	WRITE_ONCE(counter->min, nr_pages);
 
 	for (c = counter; c; c = c->parent)
@@ -243,6 +250,8 @@ void page_counter_set_low(struct page_counter *counter, unsigned long nr_pages)
 {
 	struct page_counter *c;
 
+	WARN_ON_ONCE(!counter->protection_support);
+
 	WRITE_ONCE(counter->low, nr_pages);
 
 	for (c = counter; c; c = c->parent)
Signature · 1 line
2.43.2
author maintainer
#9 lore raw

PATCH 4/4 mm: page_counters: initialize usage using ATOMIC_LONG_INIT() macro

In reply to Roman Gushchin

When a page_counter structure is initialized, there is no need to use an atomic set operation to initialize usage counters because at this point the structure is not visible to anybody else. ATOMIC_LONG_INIT() is what should be used in such cases.

---
 include/linux/page_counter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/page_counter.h +1 -1
index 5d963f54fcb8..af636dd943a7 100644
@@ -86,7 +86,7 @@ static inline void page_counter_init(struct page_counter *counter,
 	int i;
 
 	for (i = 0; i < __MCT_NR_ITEMS; i++) {
-		atomic_long_set(&counter->usage[i], 0);
+		counter->usage[i] = (atomic_long_t)ATOMIC_LONG_INIT(0);
 		counter->max[i] = PAGE_COUNTER_MAX;
 	}
 
Signature · 1 line
2.43.2