From patchwork Fri Mar 3 07:31:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13158389 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 77AB8C7EE2F for ; Fri, 3 Mar 2023 07:31:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EA3A6B0080; Fri, 3 Mar 2023 02:31:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0989D6B0081; Fri, 3 Mar 2023 02:31:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7ABF6B0082; Fri, 3 Mar 2023 02:31:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D8D456B0080 for ; Fri, 3 Mar 2023 02:31:48 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AE4311210E9 for ; Fri, 3 Mar 2023 07:31:48 +0000 (UTC) X-FDA: 80526767496.08.A1BB2B2 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by imf04.hostedemail.com (Postfix) with ESMTP id D661040006 for ; Fri, 3 Mar 2023 07:31:46 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Y8E4rBHR; spf=pass (imf04.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.216.48 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677828706; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=EaoqeXZv4LfGVEpTUejOz4FwTVyi9EWK7MkYs5EdeOk=; b=61cKjOLlJoe/icS5RoGcdCHNFhNQDBDU7Iv6Q9iAaGCGw6/G3gBFrmfU997zmlPYSmQtFN mFw5CIwwx7z6RK6GFd4JhVMIVt8yXatyMckIie4qZ9dtQ3RK4AoMfMpLRUqar3RdrAoSw7 el69uLIchhwRL+IgZZyceCYATsu4RvA= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Y8E4rBHR; spf=pass (imf04.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.216.48 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677828706; a=rsa-sha256; cv=none; b=yPR+87TY4nQul+/D6VA+6e/9hir0eNN2lCJziL+DjKqrtrAc6a66GGmUA/tsMxcaMcqca+ KxdvlxNwBoESIsnotCSUtjJfXla4uk3rxUtW/6Pf82T/9jBrEW+9EcFJxrFM6Kw5bsfy8X FEep1ERypxbsk53P3UpvbGlPS0ASh4Q= Received: by mail-pj1-f48.google.com with SMTP id l1so1675726pjt.2 for ; Thu, 02 Mar 2023 23:31:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EaoqeXZv4LfGVEpTUejOz4FwTVyi9EWK7MkYs5EdeOk=; b=Y8E4rBHRosinda0OmWTLbAlFzsIJYcyRA86sK6m3/T1oYf+s7bbu9abW0fOfmRyqH6 DgIia1zoJYEKGWghszZU9PMw1T64vinsuJwFEAjXC+tmQfefZMSQ5zlWHY6z+KFZwZTx h+L8bBXAVeU3YJfLbtRYUOCYby/LZbRFvFK0Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EaoqeXZv4LfGVEpTUejOz4FwTVyi9EWK7MkYs5EdeOk=; b=sAUv+gWH4eSsX6NKi5v5dtcxbUWWx3PZk1/5Bp9SKb/l0fXmsMvPYbTaaunfV7hgeb vQv/tPhoQI+Yt6RYXR6ecKM9v2mirYwt1Dz0Vm0H7bVwr44wYtr89dVEUSQXIn+XetI1 yCiUa/LxOO7z+35rmScbqV444wT/DTVdAO3OoQrY/1HM83rqFafBIRYNqpQrZRdvmoQE BzKEHXFXn9mutZOJc4dUzURYrpAjYY3vw/3coz4lrm5unkoQyDqJUvYLrBQh2iph+uWo XjUINxHgJ0qY36WVvCh062PS89WsTCM6MiqNWmIX37LD/MOxPmc3OGy1im06bwwQyqF7 9YIA== X-Gm-Message-State: AO0yUKXTugBPTEvAYFuazMBgHbTo1iKqXNJQ7JCWB3wtxrBufNIxNiv+ 7avdJxZT3hBY2ajkOtBov6uSDg== X-Google-Smtp-Source: AK7set9hGk3rv6YT7DUxhYeRuULEgZmvpKwfvpjPcLe4uqMwh5iXUTEgTN2COTTPag0bPN9QqfIy4Q== X-Received: by 2002:a05:6a20:938d:b0:cd:74aa:df76 with SMTP id x13-20020a056a20938d00b000cd74aadf76mr1600951pzh.50.1677828705810; Thu, 02 Mar 2023 23:31:45 -0800 (PST) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:badf:7e3e:d3f9:4956]) by smtp.gmail.com with ESMTPSA id c18-20020aa781d2000000b005a8b4dcd21asm933439pfn.15.2023.03.02.23.31.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 23:31:45 -0800 (PST) From: Sergey Senozhatsky To: Minchan Kim , Andrew Morton Cc: Yosry Ahmed , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Sergey Senozhatsky Subject: [PATCHv3 3/4] zsmalloc: rework compaction algorithm Date: Fri, 3 Mar 2023 16:31:29 +0900 Message-Id: <20230303073130.1950714-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog In-Reply-To: <20230303073130.1950714-1-senozhatsky@chromium.org> References: <20230303073130.1950714-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Stat-Signature: kygzigye59owy76weywcnndisyddfr4z X-Rspam-User: X-Rspamd-Queue-Id: D661040006 X-Rspamd-Server: rspam06 X-HE-Tag: 1677828706-10855 X-HE-Meta: U2FsdGVkX1/OXANWumDqN76hvH2g7r1p38ytPXeKtYSvzR3UAqnPqWP1XivR7dFraGUM3ODJAJl2x286CzfN7G2jNRa/yxOAIJORjwnjvdoz3TePbWaorRTqD4CB4L9Yc2luzhGlFn+NyWp5GWie5/6aeg79r/ER7/jg3c/3E7RBvkF+9KZp3RqiT9gI6sgTbo/Auer7YsOcYbHkGcf+t0HI765gEY4t5T9jJAa0NAUO6DlU6pDAD1LYnchQej+Mn01QNT1atINfiscNhL2GAxtcFjRTTOqZCkePuqnVvQ0wC4TXnVuIbO/QD095gEpx2rtPz7pG3Arov0UEqMordg+eaKSnpCfssCGE4fppeawX3Gi1Xlk0uYl+nifouh9L1u+PFFloAYgu8NS3ep822W1zm0jMaUsdhLIPZeFQTJ10kpufvPgW2Vv8DKGZZYG0emR9RCbUKPuz3Mmx+g/ALTQzvmXIauBiiQzBN99QbuVwgCyB/LYYcjIizlWevtuUBtl54XlyuUJ53C62XhAB9MqQ/vKJ4IF/9eXQKOUEiFfcAbH84yWzKLWlLW5oxoDWgUBRz559mtvhG4MsoxKVvtyYijxqo1OOBwGDaeioK4IZjzkb/gIw84RrZdMY19K3zDay9HT+PMQ6Hb0OkXjkyosGG1H//vxUmcWXrYfkRHNb19+SbM5IyFhLwwdAsb+DikpuYHFrRgNRBIAgjsq0wIMQkDjBYhuTxpuPYN3i8a+nrr5S55iGTReIY1lDH2gZvoludPQdFTrCLLroU3WsPFVsAJynHLI5ZGsXeB7yW48bwBFxml5mA8CwOni8DdzMmcw4+ersS23T8e3S14RlCmUXe0bzd+uOeWujIISI4432699E9Am9G3+do7Q5T++pyRT90o5Dn5ZaYdR3E9h8wFRtxfZ/Adm1Cp5RmOJwin1WyEtp0Eu+83WuNA+ZR85zNaI6jN9cpETTLN0Ar0w fwdTTUG6 iz+7cfm3Qvmf/Y9JeKk8zgWP7h9zOpwXVUtYP7rW0Vm8036x8JEoNBzmI5taILeie7+2SVd8SOPngYnArk4qAOWj8NIyxLByDRUw4QLSHAUmPs9K6/8DFXwvjs5WoZ7XPYot2diGGTNM1R8OmPW/QGo5p3EviXGlNANjXG2nffP/2nHEWmei/9TYT4nDj0IotY1oArtrozoSRnv8zwa7GGTabV1autXeEBZrc9rVoHWw485Kz/YyZ4ZgxN3S7nbvTEEb8eaih1o76Y1N7dVjUcI99f5WFf+9ZVTQQydY341eeEQHa+ZEGpynqBebhbhrT9dgbNcm5hAvTqn6dl3x++y0mj5TRrGMYVkDvAZB3HIcFTxPCvE1rsT9VVXmHdDfCPjWxaNT26Q2BGgs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.004543, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The zsmalloc compaction algorithm has the potential to waste some CPU cycles, particularly when compacting pages within the same fullness group. This is due to the way it selects the head page of the fullness list for source and destination pages, and how it reinserts those pages during each iteration. The algorithm may first use a page as a migration destination and then as a migration source, leading to an unnecessary back-and-forth movement of objects. Consider the following fullness list: PageA PageB PageC PageD PageE During the first iteration, the compaction algorithm will select PageA as the source and PageB as the destination. All of PageA's objects will be moved to PageB, and then PageA will be released while PageB is reinserted into the fullness list. PageB PageC PageD PageE During the next iteration, the compaction algorithm will again select the head of the list as the source and destination, meaning that PageB will now serve as the source and PageC as the destination. This will result in the objects being moved away from PageB, the same objects that were just moved to PageB in the previous iteration. To prevent this avalanche effect, the compaction algorithm should not reinsert the destination page between iterations. By doing so, the most optimal page will continue to be used and its usage ratio will increase, reducing internal fragmentation. The destination page should only be reinserted into the fullness list if: - It becomes full - No source page is available. TEST ==== It's very challenging to reliably test this series. I ended up developing my own synthetic test that has 100% reproducibility. The test generates significan fragmentation (for each size class) and then performs compaction for each class individually and tracks the number of memcpy() in zs_object_copy(), so that we can compare the amount work compaction does on per-class basis. Total amount of work (zram mm_stat objs_moved) ---------------------------------------------- Old fullness grouping, old compaction algorithm: 323977 memcpy() in zs_object_copy(). Old fullness grouping, new compaction algorithm: 262944 memcpy() in zs_object_copy(). New fullness grouping, new compaction algorithm: 213978 memcpy() in zs_object_copy(). Per-class compaction memcpy() comparison (T-test) ------------------------------------------------- x Old fullness grouping, old compaction algorithm + Old fullness grouping, new compaction algorithm N Min Max Median Avg Stddev x 140 349 3513 2461 2314.1214 806.03271 + 140 289 2778 2006 1878.1714 641.02073 Difference at 95.0% confidence -435.95 +/- 170.595 -18.8387% +/- 7.37193% (Student's t, pooled s = 728.216) x Old fullness grouping, old compaction algorithm + New fullness grouping, new compaction algorithm N Min Max Median Avg Stddev x 140 349 3513 2461 2314.1214 806.03271 + 140 226 2279 1644 1528.4143 524.85268 Difference at 95.0% confidence -785.707 +/- 159.331 -33.9527% +/- 6.88516% (Student's t, pooled s = 680.132) Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 78 ++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 630854575c30..141a3f69b642 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1780,15 +1780,14 @@ struct zs_compact_control { int obj_idx; }; -static int migrate_zspage(struct zs_pool *pool, struct size_class *class, - struct zs_compact_control *cc) +static void migrate_zspage(struct zs_pool *pool, struct size_class *class, + struct zs_compact_control *cc) { unsigned long used_obj, free_obj; unsigned long handle; struct page *s_page = cc->s_page; struct page *d_page = cc->d_page; int obj_idx = cc->obj_idx; - int ret = 0; while (1) { handle = find_alloced_obj(class, s_page, &obj_idx); @@ -1801,10 +1800,8 @@ static int migrate_zspage(struct zs_pool *pool, struct size_class *class, } /* Stop if there is no more space */ - if (zspage_full(class, get_zspage(d_page))) { - ret = -ENOMEM; + if (zspage_full(class, get_zspage(d_page))) break; - } used_obj = handle_to_obj(handle); free_obj = obj_malloc(pool, get_zspage(d_page), handle); @@ -1817,8 +1814,6 @@ static int migrate_zspage(struct zs_pool *pool, struct size_class *class, /* Remember last position in this iteration */ cc->s_page = s_page; cc->obj_idx = obj_idx; - - return ret; } static struct zspage *isolate_src_zspage(struct size_class *class) @@ -2213,7 +2208,7 @@ static unsigned long __zs_compact(struct zs_pool *pool, struct size_class *class) { struct zs_compact_control cc; - struct zspage *src_zspage; + struct zspage *src_zspage = NULL; struct zspage *dst_zspage = NULL; unsigned long pages_freed = 0; @@ -2222,50 +2217,45 @@ static unsigned long __zs_compact(struct zs_pool *pool, * as well as zpage allocation/free */ spin_lock(&pool->lock); - while ((src_zspage = isolate_src_zspage(class))) { - /* protect someone accessing the zspage(i.e., zs_map_object) */ - migrate_write_lock(src_zspage); + while (zs_can_compact(class)) { + int ret; - if (!zs_can_compact(class)) + if (!dst_zspage) { + dst_zspage = isolate_dst_zspage(class); + if (!dst_zspage) + break; + migrate_write_lock(dst_zspage); + cc.d_page = get_first_page(dst_zspage); + } + + src_zspage = isolate_src_zspage(class); + if (!src_zspage) break; + migrate_write_lock_nested(src_zspage); + cc.obj_idx = 0; cc.s_page = get_first_page(src_zspage); + migrate_zspage(pool, class, &cc); + ret = putback_zspage(class, src_zspage); + migrate_write_unlock(src_zspage); - while ((dst_zspage = isolate_dst_zspage(class))) { - migrate_write_lock_nested(dst_zspage); - - cc.d_page = get_first_page(dst_zspage); - /* - * If there is no more space in dst_page, resched - * and see if anyone had allocated another zspage. - */ - if (!migrate_zspage(pool, class, &cc)) - break; + if (ret == ZS_INUSE_RATIO_0) { + free_zspage(pool, class, src_zspage); + pages_freed += class->pages_per_zspage; + src_zspage = NULL; + } + if (get_fullness_group(class, dst_zspage) == ZS_INUSE_RATIO_100 + || spin_is_contended(&pool->lock)) { putback_zspage(class, dst_zspage); migrate_write_unlock(dst_zspage); dst_zspage = NULL; - if (spin_is_contended(&pool->lock)) - break; - } - /* Stop if we couldn't find slot */ - if (dst_zspage == NULL) - break; - - putback_zspage(class, dst_zspage); - migrate_write_unlock(dst_zspage); - - if (putback_zspage(class, src_zspage) == ZS_INUSE_RATIO_0) { - migrate_write_unlock(src_zspage); - free_zspage(pool, class, src_zspage); - pages_freed += class->pages_per_zspage; - } else - migrate_write_unlock(src_zspage); - spin_unlock(&pool->lock); - cond_resched(); - spin_lock(&pool->lock); + spin_unlock(&pool->lock); + cond_resched(); + spin_lock(&pool->lock); + } } if (src_zspage) { @@ -2273,6 +2263,10 @@ static unsigned long __zs_compact(struct zs_pool *pool, migrate_write_unlock(src_zspage); } + if (dst_zspage) { + putback_zspage(class, dst_zspage); + migrate_write_unlock(dst_zspage); + } spin_unlock(&pool->lock); return pages_freed;