[PATCH] use cycle_t instead of u64 in struct time_interpolator
[linux-drm-fsl-dcu.git] / kernel / relay.c
index 1d63ecddfa7018cd4a14f8b6dc24c352a9ddc447..ef923f6de2e7c17660ce569760cc70c28ef9ac18 100644 (file)
@@ -7,6 +7,8 @@
  * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
  *
  * Moved to kernel/relay.c by Paul Mundt, 2006.
+ * November 2006 - CPU hotplug support by Mathieu Desnoyers
+ *     (mathieu.desnoyers@polymtl.ca)
  *
  * This file is released under the GPL.
  */
 #include <linux/relay.h>
 #include <linux/vmalloc.h>
 #include <linux/mm.h>
+#include <linux/cpu.h>
+
+/* list of open channels, for cpu hotplug */
+static DEFINE_MUTEX(relay_channels_mutex);
+static LIST_HEAD(relay_channels);
 
 /*
  * close() vm_op implementation for relay file mapping.
@@ -138,7 +145,7 @@ depopulate:
  */
 struct rchan_buf *relay_create_buf(struct rchan *chan)
 {
-       struct rchan_buf *buf = kcalloc(1, sizeof(struct rchan_buf), GFP_KERNEL);
+       struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
        if (!buf)
                return NULL;
 
@@ -187,6 +194,7 @@ void relay_destroy_buf(struct rchan_buf *buf)
                        __free_page(buf->page_array[i]);
                kfree(buf->page_array);
        }
+       chan->buf[buf->cpu] = NULL;
        kfree(buf->padding);
        kfree(buf);
        kref_put(&chan->kref, relay_destroy_channel);
