[-next,1/3] blktrace: introduce 'blk_trace_swicth_state' helper

Message ID 20221017065321.2846017-2-yebin10@huawei.com
State New
Headers
Series fix possible memleak in '__blk_trace_remove' |

Commit Message

Ye Bin Oct. 17, 2022, 6:53 a.m. UTC
  Introduce 'blk_trace_swicth_state' helper. No functional changed.

Signed-off-by: Ye Bin <yebin10@huawei.com>
---
 kernel/trace/blktrace.c | 74 ++++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 38 deletions(-)
  

Comments

Christoph Hellwig Oct. 18, 2022, 7:40 a.m. UTC | #1
On Mon, Oct 17, 2022 at 02:53:19PM +0800, Ye Bin wrote:
> +static int blk_trace_switch_state(struct blk_trace *bt, int start)

This is a very confusing helper.  Please split it into two helpers
(e.g. blk_trace_{start,stop}) instead of the awakward int used as
boolean argument that makes it do two entirely unrelated things.
  

Patch

diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index 7f5eb295fe19..edd83e213580 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -353,6 +353,39 @@  static void blk_trace_cleanup(struct request_queue *q, struct blk_trace *bt)
 	put_probe_ref();
 }
 
+static int blk_trace_switch_state(struct blk_trace *bt, int start)
+{
+	/*
+	 * For starting a trace, we can transition from a setup or stopped
+	 * trace. For stopping a trace, the state must be running
+	 */
+	if (start) {
+		if (bt->trace_state == Blktrace_setup ||
+		    bt->trace_state == Blktrace_stopped) {
+			blktrace_seq++;
+			smp_mb();
+			bt->trace_state = Blktrace_running;
+			raw_spin_lock_irq(&running_trace_lock);
+			list_add(&bt->running_list, &running_trace_list);
+			raw_spin_unlock_irq(&running_trace_lock);
+
+			trace_note_time(bt);
+			return 0;
+		}
+	} else {
+		if (bt->trace_state == Blktrace_running) {
+			bt->trace_state = Blktrace_stopped;
+			raw_spin_lock_irq(&running_trace_lock);
+			list_del_init(&bt->running_list);
+			raw_spin_unlock_irq(&running_trace_lock);
+			relay_flush(bt->rchan);
+			return 0;
+		}
+	}
+
+	return -EINVAL;
+}
+
 static int __blk_trace_remove(struct request_queue *q)
 {
 	struct blk_trace *bt;
@@ -658,7 +691,6 @@  static int compat_blk_trace_setup(struct request_queue *q, char *name,
 
 static int __blk_trace_startstop(struct request_queue *q, int start)
 {
-	int ret;
 	struct blk_trace *bt;
 
 	bt = rcu_dereference_protected(q->blk_trace,
@@ -666,36 +698,7 @@  static int __blk_trace_startstop(struct request_queue *q, int start)
 	if (bt == NULL)
 		return -EINVAL;
 
-	/*
-	 * For starting a trace, we can transition from a setup or stopped
-	 * trace. For stopping a trace, the state must be running
-	 */
-	ret = -EINVAL;
-	if (start) {
-		if (bt->trace_state == Blktrace_setup ||
-		    bt->trace_state == Blktrace_stopped) {
-			blktrace_seq++;
-			smp_mb();
-			bt->trace_state = Blktrace_running;
-			raw_spin_lock_irq(&running_trace_lock);
-			list_add(&bt->running_list, &running_trace_list);
-			raw_spin_unlock_irq(&running_trace_lock);
-
-			trace_note_time(bt);
-			ret = 0;
-		}
-	} else {
-		if (bt->trace_state == Blktrace_running) {
-			bt->trace_state = Blktrace_stopped;
-			raw_spin_lock_irq(&running_trace_lock);
-			list_del_init(&bt->running_list);
-			raw_spin_unlock_irq(&running_trace_lock);
-			relay_flush(bt->rchan);
-			ret = 0;
-		}
-	}
-
-	return ret;
+	return blk_trace_switch_state(bt, start);
 }
 
 int blk_trace_startstop(struct request_queue *q, int start)
@@ -1614,13 +1617,8 @@  static int blk_trace_remove_queue(struct request_queue *q)
 	if (bt == NULL)
 		return -EINVAL;
 
-	if (bt->trace_state == Blktrace_running) {
-		bt->trace_state = Blktrace_stopped;
-		raw_spin_lock_irq(&running_trace_lock);
-		list_del_init(&bt->running_list);
-		raw_spin_unlock_irq(&running_trace_lock);
-		relay_flush(bt->rchan);
-	}
+	if (bt->trace_state == Blktrace_running)
+		blk_trace_switch_state(bt, 0);
 
 	put_probe_ref();
 	synchronize_rcu();