blktrace: fix wrong calculation of RWBS
[linux-2.6.git] / kernel / trace / blktrace.c
index e82cb9e..9af4143 100644 (file)
 static unsigned int blktrace_seq __read_mostly = 1;
 
 static struct trace_array *blk_tr;
-static int __read_mostly  blk_tracer_enabled;
+static bool blk_tracer_enabled __read_mostly;
 
 /* Select an alternative, minimalistic output than the original one */
-#define TRACE_BLK_OPT_CLASSIC  0x1
+#define TRACE_BLK_OPT_CLASSIC  0x1
 
 static struct tracer_opt blk_tracer_opts[] = {
        /* Default disable the minimalistic output */
@@ -47,10 +47,9 @@ static struct tracer_flags blk_tracer_flags = {
 };
 
 /* Global reference count of probes */
-static DEFINE_MUTEX(blk_probe_mutex);
 static atomic_t blk_probes_ref = ATOMIC_INIT(0);
 
-static int blk_register_tracepoints(void);
+static void blk_register_tracepoints(void);
 static void blk_unregister_tracepoints(void);
 
 /*
@@ -148,8 +147,8 @@ static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
 /*
  * Data direction bit lookup
  */
-static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ),
-                                        BLK_TC_ACT(BLK_TC_WRITE) };
+static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
+                                BLK_TC_ACT(BLK_TC_WRITE) };
 
 /* The ilog2() calls fall out because they're constant */
 #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
@@ -256,10 +255,8 @@ static void blk_trace_cleanup(struct blk_trace *bt)
        free_percpu(bt->sequence);
        free_percpu(bt->msg_data);
        kfree(bt);
-       mutex_lock(&blk_probe_mutex);
        if (atomic_dec_and_test(&blk_probes_ref))
                blk_unregister_tracepoints();
-       mutex_unlock(&blk_probe_mutex);
 }
 
 int blk_trace_remove(struct request_queue *q)
@@ -423,7 +420,7 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
        if (!bt->sequence)
                goto err;
 
-       bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG);
+       bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
        if (!bt->msg_data)
                goto err;
 
@@ -432,7 +429,7 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
        if (!blk_tree_root) {
                blk_tree_root = debugfs_create_dir("block", NULL);
                if (!blk_tree_root)
-                       return -ENOMEM;
+                       goto err;
        }
 
        dir = debugfs_create_dir(buts->name, blk_tree_root);
@@ -471,14 +468,6 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
        bt->pid = buts->pid;
        bt->trace_state = Blktrace_setup;
 
-       mutex_lock(&blk_probe_mutex);
-       if (atomic_add_return(1, &blk_probes_ref) == 1) {
-               ret = blk_register_tracepoints();
-               if (ret)
-                       goto probe_err;
-       }
-       mutex_unlock(&blk_probe_mutex);
-
        ret = -EBUSY;
        old_bt = xchg(&q->blk_trace, bt);
        if (old_bt) {
@@ -486,10 +475,10 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
                goto err;
        }
 
+       if (atomic_add_return(1, &blk_probes_ref) == 1)
+               blk_register_tracepoints();
+
        return 0;
