Re: [PATCH v3 2/4] mm: convert zone->managed_pages to atomic variable

From: Michal Hocko
Date: Thu Nov 08 2018 - 03:33:02 EST


On Thu 08-11-18 13:53:16, Arun KS wrote:
> totalram_pages, zone->managed_pages and totalhigh_pages updates
> are protected by managed_page_count_lock, but readers never care
> about it. Convert these variables to atomic to avoid readers
> potentially seeing a store tear.
>
> This patch converts zone->managed_pages. Subsequent patches will
> convert totalram_panges, totalhigh_pages and eventually
> managed_page_count_lock will be removed.
>
> Suggested-by: Michal Hocko <mhocko@xxxxxxxx>
> Suggested-by: Vlastimil Babka <vbabka@xxxxxxx>
> Signed-off-by: Arun KS <arunks@xxxxxxxxxxxxxx>
> Reviewed-by: Konstantin Khlebnikov <khlebnikov@xxxxxxxxxxxxxx>
> Acked-by: Michal Hocko <mhocko@xxxxxxxx>
> Acked-by: Vlastimil Babka <vbabka@xxxxxxx>
>
> ---
> Main motivation was that managed_page_count_lock handling was
> complicating things. It was discussed in lenght here,
> https://lore.kernel.org/patchwork/patch/995739/#1181785
> So it seemes better to remove the lock and convert variables
> to atomic, with preventing poteintial store-to-read tearing as
> a bonus.

Do not be afraid to put this into the changelog. It is much better to
have it there in case anybody wonders in future and use git blame rather
than chase an email archive to find it in the foot note. The same
applies to the meta patch.

> Most of the changes are done by below coccinelle script,
>
> @@
> struct zone *z;
> expression e1;
> @@
> (
> - z->managed_pages = e1
> + atomic_long_set(&z->managed_pages, e1)
> |
> - e1->managed_pages++
> + atomic_long_inc(&e1->managed_pages)
> |
> - z->managed_pages
> + zone_managed_pages(z)
> )
>
> @@
> expression e,e1;
> @@
> - e->managed_pages += e1
> + atomic_long_add(e1, &e->managed_pages)
>
> @@
> expression z;
> @@
> - z.managed_pages
> + zone_managed_pages(&z)
>
> Then, manually apply following change,
> include/linux/mmzone.h
>
> - unsigned long managed_pages;
> + atomic_long_t managed_pages;
>
> +static inline unsigned long zone_managed_pages(struct zone *zone)
> +{
> + return (unsigned long)atomic_long_read(&zone->managed_pages);
> +}
>
--
Michal Hocko
SUSE Labs