mm: introduce free_pages_prepare()
[safe/jmp/linux-2.6] / block / blk-cgroup.c
index 649b05d..a680964 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/module.h>
 #include <linux/err.h>
 #include <linux/blkdev.h>
+#include <linux/slab.h>
 #include "blk-cgroup.h"
 #include <linux/genhd.h>
 
@@ -85,12 +86,6 @@ struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
 }
 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
 
-void blkio_group_init(struct blkio_group *blkg)
-{
-       spin_lock_init(&blkg->stats_lock);
-}
-EXPORT_SYMBOL_GPL(blkio_group_init);
-
 /*
  * Add to the appropriate stat variable depending on the request type.
  * This should be called with the blkg->stats_lock held.
@@ -202,7 +197,7 @@ void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
 }
 EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
 
-void blkiocg_update_set_active_queue_stats(struct blkio_group *blkg)
+void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
 {
        unsigned long flags;
        struct blkio_group_stats *stats;
@@ -216,14 +211,51 @@ void blkiocg_update_set_active_queue_stats(struct blkio_group *blkg)
        blkio_update_group_wait_time(stats);
        spin_unlock_irqrestore(&blkg->stats_lock, flags);
 }
-EXPORT_SYMBOL_GPL(blkiocg_update_set_active_queue_stats);
+EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
+
+void blkiocg_set_start_empty_time(struct blkio_group *blkg)
+{
+       unsigned long flags;
+       struct blkio_group_stats *stats;
+
+       spin_lock_irqsave(&blkg->stats_lock, flags);
+       stats = &blkg->stats;
+
+       if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
+                       stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
+               spin_unlock_irqrestore(&blkg->stats_lock, flags);
+               return;
+       }
+
+       /*
+        * group is already marked empty. This can happen if cfqq got new
+        * request in parent group and moved to this group while being added
+        * to service tree. Just ignore the event and move on.
+        */
+       if(blkio_blkg_empty(stats)) {
+               spin_unlock_irqrestore(&blkg->stats_lock, flags);
+               return;
+       }
+
+       stats->start_empty_time = sched_clock();
+       blkio_mark_blkg_empty(stats);
+       spin_unlock_irqrestore(&blkg->stats_lock, flags);
+}
+EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
+
+void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
+                       unsigned long dequeue)
+{
+       blkg->stats.dequeue += dequeue;
+}
+EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
 #else
 static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
                                        struct blkio_group *curr_blkg) {}
 static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
 #endif
 
-void blkiocg_update_request_add_stats(struct blkio_group *blkg,
+void blkiocg_update_io_add_stats(struct blkio_group *blkg,
                        struct blkio_group *curr_blkg, bool direction,
                        bool sync)
 {
@@ -236,9 +268,9 @@ void blkiocg_update_request_add_stats(struct blkio_group *blkg,
        blkio_set_start_group_wait_time(blkg, curr_blkg);
        spin_unlock_irqrestore(&blkg->stats_lock, flags);
 }
-EXPORT_SYMBOL_GPL(blkiocg_update_request_add_stats);
+EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
 
-void blkiocg_update_request_remove_stats(struct blkio_group *blkg,
+void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
                                                bool direction, bool sync)
 {
        unsigned long flags;
@@ -248,7 +280,7 @@ void blkiocg_update_request_remove_stats(struct blkio_group *blkg,
                                        direction, sync);
        spin_unlock_irqrestore(&blkg->stats_lock, flags);
 }
-EXPORT_SYMBOL_GPL(blkiocg_update_request_remove_stats);
+EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
 
 void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
 {
@@ -260,33 +292,6 @@ void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
 }
 EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
 
-void blkiocg_set_start_empty_time(struct blkio_group *blkg, bool ignore)
-{
-       unsigned long flags;
-       struct blkio_group_stats *stats;
-
-       spin_lock_irqsave(&blkg->stats_lock, flags);
-       stats = &blkg->stats;
-
-       if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
-                       stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
-               spin_unlock_irqrestore(&blkg->stats_lock, flags);
-               return;
-       }
-
-       /*
-        * If ignore is set, we do not panic on the empty flag being set
-        * already. This is to avoid cases where there are superfluous timeslice
-        * complete events (for eg., forced_dispatch in CFQ) when no IOs are
-        * served which could result in triggering the empty check incorrectly.
-        */
-       BUG_ON(!ignore && blkio_blkg_empty(stats));
-       stats->start_empty_time = sched_clock();
-       blkio_mark_blkg_empty(stats);
-       spin_unlock_irqrestore(&blkg->stats_lock, flags);
-}
-EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
-
 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
                                uint64_t bytes, bool direction, bool sync)
 {
@@ -341,14 +346,13 @@ void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
        unsigned long flags;
 
        spin_lock_irqsave(&blkcg->lock, flags);
+       spin_lock_init(&blkg->stats_lock);
        rcu_assign_pointer(blkg->key, key);
        blkg->blkcg_id = css_id(&blkcg->css);
        hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
        spin_unlock_irqrestore(&blkcg->lock, flags);
-#ifdef CONFIG_DEBUG_BLK_CGROUP
        /* Need to take css reference ? */
        cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
-#endif
        blkg->dev = dev;
 }
 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
@@ -372,17 +376,16 @@ int blkiocg_del_blkio_group(struct blkio_group *blkg)
 
        rcu_read_lock();
        css = css_lookup(&blkio_subsys, blkg->blkcg_id);
