Skip to content

Commit 6c191cd

Browse files
hkamezawatorvalds
authored andcommitted
memcg: res_counter_read_u64(): fix potential races on 32-bit machines
res_counter_read_u64 reads u64 value without lock. It's dangerous in a 32bit environment. Add locking. Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Greg Thelen <gthelen@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: David Rientjes <rientjes@google.com> Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Minchan Kim <minchan.kim@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 61f2e7b commit 6c191cd

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎kernel/res_counter.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,24 @@ ssize_t res_counter_read(struct res_counter *counter, int member,
126126
pos, buf, s - buf);
127127
}
128128

129+
#if BITS_PER_LONG == 32
130+
u64 res_counter_read_u64(struct res_counter *counter, int member)
131+
{
132+
unsigned long flags;
133+
u64 ret;
134+
135+
spin_lock_irqsave(&counter->lock, flags);
136+
ret = *res_counter_member(counter, member);
137+
spin_unlock_irqrestore(&counter->lock, flags);
138+
139+
return ret;
140+
}
141+
#else
129142
u64 res_counter_read_u64(struct res_counter *counter, int member)
130143
{
131144
return *res_counter_member(counter, member);
132145
}
146+
#endif
133147

134148
int res_counter_memparse_write_strategy(const char *buf,
135149
unsigned long long *res)

0 commit comments

Comments
 (0)