Skip to content

Commit

Permalink
fs/aio: Use RCU accessors for kioctx_table->table[]
Browse files Browse the repository at this point in the history
commit d0264c0 upstream.

While converting ioctx index from a list to a table, db446a0
("aio: convert the ioctx list to table lookup v3") missed tagging
kioctx_table->table[] as an array of RCU pointers and using the
appropriate RCU accessors.  This introduces a small window in the
lookup path where init and access may race.

Mark kioctx_table->table[] with __rcu and use the approriate RCU
accessors when using the field.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Jann Horn <jannh@google.com>
Fixes: db446a0 ("aio: convert the ioctx list to table lookup v3")
Cc: Benjamin LaHaise <bcrl@kvack.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: stable@vger.kernel.org # v3.12+
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Tejun Heo authored and Greg Kroah-Hartman committed Mar 22, 2018
1 parent 9e7d69c commit b043e4d
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions fs/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ struct aio_ring {
#define AIO_RING_PAGES 8

struct kioctx_table {
struct rcu_head rcu;
unsigned nr;
struct kioctx *table[];
struct rcu_head rcu;
unsigned nr;
struct kioctx __rcu *table[];
};

struct kioctx_cpu {
Expand Down Expand Up @@ -583,9 +583,9 @@ static int ioctx_add_table(struct kioctx *ctx, struct mm_struct *mm)
while (1) {
if (table)
for (i = 0; i < table->nr; i++)
if (!table->table[i]) {
if (!rcu_access_pointer(table->table[i])) {
ctx->id = i;
table->table[i] = ctx;
rcu_assign_pointer(table->table[i], ctx);
spin_unlock(&mm->ioctx_lock);

/* While kioctx setup is in progress,
Expand Down Expand Up @@ -760,8 +760,8 @@ static int kill_ioctx(struct mm_struct *mm, struct kioctx *ctx,

spin_lock(&mm->ioctx_lock);
table = rcu_dereference_raw(mm->ioctx_table);
WARN_ON(ctx != table->table[ctx->id]);
table->table[ctx->id] = NULL;
WARN_ON(ctx != rcu_access_pointer(table->table[ctx->id]));
RCU_INIT_POINTER(table->table[ctx->id], NULL);
spin_unlock(&mm->ioctx_lock);

/* free_ioctx_reqs() will do the necessary RCU synchronization */
Expand Down Expand Up @@ -817,7 +817,8 @@ void exit_aio(struct mm_struct *mm)
return;

for (i = 0; i < table->nr; ++i) {
struct kioctx *ctx = table->table[i];
struct kioctx *ctx =
rcu_dereference_protected(table->table[i], true);
struct completion requests_done =
COMPLETION_INITIALIZER_ONSTACK(requests_done);

Expand Down Expand Up @@ -1003,7 +1004,7 @@ static struct kioctx *lookup_ioctx(unsigned long ctx_id)
if (!table || id >= table->nr)
goto out;

ctx = table->table[id];
ctx = rcu_dereference(table->table[id]);
if (ctx && ctx->user_id == ctx_id) {
percpu_ref_get(&ctx->users);
ret = ctx;
Expand Down

0 comments on commit b043e4d

Please sign in to comment.