From patchwork Thu Jul 27 06:29:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Yang X-Patchwork-Id: 13328887 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9870DEB64DD for ; Thu, 27 Jul 2023 06:30:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 70CF66B0072; Thu, 27 Jul 2023 02:30:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 696E56B0074; Thu, 27 Jul 2023 02:30:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 536B76B0075; Thu, 27 Jul 2023 02:30:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 401836B0072 for ; Thu, 27 Jul 2023 02:30:01 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id ECCD61CA100 for ; Thu, 27 Jul 2023 06:30:00 +0000 (UTC) X-FDA: 81056416560.30.F82778B Received: from mailgw02.mediatek.com (mailgw02.mediatek.com [216.200.240.185]) by imf24.hostedemail.com (Postfix) with ESMTP id D2DCA180014 for ; Thu, 27 Jul 2023 06:29:57 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=mediatek.com header.s=dk header.b=tj+Xxvzv; spf=pass (imf24.hostedemail.com: domain of andrew.yang@mediatek.com designates 216.200.240.185 as permitted sender) smtp.mailfrom=andrew.yang@mediatek.com; dmarc=pass (policy=quarantine) header.from=mediatek.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690439398; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=HVj27kBDVpPvZ+j4dkr5RIuiJz1r0HErENAsJMpj6ng=; b=yc10G6ziM0Voc6c7pOWO8kavIw23PMYRBpxbwfTNuJJgGsdaMw933FidWbqIzA/48xDBof eE84M52PvVF9pYtwmbmGDoi2DnIdsPCqWQtnFghD7gGmZcSU8AefEd+7yDEalydR+Tqf7F gfS40OiFjsZYPPRMAilqtO81WSoSblw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690439398; a=rsa-sha256; cv=none; b=X1mPBncfKMpmKvZDFzDnIg19mxDiT5Xl2E4goGSl0KVYxd3RixAyo8xB0CBruQnGzJrZQZ KrRlqGHHR0QJZX1Yd+a6735VlXwWzJaHaelnIWb8fJ+hd7bRqad32FAQhrONTGwL0Pwyvt LQr1dQtdNF3EU+5xMM1x8/YQ4LGuRa4= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=mediatek.com header.s=dk header.b=tj+Xxvzv; spf=pass (imf24.hostedemail.com: domain of andrew.yang@mediatek.com designates 216.200.240.185 as permitted sender) smtp.mailfrom=andrew.yang@mediatek.com; dmarc=pass (policy=quarantine) header.from=mediatek.com X-UUID: fc8ddef22c4611ee83ed1395ce914268-20230726 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=HVj27kBDVpPvZ+j4dkr5RIuiJz1r0HErENAsJMpj6ng=; b=tj+XxvzvVKQqp4VEbm1pCbjkR0B2LfxdiN/GXsGp7S++KvdCOrDDoRbUdPH8oG2lgCtG//3WMwTZ/GOuDSrjsrAVow/viUZ2/axaZOYhXIhsFwf4Y7Nf/Sb1tl2b1s93QhjH6PxFsnNusGIiQMTh+nR2IzCLCCmwSRxfe0AJvd4=; X-CID-UNFAMILIAR: 1 X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.29,REQID:d4e6346c-04be-4a78-be4b-2c2fcb7a2ea9,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:100,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:100 X-CID-INFO: VERSION:1.1.29,REQID:d4e6346c-04be-4a78-be4b-2c2fcb7a2ea9,IP:0,URL :0,TC:0,Content:0,EDM:0,RT:0,SF:100,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTIO N:quarantine,TS:100 X-CID-META: VersionHash:e7562a7,CLOUDID:26988242-d291-4e62-b539-43d7d78362ba,B ulkID:230727142932X71U816G,BulkQuantity:1,Recheck:0,SF:16|19|48|38|29|28,T C:nil,Content:0,EDM:-3,IP:nil,URL:0,File:nil,Bulk:40,QS:nil,BEC:nil,COL:0, OSI:0,OSA:0,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-CID-FACTOR: TF_CID_SPAM_SDM,TF_CID_SPAM_ASC,TF_CID_SPAM_USA,TF_CID_SPAM_FSD, TF_CID_SPAM_SNR X-UUID: fc8ddef22c4611ee83ed1395ce914268-20230726 Received: from mtkmbs14n2.mediatek.inc [(172.21.101.76)] by mailgw02.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 725371684; Wed, 26 Jul 2023 23:29:49 -0700 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Thu, 27 Jul 2023 14:29:13 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Thu, 27 Jul 2023 14:29:13 +0800 From: Andrew Yang To: Minchan Kim , Sergey Senozhatsky , Andrew Morton , Matthias Brugger , AngeloGioacchino Del Regno , Sebastian Andrzej Siewior CC: , , Andrew Yang , , , , Subject: [PATCH v2] zsmalloc: Fix races between modifications of fullness and isolated Date: Thu, 27 Jul 2023 14:29:07 +0800 Message-ID: <20230727062910.6337-1-andrew.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-MTK: N X-Stat-Signature: jxcdr9nih16bwicwbs75h1ujnrxycgwc X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: D2DCA180014 X-Rspam-User: X-HE-Tag: 1690439397-541340 X-HE-Meta: U2FsdGVkX187AdcU4JKkdTdNobBDN+C+te8s2ph6PGJ+USsuihnTCWd7fZnvMcN+W6wTHLOeURvCLf1mFEzTbHBjuMAbp69xNyW0YHfNyw+U6gOg0IG2eF0c/SVknF77Wia3iDiX4NQv1C55E4cs/aS4mKSdg2f3oGM1FCRUQ1RcsNaAHDZARiVS6ffZ7EcFmoGPS9ZXW/8mLfaiCzsujWwZy57IdruyF4ZnrkvddLPRpP6BwTffEhrtK+MJRDkSnQCZJEQOIZnMv7y13MGC65iXdsao+2Dw8FJBi2qlChMRX8OuC2H6cCQSYE9d/sOA4NoSsah1Kh8B5hgeKX8D4nbi2tqNQ1EGBaJyzJ1B7NZtwa39MikFPaGHARZgpJ6mjR9ooCzoU5fdL0S5jPhL2xfcg/gDlyetohUL/4eUthCrV5/RXU904OlFAySUyBJIQHRjeB3RDS5dwgQWYIawcMy6R35M+z69PFSyPoliHvzsw8Rqs5GKRLDHu4pc2HvD8Z9vk4vlCTpTdRxGn5M+ALYcM1c7C4Rzw1d6pvmqnvyHHtx+tcrzlkfKpk5hoRSeupzf5/UvZtbOzHUzD5JIzgu0ei3g7xDAt9fHX+dPgMGnW5fRAZlZb9IwxYxQyfu0P992Exr8r1g29VLiTKAHFN/kVuJvmzRYKTDoXt87zxi4WSRufJqJMmrH0GeET0L7mRZqCNtkiASdIpYqG0kUbqGYgVvIfOiFhv3Xnz2GUekpeh9QO75WNiUYBxaRNBrWNrMfTGC9anKiwKLvnmf7sKVX9zkzw8SnCNp6DFqTQDkwrR/JMWECdTkx6r/FVJecQrz9yXKT5E5uzZaPiNffdkA0zRnFrevfsGt33VdBJl7rJ2YKV7By0+GDFC19F4T8f7Ct/mL9dRSs9UY6sU0r5gK/3ffGXuxxkNXKG8t2Jrr7MUsA8zcC0xq3gpml/11ekt03FhVQeZjgJmyIBfV H6hBoynp iiGKtPV5FVqVh8DUfYjzFTM7bfYlDXbqCIkO8+cRNjKemChtlNCIXJeu6mPpXz9Y5F0Rgzv7IvUaq7n2vCTgBfaggDkT56+u7Oy2YDUuyU8nJGd69M0C9hWUCR8kJMwrsud4thwZ2XpLFnLn01en1X2lJzyuOjSjdfV7rS3pyJ+1ccT+pPhn0y0qz7Os4X7jw0QDUXj2cZFyd4N+IhVNrOUQ1Hd9LcRK3vzvweX2cqOf0WUsJ02/ZoKnY/A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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") Reviewed-by: Sergey Senozhatsky Reviewed-by: Nhat Pham --- 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 = {