-       if (!css)
-               goto out;
-
-       blkcg = container_of(css, struct blkio_cgroup, css);
-       spin_lock_irqsave(&blkcg->lock, flags);
-       if (!hlist_unhashed(&blkg->blkcg_node)) {
-               __blkiocg_del_blkio_group(blkg);
-               ret = 0;
+       if (css) {
+               blkcg = container_of(css, struct blkio_cgroup, css);
+               spin_lock_irqsave(&blkcg->lock, flags);
+               if (!hlist_unhashed(&blkg->blkcg_node)) {
+                       __blkiocg_del_blkio_group(blkg);
+                       ret = 0;
+               }
+               spin_unlock_irqrestore(&blkcg->lock, flags);
        }
-       spin_unlock_irqrestore(&blkcg->lock, flags);
-out:
+
        rcu_read_unlock();
        return ret;
 }
@@ -636,15 +639,6 @@ SHOW_FUNCTION_PER_GROUP(empty_time, BLKIO_STAT_EMPTY_TIME, 0);
 #endif
 #undef SHOW_FUNCTION_PER_GROUP
 
-#ifdef CONFIG_DEBUG_BLK_CGROUP
-void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
-                       unsigned long dequeue)
-{
-       blkg->stats.dequeue += dequeue;
-}
-EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
-#endif
-
 static int blkio_check_dev_num(dev_t dev)
 {
        int part = 0;
@@ -820,17 +814,15 @@ static int blkiocg_weight_device_read(struct cgroup *cgrp, struct cftype *cft,
        seq_printf(m, "dev\tweight\n");
 
        blkcg = cgroup_to_blkio_cgroup(cgrp);
-       if (list_empty(&blkcg->policy_list))
-               goto out;
-
-       spin_lock_irq(&blkcg->lock);
-       list_for_each_entry(pn, &blkcg->policy_list, node) {
-               seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
-                          MINOR(pn->dev), pn->weight);
+       if (!list_empty(&blkcg->policy_list)) {
+               spin_lock_irq(&blkcg->lock);
+               list_for_each_entry(pn, &blkcg->policy_list, node) {
+                       seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
+                                  MINOR(pn->dev), pn->weight);
+               }
+               spin_unlock_irq(&blkcg->lock);
        }
-       spin_unlock_irq(&blkcg->lock);
 
-out:
        return 0;
 }
 
@@ -922,40 +914,39 @@ static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
        struct blkio_policy_node *pn, *pntmp;
 
        rcu_read_lock();
-remove_entry:
-       spin_lock_irqsave(&blkcg->lock, flags);
+       do {
+               spin_lock_irqsave(&blkcg->lock, flags);
 
-       if (hlist_empty(&blkcg->blkg_list)) {
-               spin_unlock_irqrestore(&blkcg->lock, flags);
-               goto done;
-       }
+               if (hlist_empty(&blkcg->blkg_list)) {
+                       spin_unlock_irqrestore(&blkcg->lock, flags);
+                       break;
+               }
 
-       blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
-                               blkcg_node);
-       key = rcu_dereference(blkg->key);
-       __blkiocg_del_blkio_group(blkg);
+               blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
+                                       blkcg_node);
+               key = rcu_dereference(blkg->key);
+               __blkiocg_del_blkio_group(blkg);
 
-       spin_unlock_irqrestore(&blkcg->lock, flags);
+               spin_unlock_irqrestore(&blkcg->lock, flags);
 
-       /*
-        * This blkio_group is being unlinked as associated cgroup is going
-        * away. Let all the IO controlling policies know about this event.
-        *
-        * Currently this is static call to one io controlling policy. Once
-        * we have more policies in place, we need some dynamic registration
-        * of callback function.
-        */
-       spin_lock(&blkio_list_lock);
-       list_for_each_entry(blkiop, &blkio_list, list)
-               blkiop->ops.blkio_unlink_group_fn(key, blkg);
-       spin_unlock(&blkio_list_lock);
-       goto remove_entry;
+               /*
+                * This blkio_group is being unlinked as associated cgroup is
+                * going away. Let all the IO controlling policies know about
+                * this event. Currently this is static call to one io
+                * controlling policy. Once we have more policies in place, we
+                * need some dynamic registration of callback function.
+                */
+               spin_lock(&blkio_list_lock);
+               list_for_each_entry(blkiop, &blkio_list, list)
+                       blkiop->ops.blkio_unlink_group_fn(key, blkg);
+               spin_unlock(&blkio_list_lock);
+       } while (1);
 
-done:
        list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
                blkio_policy_delete_node(pn);
                kfree(pn);
        }
+
        free_css_id(&blkio_subsys, &blkcg->css);
        rcu_read_unlock();
        if (blkcg != &blkio_root_cgroup)
@@ -965,16 +956,16 @@ done:
 static struct cgroup_subsys_state *
 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
 {
-       struct blkio_cgroup *blkcg, *parent_blkcg;
+       struct blkio_cgroup *blkcg;
+       struct cgroup *parent = cgroup->parent;
 
-       if (!cgroup->parent) {
+       if (!parent) {
                blkcg = &blkio_root_cgroup;
                goto done;
        }
 
        /* Currently we do not support hierarchy deeper than two level (0,1) */
-       parent_blkcg = cgroup_to_blkio_cgroup(cgroup->parent);
-       if (css_depth(&parent_blkcg->css) > 0)
+       if (parent != cgroup->top_cgroup)
                return ERR_PTR(-EINVAL);
 
        blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);