Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1979288pxb; Fri, 5 Mar 2021 04:39:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJwmP4Ys3MzlKEdCo99QtFxcMLyGn+A9UHqHwbGgRalkir1s5+KHPHb/3RdgmSFdeKq7Mqjk X-Received: by 2002:a50:fd8b:: with SMTP id o11mr8847006edt.346.1614947976516; Fri, 05 Mar 2021 04:39:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614947976; cv=none; d=google.com; s=arc-20160816; b=hVgKiCy4+4S/DFWhDyovixoQZXIUJcaosLa/thGR8aUlK0p7QPlwkrDFaO4htdjpug C3CRhOkUSwOSbhtOgD32RQSzevF0K5Q0QvNlAQ4v5JQOzwCasbMoM0HYKp+UztujetDA qp7yomBUCGMldFvNOuLg0UF0qGXpNfh4zST8sv9U17KF6oCwmLKN15HZa3hWyausWhbR UzAHicIOSe17H3FmHs4wLHHPvXgFVKKqQ1HT84YrgU9w17ByGoWOT7S0E6CAQM1JUi8E xjRilEG3M4DxGhMJlSvC/0Ap1klMVEvFuZWOYkj+r5dnY5gCgdRzl/FdUsnlwkBd3ZGp QF5g== 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=NflSIRwUBaOtX2zfyAQe1nrpo2lkmL6UGCNIlTrt9QY=; b=l0YSr0z8769DStXahpGNgTOnA4BBj52Lp67hxaduOzubaKNhoTD9YueiotGO53KBvs MjdAqDuM0SHshWHarBHsd3r+BEq6iXk4rAqFatgodmgnljEdjRPybbii3I0fMGAWI8yD 8y1qzkwx84SP3bYBn1/1G8cTyFwZzA9aVlTNdS9Z9lLgc2vISKYi7RBjh9EajEN6v4ci p4pmSFmFzzpbK9DcK8DCaX0ELg3+vZskNIs65wlPd7ggXh9r6LKhe6OUwwGJCO2BSf76 1GYWTaE2+RTgHVHkpUp6bFOaTtvZO2jOCNevj4j8kIM01FvYq1xZZyOrAcsPZhKu54k8 yLgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AwtX+7Bb; 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 x9si1293695ejc.196.2021.03.05.04.39.13; Fri, 05 Mar 2021 04:39:36 -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=AwtX+7Bb; 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 S232116AbhCEMgL (ORCPT + 99 others); Fri, 5 Mar 2021 07:36:11 -0500 Received: from mail.kernel.org ([198.145.29.99]:48246 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232032AbhCEMfj (ORCPT ); Fri, 5 Mar 2021 07:35:39 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4441B65023; Fri, 5 Mar 2021 12:35:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614947738; bh=RIJhnoM5UxUY11bh/UKBnxFWhgfleX+QY8WyJL7ZNZk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AwtX+7BbHKSsoq0vXdVi+BM6gM67JOFjR+jJDbzWN0D3fPNI21tkSBScXxi0EDMUq bmz7N+0vbnXTOFlVUyboe8Ghn+TVwLBwl1HszbzeBI91grtlMKgvOs7Wytp2BMBCx7 R1uFzf6l0wmqTl4XLY6HKgUAdmPeMbwLFB0D0SkA= 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 5.4 67/72] zsmalloc: account the number of compacted pages correctly Date: Fri, 5 Mar 2021 13:22:09 +0100 Message-Id: <20210305120900.620164431@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305120857.341630346@linuxfoundation.org> References: <20210305120857.341630346@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 @@ -1072,7 +1072,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 @@ -35,7 +35,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 @@ -2216,11 +2216,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))) { @@ -2250,7 +2252,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(); @@ -2261,12 +2263,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]; @@ -2274,10 +2279,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); @@ -2294,13 +2300,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; }