linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] zsmalloc: Fix races between modifications of fullness and isolated
@ 2023-07-27  6:29 Andrew Yang
  2023-07-28  3:53 ` Sergey Senozhatsky
  2023-08-10 15:37 ` Nhat Pham
  0 siblings, 2 replies; 3+ messages in thread
From: Andrew Yang @ 2023-07-27  6:29 UTC (permalink / raw)
  To: Minchan Kim, Sergey Senozhatsky, Andrew Morton, Matthias Brugger,
	AngeloGioacchino Del Regno, Sebastian Andrzej Siewior
  Cc: wsd_upstream, casper.li, Andrew Yang, linux-mm, linux-kernel,
	linux-arm-kernel, linux-mediatek

Since fullness and isolated share the same unsigned int,
modifications of them should be protected by the same lock.

Signed-off-by: Andrew Yang <andrew.yang@mediatek.com>
Fixes: c4549b871102 ("zsmalloc: remove zspage isolation for migration")
---
v2: Moving comment too
---
 mm/zsmalloc.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index 32f5bc4074df..b58f957429f0 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -1777,6 +1777,7 @@ static void replace_sub_page(struct size_class *class, struct zspage *zspage,
 
 static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
 {
+	struct zs_pool *pool;
 	struct zspage *zspage;
 
 	/*
@@ -1786,9 +1787,10 @@ static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
 	VM_BUG_ON_PAGE(PageIsolated(page), page);
 
 	zspage = get_zspage(page);
-	migrate_write_lock(zspage);
+	pool = zspage->pool;
+	spin_lock(&pool->lock);
 	inc_zspage_isolation(zspage);
-	migrate_write_unlock(zspage);
+	spin_unlock(&pool->lock);
 
 	return true;
 }
@@ -1854,12 +1856,12 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
 	kunmap_atomic(s_addr);
 
 	replace_sub_page(class, zspage, newpage, page);
+	dec_zspage_isolation(zspage);
 	/*
 	 * Since we complete the data copy and set up new zspage structure,
 	 * it's okay to release the pool's lock.
 	 */
 	spin_unlock(&pool->lock);
-	dec_zspage_isolation(zspage);
 	migrate_write_unlock(zspage);
 
 	get_page(newpage);
@@ -1876,14 +1878,16 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
 
 static void zs_page_putback(struct page *page)
 {
+	struct zs_pool *pool;
 	struct zspage *zspage;
 
 	VM_BUG_ON_PAGE(!PageIsolated(page), page);
 
 	zspage = get_zspage(page);
-	migrate_write_lock(zspage);
+	pool = zspage->pool;
+	spin_lock(&pool->lock);
 	dec_zspage_isolation(zspage);
-	migrate_write_unlock(zspage);
+	spin_unlock(&pool->lock);
 }
 
 static const struct movable_operations zsmalloc_mops = {
-- 
2.18.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] zsmalloc: Fix races between modifications of fullness and isolated
  2023-07-27  6:29 [PATCH v2] zsmalloc: Fix races between modifications of fullness and isolated Andrew Yang
@ 2023-07-28  3:53 ` Sergey Senozhatsky
  2023-08-10 15:37 ` Nhat Pham
  1 sibling, 0 replies; 3+ messages in thread
From: Sergey Senozhatsky @ 2023-07-28  3:53 UTC (permalink / raw)
  To: Andrew Yang
  Cc: Minchan Kim, Sergey Senozhatsky, Andrew Morton, Matthias Brugger,
	AngeloGioacchino Del Regno, Sebastian Andrzej Siewior,
	wsd_upstream, casper.li, linux-mm, linux-kernel, linux-arm-kernel,
	linux-mediatek

On (23/07/27 14:29), Andrew Yang wrote:
> 
> Since fullness and isolated share the same unsigned int,
> modifications of them should be protected by the same lock.
> 
> Signed-off-by: Andrew Yang <andrew.yang@mediatek.com>
> Fixes: c4549b871102 ("zsmalloc: remove zspage isolation for migration")

Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] zsmalloc: Fix races between modifications of fullness and isolated
  2023-07-27  6:29 [PATCH v2] zsmalloc: Fix races between modifications of fullness and isolated Andrew Yang
  2023-07-28  3:53 ` Sergey Senozhatsky
@ 2023-08-10 15:37 ` Nhat Pham
  1 sibling, 0 replies; 3+ messages in thread
From: Nhat Pham @ 2023-08-10 15:37 UTC (permalink / raw)
  To: Andrew Yang
  Cc: Minchan Kim, Sergey Senozhatsky, Andrew Morton, Matthias Brugger,
	AngeloGioacchino Del Regno, Sebastian Andrzej Siewior,
	wsd_upstream, casper.li, linux-mm, linux-kernel, linux-arm-kernel,
	linux-mediatek

On Wed, Jul 26, 2023 at 11:30 PM Andrew Yang <andrew.yang@mediatek.com> wrote:
>
> Since fullness and isolated share the same unsigned int,
> modifications of them should be protected by the same lock.
>
> Signed-off-by: Andrew Yang <andrew.yang@mediatek.com>
> Fixes: c4549b871102 ("zsmalloc: remove zspage isolation for migration")
> ---
> v2: Moving comment too
> ---
>  mm/zsmalloc.c | 14 +++++++++-----
>  1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index 32f5bc4074df..b58f957429f0 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
> @@ -1777,6 +1777,7 @@ static void replace_sub_page(struct size_class *class, struct zspage *zspage,
>
>  static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
>  {
> +       struct zs_pool *pool;
>         struct zspage *zspage;
>
>         /*
> @@ -1786,9 +1787,10 @@ static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
>         VM_BUG_ON_PAGE(PageIsolated(page), page);
>
>         zspage = get_zspage(page);
> -       migrate_write_lock(zspage);
> +       pool = zspage->pool;
> +       spin_lock(&pool->lock);
>         inc_zspage_isolation(zspage);
> -       migrate_write_unlock(zspage);
> +       spin_unlock(&pool->lock);
>
>         return true;
>  }
> @@ -1854,12 +1856,12 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
>         kunmap_atomic(s_addr);
>
>         replace_sub_page(class, zspage, newpage, page);
> +       dec_zspage_isolation(zspage);
>         /*
>          * Since we complete the data copy and set up new zspage structure,
>          * it's okay to release the pool's lock.
>          */
>         spin_unlock(&pool->lock);
> -       dec_zspage_isolation(zspage);
>         migrate_write_unlock(zspage);
>
>         get_page(newpage);
> @@ -1876,14 +1878,16 @@ static int zs_page_migrate(struct page *newpage, struct page *page,
>
>  static void zs_page_putback(struct page *page)
>  {
> +       struct zs_pool *pool;
>         struct zspage *zspage;
>
>         VM_BUG_ON_PAGE(!PageIsolated(page), page);
>
>         zspage = get_zspage(page);
> -       migrate_write_lock(zspage);
> +       pool = zspage->pool;
> +       spin_lock(&pool->lock);
>         dec_zspage_isolation(zspage);
> -       migrate_write_unlock(zspage);
> +       spin_unlock(&pool->lock);
>  }
>
>  static const struct movable_operations zsmalloc_mops = {
> --
> 2.18.0
>
>

I think this fixes an issue of mine that has been bugging me for
the past couple of weeks :) Thanks a lot, Andrew!

This should be added to other Linux stable versions too, right?
(with the caveat that before 6.2, use class's lock instead of
pool's lock).

Reviewed-by: Nhat Pham <nphamcs@gmail.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-08-10 15:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-27  6:29 [PATCH v2] zsmalloc: Fix races between modifications of fullness and isolated Andrew Yang
2023-07-28  3:53 ` Sergey Senozhatsky
2023-08-10 15:37 ` Nhat Pham

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).