blkcg: clean up blkg_tryget_closest()
[sfrench/cifs-2.6.git] / include / linux / blk-cgroup.h
index bf13ecb0fe4f67189af465751cca98a441463f15..76c61318fda5b5c714cb8f2926b79c439fcb45f8 100644 (file)
@@ -499,22 +499,33 @@ static inline void blkg_get(struct blkcg_gq *blkg)
  */
 static inline bool blkg_tryget(struct blkcg_gq *blkg)
 {
-       return percpu_ref_tryget(&blkg->refcnt);
+       return blkg && percpu_ref_tryget(&blkg->refcnt);
 }
 
 /**
  * blkg_tryget_closest - try and get a blkg ref on the closet blkg
  * @blkg: blkg to get
  *
- * This walks up the blkg tree to find the closest non-dying blkg and returns
- * the blkg that it did association with as it may not be the passed in blkg.
+ * This needs to be called rcu protected.  As the failure mode here is to walk
+ * up the blkg tree, this ensure that the blkg->parent pointers are always
+ * valid.  This returns the blkg that it ended up taking a reference on or %NULL
+ * if no reference was taken.
  */
 static inline struct blkcg_gq *blkg_tryget_closest(struct blkcg_gq *blkg)
 {
-       while (!percpu_ref_tryget(&blkg->refcnt))
+       struct blkcg_gq *ret_blkg = NULL;
+
+       WARN_ON_ONCE(!rcu_read_lock_held());
+
+       while (blkg) {
+               if (blkg_tryget(blkg)) {
+                       ret_blkg = blkg;
+                       break;
+               }
                blkg = blkg->parent;
+       }
 
-       return blkg;
+       return ret_blkg;
 }
 
 /**