Re: [BUGFIX] memcg: fix res_counter_read_u64 lock aware (Was Re:[PATCH] oom: handle overflow in mem_cgroup_out_of_memory()

From: Andrew Morton
Date: Wed Jan 26 2011 - 20:08:45 EST


On Thu, 27 Jan 2011 09:53:42 +0900
KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx> wrote:

> res_counter_read_u64 reads u64 value without lock. It's dangerous
> in 32bit environment. This patch adds lock.
>
> Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@xxxxxxxxxxxxxx>
> ---
> include/linux/res_counter.h | 13 ++++++++++++-
> kernel/res_counter.c | 2 +-
> 2 files changed, 13 insertions(+), 2 deletions(-)
>
> Index: mmotm-0125/include/linux/res_counter.h
> ===================================================================
> --- mmotm-0125.orig/include/linux/res_counter.h
> +++ mmotm-0125/include/linux/res_counter.h
> @@ -68,7 +68,18 @@ struct res_counter {
> * @pos: and the offset.
> */
>
> -u64 res_counter_read_u64(struct res_counter *counter, int member);
> +u64 res_counter_read_u64_locked(struct res_counter *counter, int member);
> +
> +static inline u64 res_counter_read_u64(struct res_counter *counter, int member)
> +{
> + unsigned long flags;
> + u64 ret;
> +
> + spin_lock_irqsave(&counter->lock, flags);
> + ret = res_counter_read_u64_locked(counter, member);
> + spin_unlock_irqrestore(&counter->lock, flags);
> + return ret;
> +}
>
> ssize_t res_counter_read(struct res_counter *counter, int member,
> const char __user *buf, size_t nbytes, loff_t *pos,
> Index: mmotm-0125/kernel/res_counter.c
> ===================================================================
> --- mmotm-0125.orig/kernel/res_counter.c
> +++ mmotm-0125/kernel/res_counter.c
> @@ -126,7 +126,7 @@ ssize_t res_counter_read(struct res_coun
> pos, buf, s - buf);
> }
>
> -u64 res_counter_read_u64(struct res_counter *counter, int member)
> +u64 res_counter_read_u64_locked(struct res_counter *counter, int member)
> {
> return *res_counter_member(counter, member);
> }

We don't need the lock on 64-bit platforms!

And there's zero benefit to inlining the spin_lock/unlock(), given that
the function will always be making a function call anyway.

See i_size_read() for inspiration.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/