Skip to contentSkip to conversation

memcg: Only free spare array when readers are done

PATCH In review Martijn Coenen 4 participants 6 messages Jan 14, 2016 – Jan 15, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memcg: Only free spare array when readers are done

A spare array holding mem cgroup threshold events is kept around to make sure we can always safely deregister an event and have an array to store the new set of events in.

In the scenario where we're going from 1 to 0 registered events, the pointer to the primary array containing 1 event is copied to the spare slot, and then the spare slot is freed because no events are left. However, it is freed before calling synchronize_rcu(), which means readers may still be accessing threshold->primary after it is freed.

Fixed by only freeing after synchronize_rcu().

---
 mm/memcontrol.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
mm/memcontrol.c +6 -5
index 14cb1db..73228b6 100644
@@ -3522,16 +3522,17 @@ static void __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
 swap_buffers:
 	/* Swap primary and spare array */
 	thresholds->spare = thresholds->primary;
-	/* If all events are unregistered, free the spare array */
-	if (!new) {
-		kfree(thresholds->spare);
-		thresholds->spare = NULL;
-	}
 
 	rcu_assign_pointer(thresholds->primary, new);
 
 	/* To be sure that nobody uses thresholds */
 	synchronize_rcu();
+
+	/* If all events are unregistered, free the spare array */
+	if (!new) {
+		kfree(thresholds->spare);
+		thresholds->spare = NULL;
+	}
 unlock:
 	mutex_unlock(&memcg->thresholds_lock);
 }
Signature · 7 lines
2.6.0.rc2.230.g3dd15c0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Martijn Coenen

A spare array holding mem cgroup threshold events is kept around to make sure we can always safely deregister an event and have an array to store the new set of events in.

In the scenario where we're going from 1 to 0 registered events, the pointer to the primary array containing 1 event is copied to the spare slot, and then the spare slot is freed because no events are left. However, it is freed before calling synchronize_rcu(), which means readers may still be accessing threshold->primary after it is freed.

Have you seen this triggering in the real life?

Quoted 3 lines

Fixed by only freeing after synchronize_rcu().

Fixes 8c7577637ca3 ("memcg: free spare array to avoid memory leak")
Quoted 1 line
Cc stable

Thanks!

Quoted 34 lines
---
 mm/memcontrol.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
mm/memcontrol.c +6 -5
index 14cb1db..73228b6 100644
@@ -3522,16 +3522,17 @@ static void
 __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
 swap_buffers:
 	/* Swap primary and spare array */
 	thresholds->spare = thresholds->primary;
-	/* If all events are unregistered, free the spare array */
-	if (!new) {
-		kfree(thresholds->spare);
-		thresholds->spare = NULL;
-	}
 
 	rcu_assign_pointer(thresholds->primary, new);
 
 	/* To be sure that nobody uses thresholds */
 	synchronize_rcu();
+
+	/* If all events are unregistered, free the spare array */
+	if (!new) {
+		kfree(thresholds->spare);
+		thresholds->spare = NULL;
+	}
 unlock:
 	mutex_unlock(&memcg->thresholds_lock);

}

Signature · 1 line
2.6.0.rc2.230.g3dd15c0
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Martijn Coenen

A spare array holding mem cgroup threshold events is kept around to make sure we can always safely deregister an event and have an array to store the new set of events in.

Quoted 8 lines
Quoted 5 lines

In the scenario where we're going from 1 to 0 registered events, the pointer to the primary array containing 1 event is copied to the spare slot, and then the spare slot is freed because no events are left. However, it is freed before calling synchronize_rcu(), which means readers may still be accessing threshold->primary after it is freed.

Have you seen this triggering in the real life?

It was pretty easy to reproduce in a stress test setup, where we spawn a process, put it in a mem cgroup and setup the threshold, have it allocate a lot of memory quickly (crossing the threshold), unregister the event, kill and repeat. Usually within 30 mins.

Quoted 52 lines
Quoted 4 lines

Fixed by only freeing after synchronize_rcu().

Fixes 8c7577637ca3 ("memcg: free spare array to avoid memory leak")
Quoted 1 line
Cc stable

Thanks!

Quoted 31 lines
---
 mm/memcontrol.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