@@ -302,15 +310,16 @@ static struct rchan_callbacks default_channel_callbacks = {
 
 /**
  *     wakeup_readers - wake up readers waiting on a channel
- *     @private: the channel buffer
+ *     @work: work struct that contains the the channel buffer
  *
  *     This is the work function used to defer reader waking.  The
  *     reason waking is deferred is that calling directly from write
  *     causes problems if you're writing from say the scheduler.
  */
-static void wakeup_readers(void *private)
+static void wakeup_readers(struct work_struct *work)
 {
-       struct rchan_buf *buf = private;
+       struct rchan_buf *buf =
+               container_of(work, struct rchan_buf, wake_readers.work);
        wake_up_interruptible(&buf->read_wait);
 }
 
@@ -321,14 +330,14 @@ static void wakeup_readers(void *private)
  *
  *     See relay_reset for description of effect.
  */
-static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
+static void __relay_reset(struct rchan_buf *buf, unsigned int init)
 {
        size_t i;
 
        if (init) {
                init_waitqueue_head(&buf->read_wait);
                kref_init(&buf->kref);
-               INIT_WORK(&buf->wake_readers, NULL, NULL);
+               INIT_DELAYED_WORK(&buf->wake_readers, NULL);
        } else {
                cancel_delayed_work(&buf->wake_readers);
                flush_scheduled_work();
@@ -361,51 +370,69 @@ static inline void __relay_reset(struct rchan_buf *buf, unsigned int init)
 void relay_reset(struct rchan *chan)
 {
        unsigned int i;
-       struct rchan_buf *prev = NULL;
 
        if (!chan)
                return;
 
-       for (i = 0; i < NR_CPUS; i++) {
-               if (!chan->buf[i] || chan->buf[i] == prev)
-                       break;
-               __relay_reset(chan->buf[i], 0);
-               prev = chan->buf[i];
+       if (chan->is_global && chan->buf[0]) {
+               __relay_reset(chan->buf[0], 0);
+               return;
        }
+
+       mutex_lock(&relay_channels_mutex);
+       for_each_online_cpu(i)
+               if (chan->buf[i])
+                       __relay_reset(chan->buf[i], 0);
+       mutex_unlock(&relay_channels_mutex);
 }
 EXPORT_SYMBOL_GPL(relay_reset);
 
 /*
  *     relay_open_buf - create a new relay channel buffer
  *
- *     Internal - used by relay_open().
+ *     used by relay_open() and CPU hotplug.
  */
-static struct rchan_buf *relay_open_buf(struct rchan *chan,
-                                       const char *filename,
-                                       struct dentry *parent,
-                                       int *is_global)
+static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
 {
-       struct rchan_buf *buf;
+       struct rchan_buf *buf = NULL;
        struct dentry *dentry;
+       char *tmpname;
 
-       if (*is_global)
+       if (chan->is_global)
                return chan->buf[0];
 
+       tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
+       if (!tmpname)
+               goto end;
+       snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
+
        buf = relay_create_buf(chan);
        if (!buf)
-               return NULL;
+               goto free_name;
+
+       buf->cpu = cpu;
+       __relay_reset(buf, 1);
 
        /* Create file in fs */
-       dentry = chan->cb->create_buf_file(filename, parent, S_IRUSR,
-                                          buf, is_global);
-       if (!dentry) {
-               relay_destroy_buf(buf);
-               return NULL;
-       }
+       dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
+                                          buf, &chan->is_global);
+       if (!dentry)
+               goto free_buf;
 
        buf->dentry = dentry;
-       __relay_reset(buf, 1);
 
+       if(chan->is_global) {
+               chan->buf[0] = buf;
+               buf->cpu = 0;
+       }
+
+       goto free_name;
+
+free_buf:
+       relay_destroy_buf(buf);
+free_name:
+       kfree(tmpname);
+end:
        return buf;
 }
 
@@ -417,7 +444,7 @@ static struct rchan_buf *relay_open_buf(struct rchan *chan,
  *     The channel buffer and channel buffer data structure are then freed
  *     automatically when the last reference is given up.
  */
-static inline void relay_close_buf(struct rchan_buf *buf)
+static void relay_close_buf(struct rchan_buf *buf)
 {
        buf->finalized = 1;
        cancel_delayed_work(&buf->wake_readers);
@@ -425,7 +452,7 @@ static inline void relay_close_buf(struct rchan_buf *buf)
        kref_put(&buf->kref, relay_remove_buf);
 }
 
-static inline void setup_callbacks(struct rchan *chan,
+static void setup_callbacks(struct rchan *chan,
                                   struct rchan_callbacks *cb)
 {
        if (!cb) {
@@ -446,6 +473,47 @@ static inline void setup_callbacks(struct rchan *chan,
        chan->cb = cb;
 }
 
+/**
+ *
+ *     relay_hotcpu_callback - CPU hotplug callback
+ *     @nb: notifier block
+ *     @action: hotplug action to take
+ *     @hcpu: CPU number
+ *
+ *     Returns the success/failure of the operation. (NOTIFY_OK, NOTIFY_BAD)
+ */
+static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
+                               unsigned long action,
+                               void *hcpu)
+{
+       unsigned int hotcpu = (unsigned long)hcpu;
+       struct rchan *chan;
+
+       switch(action) {
+       case CPU_UP_PREPARE:
+               mutex_lock(&relay_channels_mutex);
+               list_for_each_entry(chan, &relay_channels, list) {
+                       if (chan->buf[hotcpu])
+                               continue;
+                       chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
+                       if(!chan->buf[hotcpu]) {
+                               printk(KERN_ERR
+                                       "relay_hotcpu_callback: cpu %d buffer "
+                                       "creation failed\n", hotcpu);
+                               mutex_unlock(&relay_channels_mutex);
+                               return NOTIFY_BAD;
+                       }
+               }
+               mutex_unlock(&relay_channels_mutex);
+               break;
+       case CPU_DEAD:
+               /* No need to flush the cpu : will be flushed upon
+                * final relay_flush() call. */
+               break;
+       }
+       return NOTIFY_OK;
+}
+
 /**
  *     relay_open - create a new relay channel
  *     @base_filename: base name of files to create
@@ -453,6 +521,7 @@ static inline void setup_callbacks(struct rchan *chan,
  *     @subbuf_size: size of sub-buffers
  *     @n_subbufs: number of sub-buffers
  *     @cb: client callback functions
+ *     @private_data: user-defined data
  *
  *     Returns channel pointer if successful, %NULL otherwise.
  *
@@ -465,20 +534,18 @@ struct rchan *relay_open(const char *base_filename,
                         struct dentry *parent,
                         size_t subbuf_size,
                         size_t n_subbufs,
-                        struct rchan_callbacks *cb)
+                        struct rchan_callbacks *cb,
+                        void *private_data)
 {
        unsigned int i;
        struct rchan *chan;
-       char *tmpname;
-       int is_global = 0;
-
        if (!base_filename)
                return NULL;
 
        if (!(subbuf_size && n_subbufs))
                return NULL;
 
-       chan = kcalloc(1, sizeof(struct rchan), GFP_KERNEL);
+       chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
        if (!chan)
                return NULL;
 
@@ -486,38 +553,32 @@ struct rchan *relay_open(const char *base_filename,
        chan->n_subbufs = n_subbufs;
        chan->subbuf_size = subbuf_size;
        chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
+       chan->parent = parent;
+       chan->private_data = private_data;
+       strlcpy(chan->base_filename, base_filename, NAME_MAX);
        setup_callbacks(chan, cb);
        kref_init(&chan->kref);
 
-       tmpname = kmalloc(NAME_MAX + 1, GFP_KERNEL);
-       if (!tmpname)
-               goto free_chan;
-
+       mutex_lock(&relay_channels_mutex);
        for_each_online_cpu(i) {
-               sprintf(tmpname, "%s%d", base_filename, i);
-               chan->buf[i] = relay_open_buf(chan, tmpname, parent,
-                                             &is_global);
+               chan->buf[i] = relay_open_buf(chan, i);
                if (!chan->buf[i])
                        goto free_bufs;
-
-               chan->buf[i]->cpu = i;
        }
+       list_add(&chan->list, &relay_channels);
+       mutex_unlock(&relay_channels_mutex);
 
-       kfree(tmpname);
        return chan;
 
 free_bufs:
-       for (i = 0; i < NR_CPUS; i++) {
+       for_each_online_cpu(i) {
                if (!chan->buf[i])
                        break;
                relay_close_buf(chan->buf[i]);
-               if (is_global)
-                       break;
        }
-       kfree(tmpname);
 
-free_chan:
        kref_put(&chan->kref, relay_destroy_channel);
+       mutex_unlock(&relay_channels_mutex);
        return NULL;
 }
 EXPORT_SYMBOL_GPL(relay_open);
@@ -549,7 +610,8 @@ size_t relay_switch_subbuf(struct rchan_buf *buf, size_t length)
                        buf->padding[old_subbuf];
                smp_mb();
                if (waitqueue_active(&buf->read_wait)) {
-                       PREPARE_WORK(&buf->wake_readers, wakeup_readers, buf);
+                       PREPARE_DELAYED_WORK(&buf->wake_readers,
+                                            wakeup_readers);
                        schedule_delayed_work(&buf->wake_readers, 1);
                }
        }
@@ -617,24 +679,26 @@ EXPORT_SYMBOL_GPL(relay_subbufs_consumed);
 void relay_close(struct rchan *chan)
 {
        unsigned int i;
-       struct rchan_buf *prev = NULL;
 
        if (!chan)
                return;
 
-       for (i = 0; i < NR_CPUS; i++) {
-               if (!chan->buf[i] || chan->buf[i] == prev)
-                       break;
-               relay_close_buf(chan->buf[i]);
-               prev = chan->buf[i];
-       }
+       mutex_lock(&relay_channels_mutex);
+       if (chan->is_global && chan->buf[0])
+               relay_close_buf(chan->buf[0]);
+       else
+               for_each_possible_cpu(i)
+                       if (chan->buf[i])
+                               relay_close_buf(chan->buf[i]);
 
        if (chan->last_toobig)
                printk(KERN_WARNING "relay: one or more items not logged "
                       "[item size (%Zd) > sub-buffer size (%Zd)]\n",
                       chan->last_toobig, chan->subbuf_size);
 
+       list_del(&chan->list);
        kref_put(&chan->kref, relay_destroy_channel);
+       mutex_unlock(&relay_channels_mutex);
 }
 EXPORT_SYMBOL_GPL(relay_close);
 
@@ -647,17 +711,20 @@ EXPORT_SYMBOL_GPL(relay_close);
 void relay_flush(struct rchan *chan)
 {
        unsigned int i;
-       struct rchan_buf *prev = NULL;
 
        if (!chan)
                return;
 
-       for (i = 0; i < NR_CPUS; i++) {
-               if (!chan->buf[i] || chan->buf[i] == prev)
-                       break;
-               relay_switch_subbuf(chan->buf[i], 0);
-               prev = chan->buf[i];
+       if (chan->is_global && chan->buf[0]) {
+               relay_switch_subbuf(chan->buf[0], 0);
+               return;
        }
+
+       mutex_lock(&relay_channels_mutex);
+       for_each_possible_cpu(i)
+               if (chan->buf[i])
+                       relay_switch_subbuf(chan->buf[i], 0);
+       mutex_unlock(&relay_channels_mutex);
 }
 EXPORT_SYMBOL_GPL(relay_flush);
 
@@ -887,7 +954,7 @@ static int subbuf_read_actor(size_t read_start,
 
        from = buf->start + read_start;
        ret = avail;
-       if (copy_to_user(desc->arg.data, from, avail)) {
+       if (copy_to_user(desc->arg.buf, from, avail)) {
                desc->error = -EFAULT;
                ret = 0;
        }
@@ -944,27 +1011,19 @@ typedef int (*subbuf_actor_t) (size_t read_start,
 /*
  *     relay_file_read_subbufs - read count bytes, bridging subbuf boundaries
  */
-static inline ssize_t relay_file_read_subbufs(struct file *filp,
-                                             loff_t *ppos,
-                                             size_t count,
-                                             subbuf_actor_t subbuf_actor,
-                                             read_actor_t actor,
-                                             void *target)
+static ssize_t relay_file_read_subbufs(struct file *filp, loff_t *ppos,
+                                       subbuf_actor_t subbuf_actor,
+                                       read_actor_t actor,
+                                       read_descriptor_t *desc)
 {
        struct rchan_buf *buf = filp->private_data;
        size_t read_start, avail;
-       read_descriptor_t desc;
        int ret;
 
-       if (!count)
+       if (!desc->count)
                return 0;
 
-       desc.written = 0;
-       desc.count = count;
-       desc.arg.data = target;
-       desc.error = 0;
-
-       mutex_lock(&filp->f_dentry->d_inode->i_mutex);
+       mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
        do {
                if (!relay_file_read_avail(buf, *ppos))
                        break;
@@ -974,19 +1033,19 @@ static inline ssize_t relay_file_read_subbufs(struct file *filp,
                if (!avail)
                        break;
 
-               avail = min(desc.count, avail);
-               ret = subbuf_actor(read_start, buf, avail, &desc, actor);
-               if (desc.error < 0)
+               avail = min(desc->count, avail);
+               ret = subbuf_actor(read_start, buf, avail, desc, actor);
+               if (desc->error < 0)
                        break;
 
                if (ret) {
                        relay_file_read_consume(buf, read_start, ret);
                        *ppos = relay_file_read_end_pos(buf, read_start, ret);
                }
-       } while (desc.count && ret);
-       mutex_unlock(&filp->f_dentry->d_inode->i_mutex);
+       } while (desc->count && ret);
+       mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
 
-       return desc.written;
+       return desc->written;
 }
 
 static ssize_t relay_file_read(struct file *filp,
@@ -994,8 +1053,13 @@ static ssize_t relay_file_read(struct file *filp,
                               size_t count,
                               loff_t *ppos)
 {
-       return relay_file_read_subbufs(filp, ppos, count, subbuf_read_actor,
-                                      NULL, buffer);
+       read_descriptor_t desc;
+       desc.written = 0;
+       desc.count = count;
+       desc.arg.buf = buffer;
+       desc.error = 0;
+       return relay_file_read_subbufs(filp, ppos, subbuf_read_actor,
+                                      NULL, &desc);
 }
 
 static ssize_t relay_file_sendfile(struct file *filp,
@@ -1004,11 +1068,16 @@ static ssize_t relay_file_sendfile(struct file *filp,
                                   read_actor_t actor,
                                   void *target)
 {
-       return relay_file_read_subbufs(filp, ppos, count, subbuf_send_actor,
-                                      actor, target);
+       read_descriptor_t desc;
+       desc.written = 0;
+       desc.count = count;
+       desc.arg.data = target;
+       desc.error = 0;
+       return relay_file_read_subbufs(filp, ppos, subbuf_send_actor,
+                                      actor, &desc);
 }
 
-struct file_operations relay_file_operations = {
+const struct file_operations relay_file_operations = {
        .open           = relay_file_open,
        .poll           = relay_file_poll,
        .mmap           = relay_file_mmap,
@@ -1018,3 +1087,12 @@ struct file_operations relay_file_operations = {
        .sendfile       = relay_file_sendfile,
 };
 EXPORT_SYMBOL_GPL(relay_file_operations);
+
+static __init int relay_init(void)
+{
+
+       hotcpu_notifier(relay_hotcpu_callback, 0);
+       return 0;
+}
+
+module_init(relay_init);