Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1979753pxb; Fri, 5 Mar 2021 04:40:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyjnZir+mXxioZWrx4VrTJqJm0q/Qk3aqAWll3Aur7/cp9mMtUKJC7IXMdSWuzlAbA7Jzmb X-Received: by 2002:aa7:d5c9:: with SMTP id d9mr9259027eds.102.1614948022596; Fri, 05 Mar 2021 04:40:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614948022; cv=none; d=google.com; s=arc-20160816; b=QGUWwByczWGLL1UHxmSPnELl6VY0EzTvuPA3KCjJ19slhMVgqCcQPWUx1w3oHqUfV3 1JhDCfrYlWjCvyLrhMBahPu44nNVybVzHqW706yB69AiCqxlud0VCtd3A9guvgwo4EkU Obgj4vMiBjGJo1DHZJRzPM7GfexnoILcFg3cqeAOLMyh9dHBPIphKExYhVKkM0vrGsSx LnZlNr+Ad6uJp1IYHS/vWhkTzm1wMCdR5UImlt+w/2jRpSgLKaf1ON/0y+TcBcasW8aB vXh5AzCsFUgpbeLn4sUUxD1FLBVynriSLTlwWa/TjOtCrxgJaT9qyuKOmG5B0SbBH9// Cqpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SEyr7PTqievexRagW1u5Yz07dfRRAVPocR3Y94PHsPU=; b=YOp0tZ1ehhueY92YWBffjUfKURnW1nx5eKYGHyKefg8cEgeWuTY/gyFS1DxX1FIso0 eOqLbuXwz3BLlwEy4Dj7wm5P2ges6rSJ/j0e71jejldQHlCHjiVqFYF6wDYQwT3P73PX hDjKSLClDHKM7pmTRrwfEkaWCeLwgL5L/zX7LdSKXdNHX4v91QeM5mnsBlM0t94nWo1k OH7PUv5SlAZilz81//SnYY8umntDIzMEMrXOqB4UXvuu5WGpPETT3zFbhUy611NwVVGL SzZ2Sm1PO8PC6Y932/udkpDf0VEeEQhhqaSPmLVw+m24fUvDQatvm8izvWfJDZX+a7yz mLQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="M5LSch/q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z1si1462856edp.254.2021.03.05.04.39.59; Fri, 05 Mar 2021 04:40:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="M5LSch/q"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233118AbhCEMi6 (ORCPT + 99 others); Fri, 5 Mar 2021 07:38:58 -0500 Received: from mail.kernel.org ([198.145.29.99]:51252 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231826AbhCEMiK (ORCPT ); Fri, 5 Mar 2021 07:38:10 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8729565026; Fri, 5 Mar 2021 12:38:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614947890; bh=wB84boOLjUW9ANmQbwvgjvYZHOMChAWo+X+sBqaK2ek=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M5LSch/qqlx+S1BF2wq/Yns8vVe95r9vErHvysrwfHwaxy4D0Y+0Ychau3TasbNXB IvZ+97BWgJnqMKj9pYZnoooIWyIxgS/TpFT5Ex+oyTD6mZjuKxOmMDFLkEFNZ6vWyP xvgK0b9tce2WIbacbkGZVA+5xL3JLQis6SqELTS8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Rokudo Yan , Minchan Kim , Sergey Senozhatsky , Andrew Morton , Linus Torvalds Subject: [PATCH 4.19 48/52] zsmalloc: account the number of compacted pages correctly Date: Fri, 5 Mar 2021 13:22:19 +0100 Message-Id: <20210305120856.014050999@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305120853.659441428@linuxfoundation.org> References: <20210305120853.659441428@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rokudo Yan commit 2395928158059b8f9858365fce7713ce7fef62e4 upstream. There exists multiple path may do zram compaction concurrently. 1. auto-compaction triggered during memory reclaim 2. userspace utils write zram/compaction node So, multiple threads may call zs_shrinker_scan/zs_compact concurrently. But pages_compacted is a per zsmalloc pool variable and modification of the variable is not serialized(through under class->lock). There are two issues here: 1. the pages_compacted may not equal to total number of pages freed(due to concurrently add). 2. zs_shrinker_scan may not return the correct number of pages freed(issued by current shrinker). The fix is simple: 1. account the number of pages freed in zs_compact locally. 2. use actomic variable pages_compacted to accumulate total number. Link: https://lkml.kernel.org/r/20210202122235.26885-1-wu-yan@tcl.com Fixes: 860c707dca155a56 ("zsmalloc: account the number of compacted pages") Signed-off-by: Rokudo Yan Cc: Minchan Kim Cc: Sergey Senozhatsky Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- drivers/block/zram/zram_drv.c | 2 +- include/linux/zsmalloc.h | 2 +- mm/zsmalloc.c | 17 +++++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -873,7 +873,7 @@ static ssize_t mm_stat_show(struct devic zram->limit_pages << PAGE_SHIFT, max_used << PAGE_SHIFT, (u64)atomic64_read(&zram->stats.same_pages), - pool_stats.pages_compacted, + atomic_long_read(&pool_stats.pages_compacted), (u64)atomic64_read(&zram->stats.huge_pages)); up_read(&zram->init_lock); --- a/include/linux/zsmalloc.h +++ b/include/linux/zsmalloc.h @@ -36,7 +36,7 @@ enum zs_mapmode { struct zs_pool_stats { /* How many pages were migrated (freed) */ - unsigned long pages_compacted; + atomic_long_t pages_compacted; }; struct zs_pool; --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -2285,11 +2285,13 @@ static unsigned long zs_can_compact(stru return obj_wasted * class->pages_per_zspage; } -static void __zs_compact(struct zs_pool *pool, struct size_class *class) +static unsigned long __zs_compact(struct zs_pool *pool, + struct size_class *class) { struct zs_compact_control cc; struct zspage *src_zspage; struct zspage *dst_zspage = NULL; + unsigned long pages_freed = 0; spin_lock(&class->lock); while ((src_zspage = isolate_zspage(class, true))) { @@ -2319,7 +2321,7 @@ static void __zs_compact(struct zs_pool putback_zspage(class, dst_zspage); if (putback_zspage(class, src_zspage) == ZS_EMPTY) { free_zspage(pool, class, src_zspage); - pool->stats.pages_compacted += class->pages_per_zspage; + pages_freed += class->pages_per_zspage; } spin_unlock(&class->lock); cond_resched(); @@ -2330,12 +2332,15 @@ static void __zs_compact(struct zs_pool putback_zspage(class, src_zspage); spin_unlock(&class->lock); + + return pages_freed; } unsigned long zs_compact(struct zs_pool *pool) { int i; struct size_class *class; + unsigned long pages_freed = 0; for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) { class = pool->size_class[i]; @@ -2343,10 +2348,11 @@ unsigned long zs_compact(struct zs_pool continue; if (class->index != i) continue; - __zs_compact(pool, class); + pages_freed += __zs_compact(pool, class); } + atomic_long_add(pages_freed, &pool->stats.pages_compacted); - return pool->stats.pages_compacted; + return pages_freed; } EXPORT_SYMBOL_GPL(zs_compact); @@ -2363,13 +2369,12 @@ static unsigned long zs_shrinker_scan(st struct zs_pool *pool = container_of(shrinker, struct zs_pool, shrinker); - pages_freed = pool->stats.pages_compacted; /* * Compact classes and calculate compaction delta. * Can run concurrently with a manually triggered * (by user) compaction. */ - pages_freed = zs_compact(pool) - pages_freed; + pages_freed = zs_compact(pool); return pages_freed ? pages_freed : SHRINK_STOP; }