-probe_err:
-       atomic_dec(&blk_probes_ref);
-       mutex_unlock(&blk_probe_mutex);
 err:
        if (bt) {
                if (bt->msg_file)
@@ -564,7 +553,7 @@ EXPORT_SYMBOL_GPL(blk_trace_startstop);
 /**
  * blk_trace_ioctl: - handle the ioctls associated with tracing
  * @bdev:      the block device
- * @cmd:       the ioctl cmd
+ * @cmd:       the ioctl cmd
  * @arg:       the argument data, if any
  *
  **/
@@ -863,7 +852,7 @@ void blk_add_driver_data(struct request_queue *q,
 }
 EXPORT_SYMBOL_GPL(blk_add_driver_data);
 
-static int blk_register_tracepoints(void)
+static void blk_register_tracepoints(void)
 {
        int ret;
 
@@ -901,7 +890,6 @@ static int blk_register_tracepoints(void)
        WARN_ON(ret);
        ret = register_trace_block_remap(blk_add_trace_remap);
        WARN_ON(ret);
-       return 0;
 }
 
 static void blk_unregister_tracepoints(void)
@@ -934,23 +922,24 @@ static void blk_unregister_tracepoints(void)
 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
 {
        int i = 0;
+       int tc = t->action >> BLK_TC_SHIFT;
 
-       if (t->action & BLK_TC_DISCARD)
+       if (tc & BLK_TC_DISCARD)
                rwbs[i++] = 'D';
-       else if (t->action & BLK_TC_WRITE)
+       else if (tc & BLK_TC_WRITE)
                rwbs[i++] = 'W';
        else if (t->bytes)
                rwbs[i++] = 'R';
        else
                rwbs[i++] = 'N';
 
-       if (t->action & BLK_TC_AHEAD)
+       if (tc & BLK_TC_AHEAD)
                rwbs[i++] = 'A';
-       if (t->action & BLK_TC_BARRIER)
+       if (tc & BLK_TC_BARRIER)
                rwbs[i++] = 'B';
-       if (t->action & BLK_TC_SYNC)
+       if (tc & BLK_TC_SYNC)
                rwbs[i++] = 'S';
-       if (t->action & BLK_TC_META)
+       if (tc & BLK_TC_META)
                rwbs[i++] = 'M';
 
        rwbs[i] = '\0';
@@ -1027,7 +1016,9 @@ static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
 
 static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
 {
-       const char *cmd = trace_find_cmdline(ent->pid);
+       char cmd[TASK_COMM_LEN];
+
+       trace_find_cmdline(ent->pid, cmd);
 
        if (t_sec(ent))
                return trace_seq_printf(s, "%llu + %u [%s]\n",
@@ -1057,19 +1048,30 @@ static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
 
 static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
 {
-       return trace_seq_printf(s, "[%s]\n", trace_find_cmdline(ent->pid));
+       char cmd[TASK_COMM_LEN];
+
+       trace_find_cmdline(ent->pid, cmd);
+
+       return trace_seq_printf(s, "[%s]\n", cmd);
 }
 
 static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
 {
-       return trace_seq_printf(s, "[%s] %llu\n", trace_find_cmdline(ent->pid),
-                               get_pdu_int(ent));
+       char cmd[TASK_COMM_LEN];
+
+       trace_find_cmdline(ent->pid, cmd);
+
+       return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
 }
 
 static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
 {
+       char cmd[TASK_COMM_LEN];
+
+       trace_find_cmdline(ent->pid, cmd);
+
        return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
-                               get_pdu_int(ent), trace_find_cmdline(ent->pid));
+                               get_pdu_int(ent), cmd);
 }
 
 /*
@@ -1086,11 +1088,8 @@ static void blk_tracer_print_header(struct seq_file *m)
 
 static void blk_tracer_start(struct trace_array *tr)
 {
-       mutex_lock(&blk_probe_mutex);
        if (atomic_add_return(1, &blk_probes_ref) == 1)
-               if (blk_register_tracepoints())
-                       atomic_dec(&blk_probes_ref);
-       mutex_unlock(&blk_probe_mutex);
+               blk_register_tracepoints();
        trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
 }
 
@@ -1098,19 +1097,15 @@ static int blk_tracer_init(struct trace_array *tr)
 {
        blk_tr = tr;
        blk_tracer_start(tr);
-       mutex_lock(&blk_probe_mutex);
-       blk_tracer_enabled++;
-       mutex_unlock(&blk_probe_mutex);
+       blk_tracer_enabled = true;
        return 0;
 }
 
 static void blk_tracer_stop(struct trace_array *tr)
 {
        trace_flags |= TRACE_ITER_CONTEXT_INFO;
-       mutex_lock(&blk_probe_mutex);
        if (atomic_dec_and_test(&blk_probes_ref))
                blk_unregister_tracepoints();
-       mutex_unlock(&blk_probe_mutex);
 }
 
 static void blk_tracer_reset(struct trace_array *tr)
@@ -1118,19 +1113,15 @@ static void blk_tracer_reset(struct trace_array *tr)
        if (!atomic_read(&blk_probes_ref))
                return;
 
-       mutex_lock(&blk_probe_mutex);
-       blk_tracer_enabled--;
-       WARN_ON(blk_tracer_enabled < 0);
-       mutex_unlock(&blk_probe_mutex);
-
+       blk_tracer_enabled = false;
        blk_tracer_stop(tr);
 }
 
-static struct {
+static const struct {
        const char *act[2];
-       int        (*print)(struct trace_seq *s, const struct trace_entry *ent);
-} what2act[] __read_mostly = {
-       [__BLK_TA_QUEUE]        = {{  "Q", "queue" },      blk_log_generic },
+       int        (*print)(struct trace_seq *s, const struct trace_entry *ent);
+} what2act[] = {
+       [__BLK_TA_QUEUE]        = {{  "Q", "queue" },      blk_log_generic },
        [__BLK_TA_BACKMERGE]    = {{  "M", "backmerge" },  blk_log_generic },
        [__BLK_TA_FRONTMERGE]   = {{  "F", "frontmerge" }, blk_log_generic },
        [__BLK_TA_GETRQ]        = {{  "G", "getrq" },      blk_log_generic },
@@ -1229,9 +1220,8 @@ static struct tracer blk_tracer __read_mostly = {
 };
 
 static struct trace_event trace_blk_event = {
-       .type           = TRACE_BLK,
+       .type           = TRACE_BLK,
        .trace          = blk_trace_event_print,
-       .latency_trace  = blk_trace_event_print,
        .binary         = blk_trace_event_print_binary,
 };
 
@@ -1271,12 +1261,10 @@ static int blk_trace_remove_queue(struct request_queue *q)
 static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
 {
        struct blk_trace *old_bt, *bt = NULL;
-       int ret;
 
-       ret = -ENOMEM;
        bt = kzalloc(sizeof(*bt), GFP_KERNEL);
        if (!bt)
-               goto err;
+               return -ENOMEM;
 
        bt->dev = dev;
        bt->act_mask = (u16)-1;
@@ -1287,83 +1275,16 @@ static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
        if (old_bt != NULL) {
                (void)xchg(&q->blk_trace, old_bt);
                kfree(bt);
-               ret = -EBUSY;
+               return -EBUSY;
        }
+
        return 0;
-err:
-       return ret;
 }
 
 /*
  * sysfs interface to enable and configure tracing
  */
 
-static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
-                                          struct device_attribute *attr,
-                                          char *buf)
-{
-       struct hd_struct *p = dev_to_part(dev);
-       struct block_device *bdev;
-       ssize_t ret = -ENXIO;
-
-       lock_kernel();
-       bdev = bdget(part_devt(p));
-       if (bdev != NULL) {
-               struct request_queue *q = bdev_get_queue(bdev);
-
-               if (q != NULL) {
-                       mutex_lock(&bdev->bd_mutex);
-                       ret = sprintf(buf, "%u\n", !!q->blk_trace);
-                       mutex_unlock(&bdev->bd_mutex);
-               }
-
-               bdput(bdev);
-       }
-
-       unlock_kernel();
-       return ret;
-}
-
-static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
-                                           struct device_attribute *attr,
-                                           const char *buf, size_t count)
-{
-       struct block_device *bdev;
-       struct request_queue *q;
-       struct hd_struct *p;
-       int value;
-       ssize_t ret = -ENXIO;
-
-       if (count == 0 || sscanf(buf, "%d", &value) != 1)
-               goto out;
-
-       lock_kernel();
-       p = dev_to_part(dev);
-       bdev = bdget(part_devt(p));
-       if (bdev == NULL)
-               goto out_unlock_kernel;
-
-       q = bdev_get_queue(bdev);
-       if (q == NULL)
-               goto out_bdput;
-
-       mutex_lock(&bdev->bd_mutex);
-       if (value)
-               ret = blk_trace_setup_queue(q, bdev->bd_dev);
-       else
-               ret = blk_trace_remove_queue(q);
-       mutex_unlock(&bdev->bd_mutex);
-
-       if (ret == 0)
-               ret = count;
-out_bdput:
-       bdput(bdev);
-out_unlock_kernel:
-       unlock_kernel();
-out:
-       return ret;
-}
-
 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
                                         struct device_attribute *attr,
                                         char *buf);
