Skip to contentSkip to conversation

mm: memcontrol: fix missing wakeup polling thread

PATCH Superseded Muchun Song 2 participants 3 messages Nov 4, 2020 – Nov 5, 2020 memcg

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: memcontrol: fix missing wakeup polling thread

When wen poll the memory.swap.events, we can miss being waked up when the swap event occurs. Because we didn't notify.

Fixes f3a53a3a1e5b ("mm, memcontrol: implement memory.swap.events")
---
 include/linux/memcontrol.h | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +16 -2
index 0f4dd7829fb2..2456cb737329 100644
@@ -1071,15 +1071,29 @@ static inline void count_memcg_event_mm(struct mm_struct *mm,
 	rcu_read_unlock();
 }
 
+static inline bool is_swap_memory_event(enum memcg_memory_event event)
+{
+	return event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
+	       event == MEMCG_SWAP_FAIL;
+}
+
 static inline void memcg_memory_event(struct mem_cgroup *memcg,
 				      enum memcg_memory_event event)
 {
+	bool swap_event = is_swap_memory_event(event);
+	struct cgroup_file *cfile;
+
 	atomic_long_inc(&memcg->memory_events_local[event]);
-	cgroup_file_notify(&memcg->events_local_file);
+	if (swap_event) {
+		cfile = &memcg->swap_events_file;
+	} else {
+		cfile = &memcg->events_file;
+		cgroup_file_notify(&memcg->events_local_file);
+	}
 
 	do {
 		atomic_long_inc(&memcg->memory_events[event]);
-		cgroup_file_notify(&memcg->events_file);
+		cgroup_file_notify(cfile);
 
 		if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
 			break;
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 5 lines · Muchun Song

When wen poll the memory.swap.events, we can miss being waked up when the swap event occurs. Because we didn't notify.

Fixes f3a53a3a1e5b ("mm, memcontrol: implement memory.swap.events")

Good catch!

Quoted 9 lines
---
 include/linux/memcontrol.h | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +0 -0
index 0f4dd7829fb2..2456cb737329 100644

include/linux/memcontrol.h static inline void count_memcg_event_mm(struct mm_struct *mm, · line 1078

Quoted 8 lines
include/linux/memcontrol.h +5 -0
@@ -1071,15 +1071,29 @@ static inline void count_memcg_event_mm(struct mm_struct *mm,
 	rcu_read_unlock();
 }
 
+static inline bool is_swap_memory_event(enum memcg_memory_event event)
+{
+	return event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
+	       event == MEMCG_SWAP_FAIL;
+}

Please inline this, it's not really worth the indirection.

Quoted 9 lines
include/linux/memcontrol.h +4 -1
 static inline void memcg_memory_event(struct mem_cgroup *memcg,
 				      enum memcg_memory_event event)
 {
+	bool swap_event = is_swap_memory_event(event);
+	struct cgroup_file *cfile;
+
 	atomic_long_inc(&memcg->memory_events_local[event]);
-	cgroup_file_notify(&memcg->events_local_file);
+	if (swap_event) {

include/linux/memcontrol.h static inline void count_memcg_event_mm(struct mm_struct *mm, · line 1096

Quoted 10 lines
include/linux/memcontrol.h +6 -1
+		cfile = &memcg->swap_events_file;
+	} else {
+		cfile = &memcg->events_file;
+		cgroup_file_notify(&memcg->events_local_file);
+	}
 
 	do {
 		atomic_long_inc(&memcg->memory_events[event]);
-		cgroup_file_notify(&memcg->events_file);
+		cgroup_file_notify(cfile);

This loop is a walk up the hierarchy and memcg keeps changing, so you cannot cache cfile up front.

		if (swap_event)
			cgroup_file_notify(&memcg->swap_events_file);
		else
			cgroup_file_notify(&memcg->events_file);
#2 lore raw
Quoted 50 lines · Johannes Weiner
Quoted 5 lines · Muchun Song

When wen poll the memory.swap.events, we can miss being waked up when the swap event occurs. Because we didn't notify.

Fixes f3a53a3a1e5b ("mm, memcontrol: implement memory.swap.events")

Good catch!

Quoted 9 lines
---
 include/linux/memcontrol.h | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +0 -0
index 0f4dd7829fb2..2456cb737329 100644

include/linux/memcontrol.h static inline void count_memcg_event_mm(struct mm_struct *mm, · line 1078

Quoted 8 lines
include/linux/memcontrol.h +5 -0
@@ -1071,15 +1071,29 @@ static inline void count_memcg_event_mm(struct mm_struct *mm,
      rcu_read_unlock();
 }
 
+static inline bool is_swap_memory_event(enum memcg_memory_event event)
+{
+     return event == MEMCG_SWAP_HIGH || event == MEMCG_SWAP_MAX ||
+            event == MEMCG_SWAP_FAIL;
+}

Please inline this, it's not really worth the indirection.

Quoted 9 lines
include/linux/memcontrol.h +4 -1
 static inline void memcg_memory_event(struct mem_cgroup *memcg,
                                    enum memcg_memory_event event)
 {
+     bool swap_event = is_swap_memory_event(event);
+     struct cgroup_file *cfile;
+
      atomic_long_inc(&memcg->memory_events_local[event]);
-     cgroup_file_notify(&memcg->events_local_file);
+     if (swap_event) {

include/linux/memcontrol.h static inline void count_memcg_event_mm(struct mm_struct *mm, · line 1096

Quoted 10 lines
include/linux/memcontrol.h +6 -1
+             cfile = &memcg->swap_events_file;
+     } else {
+             cfile = &memcg->events_file;
+             cgroup_file_notify(&memcg->events_local_file);
+     }
 
      do {
              atomic_long_inc(&memcg->memory_events[event]);
-             cgroup_file_notify(&memcg->events_file);
+             cgroup_file_notify(cfile);
Quoted 7 lines

This loop is a walk up the hierarchy and memcg keeps changing, so you cannot cache cfile up front.

                if (swap_event)
                        cgroup_file_notify(&memcg->swap_events_file);
                else
                        cgroup_file_notify(&memcg->events_file);

Oh, Yeah, Thanks for reminding me.

Signature · 2 lines
Yours,
Muchun