From nobody Fri Sep 20 13:33:38 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2388BEB64DC for ; Fri, 21 Jul 2023 06:38:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231154AbjGUGiW (ORCPT ); Fri, 21 Jul 2023 02:38:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231153AbjGUGhz (ORCPT ); Fri, 21 Jul 2023 02:37:55 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2FBD2D73 for ; Thu, 20 Jul 2023 23:37:14 -0700 (PDT) X-UUID: 04ea9c30279111ee9cb5633481061a41-20230721 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=8PU+yLqgU8QTJBPsYq/TunQLzed36dffCDsGG++Ehj4=; b=BC6yyje/DPkjGRTu+OcNYoePBzsC/1kTblnXEpQjJkrom8MJQJd+LJnxJ/Gft9iB2t7Ji2i4L9B7QzQ4wosoUuferBugCcMtZLIqxVOOrd4FYereCMegVjM52p2DaoK0w1gv8W9Jd+j/dyb5+ADnjTnN15PunAsrS8d+zXLgq2A=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.28,REQID:47b59bd3-0170-427b-a5e9-f554744b6943,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:176cd25,CLOUDID:cefef64c-06c1-468b-847d-5b62d44dbb9b,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,SPR:NO, DKR:0,DKP:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 04ea9c30279111ee9cb5633481061a41-20230721 Received: from mtkmbs10n2.mediatek.inc [(172.21.101.183)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 2070794451; Fri, 21 Jul 2023 14:37:10 +0800 Received: from mtkmbs11n1.mediatek.inc (172.21.101.185) by mtkmbs10n1.mediatek.inc (172.21.101.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Fri, 21 Jul 2023 14:37:08 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs11n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Fri, 21 Jul 2023 14:37:08 +0800 From: Andrew Yang To: Minchan Kim , Sergey Senozhatsky , Andrew Morton , Matthias Brugger , AngeloGioacchino Del Regno , Sebastian Andrzej Siewior CC: , , Andrew Yang , , , , Subject: [PATCH] zsmalloc: Fix races between modifications of fullness and isolated Date: Fri, 21 Jul 2023 14:37:01 +0800 Message-ID: <20230721063705.11455-1-andrew.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Since fullness and isolated share the same unsigned int, modifications of them should be protected by the same lock. Signed-off-by: Andrew Yang Fixes: c4549b871102 ("zsmalloc: remove zspage isolation for migration") --- mm/zsmalloc.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 32f5bc4074df..b96230402a8d 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, =20 static bool zs_page_isolate(struct page *page, isolate_mode_t mode) { + struct zs_pool *pool; struct zspage *zspage; =20 /* @@ -1786,9 +1787,10 @@ static bool zs_page_isolate(struct page *page, isola= te_mode_t mode) VM_BUG_ON_PAGE(PageIsolated(page), page); =20 zspage =3D get_zspage(page); - migrate_write_lock(zspage); + pool =3D zspage->pool; + spin_lock(&pool->lock); inc_zspage_isolation(zspage); - migrate_write_unlock(zspage); + spin_unlock(&pool->lock); =20 return true; } @@ -1858,8 +1860,8 @@ static int zs_page_migrate(struct page *newpage, stru= ct page *page, * 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); + spin_unlock(&pool->lock); migrate_write_unlock(zspage); =20 get_page(newpage); @@ -1876,14 +1878,16 @@ static int zs_page_migrate(struct page *newpage, st= ruct page *page, =20 static void zs_page_putback(struct page *page) { + struct zs_pool *pool; struct zspage *zspage; =20 VM_BUG_ON_PAGE(!PageIsolated(page), page); =20 zspage =3D get_zspage(page); - migrate_write_lock(zspage); + pool =3D zspage->pool; + spin_lock(&pool->lock); dec_zspage_isolation(zspage); - migrate_write_unlock(zspage); + spin_unlock(&pool->lock); } =20 static const struct movable_operations zsmalloc_mops =3D { --=20 2.18.0