[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <9B86F7FF-C892-4F58-A24E-E0728D2637BC@lca.pw>
Date: Thu, 30 Jan 2020 21:22:05 -0500
From: Qian Cai <cai@....pw>
To: Andrew Morton <akpm@...ux-foundation.org>
Cc: Marco Elver <elver@...gle.com>,
Matthew Wilcox <willy@...radead.org>, dennis@...nel.org,
tj@...nel.org, Christoph Lameter <cl@...ux.com>,
Linux Memory Management List <linux-mm@...ck.org>,
LKML <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] mm/util: fix a data race in __vm_enough_memory()
> On Jan 30, 2020, at 9:18 PM, Andrew Morton <akpm@...ux-foundation.org> wrote:
>
> On Thu, 30 Jan 2020 13:35:18 +0100 Marco Elver <elver@...gle.com> wrote:
>
>> On Thu, 30 Jan 2020 at 12:50, Qian Cai <cai@....pw> wrote:
>>>
>>>> On Jan 29, 2020, at 11:20 PM, Matthew Wilcox <willy@...radead.org> wrote:
>>>>
>>>> I'm really not a fan of exposing the internals of a percpu_counter outside
>>>> the percpu_counter.h file. Why shouldn't this be fixed by putting the
>>>> READ_ONCE() inside percpu_counter_read()?
>>>
>>> It is because not all places suffer from a data race. For example, in __wb_update_bandwidth(), it was protected by a lock. I was a bit worry about blindly adding READ_ONCE() inside percpu_counter_read() might has unexpected side-effect. For example, it is unnecessary to have READ_ONCE() for a volatile variable. So, I thought just to keep the change minimal with a trade off by exposing a bit internal details as you mentioned.
>>>
>>> However, I had also copied the percpu maintainers to see if they have any preferences?
>>
>> I would not add READ_ONCE to percpu_counter_read(), given the writes
>> (increments) are not atomic either, so not much is gained.
>>
>> Notice that this is inside a WARN_ONCE, so you may argue that a data
>> race here doesn't matter to the correct behaviour of the system
>> (except if you have panic_on_warn on).
>>
>> For the warning to trigger, vm_committed_as must decrease. Assume that
>> a data race (assuming bad compiler optimizations) can somehow
>> accomplish this, then the load or write must cause a transient value
>> to somehow be less than a stable value. My hypothesis is this is very
>> unlikely.
>>
>> Given the fact this is a WARN_ONCE, and the fact that a transient
>> decrease in the value is unlikely, you may consider
>> 'VM_WARN_ONCE(data_race(percpu_counter_read(&vm_committed_as)) <
>> ...)'. That way you won't modify percpu_counter_read and still catch
>> unintended races elsewhere.
>>
>
> That, or add an alternative version of per_cpu_counter_read() to the
> percpu API. A very carefully commented version!
I send a patch to use data_race() which should be sufficient,
https://lore.kernel.org/linux-mm/20200130145649.1240-1-cai@lca.pw/
Powered by blists - more mailing lists