]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
blktrace: factor out recording a blktrace event
authorJohannes Thumshirn <johannes.thumshirn@wdc.com>
Wed, 22 Oct 2025 11:41:01 +0000 (13:41 +0200)
committerJens Axboe <axboe@kernel.dk>
Wed, 22 Oct 2025 17:14:05 +0000 (11:14 -0600)
Factor out the recording of a blktrace event into its own function,
deduplicating the code.

This also enables recording different versions of the blktrace protocol
later on.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
kernel/trace/blktrace.c

index bc4b885f2ceca7c842b632cebf46df9117987355..25a0a1b09747e6043868f0ec897b2a5b477f2d9f 100644 (file)
@@ -63,6 +63,34 @@ static int blk_probes_ref;
 static void blk_register_tracepoints(void);
 static void blk_unregister_tracepoints(void);
 
+static void record_blktrace_event(struct blk_io_trace *t, pid_t pid, int cpu,
+                                 sector_t sector, int bytes, u32 what,
+                                 dev_t dev, int error, u64 cgid,
+                                 ssize_t cgid_len, void *pdu_data, int pdu_len)
+
+{
+       /*
+        * These two are not needed in ftrace as they are in the
+        * generic trace_entry, filled by tracing_generic_entry_update,
+        * but for the trace_event->bin() synthesizer benefit we do it
+        * here too.
+        */
+       t->cpu = cpu;
+       t->pid = pid;
+
+       t->sector = sector;
+       t->bytes = bytes;
+       t->action = what;
+       t->device = dev;
+       t->error = error;
+       t->pdu_len = pdu_len + cgid_len;
+
+       if (cgid_len)
+               memcpy((void *)t + sizeof(*t), &cgid, cgid_len);
+       if (pdu_len)
+               memcpy((void *)t + sizeof(*t) + cgid_len, pdu_data, pdu_len);
+}
+
 /*
  * Send out a notify message.
  */
@@ -87,7 +115,12 @@ static void trace_note(struct blk_trace *bt, pid_t pid, int action,
                if (!event)
                        return;
                t = ring_buffer_event_data(event);
-               goto record_it;
+               record_blktrace_event(t, pid, cpu, 0, 0,
+                                     action | (cgid ? __BLK_TN_CGROUP : 0),
+                                     bt->dev, 0, cgid, cgid_len, (void *)data,
+                                     len);
+               trace_buffer_unlock_commit(blk_tr, buffer, event, trace_ctx);
+               return;
        }
 
        if (!bt->rchan)
@@ -97,18 +130,11 @@ static void trace_note(struct blk_trace *bt, pid_t pid, int action,
        if (t) {
                t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
                t->time = ktime_to_ns(ktime_get());
-record_it:
-               t->device = bt->dev;
-               t->action = action | (cgid ? __BLK_TN_CGROUP : 0);
-               t->pid = pid;
-               t->cpu = cpu;
-               t->pdu_len = len + cgid_len;
-               if (cgid_len)
-                       memcpy((void *)t + sizeof(*t), &cgid, cgid_len);
-               memcpy((void *) t + sizeof(*t) + cgid_len, data, len);
-
-               if (blk_tracer)
-                       trace_buffer_unlock_commit(blk_tr, buffer, event, trace_ctx);
+
+               record_blktrace_event(t, pid, cpu, 0, 0,
+                                     action | (cgid ? __BLK_TN_CGROUP : 0),
+                                     bt->dev, 0, cgid, cgid_len, (void *)data,
+                                     len);
        }
 }
 
@@ -263,7 +289,12 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
                if (!event)
                        return;
                t = ring_buffer_event_data(event);
-               goto record_it;
+
+               record_blktrace_event(t, pid, cpu, sector, bytes, what, bt->dev,
+                                     error, cgid, cgid_len, pdu_data, pdu_len);
+
+               trace_buffer_unlock_commit(blk_tr, buffer, event, trace_ctx);
+               return;
        }
 
        if (unlikely(tsk->btrace_seq != blktrace_seq))
@@ -282,32 +313,10 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
                t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
                t->sequence = ++(*sequence);
                t->time = ktime_to_ns(ktime_get());
-record_it:
-               /*
-                * These two are not needed in ftrace as they are in the
-                * generic trace_entry, filled by tracing_generic_entry_update,
-                * but for the trace_event->bin() synthesizer benefit we do it
-                * here too.
-                */
-               t->cpu = cpu;
-               t->pid = pid;
-
-               t->sector = sector;
-               t->bytes = bytes;
-               t->action = what;
-               t->device = bt->dev;
-               t->error = error;
-               t->pdu_len = pdu_len + cgid_len;
-
-               if (cgid_len)
-                       memcpy((void *)t + sizeof(*t), &cgid, cgid_len);
-               if (pdu_len)
-                       memcpy((void *)t + sizeof(*t) + cgid_len, pdu_data, pdu_len);
-
-               if (blk_tracer) {
-                       trace_buffer_unlock_commit(blk_tr, buffer, event, trace_ctx);
-                       return;
-               }
+
+               record_blktrace_event(t, pid, cpu, sector, bytes, what,
+                                     bt->dev, error, cgid, cgid_len,
+                                     pdu_data, pdu_len);
        }
 
        local_irq_restore(flags);