Commit e58dd0de authored by Sebastian Andrzej Siewior's avatar Sebastian Andrzej Siewior Committed by Linus Torvalds

bdi: use refcount_t for reference counting instead atomic_t

refcount_t type and corresponding API should be used instead of atomic_t
when the variable is used as a reference counter.  This permits avoiding
accidental refcounter overflows that might lead to use-after-free
situations.

Link: http://lkml.kernel.org/r/20180703200141.28415-4-bigeasy@linutronix.deSigned-off-by: default avatarSebastian Andrzej Siewior <bigeasy@linutronix.de>
Reviewed-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Suggested-by: default avatarPeter Zijlstra <peterz@infradead.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent cedc5b6a
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <linux/timer.h> #include <linux/timer.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <linux/kref.h> #include <linux/kref.h>
#include <linux/refcount.h>
struct page; struct page;
struct device; struct device;
...@@ -75,7 +76,7 @@ enum wb_reason { ...@@ -75,7 +76,7 @@ enum wb_reason {
*/ */
struct bdi_writeback_congested { struct bdi_writeback_congested {
unsigned long state; /* WB_[a]sync_congested flags */ unsigned long state; /* WB_[a]sync_congested flags */
atomic_t refcnt; /* nr of attached wb's and blkg */ refcount_t refcnt; /* nr of attached wb's and blkg */
#ifdef CONFIG_CGROUP_WRITEBACK #ifdef CONFIG_CGROUP_WRITEBACK
struct backing_dev_info *__bdi; /* the associated bdi, set to NULL struct backing_dev_info *__bdi; /* the associated bdi, set to NULL
......
...@@ -404,13 +404,13 @@ static inline bool inode_cgwb_enabled(struct inode *inode) ...@@ -404,13 +404,13 @@ static inline bool inode_cgwb_enabled(struct inode *inode)
static inline struct bdi_writeback_congested * static inline struct bdi_writeback_congested *
wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp) wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
{ {
atomic_inc(&bdi->wb_congested->refcnt); refcount_inc(&bdi->wb_congested->refcnt);
return bdi->wb_congested; return bdi->wb_congested;
} }
static inline void wb_congested_put(struct bdi_writeback_congested *congested) static inline void wb_congested_put(struct bdi_writeback_congested *congested)
{ {
if (atomic_dec_and_test(&congested->refcnt)) if (refcount_dec_and_test(&congested->refcnt))
kfree(congested); kfree(congested);
} }
......
...@@ -438,10 +438,10 @@ wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp) ...@@ -438,10 +438,10 @@ wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
if (new_congested) { if (new_congested) {
/* !found and storage for new one already allocated, insert */ /* !found and storage for new one already allocated, insert */
congested = new_congested; congested = new_congested;
new_congested = NULL;
rb_link_node(&congested->rb_node, parent, node); rb_link_node(&congested->rb_node, parent, node);
rb_insert_color(&congested->rb_node, &bdi->cgwb_congested_tree); rb_insert_color(&congested->rb_node, &bdi->cgwb_congested_tree);
goto found; spin_unlock_irqrestore(&cgwb_lock, flags);
return congested;
} }
spin_unlock_irqrestore(&cgwb_lock, flags); spin_unlock_irqrestore(&cgwb_lock, flags);
...@@ -451,13 +451,13 @@ wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp) ...@@ -451,13 +451,13 @@ wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp)
if (!new_congested) if (!new_congested)
return NULL; return NULL;
atomic_set(&new_congested->refcnt, 0); refcount_set(&new_congested->refcnt, 1);
new_congested->__bdi = bdi; new_congested->__bdi = bdi;
new_congested->blkcg_id = blkcg_id; new_congested->blkcg_id = blkcg_id;
goto retry; goto retry;
found: found:
atomic_inc(&congested->refcnt); refcount_inc(&congested->refcnt);
spin_unlock_irqrestore(&cgwb_lock, flags); spin_unlock_irqrestore(&cgwb_lock, flags);
kfree(new_congested); kfree(new_congested);
return congested; return congested;
...@@ -474,7 +474,7 @@ void wb_congested_put(struct bdi_writeback_congested *congested) ...@@ -474,7 +474,7 @@ void wb_congested_put(struct bdi_writeback_congested *congested)
unsigned long flags; unsigned long flags;
local_irq_save(flags); local_irq_save(flags);
if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) { if (!refcount_dec_and_lock(&congested->refcnt, &cgwb_lock)) {
local_irq_restore(flags); local_irq_restore(flags);
return; return;
} }
...@@ -804,7 +804,7 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi) ...@@ -804,7 +804,7 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi)
if (!bdi->wb_congested) if (!bdi->wb_congested)
return -ENOMEM; return -ENOMEM;
atomic_set(&bdi->wb_congested->refcnt, 1); refcount_set(&bdi->wb_congested->refcnt, 1);
err = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL); err = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL);
if (err) { if (err) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment