Re: [PATCH] mm: fix a data race in put_page()

From: Qian Cai
Date: Mon Feb 10 2020 - 07:16:41 EST




> On Feb 10, 2020, at 2:48 AM, Marco Elver <elver@xxxxxxxxxx> wrote:
>
> Here is an alternative:
>
> Let's say KCSAN gives you this:
> /* ... Assert that the bits set in mask are not written
> concurrently; they may still be read concurrently.
> The access that immediately follows is assumed to access those
> bits and safe w.r.t. data races.
>
> For example, this may be used when certain bits of @flags may
> only be modified when holding the appropriate lock,
> but other bits may still be modified locklessly.
> ...
> */
> #define ASSERT_EXCLUSIVE_BITS(flags, mask) ....
>
> Then we can write page_zonenum as follows:
>
> static inline enum zone_type page_zonenum(const struct page *page)
> {
> + ASSERT_EXCLUSIVE_BITS(page->flags, ZONES_MASK << ZONES_PGSHIFT);
> return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK;
> }
>
> This will accomplish the following:
> 1. The current code is not touched, and we do not have to verify that
> the change is correct without KCSAN.
> 2. We're not introducing a bunch of special macros to read bits in various ways.
> 3. KCSAN will assume that the access is safe, and no data race report
> is generated.
> 4. If somebody modifies ZONES bits concurrently, KCSAN will tell you
> about the race.
> 5. We're documenting the code.
>
> Anything I missed?

I donât know. Having to write the same line twice does not feel me any better than data_race() with commenting occasionally.