Skip to contentSkip to conversation

tracing/workqueue: Get rid of searching last executed worklet in probe_worklet_complete()

PATCH Superseded Zhaolei 3 participants 8 messages May 21, 2009 – May 27, 2009 Tracing

lore
Share & tools
lore

Zhaolei

author
#0 lore raw

PATCH 1/1 tracing/workqueue: Get rid of searching last executed worklet in probe_worklet_complete()

We don't need search workqueue's worklet list for worklet which was latest executed, instead, we can use a pointer in cpu_workqueue_stats to remember which worklet was just executed.

Thanks Oleg for pointing it out.

---
 kernel/trace/trace_workqueue.c |   37 +++++++++++++------------------------
 1 files changed, 13 insertions(+), 24 deletions(-)
kernel/trace/trace_workqueue.c +13 -24
index c67be60..d90f9ac 100644
@@ -25,13 +25,6 @@ struct workfunc_stats {
 	unsigned int			inserted;
 	unsigned int			executed;
 
-	/*
-	 * save latest work_struct's pointer to use as identifier in
-	 * probe_worklet_complete, because we can't use work_struct->...
-	 * after worklet got executed
-	 */
-	void				*work;
-
 	/* save execution time temporarily for calculate executed time */
 	u64				start_time;
 	u64				max_executed_time;
@@ -46,10 +39,17 @@ struct cpu_workqueue_stats {
 	/* Protected by cpu workqueue lock */
 	unsigned int		inserted;
 	unsigned int		executed;
+
 	/* list of struct workfunc_stats in this workqueue */
 	struct list_head	workfunclist;
 
 	/*
+	 * pointer to last executed worklet's workfunc_stats in this workqueue,
+	 * used by probe_worklet_complete()
+	 */
+	struct workfunc_stats *last_workfunc;
+
+	/*
 	 * the task maybe destroyed when we read stat file
 	 * we define it to void * because we only use it as a identifier
 	 */
@@ -163,7 +163,7 @@ found_wq:
 		if (wfnode->func == work->func) {
 			wfnode->executed++;
 			wfnode->start_time = trace_clock_global();
-			wfnode->work = work;
+			node->last_workfunc = wfnode;
 			goto found_wf;
 		}
 	pr_debug("trace_workqueue: worklet not found\n");
@@ -180,7 +180,7 @@ probe_worklet_complete(struct task_struct *wq_thread, void *work)
 {
 	int cpu = cpumask_first(&wq_thread->cpus_allowed);
 	struct cpu_workqueue_stats *node;
-	struct workfunc_stats *wfnode;
+	u64 executed_time;
 	unsigned long flags;
 
 	spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
@@ -192,22 +192,11 @@ probe_worklet_complete(struct task_struct *wq_thread, void *work)
 	goto end;
 
 found_wq:
-	list_for_each_entry(wfnode, &node->workfunclist, list) {
-		u64 executed_time;
+	executed_time = trace_clock_global() - node->last_workfunc->start_time;
+	node->last_workfunc->total_time += executed_time;
+	if (executed_time > node->last_workfunc->max_executed_time)
+		node->last_workfunc->max_executed_time = executed_time;
 
-		if (wfnode->work != work)
-			continue;
-
-		executed_time = trace_clock_global() - wfnode->start_time;
-		wfnode->total_time += executed_time;
-		if (executed_time > wfnode->max_executed_time)
-			wfnode->max_executed_time = executed_time;
-		goto found_wf;
-	}
-	pr_debug("trace_workqueue: worklet not found\n");
-	goto end;
-
-found_wf:
 end:
 	spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
 }
Signature · 1 line
1.5.5.3
#1 lore raw
Quoted 4 lines · Zhaolei

We don't need search workqueue's worklet list for worklet which was latest executed, instead, we can use a pointer in cpu_workqueue_stats to remember which worklet was just executed.

Minor nit,

kernel/trace/trace_workqueue.c probe_worklet_complete(struct task_struct *wq_thread, void *work) · line 196

Quoted 8 lines
kernel/trace/trace_workqueue.c +2 -2
@@ -192,22 +192,11 @@ probe_worklet_complete(struct task_struct *wq_thread, void *work)
 	goto end;
 
 found_wq:
-	list_for_each_entry(wfnode, &node->workfunclist, list) {
-		u64 executed_time;
+	executed_time = trace_clock_global() - node->last_workfunc->start_time;
+	node->last_workfunc->total_time += executed_time;

Suppose that "enqueue" handler fails to allocate the memory for the new work_struct.

This means that the "execute" handler does not record ->last_workfunc.

In that case probe_worklet_complete() uses the wrong workfunc_stats or we can even crash if ->last_workfunc == NULL.

I think _execute() should set ->last_workfunc = NULL first, then serach for wfnode. _complete() should check ->last_workfunc != NULL and return if it is NULL.

Oleg.

author
#2 lore raw

Re: [PATCH] tracing/workqueue: Get rid of searching last executedworklet in probe_worklet_complete()

* From: "Oleg Nesterov" <[email protected]>

Quoted 20 lines · Zhaolei
Quoted 4 lines

We don't need search workqueue's worklet list for worklet which was latest executed, instead, we can use a pointer in cpu_workqueue_stats to remember which worklet was just executed.

Minor nit,

kernel/trace/trace_workqueue.c probe_worklet_complete(struct task_struct *wq_thread, void *work) · line 196

Quoted 8 lines
kernel/trace/trace_workqueue.c +2 -2
@@ -192,22 +192,11 @@ probe_worklet_complete(struct task_struct *wq_thread, void *work)
 goto end;
 
 found_wq:
- list_for_each_entry(wfnode, &node->workfunclist, list) {
- u64 executed_time;
+ executed_time = trace_clock_global() - node->last_workfunc->start_time;
+ node->last_workfunc->total_time += executed_time;

Suppose that "enqueue" handler fails to allocate the memory for the new work_struct.

Quoted 8 lines

This means that the "execute" handler does not record ->last_workfunc.

In that case probe_worklet_complete() uses the wrong workfunc_stats or we can even crash if ->last_workfunc == NULL.

I think _execute() should set ->last_workfunc = NULL first, then serach for wfnode. _complete() should check ->last_workfunc != NULL and return if it is NULL.

Hello, Oleg

Thanks!
You are right, I will send v2 to fix this.

Zhaoleiÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ¥Šwÿº{.nÇ+‰·¥Š{±þG«�éÿŠ{ayºʇڙë,j­¢f£¢·hš�ï�êÿ‘êçz_è®(­éšŽŠÝ¢j"�ú¶m§ÿÿ¾«þG«�éÿ¢¸?™¨è­Ú&£ø§~�á¶iO•æ¬z·švØ^¶m§ÿÿà ÿ¶ìÿ¢¸?–I¥

author
#3 lore raw

PATCH v2 1/1 tracing/workqueue: Get rid of searching last executed worklet in probe_worklet_complete()

In reply to Zhaolei

We don't need search workqueue's worklet list for worklet which was latest executed, instead, we can use a pointer in cpu_workqueue_stats to remember which worklet was just executed.

Thanks Oleg for pointing it out.

Changelog:
v1->v2: Oleg pointed out that if searching executed workfunc_stats failed in probe_worklet_execute(), for example, workfunc_stats's memory allocation failed previously, cpu_workqueue_stats->last_workfunc is not set to correct value, and it will cause wrong accessing in probe_worklet_complete(). This problem is fixed in v2.

---
 kernel/trace/trace_workqueue.c |   39 ++++++++++++++++-----------------------
 1 files changed, 16 insertions(+), 23 deletions(-)
kernel/trace/trace_workqueue.c +16 -23
index c67be60..740de3b 100644
@@ -25,13 +25,6 @@ struct workfunc_stats {
 	unsigned int			inserted;
 	unsigned int			executed;
 
-	/*
-	 * save latest work_struct's pointer to use as identifier in
-	 * probe_worklet_complete, because we can't use work_struct->...
-	 * after worklet got executed
-	 */
-	void				*work;
-
 	/* save execution time temporarily for calculate executed time */
 	u64				start_time;
 	u64				max_executed_time;
@@ -46,10 +39,17 @@ struct cpu_workqueue_stats {
 	/* Protected by cpu workqueue lock */
 	unsigned int		inserted;
 	unsigned int		executed;
+
 	/* list of struct workfunc_stats in this workqueue */
 	struct list_head	workfunclist;
 
 	/*
+	 * pointer to last executed worklet's workfunc_stats in this workqueue,
+	 * used by probe_worklet_complete()
+	 */
+	struct workfunc_stats *last_workfunc;
+
+	/*
 	 * the task maybe destroyed when we read stat file
 	 * we define it to void * because we only use it as a identifier
 	 */
@@ -163,9 +163,10 @@ found_wq:
 		if (wfnode->func == work->func) {
 			wfnode->executed++;
 			wfnode->start_time = trace_clock_global();
-			wfnode->work = work;
+			node->last_workfunc = wfnode;
 			goto found_wf;
 		}
+	node->last_workfunc = NULL;
 	pr_debug("trace_workqueue: worklet not found\n");
 	goto end;
 
@@ -180,7 +181,7 @@ probe_worklet_complete(struct task_struct *wq_thread, void *work)
 {
 	int cpu = cpumask_first(&wq_thread->cpus_allowed);
 	struct cpu_workqueue_stats *node;
-	struct workfunc_stats *wfnode;
+	u64 executed_time;
 	unsigned long flags;
 
 	spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
@@ -192,22 +193,14 @@ probe_worklet_complete(struct task_struct *wq_thread, void *work)
 	goto end;
 
 found_wq:
-	list_for_each_entry(wfnode, &node->workfunclist, list) {
-		u64 executed_time;
+	if (!node->last_workfunc)
+		goto end;
 
-		if (wfnode->work != work)
-			continue;
+	executed_time = trace_clock_global() - node->last_workfunc->start_time;
+	node->last_workfunc->total_time += executed_time;
+	if (executed_time > node->last_workfunc->max_executed_time)
+		node->last_workfunc->max_executed_time = executed_time;
 
-		executed_time = trace_clock_global() - wfnode->start_time;
-		wfnode->total_time += executed_time;
-		if (executed_time > wfnode->max_executed_time)
-			wfnode->max_executed_time = executed_time;
-		goto found_wf;
-	}
-	pr_debug("trace_workqueue: worklet not found\n");
-	goto end;
-
-found_wf:
 end:
 	spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
 }
Signature · 1 line
1.5.5.3
#4 lore raw
Quoted 4 lines · Zhaolei

We don't need search workqueue's worklet list for worklet which was latest executed, instead, we can use a pointer in cpu_workqueue_stats to remember which worklet was just executed.

Thanks Zhao, I think this is correct.

Oleg.

Acked-by on v2 1/1
#7 lore raw
In reply to Zhaolei
Quoted 13 lines · Zhaolei

We don't need search workqueue's worklet list for worklet which was latest executed, instead, we can use a pointer in cpu_workqueue_stats to remember which worklet was just executed.

Thanks Oleg for pointing it out.

Changelog:
v1->v2: Oleg pointed out that if searching executed workfunc_stats failed in probe_worklet_execute(), for example, workfunc_stats's memory allocation failed previously, cpu_workqueue_stats->last_workfunc is not set to correct value, and it will cause wrong accessing in probe_worklet_complete(). This problem is fixed in v2.

Quoted 2 lines

Looks good, thanks!

Frederic.

Quoted 94 lines
---
 kernel/trace/trace_workqueue.c |   39 ++++++++++++++++-----------------------
 1 files changed, 16 insertions(+), 23 deletions(-)
kernel/trace/trace_workqueue.c +16 -23
index c67be60..740de3b 100644
@@ -25,13 +25,6 @@ struct workfunc_stats {
 	unsigned int			inserted;
 	unsigned int			executed;
 
-	/*
-	 * save latest work_struct's pointer to use as identifier in
-	 * probe_worklet_complete, because we can't use work_struct->...
-	 * after worklet got executed
-	 */
-	void				*work;
-
 	/* save execution time temporarily for calculate executed time */
 	u64				start_time;
 	u64				max_executed_time;
@@ -46,10 +39,17 @@ struct cpu_workqueue_stats {
 	/* Protected by cpu workqueue lock */
 	unsigned int		inserted;
 	unsigned int		executed;
+
 	/* list of struct workfunc_stats in this workqueue */
 	struct list_head	workfunclist;
 
 	/*
+	 * pointer to last executed worklet's workfunc_stats in this workqueue,
+	 * used by probe_worklet_complete()
+	 */
+	struct workfunc_stats *last_workfunc;
+
+	/*
 	 * the task maybe destroyed when we read stat file
 	 * we define it to void * because we only use it as a identifier
 	 */
@@ -163,9 +163,10 @@ found_wq:
 		if (wfnode->func == work->func) {
 			wfnode->executed++;
 			wfnode->start_time = trace_clock_global();
-			wfnode->work = work;
+			node->last_workfunc = wfnode;
 			goto found_wf;
 		}
+	node->last_workfunc = NULL;
 	pr_debug("trace_workqueue: worklet not found\n");
 	goto end;
 
@@ -180,7 +181,7 @@ probe_worklet_complete(struct task_struct *wq_thread, void *work)
 {
 	int cpu = cpumask_first(&wq_thread->cpus_allowed);
 	struct cpu_workqueue_stats *node;
-	struct workfunc_stats *wfnode;
+	u64 executed_time;
 	unsigned long flags;
 
 	spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
@@ -192,22 +193,14 @@ probe_worklet_complete(struct task_struct *wq_thread, void *work)
 	goto end;
 
 found_wq:
-	list_for_each_entry(wfnode, &node->workfunclist, list) {
-		u64 executed_time;
+	if (!node->last_workfunc)
+		goto end;
 
-		if (wfnode->work != work)
-			continue;
+	executed_time = trace_clock_global() - node->last_workfunc->start_time;
+	node->last_workfunc->total_time += executed_time;
+	if (executed_time > node->last_workfunc->max_executed_time)
+		node->last_workfunc->max_executed_time = executed_time;
 
-		executed_time = trace_clock_global() - wfnode->start_time;
-		wfnode->total_time += executed_time;
-		if (executed_time > wfnode->max_executed_time)
-			wfnode->max_executed_time = executed_time;
-		goto found_wf;
-	}
-	pr_debug("trace_workqueue: worklet not found\n");
-	goto end;
-
-found_wf:
 end:
 	spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
 }
Signature · 1 line
1.5.5.3
#5 lore raw
In reply to Oleg Nesterov
Quoted 9 lines · Oleg Nesterov
Quoted 4 lines · Zhaolei

We don't need search workqueue's worklet list for worklet which was latest executed, instead, we can use a pointer in cpu_workqueue_stats to remember which worklet was just executed.

Thanks Zhao, I think this is correct.

Oleg.

May I add your Acked-by then?

Thanks.

#6 lore raw
Quoted 8 lines · Frederic Weisbecker
Quoted 6 lines · Oleg Nesterov
Quoted 4 lines · Zhaolei

We don't need search workqueue's worklet list for worklet which was latest executed, instead, we can use a pointer in cpu_workqueue_stats to remember which worklet was just executed.

Quoted 5 lines
Quoted 3 lines

Thanks Zhao, I think this is correct.

Oleg.

May I add your Acked-by then?

Yes, please feel free. Or reviewed-by, whatever you think more right.

Oleg.