~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/trace/blktrace.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
  3  *
  4  * This program is free software; you can redistribute it and/or modify
  5  * it under the terms of the GNU General Public License version 2 as
  6  * published by the Free Software Foundation.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11  * GNU General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU General Public License
 14  * along with this program; if not, write to the Free Software
 15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 16  *
 17  */
 18 #include <linux/kernel.h>
 19 #include <linux/blkdev.h>
 20 #include <linux/blktrace_api.h>
 21 #include <linux/percpu.h>
 22 #include <linux/init.h>
 23 #include <linux/mutex.h>
 24 #include <linux/slab.h>
 25 #include <linux/debugfs.h>
 26 #include <linux/export.h>
 27 #include <linux/time.h>
 28 #include <linux/uaccess.h>
 29 #include <linux/list.h>
 30 
 31 #include <trace/events/block.h>
 32 
 33 #include "trace_output.h"
 34 
 35 #ifdef CONFIG_BLK_DEV_IO_TRACE
 36 
 37 static unsigned int blktrace_seq __read_mostly = 1;
 38 
 39 static struct trace_array *blk_tr;
 40 static bool blk_tracer_enabled __read_mostly;
 41 
 42 static LIST_HEAD(running_trace_list);
 43 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock);
 44 
 45 /* Select an alternative, minimalistic output than the original one */
 46 #define TRACE_BLK_OPT_CLASSIC   0x1
 47 
 48 static struct tracer_opt blk_tracer_opts[] = {
 49         /* Default disable the minimalistic output */
 50         { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
 51         { }
 52 };
 53 
 54 static struct tracer_flags blk_tracer_flags = {
 55         .val  = 0,
 56         .opts = blk_tracer_opts,
 57 };
 58 
 59 /* Global reference count of probes */
 60 static DEFINE_MUTEX(blk_probe_mutex);
 61 static int blk_probes_ref;
 62 
 63 static void blk_register_tracepoints(void);
 64 static void blk_unregister_tracepoints(void);
 65 
 66 /*
 67  * Send out a notify message.
 68  */
 69 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
 70                        const void *data, size_t len)
 71 {
 72         struct blk_io_trace *t;
 73         struct ring_buffer_event *event = NULL;
 74         struct ring_buffer *buffer = NULL;
 75         int pc = 0;
 76         int cpu = smp_processor_id();
 77         bool blk_tracer = blk_tracer_enabled;
 78 
 79         if (blk_tracer) {
 80                 buffer = blk_tr->trace_buffer.buffer;
 81                 pc = preempt_count();
 82                 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
 83                                                   sizeof(*t) + len,
 84                                                   0, pc);
 85                 if (!event)
 86                         return;
 87                 t = ring_buffer_event_data(event);
 88                 goto record_it;
 89         }
 90 
 91         if (!bt->rchan)
 92                 return;
 93 
 94         t = relay_reserve(bt->rchan, sizeof(*t) + len);
 95         if (t) {
 96                 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
 97                 t->time = ktime_to_ns(ktime_get());
 98 record_it:
 99                 t->device = bt->dev;
100                 t->action = action;
101                 t->pid = pid;
102                 t->cpu = cpu;
103                 t->pdu_len = len;
104                 memcpy((void *) t + sizeof(*t), data, len);
105 
106                 if (blk_tracer)
107                         trace_buffer_unlock_commit(buffer, event, 0, pc);
108         }
109 }
110 
111 /*
112  * Send out a notify for this process, if we haven't done so since a trace
113  * started
114  */
115 static void trace_note_tsk(struct task_struct *tsk)
116 {
117         unsigned long flags;
118         struct blk_trace *bt;
119 
120         tsk->btrace_seq = blktrace_seq;
121         spin_lock_irqsave(&running_trace_lock, flags);
122         list_for_each_entry(bt, &running_trace_list, running_list) {
123                 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm,
124                            sizeof(tsk->comm));
125         }
126         spin_unlock_irqrestore(&running_trace_lock, flags);
127 }
128 
129 static void trace_note_time(struct blk_trace *bt)
130 {
131         struct timespec now;
132         unsigned long flags;
133         u32 words[2];
134 
135         getnstimeofday(&now);
136         words[0] = now.tv_sec;
137         words[1] = now.tv_nsec;
138 
139         local_irq_save(flags);
140         trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
141         local_irq_restore(flags);
142 }
143 
144 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
145 {
146         int n;
147         va_list args;
148         unsigned long flags;
149         char *buf;
150 
151         if (unlikely(bt->trace_state != Blktrace_running &&
152                      !blk_tracer_enabled))
153                 return;
154 
155         /*
156          * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
157          * message to the trace.
158          */
159         if (!(bt->act_mask & BLK_TC_NOTIFY))
160                 return;
161 
162         local_irq_save(flags);
163         buf = this_cpu_ptr(bt->msg_data);
164         va_start(args, fmt);
165         n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
166         va_end(args);
167 
168         trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
169         local_irq_restore(flags);
170 }
171 EXPORT_SYMBOL_GPL(__trace_note_message);
172 
173 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
174                          pid_t pid)
175 {
176         if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
177                 return 1;
178         if (sector && (sector < bt->start_lba || sector > bt->end_lba))
179                 return 1;
180         if (bt->pid && pid != bt->pid)
181                 return 1;
182 
183         return 0;
184 }
185 
186 /*
187  * Data direction bit lookup
188  */
189 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
190                                  BLK_TC_ACT(BLK_TC_WRITE) };
191 
192 #define BLK_TC_RAHEAD           BLK_TC_AHEAD
193 
194 /* The ilog2() calls fall out because they're constant */
195 #define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
196           (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
197 
198 /*
199  * The worker for the various blk_add_trace*() types. Fills out a
200  * blk_io_trace structure and places it in a per-cpu subbuffer.
201  */
202 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
203                      int rw, u32 what, int error, int pdu_len, void *pdu_data)
204 {
205         struct task_struct *tsk = current;
206         struct ring_buffer_event *event = NULL;
207         struct ring_buffer *buffer = NULL;
208         struct blk_io_trace *t;
209         unsigned long flags = 0;
210         unsigned long *sequence;
211         pid_t pid;
212         int cpu, pc = 0;
213         bool blk_tracer = blk_tracer_enabled;
214 
215         if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
216                 return;
217 
218         what |= ddir_act[rw & WRITE];
219         what |= MASK_TC_BIT(rw, SYNC);
220         what |= MASK_TC_BIT(rw, RAHEAD);
221         what |= MASK_TC_BIT(rw, META);
222         what |= MASK_TC_BIT(rw, DISCARD);
223         what |= MASK_TC_BIT(rw, FLUSH);
224         what |= MASK_TC_BIT(rw, FUA);
225 
226         pid = tsk->pid;
227         if (act_log_check(bt, what, sector, pid))
228                 return;
229         cpu = raw_smp_processor_id();
230 
231         if (blk_tracer) {
232                 tracing_record_cmdline(current);
233 
234                 buffer = blk_tr->trace_buffer.buffer;
235                 pc = preempt_count();
236                 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
237                                                   sizeof(*t) + pdu_len,
238                                                   0, pc);
239                 if (!event)
240                         return;
241                 t = ring_buffer_event_data(event);
242                 goto record_it;
243         }
244 
245         if (unlikely(tsk->btrace_seq != blktrace_seq))
246                 trace_note_tsk(tsk);
247 
248         /*
249          * A word about the locking here - we disable interrupts to reserve
250          * some space in the relay per-cpu buffer, to prevent an irq
251          * from coming in and stepping on our toes.
252          */
253         local_irq_save(flags);
254         t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
255         if (t) {
256                 sequence = per_cpu_ptr(bt->sequence, cpu);
257 
258                 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
259                 t->sequence = ++(*sequence);
260                 t->time = ktime_to_ns(ktime_get());
261 record_it:
262                 /*
263                  * These two are not needed in ftrace as they are in the
264                  * generic trace_entry, filled by tracing_generic_entry_update,
265                  * but for the trace_event->bin() synthesizer benefit we do it
266                  * here too.
267                  */
268                 t->cpu = cpu;
269                 t->pid = pid;
270 
271                 t->sector = sector;
272                 t->bytes = bytes;
273                 t->action = what;
274                 t->device = bt->dev;
275                 t->error = error;
276                 t->pdu_len = pdu_len;
277 
278                 if (pdu_len)
279                         memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
280 
281                 if (blk_tracer) {
282                         trace_buffer_unlock_commit(buffer, event, 0, pc);
283                         return;
284                 }
285         }
286 
287         local_irq_restore(flags);
288 }
289 
290 static struct dentry *blk_tree_root;
291 static DEFINE_MUTEX(blk_tree_mutex);
292 
293 static void blk_trace_free(struct blk_trace *bt)
294 {
295         debugfs_remove(bt->msg_file);
296         debugfs_remove(bt->dropped_file);
297         relay_close(bt->rchan);
298         debugfs_remove(bt->dir);
299         free_percpu(bt->sequence);
300         free_percpu(bt->msg_data);
301         kfree(bt);
302 }
303 
304 static void get_probe_ref(void)
305 {
306         mutex_lock(&blk_probe_mutex);
307         if (++blk_probes_ref == 1)
308                 blk_register_tracepoints();
309         mutex_unlock(&blk_probe_mutex);
310 }
311 
312 static void put_probe_ref(void)
313 {
314         mutex_lock(&blk_probe_mutex);
315         if (!--blk_probes_ref)
316                 blk_unregister_tracepoints();
317         mutex_unlock(&blk_probe_mutex);
318 }
319 
320 static void blk_trace_cleanup(struct blk_trace *bt)
321 {
322         blk_trace_free(bt);
323         put_probe_ref();
324 }
325 
326 int blk_trace_remove(struct request_queue *q)
327 {
328         struct blk_trace *bt;
329 
330         bt = xchg(&q->blk_trace, NULL);
331         if (!bt)
332                 return -EINVAL;
333 
334         if (bt->trace_state != Blktrace_running)
335                 blk_trace_cleanup(bt);
336 
337         return 0;
338 }
339 EXPORT_SYMBOL_GPL(blk_trace_remove);
340 
341 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
342                                 size_t count, loff_t *ppos)
343 {
344         struct blk_trace *bt = filp->private_data;
345         char buf[16];
346 
347         snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
348 
349         return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
350 }
351 
352 static const struct file_operations blk_dropped_fops = {
353         .owner =        THIS_MODULE,
354         .open =         simple_open,
355         .read =         blk_dropped_read,
356         .llseek =       default_llseek,
357 };
358 
359 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
360                                 size_t count, loff_t *ppos)
361 {
362         char *msg;
363         struct blk_trace *bt;
364 
365         if (count >= BLK_TN_MAX_MSG)
366                 return -EINVAL;
367 
368         msg = kmalloc(count + 1, GFP_KERNEL);
369         if (msg == NULL)
370                 return -ENOMEM;
371 
372         if (copy_from_user(msg, buffer, count)) {
373                 kfree(msg);
374                 return -EFAULT;
375         }
376 
377         msg[count] = '\0';
378         bt = filp->private_data;
379         __trace_note_message(bt, "%s", msg);
380         kfree(msg);
381 
382         return count;
383 }
384 
385 static const struct file_operations blk_msg_fops = {
386         .owner =        THIS_MODULE,
387         .open =         simple_open,
388         .write =        blk_msg_write,
389         .llseek =       noop_llseek,
390 };
391 
392 /*
393  * Keep track of how many times we encountered a full subbuffer, to aid
394  * the user space app in telling how many lost events there were.
395  */
396 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
397                                      void *prev_subbuf, size_t prev_padding)
398 {
399         struct blk_trace *bt;
400 
401         if (!relay_buf_full(buf))
402                 return 1;
403 
404         bt = buf->chan->private_data;
405         atomic_inc(&bt->dropped);
406         return 0;
407 }
408 
409 static int blk_remove_buf_file_callback(struct dentry *dentry)
410 {
411         debugfs_remove(dentry);
412 
413         return 0;
414 }
415 
416 static struct dentry *blk_create_buf_file_callback(const char *filename,
417                                                    struct dentry *parent,
418                                                    umode_t mode,
419                                                    struct rchan_buf *buf,
420                                                    int *is_global)
421 {
422         return debugfs_create_file(filename, mode, parent, buf,
423                                         &relay_file_operations);
424 }
425 
426 static struct rchan_callbacks blk_relay_callbacks = {
427         .subbuf_start           = blk_subbuf_start_callback,
428         .create_buf_file        = blk_create_buf_file_callback,
429         .remove_buf_file        = blk_remove_buf_file_callback,
430 };
431 
432 static void blk_trace_setup_lba(struct blk_trace *bt,
433                                 struct block_device *bdev)
434 {
435         struct hd_struct *part = NULL;
436 
437         if (bdev)
438                 part = bdev->bd_part;
439 
440         if (part) {
441                 bt->start_lba = part->start_sect;
442                 bt->end_lba = part->start_sect + part->nr_sects;
443         } else {
444                 bt->start_lba = 0;
445                 bt->end_lba = -1ULL;
446         }
447 }
448 
449 /*
450  * Setup everything required to start tracing
451  */
452 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
453                        struct block_device *bdev,
454                        struct blk_user_trace_setup *buts)
455 {
456         struct blk_trace *old_bt, *bt = NULL;
457         struct dentry *dir = NULL;
458         int ret, i;
459 
460         if (!buts->buf_size || !buts->buf_nr)
461                 return -EINVAL;
462 
463         strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
464         buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
465 
466         /*
467          * some device names have larger paths - convert the slashes
468          * to underscores for this to work as expected
469          */
470         for (i = 0; i < strlen(buts->name); i++)
471                 if (buts->name[i] == '/')
472                         buts->name[i] = '_';
473 
474         bt = kzalloc(sizeof(*bt), GFP_KERNEL);
475         if (!bt)
476                 return -ENOMEM;
477 
478         ret = -ENOMEM;
479         bt->sequence = alloc_percpu(unsigned long);
480         if (!bt->sequence)
481                 goto err;
482 
483         bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
484         if (!bt->msg_data)
485                 goto err;
486 
487         ret = -ENOENT;
488 
489         mutex_lock(&blk_tree_mutex);
490         if (!blk_tree_root) {
491                 blk_tree_root = debugfs_create_dir("block", NULL);
492                 if (!blk_tree_root) {
493                         mutex_unlock(&blk_tree_mutex);
494                         goto err;
495                 }
496         }
497         mutex_unlock(&blk_tree_mutex);
498 
499         dir = debugfs_create_dir(buts->name, blk_tree_root);
500 
501         if (!dir)
502                 goto err;
503 
504         bt->dir = dir;
505         bt->dev = dev;
506         atomic_set(&bt->dropped, 0);
507         INIT_LIST_HEAD(&bt->running_list);
508 
509         ret = -EIO;
510         bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
511                                                &blk_dropped_fops);
512         if (!bt->dropped_file)
513                 goto err;
514 
515         bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
516         if (!bt->msg_file)
517                 goto err;
518 
519         bt->rchan = relay_open("trace", dir, buts->buf_size,
520                                 buts->buf_nr, &blk_relay_callbacks, bt);
521         if (!bt->rchan)
522                 goto err;
523 
524         bt->act_mask = buts->act_mask;
525         if (!bt->act_mask)
526                 bt->act_mask = (u16) -1;
527 
528         blk_trace_setup_lba(bt, bdev);
529 
530         /* overwrite with user settings */
531         if (buts->start_lba)
532                 bt->start_lba = buts->start_lba;
533         if (buts->end_lba)
534                 bt->end_lba = buts->end_lba;
535 
536         bt->pid = buts->pid;
537         bt->trace_state = Blktrace_setup;
538 
539         ret = -EBUSY;
540         old_bt = xchg(&q->blk_trace, bt);
541         if (old_bt) {
542                 (void) xchg(&q->blk_trace, old_bt);
543                 goto err;
544         }
545 
546         get_probe_ref();
547 
548         return 0;
549 err:
550         blk_trace_free(bt);
551         return ret;
552 }
553 
554 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
555                     struct block_device *bdev,
556                     char __user *arg)
557 {
558         struct blk_user_trace_setup buts;
559         int ret;
560 
561         ret = copy_from_user(&buts, arg, sizeof(buts));
562         if (ret)
563                 return -EFAULT;
564 
565         ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
566         if (ret)
567                 return ret;
568 
569         if (copy_to_user(arg, &buts, sizeof(buts))) {
570                 blk_trace_remove(q);
571                 return -EFAULT;
572         }
573         return 0;
574 }
575 EXPORT_SYMBOL_GPL(blk_trace_setup);
576 
577 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
578 static int compat_blk_trace_setup(struct request_queue *q, char *name,
579                                   dev_t dev, struct block_device *bdev,
580                                   char __user *arg)
581 {
582         struct blk_user_trace_setup buts;
583         struct compat_blk_user_trace_setup cbuts;
584         int ret;
585 
586         if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
587                 return -EFAULT;
588 
589         buts = (struct blk_user_trace_setup) {
590                 .act_mask = cbuts.act_mask,
591                 .buf_size = cbuts.buf_size,
592                 .buf_nr = cbuts.buf_nr,
593                 .start_lba = cbuts.start_lba,
594                 .end_lba = cbuts.end_lba,
595                 .pid = cbuts.pid,
596         };
597 
598         ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
599         if (ret)
600                 return ret;
601 
602         if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) {
603                 blk_trace_remove(q);
604                 return -EFAULT;
605         }
606 
607         return 0;
608 }
609 #endif
610 
611 int blk_trace_startstop(struct request_queue *q, int start)
612 {
613         int ret;
614         struct blk_trace *bt = q->blk_trace;
615 
616         if (bt == NULL)
617                 return -EINVAL;
618 
619         /*
620          * For starting a trace, we can transition from a setup or stopped
621          * trace. For stopping a trace, the state must be running
622          */
623         ret = -EINVAL;
624         if (start) {
625                 if (bt->trace_state == Blktrace_setup ||
626                     bt->trace_state == Blktrace_stopped) {
627                         blktrace_seq++;
628                         smp_mb();
629                         bt->trace_state = Blktrace_running;
630                         spin_lock_irq(&running_trace_lock);
631                         list_add(&bt->running_list, &running_trace_list);
632                         spin_unlock_irq(&running_trace_lock);
633 
634                         trace_note_time(bt);
635                         ret = 0;
636                 }
637         } else {
638                 if (bt->trace_state == Blktrace_running) {
639                         bt->trace_state = Blktrace_stopped;
640                         spin_lock_irq(&running_trace_lock);
641                         list_del_init(&bt->running_list);
642                         spin_unlock_irq(&running_trace_lock);
643                         relay_flush(bt->rchan);
644                         ret = 0;
645                 }
646         }
647 
648         return ret;
649 }
650 EXPORT_SYMBOL_GPL(blk_trace_startstop);
651 
652 /**
653  * blk_trace_ioctl: - handle the ioctls associated with tracing
654  * @bdev:       the block device
655  * @cmd:        the ioctl cmd
656  * @arg:        the argument data, if any
657  *
658  **/
659 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
660 {
661         struct request_queue *q;
662         int ret, start = 0;
663         char b[BDEVNAME_SIZE];
664 
665         q = bdev_get_queue(bdev);
666         if (!q)
667                 return -ENXIO;
668 
669         mutex_lock(&bdev->bd_mutex);
670 
671         switch (cmd) {
672         case BLKTRACESETUP:
673                 bdevname(bdev, b);
674                 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
675                 break;
676 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
677         case BLKTRACESETUP32:
678                 bdevname(bdev, b);
679                 ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
680                 break;
681 #endif
682         case BLKTRACESTART:
683                 start = 1;
684         case BLKTRACESTOP:
685                 ret = blk_trace_startstop(q, start);
686                 break;
687         case BLKTRACETEARDOWN:
688                 ret = blk_trace_remove(q);
689                 break;
690         default:
691                 ret = -ENOTTY;
692                 break;
693         }
694 
695         mutex_unlock(&bdev->bd_mutex);
696         return ret;
697 }
698 
699 /**
700  * blk_trace_shutdown: - stop and cleanup trace structures
701  * @q:    the request queue associated with the device
702  *
703  **/
704 void blk_trace_shutdown(struct request_queue *q)
705 {
706         if (q->blk_trace) {
707                 blk_trace_startstop(q, 0);
708                 blk_trace_remove(q);
709         }
710 }
711 
712 /*
713  * blktrace probes
714  */
715 
716 /**
717  * blk_add_trace_rq - Add a trace for a request oriented action
718  * @q:          queue the io is for
719  * @rq:         the source request
720  * @nr_bytes:   number of completed bytes
721  * @what:       the action
722  *
723  * Description:
724  *     Records an action against a request. Will log the bio offset + size.
725  *
726  **/
727 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
728                              unsigned int nr_bytes, u32 what)
729 {
730         struct blk_trace *bt = q->blk_trace;
731 
732         if (likely(!bt))
733                 return;
734 
735         if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
736                 what |= BLK_TC_ACT(BLK_TC_PC);
737                 __blk_add_trace(bt, 0, nr_bytes, rq->cmd_flags,
738                                 what, rq->errors, rq->cmd_len, rq->cmd);
739         } else  {
740                 what |= BLK_TC_ACT(BLK_TC_FS);
741                 __blk_add_trace(bt, blk_rq_pos(rq), nr_bytes,
742                                 rq->cmd_flags, what, rq->errors, 0, NULL);
743         }
744 }
745 
746 static void blk_add_trace_rq_abort(void *ignore,
747                                    struct request_queue *q, struct request *rq)
748 {
749         blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_ABORT);
750 }
751 
752 static void blk_add_trace_rq_insert(void *ignore,
753                                     struct request_queue *q, struct request *rq)
754 {
755         blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_INSERT);
756 }
757 
758 static void blk_add_trace_rq_issue(void *ignore,
759                                    struct request_queue *q, struct request *rq)
760 {
761         blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_ISSUE);
762 }
763 
764 static void blk_add_trace_rq_requeue(void *ignore,
765                                      struct request_queue *q,
766                                      struct request *rq)
767 {
768         blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_REQUEUE);
769 }
770 
771 static void blk_add_trace_rq_complete(void *ignore,
772                                       struct request_queue *q,
773                                       struct request *rq,
774                                       unsigned int nr_bytes)
775 {
776         blk_add_trace_rq(q, rq, nr_bytes, BLK_TA_COMPLETE);
777 }
778 
779 /**
780  * blk_add_trace_bio - Add a trace for a bio oriented action
781  * @q:          queue the io is for
782  * @bio:        the source bio
783  * @what:       the action
784  * @error:      error, if any
785  *
786  * Description:
787  *     Records an action against a bio. Will log the bio offset + size.
788  *
789  **/
790 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
791                               u32 what, int error)
792 {
793         struct blk_trace *bt = q->blk_trace;
794 
795         if (likely(!bt))
796                 return;
797 
798         if (!error && !bio_flagged(bio, BIO_UPTODATE))
799                 error = EIO;
800 
801         __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
802                         bio->bi_rw, what, error, 0, NULL);
803 }
804 
805 static void blk_add_trace_bio_bounce(void *ignore,
806                                      struct request_queue *q, struct bio *bio)
807 {
808         blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0);
809 }
810 
811 static void blk_add_trace_bio_complete(void *ignore,
812                                        struct request_queue *q, struct bio *bio,
813                                        int error)
814 {
815         blk_add_trace_bio(q, bio, BLK_TA_COMPLETE, error);
816 }
817 
818 static void blk_add_trace_bio_backmerge(void *ignore,
819                                         struct request_queue *q,
820                                         struct request *rq,
821                                         struct bio *bio)
822 {
823         blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0);
824 }
825 
826 static void blk_add_trace_bio_frontmerge(void *ignore,
827                                          struct request_queue *q,
828                                          struct request *rq,
829                                          struct bio *bio)
830 {
831         blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE, 0);
832 }
833 
834 static void blk_add_trace_bio_queue(void *ignore,
835                                     struct request_queue *q, struct bio *bio)
836 {
837         blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0);
838 }
839 
840 static void blk_add_trace_getrq(void *ignore,
841                                 struct request_queue *q,
842                                 struct bio *bio, int rw)
843 {
844         if (bio)
845                 blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0);
846         else {
847                 struct blk_trace *bt = q->blk_trace;
848 
849                 if (bt)
850                         __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
851         }
852 }
853 
854 
855 static void blk_add_trace_sleeprq(void *ignore,
856                                   struct request_queue *q,
857                                   struct bio *bio, int rw)
858 {
859         if (bio)
860                 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0);
861         else {
862                 struct blk_trace *bt = q->blk_trace;
863 
864                 if (bt)
865                         __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
866                                         0, 0, NULL);
867         }
868 }
869 
870 static void blk_add_trace_plug(void *ignore, struct request_queue *q)
871 {
872         struct blk_trace *bt = q->blk_trace;
873 
874         if (bt)
875                 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
876 }
877 
878 static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
879                                     unsigned int depth, bool explicit)
880 {
881         struct blk_trace *bt = q->blk_trace;
882 
883         if (bt) {
884                 __be64 rpdu = cpu_to_be64(depth);
885                 u32 what;
886 
887                 if (explicit)
888                         what = BLK_TA_UNPLUG_IO;
889                 else
890                         what = BLK_TA_UNPLUG_TIMER;
891 
892                 __blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
893         }
894 }
895 
896 static void blk_add_trace_split(void *ignore,
897                                 struct request_queue *q, struct bio *bio,
898                                 unsigned int pdu)
899 {
900         struct blk_trace *bt = q->blk_trace;
901 
902         if (bt) {
903                 __be64 rpdu = cpu_to_be64(pdu);
904 
905                 __blk_add_trace(bt, bio->bi_iter.bi_sector,
906                                 bio->bi_iter.bi_size, bio->bi_rw, BLK_TA_SPLIT,
907                                 !bio_flagged(bio, BIO_UPTODATE),
908                                 sizeof(rpdu), &rpdu);
909         }
910 }
911 
912 /**
913  * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
914  * @ignore:     trace callback data parameter (not used)
915  * @q:          queue the io is for
916  * @bio:        the source bio
917  * @dev:        target device
918  * @from:       source sector
919  *
920  * Description:
921  *     Device mapper or raid target sometimes need to split a bio because
922  *     it spans a stripe (or similar). Add a trace for that action.
923  *
924  **/
925 static void blk_add_trace_bio_remap(void *ignore,
926                                     struct request_queue *q, struct bio *bio,
927                                     dev_t dev, sector_t from)
928 {
929         struct blk_trace *bt = q->blk_trace;
930         struct blk_io_trace_remap r;
931 
932         if (likely(!bt))
933                 return;
934 
935         r.device_from = cpu_to_be32(dev);
936         r.device_to   = cpu_to_be32(bio->bi_bdev->bd_dev);
937         r.sector_from = cpu_to_be64(from);
938 
939         __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
940                         bio->bi_rw, BLK_TA_REMAP,
941                         !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
942 }
943 
944 /**
945  * blk_add_trace_rq_remap - Add a trace for a request-remap operation
946  * @ignore:     trace callback data parameter (not used)
947  * @q:          queue the io is for
948  * @rq:         the source request
949  * @dev:        target device
950  * @from:       source sector
951  *
952  * Description:
953  *     Device mapper remaps request to other devices.
954  *     Add a trace for that action.
955  *
956  **/
957 static void blk_add_trace_rq_remap(void *ignore,
958                                    struct request_queue *q,
959                                    struct request *rq, dev_t dev,
960                                    sector_t from)
961 {
962         struct blk_trace *bt = q->blk_trace;
963         struct blk_io_trace_remap r;
964 
965         if (likely(!bt))
966                 return;
967 
968         r.device_from = cpu_to_be32(dev);
969         r.device_to   = cpu_to_be32(disk_devt(rq->rq_disk));
970         r.sector_from = cpu_to_be64(from);
971 
972         __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
973                         rq_data_dir(rq), BLK_TA_REMAP, !!rq->errors,
974                         sizeof(r), &r);
975 }
976 
977 /**
978  * blk_add_driver_data - Add binary message with driver-specific data
979  * @q:          queue the io is for
980  * @rq:         io request
981  * @data:       driver-specific data
982  * @len:        length of driver-specific data
983  *
984  * Description:
985  *     Some drivers might want to write driver-specific data per request.
986  *
987  **/
988 void blk_add_driver_data(struct request_queue *q,
989                          struct request *rq,
990                          void *data, size_t len)
991 {
992         struct blk_trace *bt = q->blk_trace;
993 
994         if (likely(!bt))
995                 return;
996 
997         if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
998                 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
999                                 BLK_TA_DRV_DATA, rq->errors, len, data);
1000         else
1001                 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
1002                                 BLK_TA_DRV_DATA, rq->errors, len, data);
1003 }
1004 EXPORT_SYMBOL_GPL(blk_add_driver_data);
1005 
1006 static void blk_register_tracepoints(void)
1007 {
1008         int ret;
1009 
1010         ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1011         WARN_ON(ret);
1012         ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1013         WARN_ON(ret);
1014         ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1015         WARN_ON(ret);
1016         ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1017         WARN_ON(ret);
1018         ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1019         WARN_ON(ret);
1020         ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1021         WARN_ON(ret);
1022         ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1023         WARN_ON(ret);
1024         ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1025         WARN_ON(ret);
1026         ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1027         WARN_ON(ret);
1028         ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1029         WARN_ON(ret);
1030         ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
1031         WARN_ON(ret);
1032         ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1033         WARN_ON(ret);
1034         ret = register_trace_block_plug(blk_add_trace_plug, NULL);
1035         WARN_ON(ret);
1036         ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
1037         WARN_ON(ret);
1038         ret = register_trace_block_split(blk_add_trace_split, NULL);
1039         WARN_ON(ret);
1040         ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1041         WARN_ON(ret);
1042         ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1043         WARN_ON(ret);
1044 }
1045 
1046 static void blk_unregister_tracepoints(void)
1047 {
1048         unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1049         unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1050         unregister_trace_block_split(blk_add_trace_split, NULL);
1051         unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
1052         unregister_trace_block_plug(blk_add_trace_plug, NULL);
1053         unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1054         unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1055         unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1056         unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1057         unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1058         unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1059         unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1060         unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1061         unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1062         unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1063         unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1064         unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1065 
1066         tracepoint_synchronize_unregister();
1067 }
1068 
1069 /*
1070  * struct blk_io_tracer formatting routines
1071  */
1072 
1073 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1074 {
1075         int i = 0;
1076         int tc = t->action >> BLK_TC_SHIFT;
1077 
1078         if (t->action == BLK_TN_MESSAGE) {
1079                 rwbs[i++] = 'N';
1080                 goto out;
1081         }
1082 
1083         if (tc & BLK_TC_FLUSH)
1084                 rwbs[i++] = 'F';
1085 
1086         if (tc & BLK_TC_DISCARD)
1087                 rwbs[i++] = 'D';
1088         else if (tc & BLK_TC_WRITE)
1089                 rwbs[i++] = 'W';
1090         else if (t->bytes)
1091                 rwbs[i++] = 'R';
1092         else
1093                 rwbs[i++] = 'N';
1094 
1095         if (tc & BLK_TC_FUA)
1096                 rwbs[i++] = 'F';
1097         if (tc & BLK_TC_AHEAD)
1098                 rwbs[i++] = 'A';
1099         if (tc & BLK_TC_SYNC)
1100                 rwbs[i++] = 'S';
1101         if (tc & BLK_TC_META)
1102                 rwbs[i++] = 'M';
1103 out:
1104         rwbs[i] = '\0';
1105 }
1106 
1107 static inline
1108 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1109 {
1110         return (const struct blk_io_trace *)ent;
1111 }
1112 
1113 static inline const void *pdu_start(const struct trace_entry *ent)
1114 {
1115         return te_blk_io_trace(ent) + 1;
1116 }
1117 
1118 static inline u32 t_action(const struct trace_entry *ent)
1119 {
1120         return te_blk_io_trace(ent)->action;
1121 }
1122 
1123 static inline u32 t_bytes(const struct trace_entry *ent)
1124 {
1125         return te_blk_io_trace(ent)->bytes;
1126 }
1127 
1128 static inline u32 t_sec(const struct trace_entry *ent)
1129 {
1130         return te_blk_io_trace(ent)->bytes >> 9;
1131 }
1132 
1133 static inline unsigned long long t_sector(const struct trace_entry *ent)
1134 {
1135         return te_blk_io_trace(ent)->sector;
1136 }
1137 
1138 static inline __u16 t_error(const struct trace_entry *ent)
1139 {
1140         return te_blk_io_trace(ent)->error;
1141 }
1142 
1143 static __u64 get_pdu_int(const struct trace_entry *ent)
1144 {
1145         const __u64 *val = pdu_start(ent);
1146         return be64_to_cpu(*val);
1147 }
1148 
1149 static void get_pdu_remap(const struct trace_entry *ent,
1150                           struct blk_io_trace_remap *r)
1151 {
1152         const struct blk_io_trace_remap *__r = pdu_start(ent);
1153         __u64 sector_from = __r->sector_from;
1154 
1155         r->device_from = be32_to_cpu(__r->device_from);
1156         r->device_to   = be32_to_cpu(__r->device_to);
1157         r->sector_from = be64_to_cpu(sector_from);
1158 }
1159 
1160 typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1161 
1162 static void blk_log_action_classic(struct trace_iterator *iter, const char *act)
1163 {
1164         char rwbs[RWBS_LEN];
1165         unsigned long long ts  = iter->ts;
1166         unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1167         unsigned secs          = (unsigned long)ts;
1168         const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1169 
1170         fill_rwbs(rwbs, t);
1171 
1172         trace_seq_printf(&iter->seq,
1173                          "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1174                          MAJOR(t->device), MINOR(t->device), iter->cpu,
1175                          secs, nsec_rem, iter->ent->pid, act, rwbs);
1176 }
1177 
1178 static void blk_log_action(struct trace_iterator *iter, const char *act)
1179 {
1180         char rwbs[RWBS_LEN];
1181         const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1182 
1183         fill_rwbs(rwbs, t);
1184         trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1185                          MAJOR(t->device), MINOR(t->device), act, rwbs);
1186 }
1187 
1188 static void blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1189 {
1190         const unsigned char *pdu_buf;
1191         int pdu_len;
1192         int i, end;
1193 
1194         pdu_buf = pdu_start(ent);
1195         pdu_len = te_blk_io_trace(ent)->pdu_len;
1196 
1197         if (!pdu_len)
1198                 return;
1199 
1200         /* find the last zero that needs to be printed */
1201         for (end = pdu_len - 1; end >= 0; end--)
1202                 if (pdu_buf[end])
1203                         break;
1204         end++;
1205 
1206         trace_seq_putc(s, '(');
1207 
1208         for (i = 0; i < pdu_len; i++) {
1209 
1210                 trace_seq_printf(s, "%s%02x",
1211                                  i == 0 ? "" : " ", pdu_buf[i]);
1212 
1213                 /*
1214                  * stop when the rest is just zeroes and indicate so
1215                  * with a ".." appended
1216                  */
1217                 if (i == end && end != pdu_len - 1) {
1218                         trace_seq_puts(s, " ..) ");
1219                         return;
1220                 }
1221         }
1222 
1223         trace_seq_puts(s, ") ");
1224 }
1225 
1226 static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1227 {
1228         char cmd[TASK_COMM_LEN];
1229 
1230         trace_find_cmdline(ent->pid, cmd);
1231 
1232         if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1233                 trace_seq_printf(s, "%u ", t_bytes(ent));
1234                 blk_log_dump_pdu(s, ent);
1235                 trace_seq_printf(s, "[%s]\n", cmd);
1236         } else {
1237                 if (t_sec(ent))
1238                         trace_seq_printf(s, "%llu + %u [%s]\n",
1239                                                 t_sector(ent), t_sec(ent), cmd);
1240                 else
1241                         trace_seq_printf(s, "[%s]\n", cmd);
1242         }
1243 }
1244 
1245 static void blk_log_with_error(struct trace_seq *s,
1246                               const struct trace_entry *ent)
1247 {
1248         if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1249                 blk_log_dump_pdu(s, ent);
1250                 trace_seq_printf(s, "[%d]\n", t_error(ent));
1251         } else {
1252                 if (t_sec(ent))
1253                         trace_seq_printf(s, "%llu + %u [%d]\n",
1254                                          t_sector(ent),
1255                                          t_sec(ent), t_error(ent));
1256                 else
1257                         trace_seq_printf(s, "%llu [%d]\n",
1258                                          t_sector(ent), t_error(ent));
1259         }
1260 }
1261 
1262 static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1263 {
1264         struct blk_io_trace_remap r = { .device_from = 0, };
1265 
1266         get_pdu_remap(ent, &r);
1267         trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1268                          t_sector(ent), t_sec(ent),
1269                          MAJOR(r.device_from), MINOR(r.device_from),
1270                          (unsigned long long)r.sector_from);
1271 }
1272 
1273 static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1274 {
1275         char cmd[TASK_COMM_LEN];
1276 
1277         trace_find_cmdline(ent->pid, cmd);
1278 
1279         trace_seq_printf(s, "[%s]\n", cmd);
1280 }
1281 
1282 static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1283 {
1284         char cmd[TASK_COMM_LEN];
1285 
1286         trace_find_cmdline(ent->pid, cmd);
1287 
1288         trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1289 }
1290 
1291 static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1292 {
1293         char cmd[TASK_COMM_LEN];
1294 
1295         trace_find_cmdline(ent->pid, cmd);
1296 
1297         trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1298                          get_pdu_int(ent), cmd);
1299 }
1300 
1301 static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1302 {
1303         const struct blk_io_trace *t = te_blk_io_trace(ent);
1304 
1305         trace_seq_putmem(s, t + 1, t->pdu_len);
1306         trace_seq_putc(s, '\n');
1307 }
1308 
1309 /*
1310  * struct tracer operations
1311  */
1312 
1313 static void blk_tracer_print_header(struct seq_file *m)
1314 {
1315         if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1316                 return;
1317         seq_puts(m, "# DEV   CPU TIMESTAMP     PID ACT FLG\n"
1318                     "#  |     |     |           |   |   |\n");
1319 }
1320 
1321 static void blk_tracer_start(struct trace_array *tr)
1322 {
1323         blk_tracer_enabled = true;
1324 }
1325 
1326 static int blk_tracer_init(struct trace_array *tr)
1327 {
1328         blk_tr = tr;
1329         blk_tracer_start(tr);
1330         return 0;
1331 }
1332 
1333 static void blk_tracer_stop(struct trace_array *tr)
1334 {
1335         blk_tracer_enabled = false;
1336 }
1337 
1338 static void blk_tracer_reset(struct trace_array *tr)
1339 {
1340         blk_tracer_stop(tr);
1341 }
1342 
1343 static const struct {
1344         const char *act[2];
1345         void       (*print)(struct trace_seq *s, const struct trace_entry *ent);
1346 } what2act[] = {
1347         [__BLK_TA_QUEUE]        = {{  "Q", "queue" },      blk_log_generic },
1348         [__BLK_TA_BACKMERGE]    = {{  "M", "backmerge" },  blk_log_generic },
1349         [__BLK_TA_FRONTMERGE]   = {{  "F", "frontmerge" }, blk_log_generic },
1350         [__BLK_TA_GETRQ]        = {{  "G", "getrq" },      blk_log_generic },
1351         [__BLK_TA_SLEEPRQ]      = {{  "S", "sleeprq" },    blk_log_generic },
1352         [__BLK_TA_REQUEUE]      = {{  "R", "requeue" },    blk_log_with_error },
1353         [__BLK_TA_ISSUE]        = {{  "D", "issue" },      blk_log_generic },
1354         [__BLK_TA_COMPLETE]     = {{  "C", "complete" },   blk_log_with_error },
1355         [__BLK_TA_PLUG]         = {{  "P", "plug" },       blk_log_plug },
1356         [__BLK_TA_UNPLUG_IO]    = {{  "U", "unplug_io" },  blk_log_unplug },
1357         [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1358         [__BLK_TA_INSERT]       = {{  "I", "insert" },     blk_log_generic },
1359         [__BLK_TA_SPLIT]        = {{  "X", "split" },      blk_log_split },
1360         [__BLK_TA_BOUNCE]       = {{  "B", "bounce" },     blk_log_generic },
1361         [__BLK_TA_REMAP]        = {{  "A", "remap" },      blk_log_remap },
1362 };
1363 
1364 static enum print_line_t print_one_line(struct trace_iterator *iter,
1365                                         bool classic)
1366 {
1367         struct trace_seq *s = &iter->seq;
1368         const struct blk_io_trace *t;
1369         u16 what;
1370         bool long_act;
1371         blk_log_action_t *log_action;
1372 
1373         t          = te_blk_io_trace(iter->ent);
1374         what       = t->action & ((1 << BLK_TC_SHIFT) - 1);
1375         long_act   = !!(trace_flags & TRACE_ITER_VERBOSE);
1376         log_action = classic ? &blk_log_action_classic : &blk_log_action;
1377 
1378         if (t->action == BLK_TN_MESSAGE) {
1379                 log_action(iter, long_act ? "message" : "m");
1380                 blk_log_msg(s, iter->ent);
1381         }
1382 
1383         if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1384                 trace_seq_printf(s, "Unknown action %x\n", what);
1385         else {
1386                 log_action(iter, what2act[what].act[long_act]);
1387                 what2act[what].print(s, iter->ent);
1388         }
1389 
1390         return trace_handle_return(s);
1391 }
1392 
1393 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1394                                                int flags, struct trace_event *event)
1395 {
1396         return print_one_line(iter, false);
1397 }
1398 
1399 static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1400 {
1401         struct trace_seq *s = &iter->seq;
1402         struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1403         const int offset = offsetof(struct blk_io_trace, sector);
1404         struct blk_io_trace old = {
1405                 .magic    = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1406                 .time     = iter->ts,
1407         };
1408 
1409         trace_seq_putmem(s, &old, offset);
1410         trace_seq_putmem(s, &t->sector,
1411                          sizeof(old) - offset + t->pdu_len);
1412 }
1413 
1414 static enum print_line_t
1415 blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1416                              struct trace_event *event)
1417 {
1418         blk_trace_synthesize_old_trace(iter);
1419 
1420         return trace_handle_return(&iter->seq);
1421 }
1422 
1423 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1424 {
1425         if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1426                 return TRACE_TYPE_UNHANDLED;
1427 
1428         return print_one_line(iter, true);
1429 }
1430 
1431 static int
1432 blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1433 {
1434         /* don't output context-info for blk_classic output */
1435         if (bit == TRACE_BLK_OPT_CLASSIC) {
1436                 if (set)
1437                         trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1438                 else
1439                         trace_flags |= TRACE_ITER_CONTEXT_INFO;
1440         }
1441         return 0;
1442 }
1443 
1444 static struct tracer blk_tracer __read_mostly = {
1445         .name           = "blk",
1446         .init           = blk_tracer_init,
1447         .reset          = blk_tracer_reset,
1448         .start          = blk_tracer_start,
1449         .stop           = blk_tracer_stop,
1450         .print_header   = blk_tracer_print_header,
1451         .print_line     = blk_tracer_print_line,
1452         .flags          = &blk_tracer_flags,
1453         .set_flag       = blk_tracer_set_flag,
1454 };
1455 
1456 static struct trace_event_functions trace_blk_event_funcs = {
1457         .trace          = blk_trace_event_print,
1458         .binary         = blk_trace_event_print_binary,
1459 };
1460 
1461 static struct trace_event trace_blk_event = {
1462         .type           = TRACE_BLK,
1463         .funcs          = &trace_blk_event_funcs,
1464 };
1465 
1466 static int __init init_blk_tracer(void)
1467 {
1468         if (!register_ftrace_event(&trace_blk_event)) {
1469                 pr_warning("Warning: could not register block events\n");
1470                 return 1;
1471         }
1472 
1473         if (register_tracer(&blk_tracer) != 0) {
1474                 pr_warning("Warning: could not register the block tracer\n");
1475                 unregister_ftrace_event(&trace_blk_event);
1476                 return 1;
1477         }
1478 
1479         return 0;
1480 }
1481 
1482 device_initcall(init_blk_tracer);
1483 
1484 static int blk_trace_remove_queue(struct request_queue *q)
1485 {
1486         struct blk_trace *bt;
1487 
1488         bt = xchg(&q->blk_trace, NULL);
1489         if (bt == NULL)
1490                 return -EINVAL;
1491 
1492         put_probe_ref();
1493         blk_trace_free(bt);
1494         return 0;
1495 }
1496 
1497 /*
1498  * Setup everything required to start tracing
1499  */
1500 static int blk_trace_setup_queue(struct request_queue *q,
1501                                  struct block_device *bdev)
1502 {
1503         struct blk_trace *old_bt, *bt = NULL;
1504         int ret = -ENOMEM;
1505 
1506         bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1507         if (!bt)
1508                 return -ENOMEM;
1509 
1510         bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1511         if (!bt->msg_data)
1512                 goto free_bt;
1513 
1514         bt->dev = bdev->bd_dev;
1515         bt->act_mask = (u16)-1;
1516 
1517         blk_trace_setup_lba(bt, bdev);
1518 
1519         old_bt = xchg(&q->blk_trace, bt);
1520         if (old_bt != NULL) {
1521                 (void)xchg(&q->blk_trace, old_bt);
1522                 ret = -EBUSY;
1523                 goto free_bt;
1524         }
1525 
1526         get_probe_ref();
1527         return 0;
1528 
1529 free_bt:
1530         blk_trace_free(bt);
1531         return ret;
1532 }
1533 
1534 /*
1535  * sysfs interface to enable and configure tracing
1536  */
1537 
1538 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1539                                          struct device_attribute *attr,
1540                                          char *buf);
1541 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1542                                           struct device_attribute *attr,
1543                                           const char *buf, size_t count);
1544 #define BLK_TRACE_DEVICE_ATTR(_name) \
1545         DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1546                     sysfs_blk_trace_attr_show, \
1547                     sysfs_blk_trace_attr_store)
1548 
1549 static BLK_TRACE_DEVICE_ATTR(enable);
1550 static BLK_TRACE_DEVICE_ATTR(act_mask);
1551 static BLK_TRACE_DEVICE_ATTR(pid);
1552 static BLK_TRACE_DEVICE_ATTR(start_lba);
1553 static BLK_TRACE_DEVICE_ATTR(end_lba);
1554 
1555 static struct attribute *blk_trace_attrs[] = {
1556         &dev_attr_enable.attr,
1557         &dev_attr_act_mask.attr,
1558         &dev_attr_pid.attr,
1559         &dev_attr_start_lba.attr,
1560         &dev_attr_end_lba.attr,
1561         NULL
1562 };
1563 
1564 struct attribute_group blk_trace_attr_group = {
1565         .name  = "trace",
1566         .attrs = blk_trace_attrs,
1567 };
1568 
1569 static const struct {
1570         int mask;
1571         const char *str;
1572 } mask_maps[] = {
1573         { BLK_TC_READ,          "read"          },
1574         { BLK_TC_WRITE,         "write"         },
1575         { BLK_TC_FLUSH,         "flush"         },
1576         { BLK_TC_SYNC,          "sync"          },
1577         { BLK_TC_QUEUE,         "queue"         },
1578         { BLK_TC_REQUEUE,       "requeue"       },
1579         { BLK_TC_ISSUE,         "issue"         },
1580         { BLK_TC_COMPLETE,      "complete"      },
1581         { BLK_TC_FS,            "fs"            },
1582         { BLK_TC_PC,            "pc"            },
1583         { BLK_TC_AHEAD,         "ahead"         },
1584         { BLK_TC_META,          "meta"          },
1585         { BLK_TC_DISCARD,       "discard"       },
1586         { BLK_TC_DRV_DATA,      "drv_data"      },
1587         { BLK_TC_FUA,           "fua"           },
1588 };
1589 
1590 static int blk_trace_str2mask(const char *str)
1591 {
1592         int i;
1593         int mask = 0;
1594         char *buf, *s, *token;
1595 
1596         buf = kstrdup(str, GFP_KERNEL);
1597         if (buf == NULL)
1598                 return -ENOMEM;
1599         s = strstrip(buf);
1600 
1601         while (1) {
1602                 token = strsep(&s, ",");
1603                 if (token == NULL)
1604                         break;
1605 
1606                 if (*token == '\0')
1607                         continue;
1608 
1609                 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1610                         if (strcasecmp(token, mask_maps[i].str) == 0) {
1611                                 mask |= mask_maps[i].mask;
1612                                 break;
1613                         }
1614                 }
1615                 if (i == ARRAY_SIZE(mask_maps)) {
1616                         mask = -EINVAL;
1617                         break;
1618                 }
1619         }
1620         kfree(buf);
1621 
1622         return mask;
1623 }
1624 
1625 static ssize_t blk_trace_mask2str(char *buf, int mask)
1626 {
1627         int i;
1628         char *p = buf;
1629 
1630         for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1631                 if (mask & mask_maps[i].mask) {
1632                         p += sprintf(p, "%s%s",
1633                                     (p == buf) ? "" : ",", mask_maps[i].str);
1634                 }
1635         }
1636         *p++ = '\n';
1637 
1638         return p - buf;
1639 }
1640 
1641 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1642 {
1643         if (bdev->bd_disk == NULL)
1644                 return NULL;
1645 
1646         return bdev_get_queue(bdev);
1647 }
1648 
1649 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1650                                          struct device_attribute *attr,
1651                                          char *buf)
1652 {
1653         struct hd_struct *p = dev_to_part(dev);
1654         struct request_queue *q;
1655         struct block_device *bdev;
1656         ssize_t ret = -ENXIO;
1657 
1658         bdev = bdget(part_devt(p));
1659         if (bdev == NULL)
1660                 goto out;
1661 
1662         q = blk_trace_get_queue(bdev);
1663         if (q == NULL)
1664                 goto out_bdput;
1665 
1666         mutex_lock(&bdev->bd_mutex);
1667 
1668         if (attr == &dev_attr_enable) {
1669                 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1670                 goto out_unlock_bdev;
1671         }
1672 
1673         if (q->blk_trace == NULL)
1674                 ret = sprintf(buf, "disabled\n");
1675         else if (attr == &dev_attr_act_mask)
1676                 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
1677         else if (attr == &dev_attr_pid)
1678                 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1679         else if (attr == &dev_attr_start_lba)
1680                 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1681         else if (attr == &dev_attr_end_lba)
1682                 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1683 
1684 out_unlock_bdev:
1685         mutex_unlock(&bdev->bd_mutex);
1686 out_bdput:
1687         bdput(bdev);
1688 out:
1689         return ret;
1690 }
1691 
1692 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1693                                           struct device_attribute *attr,
1694                                           const char *buf, size_t count)
1695 {
1696         struct block_device *bdev;
1697         struct request_queue *q;
1698         struct hd_struct *p;
1699         u64 value;
1700         ssize_t ret = -EINVAL;
1701 
1702         if (count == 0)
1703                 goto out;
1704 
1705         if (attr == &dev_attr_act_mask) {
1706                 if (sscanf(buf, "%llx", &value) != 1) {
1707                         /* Assume it is a list of trace category names */
1708                         ret = blk_trace_str2mask(buf);
1709                         if (ret < 0)
1710                                 goto out;
1711                         value = ret;
1712                 }
1713         } else if (sscanf(buf, "%llu", &value) != 1)
1714                 goto out;
1715 
1716         ret = -ENXIO;
1717 
1718         p = dev_to_part(dev);
1719         bdev = bdget(part_devt(p));
1720         if (bdev == NULL)
1721                 goto out;
1722 
1723         q = blk_trace_get_queue(bdev);
1724         if (q == NULL)
1725                 goto out_bdput;
1726 
1727         mutex_lock(&bdev->bd_mutex);
1728 
1729         if (attr == &dev_attr_enable) {
1730                 if (value)
1731                         ret = blk_trace_setup_queue(q, bdev);
1732                 else
1733                         ret = blk_trace_remove_queue(q);
1734                 goto out_unlock_bdev;
1735         }
1736 
1737         ret = 0;
1738         if (q->blk_trace == NULL)
1739                 ret = blk_trace_setup_queue(q, bdev);
1740 
1741         if (ret == 0) {
1742                 if (attr == &dev_attr_act_mask)
1743                         q->blk_trace->act_mask = value;
1744                 else if (attr == &dev_attr_pid)
1745                         q->blk_trace->pid = value;
1746                 else if (attr == &dev_attr_start_lba)
1747                         q->blk_trace->start_lba = value;
1748                 else if (attr == &dev_attr_end_lba)
1749                         q->blk_trace->end_lba = value;
1750         }
1751 
1752 out_unlock_bdev:
1753         mutex_unlock(&bdev->bd_mutex);
1754 out_bdput:
1755         bdput(bdev);
1756 out:
1757         return ret ? ret : count;
1758 }
1759 
1760 int blk_trace_init_sysfs(struct device *dev)
1761 {
1762         return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1763 }
1764 
1765 void blk_trace_remove_sysfs(struct device *dev)
1766 {
1767         sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1768 }
1769 
1770 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1771 
1772 #ifdef CONFIG_EVENT_TRACING
1773 
1774 void blk_dump_cmd(char *buf, struct request *rq)
1775 {
1776         int i, end;
1777         int len = rq->cmd_len;
1778         unsigned char *cmd = rq->cmd;
1779 
1780         if (rq->cmd_type != REQ_TYPE_BLOCK_PC) {
1781                 buf[0] = '\0';
1782                 return;
1783         }
1784 
1785         for (end = len - 1; end >= 0; end--)
1786                 if (cmd[end])
1787                         break;
1788         end++;
1789 
1790         for (i = 0; i < len; i++) {
1791                 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1792                 if (i == end && end != len - 1) {
1793                         sprintf(buf, " ..");
1794                         break;
1795                 }
1796         }
1797 }
1798 
1799 void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1800 {
1801         int i = 0;
1802 
1803         if (rw & REQ_FLUSH)
1804                 rwbs[i++] = 'F';
1805 
1806         if (rw & WRITE)
1807                 rwbs[i++] = 'W';
1808         else if (rw & REQ_DISCARD)
1809                 rwbs[i++] = 'D';
1810         else if (bytes)
1811                 rwbs[i++] = 'R';
1812         else
1813                 rwbs[i++] = 'N';
1814 
1815         if (rw & REQ_FUA)
1816                 rwbs[i++] = 'F';
1817         if (rw & REQ_RAHEAD)
1818                 rwbs[i++] = 'A';
1819         if (rw & REQ_SYNC)
1820                 rwbs[i++] = 'S';
1821         if (rw & REQ_META)
1822                 rwbs[i++] = 'M';
1823         if (rw & REQ_SECURE)
1824                 rwbs[i++] = 'E';
1825 
1826         rwbs[i] = '\0';
1827 }
1828 EXPORT_SYMBOL_GPL(blk_fill_rwbs);
1829 
1830 #endif /* CONFIG_EVENT_TRACING */
1831 
1832 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp