Skip to contentSkip to conversation

mm: page_counter: mitigate consequences of a page_counter underflow

PATCH Applied Johannes Weiner 4 participants 4 messages Apr 8, 2021

Subsystems (3)
lore
Share & tools
lore

Johannes Weiner

author maintainer
#0 lore raw

PATCH 1/1 mm: page_counter: mitigate consequences of a page_counter underflow

When the unsigned page_counter underflows, even just by a few pages, a cgroup will not be able to run anything afterwards and trigger the OOM killer in a loop.

Underflows shouldn't happen, but when they do in practice, we may just be off by a small amount that doesn't interfere with the normal operation - consequences don't need to be that dire.

Reset the page_counter to 0 upon underflow. We'll issue a warning that the accounting will be off and then try to keep limping along.

[ We used to do this with the original res_counter, where it was a
more straight-forward correction inside the spinlock section. I didn't carry it forward into the lockless page counters for simplicity, but it turns out this is quite useful in practice. ]

---
 mm/page_counter.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/page_counter.c +6 -2
index c6860f51b6c6..7d83641eb86b 100644
@@ -52,9 +52,13 @@ void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages)
 	long new;
 
 	new = atomic_long_sub_return(nr_pages, &counter->usage);
-	propagate_protected_usage(counter, new);
 	/* More uncharges than charges? */
-	WARN_ON_ONCE(new < 0);
+	if (WARN_ONCE(new < 0, "page_counter underflow: %ld nr_pages=%lu\n",
+		      new, nr_pages)) {
+		new = 0;
+		atomic_long_set(&counter->usage, new);
+	}
+	propagate_protected_usage(counter, new);
 }
 
 /**
Signature · 1 line
2.31.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Johannes Weiner

When the unsigned page_counter underflows, even just by a few pages, a cgroup will not be able to run anything afterwards and trigger the OOM killer in a loop.

Underflows shouldn't happen, but when they do in practice, we may just be off by a small amount that doesn't interfere with the normal operation - consequences don't need to be that dire.

Yes, I do agree.

Quoted 2 lines

Reset the page_counter to 0 upon underflow. We'll issue a warning that the accounting will be off and then try to keep limping along.

I do not remember any reports about the existing WARN_ON but it is not really hard to imagine a charging imbalance to be introduced easily.

Quoted 4 lines

[ We used to do this with the original res_counter, where it was a
more straight-forward correction inside the spinlock section. I didn't carry it forward into the lockless page counters for simplicity, but it turns out this is quite useful in practice. ]

The lack of external synchronization makes it more tricky because certain charges might get just lost depending on the ordering. This sucks but considering that the system is already botched and counters cannot be trusted this is definitely better than a potentially completely unusable memcg. It would be nice to mention that in the above paragraph as a caveat.

Quoted 1 line
Quoted 26 lines
---
 mm/page_counter.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/page_counter.c +6 -2
index c6860f51b6c6..7d83641eb86b 100644
@@ -52,9 +52,13 @@ void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages)
 	long new;
 
 	new = atomic_long_sub_return(nr_pages, &counter->usage);
-	propagate_protected_usage(counter, new);
 	/* More uncharges than charges? */
-	WARN_ON_ONCE(new < 0);
+	if (WARN_ONCE(new < 0, "page_counter underflow: %ld nr_pages=%lu\n",
+		      new, nr_pages)) {
+		new = 0;
+		atomic_long_set(&counter->usage, new);
+	}
+	propagate_protected_usage(counter, new);
 }
 
 /**
Signature · 1 line
2.31.1
Signature · 2 lines
Michal Hocko
SUSE Labs
Acked-by on v1 1/1
#2 lore raw
In reply to Johannes Weiner
Quoted 11 lines · Johannes Weiner

When the unsigned page_counter underflows, even just by a few pages, a cgroup will not be able to run anything afterwards and trigger the OOM killer in a loop.

Underflows shouldn't happen, but when they do in practice, we may just be off by a small amount that doesn't interfere with the normal operation - consequences don't need to be that dire.

Reset the page_counter to 0 upon underflow. We'll issue a warning that the accounting will be off and then try to keep limping along.

Quoted 6 lines

[ We used to do this with the original res_counter, where it was a
more straight-forward correction inside the spinlock section. I didn't carry it forward into the lockless page counters for simplicity, but it turns out this is quite useful in practice. ]

Quoted 28 lines

---
mm/page_counter.c | 8 ++++++--

1 file changed, 6 insertions(+), 2 deletions(-)
mm/page_counter.c +6 -2
index c6860f51b6c6..7d83641eb86b 100644
@@ -52,9 +52,13 @@ void page_counter_cancel(struct page_counter *counter, unsigned long nr_pages)
 	long new;
 
 	new = atomic_long_sub_return(nr_pages, &counter->usage);
-	propagate_protected_usage(counter, new);
 	/* More uncharges than charges? */
-	WARN_ON_ONCE(new < 0);
+	if (WARN_ONCE(new < 0, "page_counter underflow: %ld nr_pages=%lu\n",
+		      new, nr_pages)) {
+		new = 0;
+		atomic_long_set(&counter->usage, new);
+	}
+	propagate_protected_usage(counter, new);
 }
 
 /**
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#3 lore raw
In reply to Johannes Weiner
Quoted 12 lines · Johannes Weiner

When the unsigned page_counter underflows, even just by a few pages, a cgroup will not be able to run anything afterwards and trigger the OOM killer in a loop.

Underflows shouldn't happen, but when they do in practice, we may just be off by a small amount that doesn't interfere with the normal operation - consequences don't need to be that dire.

Reset the page_counter to 0 upon underflow. We'll issue a warning that the accounting will be off and then try to keep limping along.

Quoted 6 lines

[ We used to do this with the original res_counter, where it was a
more straight-forward correction inside the spinlock section. I didn't carry it forward into the lockless page counters for simplicity, but it turns out this is quite useful in practice. ]