cgroups.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH resend 1/3] vmpressure: change vmpressure::sr_lock to spinlock
@ 2013-07-19 16:51 Michal Hocko
       [not found] ` <1374252671-11939-1-git-send-email-mhocko-AlSwsSmVLrQ@public.gmane.org>
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Michal Hocko @ 2013-07-19 16:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Anton Vorontsov, Johannes Weiner, KAMEZAWA Hiroyuki,
	KOSAKI Motohiro, Tejun Heo, Li Zefan, linux-mm, cgroups,
	linux-kernel

There is nothing that can sleep inside critical sections protected by
this lock and those sections are really small so there doesn't make much
sense to use mutex for them. Change the log to a spinlock

Brought-up-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.cz>
---
 include/linux/vmpressure.h |  2 +-
 mm/vmpressure.c            | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/include/linux/vmpressure.h b/include/linux/vmpressure.h
index 76be077..2081680 100644
--- a/include/linux/vmpressure.h
+++ b/include/linux/vmpressure.h
@@ -12,7 +12,7 @@ struct vmpressure {
 	unsigned long scanned;
 	unsigned long reclaimed;
 	/* The lock is used to keep the scanned/reclaimed above in sync. */
-	struct mutex sr_lock;
+	struct spinlock sr_lock;
 
 	/* The list of vmpressure_event structs. */
 	struct list_head events;
diff --git a/mm/vmpressure.c b/mm/vmpressure.c
index 736a601..f4ee6a1 100644
--- a/mm/vmpressure.c
+++ b/mm/vmpressure.c
@@ -180,12 +180,12 @@ static void vmpressure_work_fn(struct work_struct *work)
 	if (!vmpr->scanned)
 		return;
 
-	mutex_lock(&vmpr->sr_lock);
+	spin_lock(&vmpr->sr_lock);
 	scanned = vmpr->scanned;
 	reclaimed = vmpr->reclaimed;
 	vmpr->scanned = 0;
 	vmpr->reclaimed = 0;
-	mutex_unlock(&vmpr->sr_lock);
+	spin_unlock(&vmpr->sr_lock);
 
 	do {
 		if (vmpressure_event(vmpr, scanned, reclaimed))
@@ -240,11 +240,11 @@ void vmpressure(gfp_t gfp, struct mem_cgroup *memcg,
 	if (!scanned)
 		return;
 
-	mutex_lock(&vmpr->sr_lock);
+	spin_lock(&vmpr->sr_lock);
 	vmpr->scanned += scanned;
 	vmpr->reclaimed += reclaimed;
 	scanned = vmpr->scanned;
-	mutex_unlock(&vmpr->sr_lock);
+	spin_unlock(&vmpr->sr_lock);
 
 	if (scanned < vmpressure_win || work_pending(&vmpr->work))
 		return;
@@ -367,7 +367,7 @@ void vmpressure_unregister_event(struct cgroup *cg, struct cftype *cft,
  */
 void vmpressure_init(struct vmpressure *vmpr)
 {
-	mutex_init(&vmpr->sr_lock);
+	spin_lock_init(&vmpr->sr_lock);
 	mutex_init(&vmpr->events_lock);
 	INIT_LIST_HEAD(&vmpr->events);
 	INIT_WORK(&vmpr->work, vmpressure_work_fn);
-- 
1.8.3.2

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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH resend 2/3] vmpressure: do not check for pending work to prevent from new work
       [not found] ` <1374252671-11939-1-git-send-email-mhocko-AlSwsSmVLrQ@public.gmane.org>
@ 2013-07-19 16:51   ` Michal Hocko
  2013-07-23 16:17     ` Tejun Heo
  0 siblings, 1 reply; 6+ messages in thread
From: Michal Hocko @ 2013-07-19 16:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Anton Vorontsov, Johannes Weiner, KAMEZAWA Hiroyuki,
	KOSAKI Motohiro, Tejun Heo, Li Zefan,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg, cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA

because it is racy and it doesn't give us much anyway as schedule_work
handles this case already.

Brought-up-by: Tejun Heo <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Signed-off-by: Michal Hocko <mhocko-AlSwsSmVLrQ@public.gmane.org>
---
 mm/vmpressure.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/vmpressure.c b/mm/vmpressure.c
index f4ee6a1..192f973 100644
--- a/mm/vmpressure.c
+++ b/mm/vmpressure.c
@@ -246,7 +246,7 @@ void vmpressure(gfp_t gfp, struct mem_cgroup *memcg,
 	scanned = vmpr->scanned;
 	spin_unlock(&vmpr->sr_lock);
 
-	if (scanned < vmpressure_win || work_pending(&vmpr->work))
+	if (scanned < vmpressure_win)
 		return;
 	schedule_work(&vmpr->work);
 }