mm/memcontrol.c +6 -5
index 14cb1db..73228b6 100644
@@ -3522,16 +3522,17 @@ static void
 __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
 swap_buffers:
      /* Swap primary and spare array */
      thresholds->spare = thresholds->primary;
-     /* If all events are unregistered, free the spare array */
-     if (!new) {
-             kfree(thresholds->spare);
-             thresholds->spare = NULL;
-     }
 
      rcu_assign_pointer(thresholds->primary, new);
 
      /* To be sure that nobody uses thresholds */
      synchronize_rcu();
+
+     /* If all events are unregistered, free the spare array */
+     if (!new) {
+             kfree(thresholds->spare);
+             thresholds->spare = NULL;
+     }
 unlock:
      mutex_unlock(&memcg->thresholds_lock);
Quoted 3 lines

}
--
2.6.0.rc2.230.g3dd15c0

--
Michal Hocko
SUSE Labs

#3 lore raw
In reply to Michal Hocko
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Martijn Coenen

A spare array holding mem cgroup threshold events is kept around to make sure we can always safely deregister an event and have an array to store the new set of events in.

Quoted 7 lines
Quoted 5 lines

In the scenario where we're going from 1 to 0 registered events, the pointer to the primary array containing 1 event is copied to the spare slot, and then the spare slot is freed because no events are left. However, it is freed before calling synchronize_rcu(), which means readers may still be accessing threshold->primary after it is freed.

Have you seen this triggering in the real life?

(Sorry for the HTML mess).
It was pretty easy to reproduce in a stress test setup, where we spawn a process, put it in a mem cgroup and setup the threshold, have it allocate a lot of memory quickly (crossing the threshold), unregister the event, kill and repeat. Usually within 30 mins.

Quoted 43 lines
Quoted 4 lines

Fixed by only freeing after synchronize_rcu().

Fixes 8c7577637ca3 ("memcg: free spare array to avoid memory leak")
Quoted 1 line
Cc stable

Thanks!

Quoted 30 lines
---
 mm/memcontrol.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
mm/memcontrol.c +6 -5
index 14cb1db..73228b6 100644
@@ -3522,16 +3522,17 @@ static void
 __mem_cgroup_usage_unregister_event(struct mem_cgroup *memcg,
 swap_buffers:
      /* Swap primary and spare array */
      thresholds->spare = thresholds->primary;
-     /* If all events are unregistered, free the spare array */
-     if (!new) {
-             kfree(thresholds->spare);
-             thresholds->spare = NULL;
-     }
 
      rcu_assign_pointer(thresholds->primary, new);
 
      /* To be sure that nobody uses thresholds */
      synchronize_rcu();
+
+     /* If all events are unregistered, free the spare array */
+     if (!new) {
+             kfree(thresholds->spare);
+             thresholds->spare = NULL;
+     }
 unlock:
Quoted 8 lines
Quoted 4 lines
mm/memcontrol.c +0 -0
      mutex_unlock(&memcg->thresholds_lock);

}
--
2.6.0.rc2.230.g3dd15c0

--
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 1/1
#4 lore raw
In reply to Martijn Coenen
Quoted 10 lines · Martijn Coenen

A spare array holding mem cgroup threshold events is kept around to make sure we can always safely deregister an event and have an array to store the new set of events in.

In the scenario where we're going from 1 to 0 registered events, the pointer to the primary array containing 1 event is copied to the spare slot, and then the spare slot is freed because no events are left. However, it is freed before calling synchronize_rcu(), which means readers may still be accessing threshold->primary after it is freed.

Quoted 3 lines

Fixed by only freeing after synchronize_rcu().

maintainer Acked-by on v1 1/1
#5 lore raw
In reply to Martijn Coenen
Quoted 10 lines · Martijn Coenen

A spare array holding mem cgroup threshold events is kept around to make sure we can always safely deregister an event and have an array to store the new set of events in.

In the scenario where we're going from 1 to 0 registered events, the pointer to the primary array containing 1 event is copied to the spare slot, and then the spare slot is freed because no events are left. However, it is freed before calling synchronize_rcu(), which means readers may still be accessing threshold->primary after it is freed.

Quoted 3 lines

Fixed by only freeing after synchronize_rcu().