@@ -1375,8 +1296,7 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
                    sysfs_blk_trace_attr_show, \
                    sysfs_blk_trace_attr_store)
 
-static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
-                  sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
+static BLK_TRACE_DEVICE_ATTR(enable);
 static BLK_TRACE_DEVICE_ATTR(act_mask);
 static BLK_TRACE_DEVICE_ATTR(pid);
 static BLK_TRACE_DEVICE_ATTR(start_lba);
@@ -1443,6 +1363,14 @@ static int blk_str2act_mask(const char *str)
        return mask;
 }
 
+static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
+{
+       if (bdev->bd_disk == NULL)
+               return NULL;
+
+       return bdev_get_queue(bdev);
+}
+
 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
                                         struct device_attribute *attr,
                                         char *buf)
@@ -1457,10 +1385,17 @@ static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
        if (bdev == NULL)
                goto out_unlock_kernel;
 
-       q = bdev_get_queue(bdev);
+       q = blk_trace_get_queue(bdev);
        if (q == NULL)
                goto out_bdput;
+
        mutex_lock(&bdev->bd_mutex);
+
+       if (attr == &dev_attr_enable) {
+               ret = sprintf(buf, "%u\n", !!q->blk_trace);
+               goto out_unlock_bdev;
+       }
+
        if (q->blk_trace == NULL)
                ret = sprintf(buf, "disabled\n");
        else if (attr == &dev_attr_act_mask)
@@ -1471,6 +1406,8 @@ static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
                ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
        else if (attr == &dev_attr_end_lba)
                ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
+
+out_unlock_bdev:
        mutex_unlock(&bdev->bd_mutex);
 out_bdput:
        bdput(bdev);
@@ -1508,11 +1445,20 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
        if (bdev == NULL)
                goto out_unlock_kernel;
 
-       q = bdev_get_queue(bdev);
+       q = blk_trace_get_queue(bdev);
        if (q == NULL)
                goto out_bdput;
 
        mutex_lock(&bdev->bd_mutex);
+
+       if (attr == &dev_attr_enable) {
+               if (value)
+                       ret = blk_trace_setup_queue(q, bdev->bd_dev);
+               else
+                       ret = blk_trace_remove_queue(q);
+               goto out_unlock_bdev;
+       }
+
        ret = 0;
        if (q->blk_trace == NULL)
                ret = blk_trace_setup_queue(q, bdev->bd_dev);
@@ -1526,13 +1472,15 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
                        q->blk_trace->start_lba = value;
                else if (attr == &dev_attr_end_lba)
                        q->blk_trace->end_lba = value;
-               ret = count;
        }
+
+out_unlock_bdev:
        mutex_unlock(&bdev->bd_mutex);
 out_bdput:
        bdput(bdev);
 out_unlock_kernel:
        unlock_kernel();
 out:
-       return ret;
+       return ret ? ret : count;
 }
+