-- 
1.8.3.2

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH resend 3/3] vmpressure: Make sure there are no events queued after memcg is offlined
  2013-07-19 16:51 [PATCH resend 1/3] vmpressure: change vmpressure::sr_lock to spinlock Michal Hocko
       [not found] ` <1374252671-11939-1-git-send-email-mhocko-AlSwsSmVLrQ@public.gmane.org>
@ 2013-07-19 16:51 ` Michal Hocko
  2013-07-23 16:18   ` Tejun Heo
  2013-07-23 16:17 ` [PATCH resend 1/3] vmpressure: change vmpressure::sr_lock to spinlock Tejun Heo
  2 siblings, 1 reply; 6+ messages in thread
From: Michal Hocko @ 2013-07-19 16:51 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Anton Vorontsov, Johannes Weiner, KAMEZAWA Hiroyuki,
	KOSAKI Motohiro, Tejun Heo, Li Zefan, linux-mm, cgroups,
	linux-kernel

vmpressure is called synchronously from the reclaim where the
target_memcg is guaranteed to be alive but the eventfd is signaled from
the work queue context. This means that memcg (along with vmpressure
structure which is embedded into it) might go away while the work item
is pending which would result in use-after-release bug.

We have two possible ways how to fix this. Either vmpressure pins memcg
before it schedules vmpr->work and unpin it in vmpressure_work_fn or
explicitely flush the work item from the css_offline context (as
suggested by Tejun).

This patch implements the later one and it introduces vmpressure_cleanup
which flushes the vmpressure work queue item item. It hooks into
mem_cgroup_css_offline after the memcg itself is cleaned up.

Reported-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.cz>
---
 include/linux/vmpressure.h |  1 +
 mm/memcontrol.c            |  1 +
 mm/vmpressure.c            | 16 ++++++++++++++++
 3 files changed, 18 insertions(+)

diff --git a/include/linux/vmpressure.h b/include/linux/vmpressure.h
index 2081680..0c9bc9a 100644
--- a/include/linux/vmpressure.h
+++ b/include/linux/vmpressure.h
@@ -30,6 +30,7 @@ extern void vmpressure(gfp_t gfp, struct mem_cgroup *memcg,
 extern void vmpressure_prio(gfp_t gfp, struct mem_cgroup *memcg, int prio);
 
 extern void vmpressure_init(struct vmpressure *vmpr);
+extern void vmpressure_cleanup(struct vmpressure * vmpr);
 extern struct vmpressure *memcg_to_vmpressure(struct mem_cgroup *memcg);
 extern struct cgroup_subsys_state *vmpressure_to_css(struct vmpressure *vmpr);
 extern struct vmpressure *css_to_vmpressure(struct cgroup_subsys_state *css);
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 6e120e4..198759c 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6326,6 +6326,7 @@ static void mem_cgroup_css_offline(struct cgroup *cont)
 	mem_cgroup_invalidate_reclaim_iterators(memcg);
 	mem_cgroup_reparent_charges(memcg);
 	mem_cgroup_destroy_all_caches(memcg);
+	vmpressure_cleanup(&memcg->vmpressure);
 }
 
 static void mem_cgroup_css_free(struct cgroup *cont)
diff --git a/mm/vmpressure.c b/mm/vmpressure.c
index 192f973..0c1e37d 100644
--- a/mm/vmpressure.c
+++ b/mm/vmpressure.c
@@ -372,3 +372,19 @@ void vmpressure_init(struct vmpressure *vmpr)
 	INIT_LIST_HEAD(&vmpr->events);
 	INIT_WORK(&vmpr->work, vmpressure_work_fn);
 }
+
+/**
+ * vmpressure_cleanup() - shuts down vmpressure control structure
+ * @vmpr:	Structure to be cleaned up
+ *
+ * This function should be called before the structure in which it is
+ * embedded is cleaned up.
+ */
+void vmpressure_cleanup(struct vmpressure *vmpr)
+{
+	/*
+	 * Make sure there is no pending work before eventfd infrastructure
+	 * goes away.
+	 */
+	flush_work(&vmpr->work);
+}
-- 
1.8.3.2

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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH resend 1/3] vmpressure: change vmpressure::sr_lock to spinlock
  2013-07-19 16:51 [PATCH resend 1/3] vmpressure: change vmpressure::sr_lock to spinlock Michal Hocko
       [not found] ` <1374252671-11939-1-git-send-email-mhocko-AlSwsSmVLrQ@public.gmane.org>
  2013-07-19 16:51 ` [PATCH resend 3/3] vmpressure: Make sure there are no events queued after memcg is offlined Michal Hocko
