Re: [PATCH 3/3] mm: gup: pack has_pinned in MMF_HAS_PINNED

From: Linus Torvalds
Date: Fri May 07 2021 - 03:39:56 EST


On Thu, May 6, 2021 at 11:43 PM John Hubbard <jhubbard@xxxxxxxxxx> wrote:
>
> +static void set_mm_has_pinned_flag(unsigned long *mm_flags)
> +{
> + /*
> + * Avoid setting the bit unless necessary. This matters a lot with
> + * large SMP machines.
> + */
> + if (!test_bit(MMF_HAS_PINNED, mm_flags))
> + set_bit(MMF_HAS_PINNED, mm_flags);
> +}

Yes, please do split it up like this.

But please make it explicitly inline, and move the comment to above
the function.

And add the important key part to it: that the bit is never cleared.

That idempotent behavior of the "set_bit()" is what makes it safe to
do this non-atomic test-and-set (yes, the "set_bit()" itself is
atomic, but the sequence above is not).

Side note: we do have a few other places where this kind of thing
happens, so it *might* make sense to even make this a generic pattern
in case somebody can come up with a good descriptive name for that
("set_bit_if_not_set()" sounds descriptive, but the subtle
non-atomicity should probably be part of it).

> + if (flags & FOLL_PIN)
> + set_mm_has_pinned_flag(&mm->flags);
>
> ...which is now very readable, once again.

Yes, that does look much better.

Thanks,

Linus