From patchwork Fri Mar 3 07:31:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13158387 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 2DE46C7EE32 for ; Fri, 3 Mar 2023 07:31:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 641176B0074; Fri, 3 Mar 2023 02:31:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C8336B0075; Fri, 3 Mar 2023 02:31:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48FF16B0078; Fri, 3 Mar 2023 02:31:42 -0500 (EST) 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 39AB96B0074 for ; Fri, 3 Mar 2023 02:31:42 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 0C6111C6A2D for ; Fri, 3 Mar 2023 07:31:42 +0000 (UTC) X-FDA: 80526767244.29.AEC7315 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf22.hostedemail.com (Postfix) with ESMTP id 43627C0011 for ; Fri, 3 Mar 2023 07:31:40 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=BraWG+bY; spf=pass (imf22.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.210.179 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=1677828700; 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=wJN4vQ0bDt9mp9ZIcGCUIy6xbfFhJeCXLnKUnVtISUQ=; b=Y3OXpsq991O4mFopymwerNNsYZ+UdAOZtNNOThV48vUh6oaiVtn9kk/iMVo2ty2DBCImwV TY6kFRK2c78UziXRq2MB7EzBL56+fJoLQwVTu0zsN8MRFrixkNKIostH77VPRMf8MVttsy mCBi1/4RpEU6mye4Zbg6nolZRjEJUvY= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=BraWG+bY; spf=pass (imf22.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.210.179 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=1677828700; a=rsa-sha256; cv=none; b=FE7h+xFXfYZVefc8++I8ke77kE6jRbUKHV61+GaH07zF3jrUZwvlkrxhAwWYLVRf8BWQNM s2bh485a65Fx+Lq7/bbKTh0i0wDp4xgk0AaO81yfKr8YIGeGW/0WcT157kwekJsz2Axms0 stzwTNvm4NFjZiImYLF1adm48ws8Mm4= Received: by mail-pf1-f179.google.com with SMTP id cp12so993894pfb.5 for ; Thu, 02 Mar 2023 23:31:39 -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=wJN4vQ0bDt9mp9ZIcGCUIy6xbfFhJeCXLnKUnVtISUQ=; b=BraWG+bYowp5bBIS+PtIYQF9Rpx33u9GNrykqtbmeWA6S6s4w1E5GxGPFXzBgM55Vb AHS1hwM0Iu+sWFlALS1u15V9RuJ4TL2Xg+HZKBEW6jfMKUIEJ45Z32ZfLrwkkc25i1Uw 6xNkDv6D+iW6Zzf19xw4IgYO1f5MjYoI/q3wE= 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=wJN4vQ0bDt9mp9ZIcGCUIy6xbfFhJeCXLnKUnVtISUQ=; b=KzAf0sFmnactyZskAMM1ZbW9sXwd20Wm5wxi2G1LmL415Na+QM+gf6e/MCIR85M3s3 PmSFF9AiM/sEdTpbcr81KCbVQ+BdvJ/OPQp4BBgmXR7oyfdosYFxg8Cmcf6U2pz2RlIk Q9+nRa8zxQqtks4svq0DpnRPbYsOh+4F0QYyE51yUQ7ilxbtkGzoBTBkJ077Z8dY4jR0 os1bQc9f1913SKlx60t9pOmZeRq2N99+uV2frPnydAFx1g7ktsBbe0CdrdLIdOD683to 8dvZGB+GFnSjSVjFwcYbk7iM45jy6p7GeQ1ZZW4w02MLOjRCwaiy2hdoJJCmb/l/+FAf XnPg== X-Gm-Message-State: AO0yUKXemYPe9FQdLZj6NprmCFO3SIgQvxOG/dw83ChuB+tyzCnTAcRD cy1gNQkC0w5ogne6mQjqGVlsCOfhVsMS56jy X-Google-Smtp-Source: AK7set+MG8sg99gbwmv4ChCLIROydfYD4dg6WLUwYOTSp1GoDMJXVYpVbhjFtk4IbuYTEwXHoy1SUg== X-Received: by 2002:aa7:9f84:0:b0:5e5:6452:223f with SMTP id z4-20020aa79f84000000b005e56452223fmr1171701pfr.8.1677828699061; Thu, 02 Mar 2023 23:31:39 -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.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 23:31:38 -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 1/4] zsmalloc: remove insert_zspage() ->inuse optimization Date: Fri, 3 Mar 2023 16:31:27 +0900 Message-Id: <20230303073130.1950714-2-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-Rspamd-Queue-Id: 43627C0011 X-Stat-Signature: pnt4aut367ocehm78qrxfesxaff9pmhr X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1677828700-29160 X-HE-Meta: U2FsdGVkX18Wd7UR8lkX6QSGTezCl6DS8j5O9+3seQoONCTqX1z5xo52otKDPrVooOGfLfB7ie8CuMD60b1A5YabcwAsXeqDlREDZskFZbODhNkH1yXG+afhDYpFtcu07TVeoCt561wOCvfAg/92Iwr+z0wiEPCQ0YM16N3DwYAyoSAlKNZJWyeDPb3rlC5RdL0JT/tzXUgLs3gMNvZ3EazbGmEUqxvZp2e03qr2Rr/aOb1FBarqkWo0jolj+Yg9Ho7n8HTAsrg2gx58p1je5h/2ys7wlnzpgL7+rne4C7zDf8yPGkvSQY7l+9k6dO1+yTVaHbr4XuYHr8GxTac5de0TwZfg7u4cjDSYA65jWeukAqlXhE33VMRcsTfzK0DLtpaoZ+KIE61yo4SYgOXg3NY+KhRrYESMOckXam9VF2DmDMqyLrOgpxURqNtoCHv3nzNTZXArV6O/DT0lnYvrfVf621rWYCdEOpu19MI+doCHpSmJXZRlDMdIcAp17gdv7IeoaZ63ueLM/lnWUwREzkv2nn8CzZ76L3XgQ8f0ue3j+ad6xRb8xxVSIDxQ9HC86HeIYMuc51WlQeVj44QiyFYoGmFv4WAQ5SGEH53M3SUAl5kriYri4WAIP59EEIQ0uPPC7VgPlqgjD0HCe5ykXA12pHEdzNjydv4tEh8hmsoJsVrI9jWmYniOUpiHbaKVXj0jyQFz2rfv7BAhtxP7vvxkXq876XDY+zhIn+PyDPTocehPlSSA38VDon28p2n0xWpaR2R0ibMS2WNogFZLEqMt66oN85NBrIHD3vfn01Sd/m36R5zq9nXbgQ/XJeWYBNRobyDvL3zA3yY3Ltn6jfH0pfjrC5GM4nBjuq58JS6ffHtfLiaCGCj2In4Y3BixfCiPJrdUIqyiWEKYELPVJU/Hkfdz6a0DMtjXo4YA0DdCOSMUXpmFkgOP5mJBaEZGB/mJnr3ERYk8wdbaWXJ 4zXs6jgS QBc/SXdg80hfXt3eLiE/195eyJXiOaLqpljDODxVON6iclTHykAhV/d4Vltqe/gBWp9mAH+nAWizZI5ucfc4JuV9IzId4mDzYYohLLvsHZXkBmBI5fyzArs8vuTkveJFbXhbw+jZluhePW0i4ssODPZt5qE1JCw/m5m/bXTM5XcfPrHYzQ5wr9kK/fbeWUde5n37CeWo/YscjbPftgEqqqvpWkJY54Qs/PtnlshuWNErDyO/9yl5vHlRb4iIT1hal2n6XmoCLz4c946pikVU1HjRcnl3Hu5bRGAhyMdzmUNMkpsw30wux53BWveoeI2b2jzFBcZQJioTOxZGRrf7FsjJf+TmN4xM9nwugjbL+9vo+05zUJjcyDFhnPuEWGJ4Jw8TUnfsvzaTTANdtlcxmVezHoiQbL0JILmPzO+M74q9mdlaa0Pr2ZCAGFyK3Vw7MlDMn7rKldBZ1I5kcvS4UUdmykj91t55mC5pY/GSNz4JhQNk= 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: This optimization has no effect. It only ensures that when a zspage was added to its corresponding fullness list, its "inuse" counter was higher or lower than the "inuse" counter of the zspage at the head of the list. The intention was to keep busy zspages at the head, so they could be filled up and moved to the ZS_FULL fullness group more quickly. However, this doesn't work as the "inuse" counter of a zspage can be modified by obj_free() but the zspage may still belong to the same fullness list. So, fix_fullness_group() won't change the zspage's position in relation to the head's "inuse" counter, leading to a largely random order of zspages within the fullness list. For instance, consider a printout of the "inuse" counters of the first 10 zspages in a class that holds 93 objects per zspage: ZS_ALMOST_EMPTY: 36 67 68 64 35 54 63 52 As we can see the zspage with the lowest "inuse" counter is actually the head of the fullness list. Remove this pointless "optimisation". Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 3aed46ab7e6c..1cd180caff76 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -753,32 +753,19 @@ static enum fullness_group get_fullness_group(struct size_class *class, } /* - * Each size class maintains various freelists and zspages are assigned - * to one of these freelists based on the number of live objects they - * have. This functions inserts the given zspage into the freelist - * identified by . + * This function adds the given zspage to the fullness list identified + * by . */ static void insert_zspage(struct size_class *class, struct zspage *zspage, enum fullness_group fullness) { - struct zspage *head; - class_stat_inc(class, fullness, 1); - head = list_first_entry_or_null(&class->fullness_list[fullness], - struct zspage, list); - /* - * We want to see more ZS_FULL pages and less almost empty/full. - * Put pages with higher ->inuse first. - */ - if (head && get_zspage_inuse(zspage) < get_zspage_inuse(head)) - list_add(&zspage->list, &head->list); - else - list_add(&zspage->list, &class->fullness_list[fullness]); + list_add(&zspage->list, &class->fullness_list[fullness]); } /* - * This function removes the given zspage from the freelist identified + * This function removes the given zspage from the fullness list identified * by . */ static void remove_zspage(struct size_class *class, From patchwork Fri Mar 3 07:31:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13158388 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 E3956C64EC4 for ; Fri, 3 Mar 2023 07:31:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 52D396B007B; Fri, 3 Mar 2023 02:31:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4DDD06B007D; Fri, 3 Mar 2023 02:31:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 331996B007E; Fri, 3 Mar 2023 02:31:46 -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 229FD6B007B for ; Fri, 3 Mar 2023 02:31:46 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F17961C6A7F for ; Fri, 3 Mar 2023 07:31:45 +0000 (UTC) X-FDA: 80526767370.25.024D4C1 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf27.hostedemail.com (Postfix) with ESMTP id 2701740016 for ; Fri, 3 Mar 2023 07:31:43 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=bT7fROkd; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf27.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.215.169 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677828704; 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=+f1ViRZ4YeKX8ZgWQLobABDWwzOU0KOgFG1pN8DNKf4=; b=Lufs6YgJhycs3HCXQOFTRy27rMItW3cLwChjrewVNCETLhYy/s7lmwM91vzcw/j/o4cqnu rXsmq8aiVXYkbYa0VIqZIkWFR+/TimmxRm0D64tHLZycrxWlvp1+9B5VI+Y0Fd4qUHG/NT RjawIu4TZbDIDhcnlsWIWVjj7UcSibU= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=bT7fROkd; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf27.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.215.169 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677828704; a=rsa-sha256; cv=none; b=ZuVndSFQH9GsPK1PId5EeesNdasD2QbisbWwNIcRE1MXPOMgybmib4P2m8X77cnReDYeqY /1Uc/9QrbFRNCAz27FYVBLKDRzrPWw3XIFnRcYJNnHLlmEQVxwnrJeKcmnh9nTlx4F9hy+ BUOcLQ17ypaiBdP/JruSQNmXnTYBUNE= Received: by mail-pg1-f169.google.com with SMTP id y19so939538pgk.5 for ; Thu, 02 Mar 2023 23:31:43 -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=+f1ViRZ4YeKX8ZgWQLobABDWwzOU0KOgFG1pN8DNKf4=; b=bT7fROkdVv1z9VsE+CkjDQg2rQwAoxGdS4/qtyQNAOwINX6daVeSyp9Ne+MoY7zLR+ 5KA7Dq1F/pHFzkOompHF4jDph0QXHiF2Jc6mkQ0f5xxymHPSXkIHyrlDW8YwjdkBJHig Hwqyaq/KM/J++Fv4M64EcvXERjtiu6/KXaKiY= 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=+f1ViRZ4YeKX8ZgWQLobABDWwzOU0KOgFG1pN8DNKf4=; b=z882NQ8lXF7nc/Z+yVXUWjkxSA0z8n2rcX/ev2Rgz94eQdXoqYKgNsfBq4B3Gpo0xF LI631WK24Bng9RPyGq/PexyzKGO2rqdJIaNrb95cGHIzBRAVWrsRGDYi5WPy0+S6wXqq cZDZyhVpefn+PWdob46oxOogi46NWl/kXGBTCAto/6DqRo/aRxhAiLeURuTcQQx2m7cA ObQmzyhcXcyT8MdBlYRZVKmZDCaQKyuyL8xWfwYAELpYFv9/gzoOnzkQsOx0A9M6j3F2 mb2tY+bshJJm2w3nJTSR3UXOw0HlFTOLH0Tzr1VKBjHoULbsyL68qCh0NLYlE8NKJ1jO ox6A== X-Gm-Message-State: AO0yUKXDnzeIODsJpMNfFwmyxANHd0aGoc9ef4Wzpvd777VVD38dia5i RWWaIYSYOwFhV7hfSjGP2E/fOw== X-Google-Smtp-Source: AK7set+SgxBsT6SwLd3BD5CWFP/8kCRryFEXMoIGk9mGe0JgTO8Qr80TVtT52zZKlS9sfqTZm3/vWQ== X-Received: by 2002:a62:848d:0:b0:593:89ab:2ec4 with SMTP id k135-20020a62848d000000b0059389ab2ec4mr1506693pfd.10.1677828702872; Thu, 02 Mar 2023 23:31:42 -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.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 23:31:42 -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 2/4] zsmalloc: fine-grained inuse ratio based fullness grouping Date: Fri, 3 Mar 2023 16:31:28 +0900 Message-Id: <20230303073130.1950714-3-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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2701740016 X-Stat-Signature: qu37gtcwear87b9ae8gwmmjeqb93ak9j X-HE-Tag: 1677828703-900478 X-HE-Meta: U2FsdGVkX1/xaw2corEDBAdYBGE/bi96g1BhLt6JYADzTFGHT8OMLBaAXcXQKyRqGclq16K9jl3cISK9OarQBaNjpf5V2wsVz4yvyVmER+PSPEutEOZ5B6wHJQ7Ug9obzjZSyOpJR/QZoBFVLoY5K6bg6Z5Tvm2sr7dzIN498kWP7K/R4nNLPjy1cf43nYQVpl7UsX6aN8uRF/zJEiuwIdk1/vmzIG/+hz717gOCMNdY5X136iiSuet3qXCDkeLLv/5VEi4YKddM6VATdC9cOxDj1WTeJoKvIs9BaRznPEGn09h47mZGxvakM0opZ724WEDusyxeljkp9byyVI618tJ4xKTyM+nJ9J6RLh5QudJjMU6tat9ecRs4USmzmCJ29JuFv8bOy+0PFOu3X26E0SFYy2/H9Lt5bXcyjxyYSpEBHUHNO0C3yFzTNgZkiXbXPdSzsWUvyPbOx+vptn+PMt8kqqEkcxsXJIymBzOiVojb9bP3VSbxLDrKNAD+MKUCaCB4jMW11ANUXqTfSFGwzbd/g/McMoi0zIagp2wvwtYtGxLIja8PouV2F8KQmStEpFS1ZZoTC0aAk0SoPNbNI2dLd4eiwO2nBIs+cJqw/ilE/v9YKNQh1bMgrc+ToOX7TdANndQDiq999YAXf4r1cTE4bSKUsBPchS3tDJ+sN4CGlXeQF2XmwsIQWLiPCh8f0wxiSo5K2XPPFbRCutD226SD2UhVFXJ1piOS0XFdayKdFgOBiquvP6chO8VmxRW9FjDShAFt8B+WIozg18okhkZmHC9ynaLCujFasJ/kfOxM2YiM8DhnmGx0i3u5b4wRWwL09Bau118C+u47EIlxKWYsLeJk8GPpKHG7EJhFYofWf1jWr82y20iG06fKfd+5b20WkVYl2kH0gkVV5EbPwkr+1f7Y8No0sc2w7jQt/corYLW4UPwhZGE+SKgVwQJgElyXeZ4kdRD4LC0Chr5 R5m2vlzj A2UhYfTt7OJzutkGdVT9lmYspi0MEvCeeRKGJ/wm2EtPlpjxuDZO42ObgQdvTcJu2fcsL1AO0p5eniqZObZJmUUfGYPncdTrbDzJ8wLvm/mPy7NLDqIMuOpCUT8NOGs2kgWO/DZyX7B9bT10mVLwqCM87gbhMG+k9PuHLtI+cJhd5eF5LlBi57jh7E41TPMu09+xZyc8jw+1G257pO8XasuX5WLztQLpngjBYD2/dKtdEIzOKKi4wvTBQXoh6UeIXjeYmiIo4/7NX/pHRXV0Tob5M5atHeVWCX9bl4xpZGR3fA0NszI2RkDsUarind/xkMb48Rf4pQ9oqxW+ptTaskS0gUid4mp5Rk3SIo1s/Q7hWs419fppZYQTRtIhtsvsxwFiYwpn3PWk+6mnm4ltKHYvO2np/3NDmBp0pshz5PceeBXfPlIvr6juLYyj4Pb9bnVAQ 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: Each zspage maintains ->inuse counter which keeps track of the number of objects stored in the zspage. The ->inuse counter also determines the zspage's "fullness group" which is calculated as the ratio of the "inuse" objects to the total number of objects the zspage can hold (objs_per_zspage). The closer the ->inuse counter is to objs_per_zspage, the better. Each size class maintains several fullness lists, that keep track of zspages of particular "fullness". Pages within each fullness list are stored in random order with regard to the ->inuse counter. This is because sorting the zspages by ->inuse counter each time obj_malloc() or obj_free() is called would be too expensive. However, the ->inuse counter is still a crucial factor in many situations. For the two major zsmalloc operations, zs_malloc() and zs_compact(), we typically select the head zspage from the corresponding fullness list as the best candidate zspage. However, this assumption is not always accurate. For the zs_malloc() operation, the optimal candidate zspage should have the highest ->inuse counter. This is because the goal is to maximize the number of ZS_FULL zspages and make full use of all allocated memory. For the zs_compact() operation, the optimal source zspage should have the lowest ->inuse counter. This is because compaction needs to move objects in use to another page before it can release the zspage and return its physical pages to the buddy allocator. The fewer objects in use, the quicker compaction can release the zspage. Additionally, compaction is measured by the number of pages it releases. This patch reworks the fullness grouping mechanism. Instead of having two groups - ZS_ALMOST_EMPTY (usage ratio below 3/4) and ZS_ALMOST_FULL (usage ration above 3/4) - that result in too many zspages being included in the ALMOST_EMPTY group for specific classes, size classes maintain a larger number of fullness lists that give strict guarantees on the minimum and maximum ->inuse values within each group. Each group represents a 10% change in the ->inuse ratio compared to neighboring groups. In essence, there are groups for zspages with 0%, 10%, 20% usage ratios, and so on, up to 100%. This enhances the selection of candidate zspages for both zs_malloc() and zs_compact(). A printout of the ->inuse counters of the first 7 zspages per (random) class fullness group: class-768 objs_per_zspage 16: fullness 100%: empty fullness 99%: empty fullness 90%: empty fullness 80%: empty fullness 70%: empty fullness 60%: 8 8 9 9 8 8 8 fullness 50%: empty fullness 40%: 5 5 6 5 5 5 5 fullness 30%: 4 4 4 4 4 4 4 fullness 20%: 2 3 2 3 3 2 2 fullness 10%: 1 1 1 1 1 1 1 fullness 0%: empty The zs_malloc() function searches through the groups of pages starting with the one having the highest usage ratio. This means that it always selects a zspage from the group with the least internal fragmentation (highest usage ratio) and makes it even less fragmented by increasing its usage ratio. The zs_compact() function, on the other hand, begins by scanning the group with the highest fragmentation (lowest usage ratio) to locate the source page. The first available zspage is selected, and then the function moves downward to find a destination zspage in the group with the lowest internal fragmentation (highest usage ratio). Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 245 ++++++++++++++++++++++++++------------------------ 1 file changed, 128 insertions(+), 117 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 1cd180caff76..630854575c30 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -127,7 +127,7 @@ #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1) #define HUGE_BITS 1 -#define FULLNESS_BITS 2 +#define FULLNESS_BITS 4 #define CLASS_BITS 8 #define ISOLATED_BITS 5 #define MAGIC_VAL_BITS 8 @@ -159,51 +159,46 @@ #define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \ ZS_SIZE_CLASS_DELTA) + 1) +/* + * Pages are distinguished by the ratio of used memory (that is the ratio + * of ->inuse objects to all objects that page can store). For example, + * INUSE_RATIO_10 means that the ratio of used objects is > 0% and <= 10%. + * + * The number of fullness groups is not random. It allows us to keep + * diffeence between the least busy page in the group (minimum permitted + * number of ->inuse objects) and the most busy page (maximum permitted + * number of ->inuse objects) at a reasonable value. + */ enum fullness_group { - ZS_EMPTY, - ZS_ALMOST_EMPTY, - ZS_ALMOST_FULL, - ZS_FULL, - NR_ZS_FULLNESS, + ZS_INUSE_RATIO_0, + ZS_INUSE_RATIO_10, + /* NOTE: 5 more fullness groups here */ + ZS_INUSE_RATIO_70 = 7, + /* NOTE: 2 more fullness groups here */ + ZS_INUSE_RATIO_99 = 10, + ZS_INUSE_RATIO_100, + NR_FULLNESS_GROUPS, }; enum class_stat_type { - CLASS_EMPTY, - CLASS_ALMOST_EMPTY, - CLASS_ALMOST_FULL, - CLASS_FULL, - OBJ_ALLOCATED, - OBJ_USED, - NR_ZS_STAT_TYPE, + /* NOTE: stats for 12 fullness groups here: from inuse 0 to 100 */ + ZS_OBJS_ALLOCATED = NR_FULLNESS_GROUPS, + ZS_OBJS_INUSE, + NR_CLASS_STAT_TYPES, }; struct zs_size_stat { - unsigned long objs[NR_ZS_STAT_TYPE]; + unsigned long objs[NR_CLASS_STAT_TYPES]; }; #ifdef CONFIG_ZSMALLOC_STAT static struct dentry *zs_stat_root; #endif -/* - * We assign a page to ZS_ALMOST_EMPTY fullness group when: - * n <= N / f, where - * n = number of allocated objects - * N = total number of objects zspage can store - * f = fullness_threshold_frac - * - * Similarly, we assign zspage to: - * ZS_ALMOST_FULL when n > N / f - * ZS_EMPTY when n == 0 - * ZS_FULL when n == N - * - * (see: fix_fullness_group()) - */ -static const int fullness_threshold_frac = 4; static size_t huge_class_size; struct size_class { - struct list_head fullness_list[NR_ZS_FULLNESS]; + struct list_head fullness_list[NR_FULLNESS_GROUPS]; /* * Size of objects stored in this class. Must be multiple * of ZS_ALIGN. @@ -547,8 +542,8 @@ static inline void set_freeobj(struct zspage *zspage, unsigned int obj) } static void get_zspage_mapping(struct zspage *zspage, - unsigned int *class_idx, - enum fullness_group *fullness) + unsigned int *class_idx, + int *fullness) { BUG_ON(zspage->magic != ZSPAGE_MAGIC); @@ -557,14 +552,14 @@ static void get_zspage_mapping(struct zspage *zspage, } static struct size_class *zspage_class(struct zs_pool *pool, - struct zspage *zspage) + struct zspage *zspage) { return pool->size_class[zspage->class]; } static void set_zspage_mapping(struct zspage *zspage, - unsigned int class_idx, - enum fullness_group fullness) + unsigned int class_idx, + int fullness) { zspage->class = class_idx; zspage->fullness = fullness; @@ -588,23 +583,19 @@ static int get_size_class_index(int size) return min_t(int, ZS_SIZE_CLASSES - 1, idx); } -/* type can be of enum type class_stat_type or fullness_group */ static inline void class_stat_inc(struct size_class *class, int type, unsigned long cnt) { class->stats.objs[type] += cnt; } -/* type can be of enum type class_stat_type or fullness_group */ static inline void class_stat_dec(struct size_class *class, int type, unsigned long cnt) { class->stats.objs[type] -= cnt; } -/* type can be of enum type class_stat_type or fullness_group */ -static inline unsigned long zs_stat_get(struct size_class *class, - int type) +static inline unsigned long zs_stat_get(struct size_class *class, int type) { return class->stats.objs[type]; } @@ -646,16 +637,26 @@ static int zs_stats_size_show(struct seq_file *s, void *v) "pages_per_zspage", "freeable"); for (i = 0; i < ZS_SIZE_CLASSES; i++) { + int fg; + class = pool->size_class[i]; if (class->index != i) continue; spin_lock(&pool->lock); - class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL); - class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY); - obj_allocated = zs_stat_get(class, OBJ_ALLOCATED); - obj_used = zs_stat_get(class, OBJ_USED); + + /* + * Replecate old behaviour for almost_full and almost_empty + * stats. + */ + for (fg = ZS_INUSE_RATIO_70; fg <= ZS_INUSE_RATIO_99; fg++) + class_almost_full = zs_stat_get(class, fg); + for (fg = ZS_INUSE_RATIO_10; fg < ZS_INUSE_RATIO_70; fg++) + class_almost_empty = zs_stat_get(class, fg); + + obj_allocated = zs_stat_get(class, ZS_OBJS_ALLOCATED); + obj_used = zs_stat_get(class, ZS_OBJS_INUSE); freeable = zs_can_compact(class); spin_unlock(&pool->lock); @@ -723,42 +724,39 @@ static inline void zs_pool_stat_destroy(struct zs_pool *pool) } #endif - /* * For each size class, zspages are divided into different groups - * depending on how "full" they are. This was done so that we could - * easily find empty or nearly empty zspages when we try to shrink - * the pool (not yet implemented). This function returns fullness + * depending on their usage ratio. This function returns fullness * status of the given page. */ -static enum fullness_group get_fullness_group(struct size_class *class, - struct zspage *zspage) +static int get_fullness_group(struct size_class *class, struct zspage *zspage) { - int inuse, objs_per_zspage; - enum fullness_group fg; + int inuse, objs_per_zspage, ratio; inuse = get_zspage_inuse(zspage); objs_per_zspage = class->objs_per_zspage; if (inuse == 0) - fg = ZS_EMPTY; - else if (inuse == objs_per_zspage) - fg = ZS_FULL; - else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac) - fg = ZS_ALMOST_EMPTY; - else - fg = ZS_ALMOST_FULL; + return ZS_INUSE_RATIO_0; + if (inuse == objs_per_zspage) + return ZS_INUSE_RATIO_100; - return fg; + ratio = 100 * inuse / objs_per_zspage; + /* + * Take integer division into consideration: a page with one inuse + * object out of 127 possible, will endup having 0 usage ratio, + * which is wrong as it belongs in ZS_INUSE_RATIO_10 fullness group. + */ + return ratio / 10 + 1; } /* * This function adds the given zspage to the fullness list identified - * by . + * by . */ static void insert_zspage(struct size_class *class, struct zspage *zspage, - enum fullness_group fullness) + int fullness) { class_stat_inc(class, fullness, 1); list_add(&zspage->list, &class->fullness_list[fullness]); @@ -766,11 +764,11 @@ static void insert_zspage(struct size_class *class, /* * This function removes the given zspage from the fullness list identified - * by . + * by . */ static void remove_zspage(struct size_class *class, struct zspage *zspage, - enum fullness_group fullness) + int fullness) { VM_BUG_ON(list_empty(&class->fullness_list[fullness])); @@ -781,17 +779,16 @@ static void remove_zspage(struct size_class *class, /* * Each size class maintains zspages in different fullness groups depending * on the number of live objects they contain. When allocating or freeing - * objects, the fullness status of the page can change, say, from ALMOST_FULL - * to ALMOST_EMPTY when freeing an object. This function checks if such - * a status change has occurred for the given page and accordingly moves the - * page from the freelist of the old fullness group to that of the new - * fullness group. + * objects, the fullness status of the page can change, for instance, from + * INUSE_RATIO_80 to INUSE_RATIO_70 when freeing an object. This function + * checks if such a status change has occurred for the given page and + * accordingly moves the page from the list of the old fullness group to that + * of the new fullness group. */ -static enum fullness_group fix_fullness_group(struct size_class *class, - struct zspage *zspage) +static int fix_fullness_group(struct size_class *class, struct zspage *zspage) { int class_idx; - enum fullness_group currfg, newfg; + int currfg, newfg; get_zspage_mapping(zspage, &class_idx, &currfg); newfg = get_fullness_group(class, zspage); @@ -964,7 +961,7 @@ static void __free_zspage(struct zs_pool *pool, struct size_class *class, struct zspage *zspage) { struct page *page, *next; - enum fullness_group fg; + int fg; unsigned int class_idx; get_zspage_mapping(zspage, &class_idx, &fg); @@ -972,7 +969,7 @@ static void __free_zspage(struct zs_pool *pool, struct size_class *class, assert_spin_locked(&pool->lock); VM_BUG_ON(get_zspage_inuse(zspage)); - VM_BUG_ON(fg != ZS_EMPTY); + VM_BUG_ON(fg != ZS_INUSE_RATIO_0); /* Free all deferred handles from zs_free */ free_handles(pool, class, zspage); @@ -990,7 +987,7 @@ static void __free_zspage(struct zs_pool *pool, struct size_class *class, cache_free_zspage(pool, zspage); - class_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage); + class_stat_dec(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage); atomic_long_sub(class->pages_per_zspage, &pool->pages_allocated); } @@ -1011,7 +1008,7 @@ static void free_zspage(struct zs_pool *pool, struct size_class *class, return; } - remove_zspage(class, zspage, ZS_EMPTY); + remove_zspage(class, zspage, ZS_INUSE_RATIO_0); #ifdef CONFIG_ZPOOL list_del(&zspage->lru); #endif @@ -1147,9 +1144,9 @@ static struct zspage *find_get_zspage(struct size_class *class) int i; struct zspage *zspage; - for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) { + for (i = ZS_INUSE_RATIO_99; i >= ZS_INUSE_RATIO_0; i--) { zspage = list_first_entry_or_null(&class->fullness_list[i], - struct zspage, list); + struct zspage, list); if (zspage) break; } @@ -1508,7 +1505,7 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) { unsigned long handle, obj; struct size_class *class; - enum fullness_group newfg; + int newfg; struct zspage *zspage; if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE)) @@ -1530,7 +1527,7 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) /* Now move the zspage to another fullness group, if required */ fix_fullness_group(class, zspage); record_obj(handle, obj); - class_stat_inc(class, OBJ_USED, 1); + class_stat_inc(class, ZS_OBJS_INUSE, 1); spin_unlock(&pool->lock); return handle; @@ -1552,8 +1549,8 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp) record_obj(handle, obj); atomic_long_add(class->pages_per_zspage, &pool->pages_allocated); - class_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage); - class_stat_inc(class, OBJ_USED, 1); + class_stat_inc(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage); + class_stat_inc(class, ZS_OBJS_INUSE, 1); /* We completely set up zspage so mark them as movable */ SetZsPageMovable(pool, zspage); @@ -1609,7 +1606,7 @@ void zs_free(struct zs_pool *pool, unsigned long handle) struct page *f_page; unsigned long obj; struct size_class *class; - enum fullness_group fullness; + int fullness; if (IS_ERR_OR_NULL((void *)handle)) return; @@ -1624,7 +1621,7 @@ void zs_free(struct zs_pool *pool, unsigned long handle) zspage = get_zspage(f_page); class = zspage_class(pool, zspage); - class_stat_dec(class, OBJ_USED, 1); + class_stat_dec(class, ZS_OBJS_INUSE, 1); #ifdef CONFIG_ZPOOL if (zspage->under_reclaim) { @@ -1642,7 +1639,7 @@ void zs_free(struct zs_pool *pool, unsigned long handle) obj_free(class->size, obj, NULL); fullness = fix_fullness_group(class, zspage); - if (fullness == ZS_EMPTY) + if (fullness == ZS_INUSE_RATIO_0) free_zspage(pool, class, zspage); spin_unlock(&pool->lock); @@ -1824,22 +1821,33 @@ static int migrate_zspage(struct zs_pool *pool, struct size_class *class, return ret; } -static struct zspage *isolate_zspage(struct size_class *class, bool source) +static struct zspage *isolate_src_zspage(struct size_class *class) { - int i; struct zspage *zspage; - enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL}; + int fg; - if (!source) { - fg[0] = ZS_ALMOST_FULL; - fg[1] = ZS_ALMOST_EMPTY; + for (fg = ZS_INUSE_RATIO_10; fg <= ZS_INUSE_RATIO_99; fg++) { + zspage = list_first_entry_or_null(&class->fullness_list[fg], + struct zspage, list); + if (zspage) { + remove_zspage(class, zspage, fg); + return zspage; + } } - for (i = 0; i < 2; i++) { - zspage = list_first_entry_or_null(&class->fullness_list[fg[i]], - struct zspage, list); + return zspage; +} + +static struct zspage *isolate_dst_zspage(struct size_class *class) +{ + struct zspage *zspage; + int fg; + + for (fg = ZS_INUSE_RATIO_99; fg >= ZS_INUSE_RATIO_10; fg--) { + zspage = list_first_entry_or_null(&class->fullness_list[fg], + struct zspage, list); if (zspage) { - remove_zspage(class, zspage, fg[i]); + remove_zspage(class, zspage, fg); return zspage; } } @@ -1852,12 +1860,11 @@ static struct zspage *isolate_zspage(struct size_class *class, bool source) * @class: destination class * @zspage: target page * - * Return @zspage's fullness_group + * Return @zspage's fullness status */ -static enum fullness_group putback_zspage(struct size_class *class, - struct zspage *zspage) +static int putback_zspage(struct size_class *class, struct zspage *zspage) { - enum fullness_group fullness; + int fullness; fullness = get_fullness_group(class, zspage); insert_zspage(class, zspage, fullness); @@ -2121,7 +2128,7 @@ static void async_free_zspage(struct work_struct *work) int i; struct size_class *class; unsigned int class_idx; - enum fullness_group fullness; + int fullness; struct zspage *zspage, *tmp; LIST_HEAD(free_pages); struct zs_pool *pool = container_of(work, struct zs_pool, @@ -2133,7 +2140,7 @@ static void async_free_zspage(struct work_struct *work) continue; spin_lock(&pool->lock); - list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages); + list_splice_init(&class->fullness_list[ZS_INUSE_RATIO_0], &free_pages); spin_unlock(&pool->lock); } @@ -2142,7 +2149,7 @@ static void async_free_zspage(struct work_struct *work) lock_zspage(zspage); get_zspage_mapping(zspage, &class_idx, &fullness); - VM_BUG_ON(fullness != ZS_EMPTY); + VM_BUG_ON(fullness != ZS_INUSE_RATIO_0); class = pool->size_class[class_idx]; spin_lock(&pool->lock); #ifdef CONFIG_ZPOOL @@ -2190,8 +2197,8 @@ static inline void zs_flush_migration(struct zs_pool *pool) { } static unsigned long zs_can_compact(struct size_class *class) { unsigned long obj_wasted; - unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED); - unsigned long obj_used = zs_stat_get(class, OBJ_USED); + unsigned long obj_allocated = zs_stat_get(class, ZS_OBJS_ALLOCATED); + unsigned long obj_used = zs_stat_get(class, ZS_OBJS_INUSE); if (obj_allocated <= obj_used) return 0; @@ -2215,7 +2222,7 @@ static unsigned long __zs_compact(struct zs_pool *pool, * as well as zpage allocation/free */ spin_lock(&pool->lock); - while ((src_zspage = isolate_zspage(class, true))) { + while ((src_zspage = isolate_src_zspage(class))) { /* protect someone accessing the zspage(i.e., zs_map_object) */ migrate_write_lock(src_zspage); @@ -2225,7 +2232,7 @@ static unsigned long __zs_compact(struct zs_pool *pool, cc.obj_idx = 0; cc.s_page = get_first_page(src_zspage); - while ((dst_zspage = isolate_zspage(class, false))) { + while ((dst_zspage = isolate_dst_zspage(class))) { migrate_write_lock_nested(dst_zspage); cc.d_page = get_first_page(dst_zspage); @@ -2250,7 +2257,7 @@ static unsigned long __zs_compact(struct zs_pool *pool, putback_zspage(class, dst_zspage); migrate_write_unlock(dst_zspage); - if (putback_zspage(class, src_zspage) == ZS_EMPTY) { + 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; @@ -2408,7 +2415,7 @@ struct zs_pool *zs_create_pool(const char *name) int pages_per_zspage; int objs_per_zspage; struct size_class *class; - int fullness = 0; + int fullness; size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA; if (size > ZS_MAX_ALLOC_SIZE) @@ -2462,9 +2469,12 @@ struct zs_pool *zs_create_pool(const char *name) class->pages_per_zspage = pages_per_zspage; class->objs_per_zspage = objs_per_zspage; pool->size_class[i] = class; - for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS; - fullness++) + + fullness = ZS_INUSE_RATIO_0; + while (fullness < NR_FULLNESS_GROUPS) { INIT_LIST_HEAD(&class->fullness_list[fullness]); + fullness++; + } prev_class = class; } @@ -2510,11 +2520,12 @@ void zs_destroy_pool(struct zs_pool *pool) if (class->index != i) continue; - for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) { - if (!list_empty(&class->fullness_list[fg])) { - pr_info("Freeing non-empty class with size %db, fullness group %d\n", - class->size, fg); - } + for (fg = ZS_INUSE_RATIO_0; fg < NR_FULLNESS_GROUPS; fg++) { + if (list_empty(&class->fullness_list[fg])) + continue; + + pr_err("Class-%d fullness group %d is not empty\n", + class->size, fg); } kfree(class); } @@ -2616,7 +2627,7 @@ static int zs_reclaim_page(struct zs_pool *pool, unsigned int retries) unsigned long handle; struct zspage *zspage; struct page *page; - enum fullness_group fullness; + int fullness; /* Lock LRU and fullness list */ spin_lock(&pool->lock); @@ -2686,7 +2697,7 @@ static int zs_reclaim_page(struct zs_pool *pool, unsigned int retries) * while the page is removed from the pool. Fix it * up for the check in __free_zspage(). */ - zspage->fullness = ZS_EMPTY; + zspage->fullness = ZS_INUSE_RATIO_0; __free_zspage(pool, class, zspage); spin_unlock(&pool->lock); 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; From patchwork Fri Mar 3 07:31:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13158390 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 6B9E2C678D4 for ; Fri, 3 Mar 2023 07:31:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04BB86B0083; Fri, 3 Mar 2023 02:31:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3EBC6B0085; Fri, 3 Mar 2023 02:31:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDED36B0087; Fri, 3 Mar 2023 02:31:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CF8E56B0083 for ; Fri, 3 Mar 2023 02:31:51 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 96835A1176 for ; Fri, 3 Mar 2023 07:31:51 +0000 (UTC) X-FDA: 80526767622.15.BC2B5A1 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf22.hostedemail.com (Postfix) with ESMTP id BBF09C0002 for ; Fri, 3 Mar 2023 07:31:49 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=kDqZjoYf; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf22.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.215.173 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677828709; 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=3mmQBxcW7ejWxn4u/9kaDGpuSUOVrknjbsls0Wrd7oQ=; b=TZvgyUdK5EB1UWWLsFNGyidBQwHZa/Iy11xK9+bW7+QL9JXAaz7F4g8SnlVbLdgXDpeaMZ wxJC19pZN6+7eBV6c2uAd1uIQkEB9EXl3AkWeEzGZSO8dLth+GFU1wKqcfIPxifChec7XS /nM1K1VKBuhjMweJH+PjKoOMlRf22sw= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=kDqZjoYf; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf22.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.215.173 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677828709; a=rsa-sha256; cv=none; b=kGKFoxFV1Zb4eX8el27oN9sJXEPsXxsreFTqhZt3kDeKZQ6kisyCaWeBVboO/TyVyvyAeL yUPr+jSxmhb1Yp74x79jPCaC6EZpYb1v3FnOdREtDEdHZ4oOMqoqFjbPjJykBjRNZYuEKE AIvCGlfIq6S6oBKKrPWG83SUeud/Ik0= Received: by mail-pg1-f173.google.com with SMTP id bn17so924784pgb.10 for ; Thu, 02 Mar 2023 23:31:49 -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=3mmQBxcW7ejWxn4u/9kaDGpuSUOVrknjbsls0Wrd7oQ=; b=kDqZjoYfdctRAdTqU0bk8va0TLut3cg+xNPzeKMEXmxSfnkUW0t8MPd7IZbuldVC2q FOlOZCbIVtUpKCU9LNrNP5wk0pFFIqdWENvaSq2cZ/4I5/vlAfa5bgI5MVDzHhkQ7D1+ ZvuShnw/whNxLNAM78sHeZNXh9Zc2UpXJzE58= 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=3mmQBxcW7ejWxn4u/9kaDGpuSUOVrknjbsls0Wrd7oQ=; b=4FxpVelJ9pcsR5cifSaOqd6LrDWUkvYdLoAHZJ2n8SfwwMRKvX7GYsrUSFmnsmrGSk V+vKB8QqsUFCi3H+cqrMyN3LCpOGfGgMSsgrDm8qBQULORwr0YocF8r2yJ1Bajb0WJcs /eeE8mQwDbq1m0PriyqB6qINg7kUoFQ5HW0Q9uA9qXIq2O8TcN165YIqJuYyP4R6gK0t yMiXLxjPfuh1UtSf5gRtVyD/iynt5KjmnQ0TtFWvXl06r9s1UubdrLKa2KW7yv83CZbo 2OupeoTa8g6m+6t77M11afBHhNjYtOBmGoQ0Snp4jHeRvbb200AyXj+IqWlu6MPgUKlc q0SQ== X-Gm-Message-State: AO0yUKVSHP2mDWHUIj/fR9/RtTedqLWr/PBBSCHfe9e2RqqEWXTb3kZv 2EtwhV3046+6KC2P7BrdoDhC9tcJt7+O11YH X-Google-Smtp-Source: AK7set95IKXsbDSABhjpYLGQU6qZ7FB8tuGqKxll1zoDEf9YglRFwASYw4TOOIRNu/lNrenPkdhAzA== X-Received: by 2002:a62:7b45:0:b0:5a9:b4eb:d262 with SMTP id w66-20020a627b45000000b005a9b4ebd262mr1432036pfc.1.1677828708593; Thu, 02 Mar 2023 23:31:48 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 23:31:48 -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 4/4] zsmalloc: show per fullness group class stats Date: Fri, 3 Mar 2023 16:31:30 +0900 Message-Id: <20230303073130.1950714-5-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-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: BBF09C0002 X-Stat-Signature: 3c89rwsrxic8bdywarqhh459j1etaw5u X-HE-Tag: 1677828709-503234 X-HE-Meta: U2FsdGVkX19aKuBT0lfweFckEThQH8Uk5cOgXTFE74x8QmLSOkBoeF9mddZC7DcXrvYXJmDYVmWYpk4QfYRngIArbQM8Y/ra9iHFddkTAGuQBi8el5gGNMYAKpSJ2Ihi/VLZwsjtj+b6JXJPAeC9BL8htoPYqDTgyyEyfnGC2Sit01CXNUk8X28E2ArBc4D2MEoqCmYW6HatySeg0Zzs/I15wdmmgsbhenqYhh182nQfH7Z1XRa2g2XvcX5Roeq+Ct2UMBChmahveopLJ4ljUH3hm23qZ3UGY4Xl+/ygqBisWOmzk4q0rC8NmlDakjrQkpCF34vmU0w93+Pmfb2xEJb9vp/Vr1ALEah0LIhFDSfyNSVqY9t0BqyLEiIIrIG25KBaKmZMVXSM5XFhsnslfvV9cO4AVqNHjRPzX+Kpn+3OhRZaaOARBMuq+9rjjnwnOf6TlkrmBFTpEgiHTBP2NUQ7sjVs2Kya2DVfSP9I3XzKjv2jVx/vdCzrxrDnaXOoChsTo3f5YF6XmXxpyWLR9sV6klGuMCXOToHOnY3Li1dTiShsk3I5qDGIEEumZ1GMttgBusMzVr9Pq/5B4voaOuQYZ+YvttApXZoMZeFmEUxPI4g4f9DPOc4PhO2Mx+0WpoCpgA5uSaMkiNt68UkX6LOGIfCJCEBzVU6g85fMS3rjQF7hB1C8bwWjRHoW6hSO9lGklzB7hK/x3Fd4FymRkXcXvFavHOEcVmOwnVlJAIFn2i4hjwvjp3bsSDw2IEi85RgzuqomQVqwPl+kS4kgQOxnE6rFU/4TqNf9U3JoEczdh90xRS6/Fa3DrbKHPR1o/94Q4i9rcCIG3JJY77PCeSMbiP29obPgLk2JPROZncpfXskBFu4bGHdCZLdKStj94A0CkQeCxdEPHDz2Ogj4ui151/7VMF/6yJixDraT30asjWbC4C/Q+9VABOn6fEimuzItVA0PVQGRDDnhQ6W e9DlqmRL k7w2pmLJqxJx7uR9J6zhZ+jSFm2uim0cxioAP7V2XzBfskpa1ZPfBkvALEwAqPS7bFYNM30JFZw6mGYIXC93cvd8CFMwJB3JYHMJ9Z7UUer9QGJ2S7FK3olrSIuPJsLwBpzKdoqrzA57pFZ6BTevy2kwqyfh0nwCA5IfhgRuoW3yGLVWfKmQ3hcn+Y4s54Q2pUoEMbssQow5c5LtwsHCRwiecxfoR7AP/+ZoH4hDHWdjXbxaJFkDcJlQuFYk1qJEUCR6QrvIfKCz/BuHeHKljZULCS2bWZlJs8hQY2MB2xAbIbVpTQd68BjTJ7AZFNIegQpFHB46cvxdQpjf06KLMIGhXIUL8tWnyhBiRvATXAf3Qb7+iRycDZCBtOCfhxUg6CQJi0LPI5C0El3sSy9xRlu6QTw== 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: We keep the old fullness (3/4 threshold) reporting in zs_stats_size_show(). Switch from allmost full/empty stats to fine-grained per inuse ratio (fullness group) reporting, which gives signicantly more data on classes fragmentation. Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 141a3f69b642..5ee9a76b3446 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -172,9 +172,7 @@ enum fullness_group { ZS_INUSE_RATIO_0, ZS_INUSE_RATIO_10, - /* NOTE: 5 more fullness groups here */ - ZS_INUSE_RATIO_70 = 7, - /* NOTE: 2 more fullness groups here */ + /* NOTE: 8 more fullness groups here */ ZS_INUSE_RATIO_99 = 10, ZS_INUSE_RATIO_100, NR_FULLNESS_GROUPS, @@ -625,19 +623,19 @@ static int zs_stats_size_show(struct seq_file *s, void *v) struct zs_pool *pool = s->private; struct size_class *class; int objs_per_zspage; - unsigned long class_almost_full, class_almost_empty; unsigned long obj_allocated, obj_used, pages_used, freeable; - unsigned long total_class_almost_full = 0, total_class_almost_empty = 0; unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0; unsigned long total_freeable = 0; + unsigned long inuse_totals[NR_FULLNESS_GROUPS] = {0, }; + int fg; - seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n", - "class", "size", "almost_full", "almost_empty", + seq_printf(s, " %5s %5s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s %13s %10s %10s %16s %8s\n", + "class", "size", "10%", "20%", "30%", "40%", + "50%", "60%", "70%", "80%", "90%", "99%", "100%", "obj_allocated", "obj_used", "pages_used", "pages_per_zspage", "freeable"); for (i = 0; i < ZS_SIZE_CLASSES; i++) { - int fg; class = pool->size_class[i]; @@ -646,14 +644,12 @@ static int zs_stats_size_show(struct seq_file *s, void *v) spin_lock(&pool->lock); - /* - * Replecate old behaviour for almost_full and almost_empty - * stats. - */ - for (fg = ZS_INUSE_RATIO_70; fg <= ZS_INUSE_RATIO_99; fg++) - class_almost_full = zs_stat_get(class, fg); - for (fg = ZS_INUSE_RATIO_10; fg < ZS_INUSE_RATIO_70; fg++) - class_almost_empty = zs_stat_get(class, fg); + seq_printf(s, " %5u %5u ", i, class->size); + + for (fg = ZS_INUSE_RATIO_10; fg <= ZS_INUSE_RATIO_100; fg++) { + inuse_totals[fg] += zs_stat_get(class, fg); + seq_printf(s, "%8lu ", zs_stat_get(class, fg)); + } obj_allocated = zs_stat_get(class, ZS_OBJS_ALLOCATED); obj_used = zs_stat_get(class, ZS_OBJS_INUSE); @@ -664,14 +660,10 @@ static int zs_stats_size_show(struct seq_file *s, void *v) pages_used = obj_allocated / objs_per_zspage * class->pages_per_zspage; - seq_printf(s, " %5u %5u %11lu %12lu %13lu" - " %10lu %10lu %16d %8lu\n", - i, class->size, class_almost_full, class_almost_empty, - obj_allocated, obj_used, pages_used, - class->pages_per_zspage, freeable); + seq_printf(s, "%13lu %10lu %10lu %16d %8lu\n", + obj_allocated, obj_used, pages_used, + class->pages_per_zspage, freeable); - total_class_almost_full += class_almost_full; - total_class_almost_empty += class_almost_empty; total_objs += obj_allocated; total_used_objs += obj_used; total_pages += pages_used; @@ -679,10 +671,14 @@ static int zs_stats_size_show(struct seq_file *s, void *v) } seq_puts(s, "\n"); - seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n", - "Total", "", total_class_almost_full, - total_class_almost_empty, total_objs, - total_used_objs, total_pages, "", total_freeable); + seq_printf(s, " %5s %5s ", "Total", ""); + + for (fg = ZS_INUSE_RATIO_10; fg <= ZS_INUSE_RATIO_100; fg++) + seq_printf(s, "%8lu ", inuse_totals[fg]); + + seq_printf(s, "%13lu %10lu %10lu %16s %8lu\n", + total_objs, total_used_objs, total_pages, "", + total_freeable); return 0; }