@ 2013-07-23 16:17 ` Tejun Heo
  2 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2013-07-23 16:17 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Andrew Morton, Anton Vorontsov, Johannes Weiner,
	KAMEZAWA Hiroyuki, KOSAKI Motohiro, Li Zefan, linux-mm, cgroups,
	linux-kernel

Hello, Michal.

Sorry about the delay.  Was on the road.

On Fri, Jul 19, 2013 at 06:51:09PM +0200, Michal Hocko wrote:
> There is nothing that can sleep inside critical sections protected by
> this lock and those sections are really small so there doesn't make much
> sense to use mutex for them. Change the log to a spinlock
> 
> Brought-up-by: Tejun Heo <tj@kernel.org>
> Signed-off-by: Michal Hocko <mhocko@suse.cz>

Reviewed-by: Tejun Heo <tj@kernel.org>

Thanks.

-- 
tejun

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH resend 2/3] vmpressure: do not check for pending work to prevent from new work
  2013-07-19 16:51   ` [PATCH resend 2/3] vmpressure: do not check for pending work to prevent from new work Michal Hocko
@ 2013-07-23 16:17     ` Tejun Heo
  0 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2013-07-23 16:17 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Andrew Morton, Anton Vorontsov, Johannes Weiner,
	KAMEZAWA Hiroyuki, KOSAKI Motohiro, Li Zefan, linux-mm, cgroups,
	linux-kernel

On Fri, Jul 19, 2013 at 06:51:10PM +0200, Michal Hocko wrote:
> because it is racy and it doesn't give us much anyway as schedule_work
> handles this case already.
> 
> Brought-up-by: Tejun Heo <tj@kernel.org>
> Signed-off-by: Michal Hocko <mhocko@suse.cz>

Acked-by: Tejun Heo <tj@kernel.org>

Thanks.

-- 
tejun

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH resend 3/3] vmpressure: Make sure there are no events queued after memcg is offlined
  2013-07-19 16:51 ` [PATCH resend 3/3] vmpressure: Make sure there are no events queued after memcg is offlined Michal Hocko
@ 2013-07-23 16:18   ` Tejun Heo
  0 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2013-07-23 16:18 UTC (permalink / raw)
  To: Michal Hocko
  Cc: Andrew Morton, Anton Vorontsov, Johannes Weiner,
	KAMEZAWA Hiroyuki, KOSAKI Motohiro, Li Zefan, linux-mm, cgroups,
	linux-kernel

On Fri, Jul 19, 2013 at 06:51:11PM +0200, Michal Hocko wrote:
> vmpressure is called synchronously from the reclaim where the
> target_memcg is guaranteed to be alive but the eventfd is signaled from
> the work queue context. This means that memcg (along with vmpressure
> structure which is embedded into it) might go away while the work item
> is pending which would result in use-after-release bug.
> 
> We have two possible ways how to fix this. Either vmpressure pins memcg
> before it schedules vmpr->work and unpin it in vmpressure_work_fn or
> explicitely flush the work item from the css_offline context (as
> suggested by Tejun).
> 
> This patch implements the later one and it introduces vmpressure_cleanup
> which flushes the vmpressure work queue item item. It hooks into
> mem_cgroup_css_offline after the memcg itself is cleaned up.
> 
> Reported-by: Tejun Heo <tj@kernel.org>
> Signed-off-by: Michal Hocko <mhocko@suse.cz>

Acked-by: Tejun Heo <tj@kernel.org>

Thanks!

-- 
tejun

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-07-23 16:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-19 16:51 [PATCH resend 1/3] vmpressure: change vmpressure::sr_lock to spinlock Michal Hocko
     [not found] ` <1374252671-11939-1-git-send-email-mhocko-AlSwsSmVLrQ@public.gmane.org>
2013-07-19 16:51   ` [PATCH resend 2/3] vmpressure: do not check for pending work to prevent from new work Michal Hocko
2013-07-23 16:17     ` Tejun Heo
2013-07-19 16:51 ` [PATCH resend 3/3] vmpressure: Make sure there are no events queued after memcg is offlined Michal Hocko
2013-07-23 16:18   ` Tejun Heo
2013-07-23 16:17 ` [PATCH resend 1/3] vmpressure: change vmpressure::sr_lock to spinlock Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).