From patchwork Thu Feb 23 03:04:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 60765 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp85881wrd; Wed, 22 Feb 2023 19:05:55 -0800 (PST) X-Google-Smtp-Source: AK7set98FfV1cP76Wr3a2Lqx+OtiPCT6U6JCv3rhxZgkQQXyM1OOyNBphOlx1JkDMYUxzipUw1YA X-Received: by 2002:a17:906:b7cd:b0:8b1:7ae9:647 with SMTP id fy13-20020a170906b7cd00b008b17ae90647mr19419864ejb.76.1677121555515; Wed, 22 Feb 2023 19:05:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677121555; cv=none; d=google.com; s=arc-20160816; b=JD6B5iwcxOECtfgWZ15biNm5cB43NdZU/UT4z6aSucQyFhkHJewLSSrhXXlUkzJWxn 8tfaJxDNHOdPcQtgeplsMlBZW6kp2otpiFxr8j57FUWzakRxE2fR/uoiCd0UXygozveD c8Q8Dd3cGqBtSCmS66p4CrMyk373gF/8ERNj4QYFJiLOxkKfu/ckA7C/G6snIcEwy+fH gsy028Zmj7aT/fwxXTM2P/7UxjAskqxtLy2vsKLZUgAj6lq4pwX6OeUjezMyci4PoFIo Zb9WXnKFKT3zHAg3x7q9SunSQ828iYFSsYd/CFDRnZuDhsPtu7wS3MhSPulojrO+aA1Z MuBA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7w1cKhAAphXJ2vOUap7qolI6gnUUf7d9gByR138SJog=; b=Ho5UwN33MTJJMW5bb7bS3apOGC71rGvZDqYoLyY75ppVNHHUR3ofTNqor7vb+cvOQ8 QcsLK2K0veQiCs5W3VUkKYJ6Lj50nEQakmyKqI8vLUEMq+QmApxa24LqMQb7Wdp/Q96x cS1O45Kgzp63hLaI2PNEAP2pa8vVOO+J98in34yMb0NksTncZTfIegHE/bCa+PH4cpZu y2IhoISxz2XTRR9oq3yzWlCCAKl4qCLq4U/Dv6s+5WXBxUoJZVtfTgeAdk6bXTMsUuLi FjZ/5oAJSqR+iwd9EQBLaeueR5LHcEyRmWBF6EjlKGFol8YndP2j3QNzrslPzxXjVavJ pfwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CmpbZQ5m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o12-20020aa7c50c000000b004aab2fed3e7si2193698edq.275.2023.02.22.19.05.10; Wed, 22 Feb 2023 19:05:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CmpbZQ5m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233233AbjBWDFE (ORCPT + 99 others); Wed, 22 Feb 2023 22:05:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233563AbjBWDFC (ORCPT ); Wed, 22 Feb 2023 22:05:02 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABB77113CD for ; Wed, 22 Feb 2023 19:05:01 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id gi3-20020a17090b110300b0023762f642dcso498770pjb.4 for ; Wed, 22 Feb 2023 19:05:01 -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=7w1cKhAAphXJ2vOUap7qolI6gnUUf7d9gByR138SJog=; b=CmpbZQ5mXsvkYH52sH4fUdQAKboV5mAC+PL5L2EQo9+52dN8FTsKfuwkHN31kEURgt zJmYtdNdQZoFlGbZMbVJxlfCAiCY1teEF6fHg9QB2ulPwRSIVYrc1aMPXJGQEnc5XvNi xpv0MPmkAscsnxtGgUib+qZbsAdfI51mc7cf4= 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=7w1cKhAAphXJ2vOUap7qolI6gnUUf7d9gByR138SJog=; b=M6nc92R02u6p6MpfoEiRLFtaVIfe5kWUD35V+OVKi7KmZOqwo4tP6baWJYZuki7ZOW ca1PfXYbBHtxEs/Y7u5Fi7PFcq6KGWuin5K6LiawyLnomUvXIRX/k5vFuzyA/PvGFoOr Ls7k0qbe7q6VXFOnQBVO8MPOKkGBUoJTFjN6rPkjrUyb1EbaFVLVESyygtUQR1WDY6bZ b8Aj/Z5HuACQT2IN+NYuo99LjsxAftI9Arult8McPOv9WJFhZtWGYl1Qji22jGqdgc4L cgXKqBJQVLcD3U4Luk6krvF8dwmT3Aj3rlOURcoS2VsTFSy9X/u70L+1Cg3WofN6lcSD s6bA== X-Gm-Message-State: AO0yUKXuJpwyw8Yol5v0IbVKqDmY16xxHmagLBVfmorHfGuHfb8Ay+e0 kwdxlG7OMV/NR+NDYFNrZoBzPA== X-Received: by 2002:a17:902:7243:b0:19c:bcbc:a3dc with SMTP id c3-20020a170902724300b0019cbcbca3dcmr685857pll.25.1677121501195; Wed, 22 Feb 2023 19:05:01 -0800 (PST) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:6de2:9e85:b508:57b8]) by smtp.gmail.com with ESMTPSA id jl21-20020a170903135500b0019926c77577sm608520plb.90.2023.02.22.19.04.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 19:05:00 -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: [PATCHv2 1/6] zsmalloc: remove insert_zspage() ->inuse optimization Date: Thu, 23 Feb 2023 12:04:46 +0900 Message-Id: <20230223030451.543162-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog In-Reply-To: <20230223030451.543162-1-senozhatsky@chromium.org> References: <20230223030451.543162-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758589412641620878?= X-GMAIL-MSGID: =?utf-8?q?1758589412641620878?= This optimization has no effect. It only ensures that when a page was added to its corresponding fullness list, its "inuse" counter was higher or lower than the "inuse" counter of the page at the head of the list. The intention was to keep busy pages 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 page can be modified by obj_free() but the page may still belong to the same fullness list. So, fix_fullness_group() won't change the page's position in relation to the head's "inuse" counter, leading to a largely random order of pages within the fullness list. For instance, consider a printout of the "inuse" counters of the first 10 pages 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 page with the lowest "inuse" counter is actually the head of the fullness list. Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 3aed46ab7e6c..b57a89ed6f30 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -753,37 +753,24 @@ 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 *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, - struct zspage *zspage, - enum fullness_group fullness) + struct zspage *zspage, + enum fullness_group fullness) { VM_BUG_ON(list_empty(&class->fullness_list[fullness])); From patchwork Thu Feb 23 03:04:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 60768 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp85946wrd; Wed, 22 Feb 2023 19:06:05 -0800 (PST) X-Google-Smtp-Source: AK7set+GAiShZ9ds0KVsZzPCZZw/f513HWGKEOH5leTkGWwZSbu5iJg+eY0R734nOMNCok5tKXNB X-Received: by 2002:a17:907:9198:b0:860:c12c:14f9 with SMTP id bp24-20020a170907919800b00860c12c14f9mr13356176ejb.40.1677121565273; Wed, 22 Feb 2023 19:06:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677121565; cv=none; d=google.com; s=arc-20160816; b=YvfRa+nzxXQtcCCGwa8zylKG0YnAzDNxnBVoKa8FdI3XNrtrsgqxDILddSE/GPzK1F N8QWqvMgSpb2YhDRTPATc3pzrU730NM69JFFt+IY6B60DW22YQ5k605fVyJ9GWU9WF95 qrFiEVh7hz1xKxPsaTS2PGxLjQfXl4geHLgZiKMN7bY9g8vub34truPNJGY+3jqftJCN r4Mw/GOzPLZbrQBjZZW43CV3zp/J5ikrp2ojMWDdiKJwPQGRCAvrsjx0IVPoNCleRRMn Q9cgp/3x7bM2FsPNwulOwhXUqAmqOvLTlfCb0cYF6rZXUPwQAhZl9WmeEe9+MdqCnNaz lItA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=I+a7RvRhurz1Ai+VZTI1SaXOAjw7CakUvWehQGGNcJ0=; b=FBRCpm79I63nyJlhcQOClfeh0ppq1bMJ2AmXvoQxynJLB9U3uyXoMoaN9w4DavlYrT XLRYukJJvhHhKu5F0Ecwk8MWncif7WKZlx6CPieqaYqwA84+Yq1KhVjOnzFB1TsSy288 vEt6v3Lw3o/MxynEHjcuLtdQhLp4eIJ9bqbE8jjUzs5cHDb0v63tLne91Z7H+IJzGy/1 MnOT7Q7wZa/MXUjFjLcFraVLcCLA3imy9keGmEbNXWtBQAmBhC9hKbSUwpADWPH2GNmz Gcg5Rmru0spjuCvpgCzUpDX8DW7tVyMnIwfc+MT0iV1rgitDLbaJ5OLCPDmPB9EnnIpe NXOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="PVE/woFY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g1-20020a50ee01000000b004ad099e74b5si8364916eds.556.2023.02.22.19.05.42; Wed, 22 Feb 2023 19:06:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="PVE/woFY"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233705AbjBWDFK (ORCPT + 99 others); Wed, 22 Feb 2023 22:05:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233609AbjBWDFG (ORCPT ); Wed, 22 Feb 2023 22:05:06 -0500 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8672F457E9 for ; Wed, 22 Feb 2023 19:05:04 -0800 (PST) Received: by mail-pl1-x62f.google.com with SMTP id i3so819949plg.6 for ; Wed, 22 Feb 2023 19:05:04 -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=I+a7RvRhurz1Ai+VZTI1SaXOAjw7CakUvWehQGGNcJ0=; b=PVE/woFYX/y2Nv/sSWN/ThQ7WpqOXClosC23oGEVbNLw+KQEHwBO6RQseLQLRiTTO8 rQ3VjL8xKc7Wgb/Jl357h+iW+SZvTOhE+otycFTz0/T665RZa055Y/XJ5GzLILgml5tC 2n9hS+yqHu+gRYPQ1oJQZHmz4yUqpgjh5Y0UU= 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=I+a7RvRhurz1Ai+VZTI1SaXOAjw7CakUvWehQGGNcJ0=; b=yLXp8f1bURQ9mQhuqK2qhd1QkTAi3wbKfExCGDkf2sSh0vdhqg4Zpl8teme4eW+ywy QDb0DANUZajXt9/f9n/8W5HyyjzalHaAUDtyAdQciI3IpNcxYyF07Xe00d6M00w0jCZv js923Wwo9Q5UWTa3gj4RZfsj4F8cuXE4lzfh+tOCg6RogtsFezCc+46Mz03sW3+XZB/H NPIhQBwmoOi/2rO79k7YXdIFSzcd9SRVXTT+Ti45u74ipPbdGk9GF/nKv4uq+3JVAq19 hwu90YyaF7b3KxmBCpyOY9rukoRCeYKPJBexhw3lUA54AnWllASEetdHtYBim5DLpNZJ uPFA== X-Gm-Message-State: AO0yUKUrGpBT0lEmoz+MLSy4D3s2KmOnytZBLBg8wyjnddILGkFwcsXV NqwZUVdkbQlsSrDfmtXjJ7QshQ== X-Received: by 2002:a17:902:ecd0:b0:19b:2332:18cb with SMTP id a16-20020a170902ecd000b0019b233218cbmr12943938plh.1.1677121503834; Wed, 22 Feb 2023 19:05:03 -0800 (PST) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:6de2:9e85:b508:57b8]) by smtp.gmail.com with ESMTPSA id jl21-20020a170903135500b0019926c77577sm608520plb.90.2023.02.22.19.05.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 19:05:03 -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: [PATCHv2 2/6] zsmalloc: remove stat and fullness enums Date: Thu, 23 Feb 2023 12:04:47 +0900 Message-Id: <20230223030451.543162-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog In-Reply-To: <20230223030451.543162-1-senozhatsky@chromium.org> References: <20230223030451.543162-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758589422867201741?= X-GMAIL-MSGID: =?utf-8?q?1758589422867201741?= The fullness_group enum is nested (sub-enum) within the class_stat_type enum. zsmalloc requires the values in both enums to match, because zsmalloc passes these values to generic functions, e.g. class_stat_inc() and class_stat_dec(), after casting them to integers. Replace these enums (and enum nesting) and use simple defines instead. Also rename some of zsmalloc stats defines, as they sort of clash with zspage object tags. Suggested-by: Yosry Ahmed Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 104 ++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 59 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index b57a89ed6f30..38ae8963c0eb 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -159,26 +159,18 @@ #define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \ ZS_SIZE_CLASS_DELTA) + 1) -enum fullness_group { - ZS_EMPTY, - ZS_ALMOST_EMPTY, - ZS_ALMOST_FULL, - ZS_FULL, - NR_ZS_FULLNESS, -}; +#define ZS_EMPTY 0 +#define ZS_ALMOST_EMPTY 1 +#define ZS_ALMOST_FULL 2 +#define ZS_FULL 3 +#define ZS_OBJS_ALLOCATED 4 +#define ZS_OBJS_INUSE 5 -enum class_stat_type { - CLASS_EMPTY, - CLASS_ALMOST_EMPTY, - CLASS_ALMOST_FULL, - CLASS_FULL, - OBJ_ALLOCATED, - OBJ_USED, - NR_ZS_STAT_TYPE, -}; +#define NR_ZS_STAT 6 +#define NR_ZS_FULLNESS 4 struct zs_size_stat { - unsigned long objs[NR_ZS_STAT_TYPE]; + unsigned long objs[NR_ZS_STAT]; }; #ifdef CONFIG_ZSMALLOC_STAT @@ -547,8 +539,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 +549,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 +580,20 @@ 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) + 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) + 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) + int type) { return class->stats.objs[type]; } @@ -652,10 +641,10 @@ static int zs_stats_size_show(struct seq_file *s, void *v) 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); + class_almost_full = zs_stat_get(class, ZS_ALMOST_FULL); + class_almost_empty = zs_stat_get(class, ZS_ALMOST_EMPTY); + 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); @@ -731,11 +720,10 @@ static inline void zs_pool_stat_destroy(struct zs_pool *pool) * the pool (not yet implemented). 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 fg; inuse = get_zspage_inuse(zspage); objs_per_zspage = class->objs_per_zspage; @@ -754,11 +742,11 @@ static enum fullness_group get_fullness_group(struct size_class *class, /* * 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 +754,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])); @@ -787,11 +775,10 @@ static void remove_zspage(struct size_class *class, * page from the freelist 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 +951,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); @@ -990,7 +977,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); } @@ -1508,7 +1495,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 +1517,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 +1539,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 +1596,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 +1611,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) { @@ -1828,7 +1815,7 @@ static struct zspage *isolate_zspage(struct size_class *class, bool source) { int i; struct zspage *zspage; - enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL}; + int fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL}; if (!source) { fg[0] = ZS_ALMOST_FULL; @@ -1852,12 +1839,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 +2107,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, @@ -2190,8 +2176,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; @@ -2616,7 +2602,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); From patchwork Thu Feb 23 03:04:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 60767 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp85900wrd; Wed, 22 Feb 2023 19:05:57 -0800 (PST) X-Google-Smtp-Source: AK7set9aXjfqHiILsTAbyCsTACd/BtcsI1+hhOOl3cetGdGX9S5XzWUJD5SHp13BGoCYdasX3RQE X-Received: by 2002:a17:906:8258:b0:8ae:b008:9b5a with SMTP id f24-20020a170906825800b008aeb0089b5amr17272991ejx.69.1677121557693; Wed, 22 Feb 2023 19:05:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677121557; cv=none; d=google.com; s=arc-20160816; b=SZshdO8TDKgPwp8n5Dx8cjdN0HXjlqCsTCvgFihJGebgfFqbj8wlc1TOpuQIVsOHCY 4vQ2GsmeC6YngVrPSFsTKGGyIQWaz0HBW9bCXcz/riA/3V1dCGoTa8ljUnWBaXkg1ZpJ V59vU1OL8H9JcXag9wrUGASVrBFYoUYWAijp7fgOAHhkuhYsrF6XI8ITMTWF3xxyR5bZ lmxam00wfEZYqsYIBWxgUYwdQthTbJEXsPFufsNuYB3+Teg+wLnqJVSB3WXW0fS5G5Xh KWB4aGVYU3wgboT/BY9xFOuLxJJ/J8s9bf9d7xKfeF8MY/AmIMrHDaiWIMmI/UdKjH49 vEzA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8+oVIrjvupRwrVwXlg7PumbsuiNQvLcBtQYCdoukFrI=; b=M/xTqYKRkWEElz/96WdHbp3IPCM+B+0+8Xm52J9SqY/sQsq+eMRtNvr1JzR3cxeSaN BEMXr8gUe139nFdCK4fTv4kR4xjPSm4vJN8XBKgOjoHVJRR53mlNAj7AMS48cZbD72aO 27/p4+5LPzeQB5l57RycuMHWCXqMaFG+QKY2O+KMnXeSGX2KaEt8hgN32zx/9oSjMUHU Eeexm1jDpDTB5aKsrrcSWcim+qZ0ti8a2NKiLtCYm/vs7iQAdmYAEnxxvfiJXifI1OQX 65ASfm0IffUHrUJMF7G81H/QH71d8arohOhWlv97f93zO8Z7Z696Ou4O21nmfaoCQVgb XS3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RqUAZ7Nv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gc33-20020a1709072b2100b008e6279ea50bsi4173332ejc.932.2023.02.22.19.05.28; Wed, 22 Feb 2023 19:05:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=RqUAZ7Nv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233876AbjBWDFM (ORCPT + 99 others); Wed, 22 Feb 2023 22:05:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233778AbjBWDFK (ORCPT ); Wed, 22 Feb 2023 22:05:10 -0500 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CA56460A5 for ; Wed, 22 Feb 2023 19:05:07 -0800 (PST) Received: by mail-pl1-x62a.google.com with SMTP id c1so11510932plg.4 for ; Wed, 22 Feb 2023 19:05:07 -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=8+oVIrjvupRwrVwXlg7PumbsuiNQvLcBtQYCdoukFrI=; b=RqUAZ7NvMZZunMUB/MmiD0jYODuJNYZpUHSO0ZTUtA0mNcmpTGprRdyBcM13rR+YkV BqEL4V2jbOwYKfcCfOjCE6vYdJMPvmebObR8Rn82FOYHiwGm/nz48QVNQY69a7MM+Xgi jsEu9m1WWpq9GBL2stEA0/m3bW+gDboZWkQZQ= 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=8+oVIrjvupRwrVwXlg7PumbsuiNQvLcBtQYCdoukFrI=; b=w14Rdm31efB7q77mNqsNM9JaSYFnPvW1Nom5lMyuKxp5GMTW51ysNk+KNLkGCdh2db WmONSIc2arvrPBjQ1OsrbQjRr1ku3LtJ+5i6GNN1rGTkZBr//LRWLldle7xTAP84QvEw ZPBHTWrErHHbs+VOVESW3ReGWerOHWrvNcL98Dw1kkb8u3z9yk+G0X+D6ut7Kipwjizt Rv+7TVOj5nANwryknonHWDb7ovIG3FVx09nyWSZvwn5oFk7z9zTVR4IzElwHf8kxnR5l Yj4dWtcLKYHdyCtV7dMOm6s4Ry9EAms/njpJKf2JIRvzjlaTb7EDFNcrAtZeRHKVWnBe NtxA== X-Gm-Message-State: AO0yUKUvi8RsUmPtvRvShvRRzaM2gZLExMD0wDZQ2V1fgsEbOaPMZOmV sDjJJSUC6zyAAVLsNM2Da72lu1Mtps+UOa9V X-Received: by 2002:a17:902:ea07:b0:19a:887d:98ac with SMTP id s7-20020a170902ea0700b0019a887d98acmr13433066plg.46.1677121506725; Wed, 22 Feb 2023 19:05:06 -0800 (PST) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:6de2:9e85:b508:57b8]) by smtp.gmail.com with ESMTPSA id jl21-20020a170903135500b0019926c77577sm608520plb.90.2023.02.22.19.05.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 19:05:06 -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: [PATCHv2 3/6] zsmalloc: fine-grained inuse ratio based fullness grouping Date: Thu, 23 Feb 2023 12:04:48 +0900 Message-Id: <20230223030451.543162-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog In-Reply-To: <20230223030451.543162-1-senozhatsky@chromium.org> References: <20230223030451.543162-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758589414310538316?= X-GMAIL-MSGID: =?utf-8?q?1758589414310538316?= Each zspage maintains ->inuse counter which keeps track of the number of objects stored in the page. The ->inuse counter also determines the page's "fullness group" which is calculated as the ratio of the "inuse" objects to the total number of objects the page 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 pages 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 page from the corresponding fullness list as the best candidate page. However, this assumption is not always accurate. For the zs_malloc() operation, the optimal candidate page should have the highest ->inuse counter. This is because the goal is to maximize the number of ZS_FULL pages and make full use of all allocated memory. For the zs_compact() operation, the optimal candidate page 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 page. 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 pages 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 pages with 0%, 10%, 20% usage ratios, and so on, up to 100%. This enhances the selection of candidate pages for both zs_malloc() and zs_compact(). A printout of the ->inuse counters of the first 7 pages 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 page 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 page is selected, and then the function moves downward to find a destination page in the group with the lowest internal fragmentation (highest usage ratio). Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 183 +++++++++++++++++++++++++++++--------------------- 1 file changed, 107 insertions(+), 76 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 38ae8963c0eb..1a92ebe338eb 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,15 +159,33 @@ #define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \ ZS_SIZE_CLASS_DELTA) + 1) -#define ZS_EMPTY 0 -#define ZS_ALMOST_EMPTY 1 -#define ZS_ALMOST_FULL 2 -#define ZS_FULL 3 -#define ZS_OBJS_ALLOCATED 4 -#define ZS_OBJS_INUSE 5 - -#define NR_ZS_STAT 6 -#define NR_ZS_FULLNESS 4 +/* + * 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_30 means that the ratio of used objects is > 20% and <= 30%. + * + * 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. + */ +#define ZS_INUSE_RATIO_0 0 +#define ZS_INUSE_RATIO_10 1 +#define ZS_INUSE_RATIO_20 2 +#define ZS_INUSE_RATIO_30 3 +#define ZS_INUSE_RATIO_40 4 +#define ZS_INUSE_RATIO_50 5 +#define ZS_INUSE_RATIO_60 6 +#define ZS_INUSE_RATIO_70 7 +#define ZS_INUSE_RATIO_80 8 +#define ZS_INUSE_RATIO_90 9 +#define ZS_INUSE_RATIO_99 10 +#define ZS_INUSE_RATIO_100 11 +#define ZS_OBJS_ALLOCATED 12 +#define ZS_OBJS_INUSE 13 + +#define NR_ZS_INUSE_RATIO 12 +#define NR_ZS_STAT 14 struct zs_size_stat { unsigned long objs[NR_ZS_STAT]; @@ -177,25 +195,10 @@ struct zs_size_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_ZS_INUSE_RATIO]; /* * Size of objects stored in this class. Must be multiple * of ZS_ALIGN. @@ -641,8 +644,23 @@ static int zs_stats_size_show(struct seq_file *s, void *v) continue; spin_lock(&pool->lock); - class_almost_full = zs_stat_get(class, ZS_ALMOST_FULL); - class_almost_empty = zs_stat_get(class, ZS_ALMOST_EMPTY); + + /* + * Replecate old behaviour for almost_full and almost_empty + * stats. + */ + class_almost_full = zs_stat_get(class, ZS_INUSE_RATIO_99); + class_almost_full += zs_stat_get(class, ZS_INUSE_RATIO_90); + class_almost_full += zs_stat_get(class, ZS_INUSE_RATIO_80); + class_almost_full += zs_stat_get(class, ZS_INUSE_RATIO_70); + + class_almost_empty = zs_stat_get(class, ZS_INUSE_RATIO_60); + class_almost_empty += zs_stat_get(class, ZS_INUSE_RATIO_50); + class_almost_empty += zs_stat_get(class, ZS_INUSE_RATIO_40); + class_almost_empty += zs_stat_get(class, ZS_INUSE_RATIO_30); + class_almost_empty += zs_stat_get(class, ZS_INUSE_RATIO_20); + class_almost_empty += zs_stat_get(class, ZS_INUSE_RATIO_10); + obj_allocated = zs_stat_get(class, ZS_OBJS_ALLOCATED); obj_used = zs_stat_get(class, ZS_OBJS_INUSE); freeable = zs_can_compact(class); @@ -712,32 +730,30 @@ 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 int get_fullness_group(struct size_class *class, struct zspage *zspage) { - int inuse, objs_per_zspage; - int 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; } /* @@ -769,11 +785,11 @@ 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 int fix_fullness_group(struct size_class *class, struct zspage *zspage) { @@ -959,7 +975,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); @@ -998,7 +1014,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 @@ -1134,9 +1150,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; } @@ -1629,7 +1645,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); @@ -1811,22 +1827,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; - int 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; } } @@ -2119,7 +2146,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); } @@ -2128,7 +2155,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 @@ -2201,7 +2228,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); @@ -2211,7 +2238,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); @@ -2236,7 +2263,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; @@ -2394,7 +2421,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) @@ -2448,9 +2475,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_ZS_INUSE_RATIO) { INIT_LIST_HEAD(&class->fullness_list[fullness]); + fullness++; + } prev_class = class; } @@ -2496,11 +2526,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_ZS_INUSE_RATIO; 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); } @@ -2672,7 +2703,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 Thu Feb 23 03:04:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 60766 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp85888wrd; Wed, 22 Feb 2023 19:05:56 -0800 (PST) X-Google-Smtp-Source: AK7set9ckq1/KgJ2T3D0ZtaaIbe/ybQn9M3jgtPEw+72fmKecKW3vFW5jYttgA6MITlNj/bsXCH7 X-Received: by 2002:a17:906:2e89:b0:8b1:383d:cd50 with SMTP id o9-20020a1709062e8900b008b1383dcd50mr19529517eji.48.1677121556429; Wed, 22 Feb 2023 19:05:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677121556; cv=none; d=google.com; s=arc-20160816; b=jOuhItjcf1W4Jsip7PZQMniCT3rxtWDDFv0Lxgjv1Q22E1xbhxMEiaFqR5WNUwSb0q j7MdVn++FWlfRKSLBUFjNrrH+oCXme2tthzcLmm4kXgOD/HKN5UP1yNhhJfjDqGTUbAX iy07cz9ylXn5z0gAkvo8QDoXmkt5zBfCEJYmiLpb8i1tiSC4yLuikksiz0ZHeKkVUKDw Eavn+En/k/SYXL2Ps1SiH42LCsTjdNVEPPN4wzp78pRmnqodMEPZ++G5yfu1sJ4uT/HM TyvsReHuTcsm6lPTIawYAyo+z57mQiXSPwDnRYpUvMy75ddvC9w6V8MVT2xCu7VzYIqF emrQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GiCIpL03JBPiX3r0aUEMNZiww2WahzTmtx5CHsJtG58=; b=xmb9M58M4f6KVcy2C4Bd/3bhUbUGE2/360cyneKZthfP6TcK699UTmwHDYN/Wpb1yh TaZ6HrtCsR0vzkKA/gn1b3yLUnz0x0ttVoMBn++Wi8Lor6Yq/3f6eiHrgYa/gFPOdYTs Hq7WdW3EHF6iXkmbLRd0MIjpjrBaZuImq552HebaqevwrBwdtF0tDVlkqqzO4LR+YFzi RuvUoXu7/3p1isTsP/oumsUHKD15FkHC9m91bcf0LoJuExZaZBXjK7UPGmH/9tpFsQYP Q3I6F0vBsC8A/smHvSfAc+R+YUJKQPrfn+n1cCdFVld9OezWN6e/GNn3kWujrq58z8Oj 645w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FukfwasN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d22-20020a17090692d600b008cce79ba44esi12543122ejx.641.2023.02.22.19.05.33; Wed, 22 Feb 2023 19:05:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=FukfwasN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233598AbjBWDFR (ORCPT + 99 others); Wed, 22 Feb 2023 22:05:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233863AbjBWDFL (ORCPT ); Wed, 22 Feb 2023 22:05:11 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33ACD4608A for ; Wed, 22 Feb 2023 19:05:10 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id il18-20020a17090b165200b0023127b2d602so10605909pjb.2 for ; Wed, 22 Feb 2023 19:05:10 -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=GiCIpL03JBPiX3r0aUEMNZiww2WahzTmtx5CHsJtG58=; b=FukfwasNWu2Rzz92i5So8JtB90Utw+Izt47hiVM88qDg1BDIfH+2JocdZt1RMXn+V/ 4dm6ceSVu9mc/bdHNTCeWGCNCPyvnKlp70VGAgY9rIQj+GssKAweQyHmVuNintyLSZId fBGzg7ROy/QxWOcEJqobjmpM/xp/ZJgkisuco= 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=GiCIpL03JBPiX3r0aUEMNZiww2WahzTmtx5CHsJtG58=; b=7Lyxr/U7UABOniLA0EmZhxTIiyoziUUQSku6lGsBs763jorhg4fWr1H3BD0lh4ye7/ WQThZS3bEbGfRY4neg1g2HynFzC5/dXQUdjgqiHpVV/BdJZj9jDcYLIg0X1OfVr48N8P QriE6n+MkBnoBZn/KOKzMWeOtwzyYcgpCkLIGBaEen/0QhiSDrWgd9k8HNSnHl/QpB/a GaYcA0lXmXpsvSvuluriw0CZn5CmTlQESNvxfC4nsb1sj1bYGOEEV0YIiKiieGevqVcL BdvraAUkWfBuVig1Eo6KBC9dmW42+MpcAih5G4ZkxvNnMLQ08BcY24b+nIyCwrjdab/f merQ== X-Gm-Message-State: AO0yUKXdMR0u3Kq4pHWPCRUeW0cyGGNL7ngTF6kFXMRtZCEhr+XczmRF u9YARJJTVncLY8lx8t61Cyzd9vmEEgyGLw20 X-Received: by 2002:a17:903:284:b0:19a:9797:1631 with SMTP id j4-20020a170903028400b0019a97971631mr10850314plr.3.1677121509605; Wed, 22 Feb 2023 19:05:09 -0800 (PST) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:6de2:9e85:b508:57b8]) by smtp.gmail.com with ESMTPSA id jl21-20020a170903135500b0019926c77577sm608520plb.90.2023.02.22.19.05.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 19:05:09 -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: [PATCHv2 4/6] zsmalloc: rework compaction algorithm Date: Thu, 23 Feb 2023 12:04:49 +0900 Message-Id: <20230223030451.543162-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog In-Reply-To: <20230223030451.543162-1-senozhatsky@chromium.org> References: <20230223030451.543162-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758589412793137240?= X-GMAIL-MSGID: =?utf-8?q?1758589412793137240?= 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. Signed-off-by: Sergey Senozhatsky --- mm/zsmalloc.c | 82 ++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 1a92ebe338eb..eacf9e32da5c 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1786,15 +1786,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); @@ -1807,10 +1806,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); @@ -1823,8 +1820,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) @@ -2228,57 +2223,56 @@ 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); - - if (!zs_can_compact(class)) - break; - - cc.obj_idx = 0; - cc.s_page = get_first_page(src_zspage); - - while ((dst_zspage = isolate_dst_zspage(class))) { - migrate_write_lock_nested(dst_zspage); - + while (1) { + if (!dst_zspage) { + dst_zspage = isolate_dst_zspage(class); + if (!dst_zspage) + goto out; + migrate_write_lock(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 (!zs_can_compact(class)) { putback_zspage(class, dst_zspage); migrate_write_unlock(dst_zspage); - dst_zspage = NULL; - if (spin_is_contended(&pool->lock)) - break; + goto out; } - /* Stop if we couldn't find slot */ - if (dst_zspage == NULL) - break; + src_zspage = isolate_src_zspage(class); + if (!src_zspage) { + putback_zspage(class, dst_zspage); + migrate_write_unlock(dst_zspage); + goto out; + } - putback_zspage(class, dst_zspage); - migrate_write_unlock(dst_zspage); + migrate_write_lock_nested(src_zspage); + + cc.obj_idx = 0; + cc.s_page = get_first_page(src_zspage); + migrate_zspage(pool, class, &cc); 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 + } else { migrate_write_unlock(src_zspage); - spin_unlock(&pool->lock); - cond_resched(); - spin_lock(&pool->lock); - } + } - if (src_zspage) { - putback_zspage(class, src_zspage); - migrate_write_unlock(src_zspage); - } + 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 (!dst_zspage) { + spin_unlock(&pool->lock); + cond_resched(); + spin_lock(&pool->lock); + } + } +out: spin_unlock(&pool->lock); return pages_freed; From patchwork Thu Feb 23 03:04:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 60769 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp85965wrd; Wed, 22 Feb 2023 19:06:08 -0800 (PST) X-Google-Smtp-Source: AK7set89vwwthuNF1cuw2uyn4nAyS+BtY2LEn/kyazGRsVTsX9PFC40saox1gs16n7vkK/X0Fgdw X-Received: by 2002:a17:906:8601:b0:89c:d072:e33e with SMTP id o1-20020a170906860100b0089cd072e33emr16384981ejx.49.1677121567856; Wed, 22 Feb 2023 19:06:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677121567; cv=none; d=google.com; s=arc-20160816; b=MAsolrddTztv1ImVznlkC/CNzhjVnizcoVJzh6wHXJs8GcUNv4WhSoQboCdKIYAuEd zktp9qNnL9KlTRgvnizTHp6zQXhvgA1Q65ekTzBnwMbKiV+YzMEWYHJX4UgHRVewRjUL Kf0NHdE+PFghm8/1/az0Rp51UfJc23hvikt2PpFcg/offH2fasOYJhpT2Obc4SJ3nR4P Q/7jo7HbdfYuOb+3oqOxlI5+REUicJg7fmIslFFyYFwQLZjPWsZpyU3SjTkpV7ZoSusg ZQt3pnXoTdTUwey/RTdyXOxbEmVwyMKmDynRorrgIDvbpQFYZU8jV+XY+XIza0n3mMk4 d5DQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BpGG/okMdj5ni9xegYLzCx8QuNxJjUnFEqNX1SXn5aY=; b=ehobV1HrRisMRVzCIwgZTyGsmIPGbsHvn/jzZbRDGs4ii/lpVd5yJ8RJEdMa93aU8p cu3he4xrgnrEgL/X6N/1OTnk0xz2aD0HKhP0hJhQCdR9rnd/3Jt9iqeKnJaCiStWlEzl hhlI8euGcVCYhHI4A6e2BZ/smOAABaSyKewWzoP8hztf78qESI5rOtXNyTaNHSF/Fj3H r5/FDNP4rC/ldUdR/T6QlFlW2N3LgItXZsqCGwYygow5fLDWN+EsdGK31/lLQrGe9BPw m0sAXk7g9tKPDSCTS5gHP61MQq6a5/AjUO2vSBZoSRwlJ13b8BqMlHQGFyDA8ldXlKtg dSlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eXWiDf6C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v23-20020a17090651d700b008b17ac6b0fbsi22328609ejk.512.2023.02.22.19.05.44; Wed, 22 Feb 2023 19:06:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eXWiDf6C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233044AbjBWDFc (ORCPT + 99 others); Wed, 22 Feb 2023 22:05:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233883AbjBWDFS (ORCPT ); Wed, 22 Feb 2023 22:05:18 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A00B34740C for ; Wed, 22 Feb 2023 19:05:12 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id i10so2359460plr.9 for ; Wed, 22 Feb 2023 19:05:12 -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=BpGG/okMdj5ni9xegYLzCx8QuNxJjUnFEqNX1SXn5aY=; b=eXWiDf6CYI70E3qaKKdmQKpYXcXl5mJcHwRXnE9PqLSpLMIBznVHG6rsd6Yik0X/kL 7XBQaZKnWMaK+ZCn7rGTemi+XWQrhgo9ktJrz62HQnL1ZI8wqWskCMnCOUoRKkFs/oq2 x5q7YV9lJ90uWhY6HMhvkqjtbT42l++wXe/Sg= 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=BpGG/okMdj5ni9xegYLzCx8QuNxJjUnFEqNX1SXn5aY=; b=K31aiZZT3KDW4nh5Yb11W7XXuBgF+3+vkIbnAz9Dx4Fxb45JIrMPSV/4C03YMaKGJj czrS/mEmu4ZAHuulthxubrCSB3Esnv18VYybS4DFPGETw9mAH2wyQiYvglGN1Xhowm3t Zm6cOpay9J1N0uWzjCSANdZCvFQwu3CpejLaUJ+uYxrwQjbh5bwKgYeRSKLLiRIXBeD2 1YJijpnD2UJZJ7SkIm44yQ8g0sZ916+5nqfMLTSqODBL75vrd/SeweFPrBPDSrGpcrAt fy44QvE5NO/yQf6OZt+fwpvdA6UolxGU21f/e7TkJ8zi896HuooS6iwYLK+b8jwZ27jL ktbw== X-Gm-Message-State: AO0yUKWrTDw1LVGi0XyO4woD59knjuxIBCvvmiHPr3mpLph7MEHk2l2v uTi3awTVq7qnk8NQ2S6S9VkNLGiwBdsnFQ7M X-Received: by 2002:a17:902:e5c9:b0:19c:355c:6eb5 with SMTP id u9-20020a170902e5c900b0019c355c6eb5mr14014208plf.30.1677121512118; Wed, 22 Feb 2023 19:05:12 -0800 (PST) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:6de2:9e85:b508:57b8]) by smtp.gmail.com with ESMTPSA id jl21-20020a170903135500b0019926c77577sm608520plb.90.2023.02.22.19.05.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 19:05:11 -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: [PATCHv2 5/6] zsmalloc: extend compaction statistics Date: Thu, 23 Feb 2023 12:04:50 +0900 Message-Id: <20230223030451.543162-6-senozhatsky@chromium.org> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog In-Reply-To: <20230223030451.543162-1-senozhatsky@chromium.org> References: <20230223030451.543162-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758589425242190925?= X-GMAIL-MSGID: =?utf-8?q?1758589425242190925?= Extend zsmalloc zs_pool_stats with a new member that holds the number of objects pool compaction moved between pool pages. Signed-off-by: Sergey Senozhatsky --- include/linux/zsmalloc.h | 2 ++ mm/zsmalloc.c | 1 + 2 files changed, 3 insertions(+) diff --git a/include/linux/zsmalloc.h b/include/linux/zsmalloc.h index a48cd0ffe57d..8b3fa5b4a68c 100644 --- a/include/linux/zsmalloc.h +++ b/include/linux/zsmalloc.h @@ -36,6 +36,8 @@ enum zs_mapmode { struct zs_pool_stats { /* How many pages were migrated (freed) */ atomic_long_t pages_compacted; + /* How many objects were migrated during compaction */ + atomic_long_t objs_moved; }; struct zs_pool; diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index eacf9e32da5c..f7e69df48fb0 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1815,6 +1815,7 @@ static void migrate_zspage(struct zs_pool *pool, struct size_class *class, obj_idx++; record_obj(handle, free_obj); obj_free(class->size, used_obj, NULL); + atomic_long_inc(&pool->stats.objs_moved); } /* Remember last position in this iteration */ From patchwork Thu Feb 23 03:04:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 60770 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp86049wrd; Wed, 22 Feb 2023 19:06:18 -0800 (PST) X-Google-Smtp-Source: AK7set+dMji+opprxL/fg5dSjUs+Y78Udmn2RYuWZXbS7AztyojVZ9hOwZuJF9XEOpATI+i2uNr/ X-Received: by 2002:aa7:df0a:0:b0:4ae:e51e:6956 with SMTP id c10-20020aa7df0a000000b004aee51e6956mr9074861edy.13.1677121577943; Wed, 22 Feb 2023 19:06:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677121577; cv=none; d=google.com; s=arc-20160816; b=N7U1xhaaEqwif0svF5emsE/A7nEFSAUXIwGpv2QadZEehssUgpGVRkAHGWxRmMuH2k Nv/+cUBYNRdbIXVTuqSx7A3OjPWY02NrkYfPWAiOkOhsX+dPRfqjhUZOOsGEDTNoY+zh TppHNoK98LKMQ3lMiBO8WDWXsCfigX7soIok3hJQZCywVy2wOgcjrAl10mG8UEBMkmoa He5HB1GY7l6oh3+bN3eLXfTaF2+NXM58Lp1Z/8RznjgQQBln3wHXgibZCx4dyC3vGN7w L6VeSWoILOPOVeztIfdGCFE7/oDvUKFiKVHTmebp0uPbHOeWY/TAnXLlyYoZDy/XZN// yesQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zvL3mMUiF6TzdAKKcnyDSGCQ6fTbHMRv58umlvVWhug=; b=vQQQhINYO1S3gS7+nUGsUyLTKdHPfOgNnTAdok9ZqCfPXUi60r13iHnbXLQ6DFkmLS 9r9b8AWMjB6+7J1ecZB33GlVfjjuE955mxuNRYU67t0fwdTCDyXfen6GiiHcGme1o9aG l0a6pTJw91hX3i++mo2j8Yy94UcK6Oqv/BVg/ldMIlO1y5tsRjfHnwBguTgNa4wLm3VZ lrXko3jAkW5G3O9mt7OSXjsjosOhcgIXPVmTDkqf8RIgLE5Tde/1aY/BlKSb8lFcCTx7 lEhp312Yq0ELQ0ZigajH7IO6mhOZwtrF/7woW6Tj/e2RdAA3xkiis666dmmBn9AgeBB5 7iwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=To5SpBeX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id vl7-20020a17090730c700b008dc98b197dbsi7638337ejb.184.2023.02.22.19.05.54; Wed, 22 Feb 2023 19:06:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=To5SpBeX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233818AbjBWDFl (ORCPT + 99 others); Wed, 22 Feb 2023 22:05:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233917AbjBWDFb (ORCPT ); Wed, 22 Feb 2023 22:05:31 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B621248E01 for ; Wed, 22 Feb 2023 19:05:15 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id ky4so12450915plb.3 for ; Wed, 22 Feb 2023 19:05:15 -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=zvL3mMUiF6TzdAKKcnyDSGCQ6fTbHMRv58umlvVWhug=; b=To5SpBeXwNb1lYh6OtMcdA7YXbEuqfheT/FB+PL18Ki7W7SwW3kIj63xK421vOkft8 /Pk58+kjibDmqthKvSgleL1UrM3fGkIT+Ishh6Lj7spySCW5r10AOqyEFNCPUqfajtN/ z+lLB16S5+B2UtABxWkJ1FFeWDsRq0a98+46w= 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=zvL3mMUiF6TzdAKKcnyDSGCQ6fTbHMRv58umlvVWhug=; b=HTPdOvUGisF+NKnVrMv57s8X5ViIevfjuME0u0YSgK2nIs89OHb0eDyQdfek73y6dY eXlPagG6LgGXji84K6uj1QJdYK8J6/2wma/B6C6SzxbPm9YpsaeX4X6hxbRiauR48im9 XmZ1ngR5ipyiEkW8ZSgld0RHrJKOkotIcN8HtkBd/2+PkP8AB8ieWxpeWlsfSQuAFmJC M60lZKqvaQHRc6kGbR/agCJqxkOH5mlqTC8kS8Wkc4GhXzHkuD8Z73BC81qysVJrM4Sg Ri6fkNk305PmWPOSGzUhamSENrRjk+x+QjVPDJALvUB08Y5qrYhJSUptLIdC3FNydXr0 lXrQ== X-Gm-Message-State: AO0yUKWJKL5fYLXfb2eYggMpat5pEhlp09ufPsKBMtY6rQkDVvuZZT5Q y8JxsYBzOIyKS0rD58BJOtCVWQ== X-Received: by 2002:a17:90b:4a4d:b0:233:9fff:888e with SMTP id lb13-20020a17090b4a4d00b002339fff888emr11273419pjb.39.1677121514958; Wed, 22 Feb 2023 19:05:14 -0800 (PST) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:6de2:9e85:b508:57b8]) by smtp.gmail.com with ESMTPSA id jl21-20020a170903135500b0019926c77577sm608520plb.90.2023.02.22.19.05.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 19:05:14 -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: [PATCHv2 6/6] zram: show zsmalloc objs_moved stat in mm_stat Date: Thu, 23 Feb 2023 12:04:51 +0900 Message-Id: <20230223030451.543162-7-senozhatsky@chromium.org> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog In-Reply-To: <20230223030451.543162-1-senozhatsky@chromium.org> References: <20230223030451.543162-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758589435440575983?= X-GMAIL-MSGID: =?utf-8?q?1758589435440575983?= Extend zram mm_show with new objs_moved zs_pool_stats. Signed-off-by: Sergey Senozhatsky --- Documentation/admin-guide/blockdev/zram.rst | 1 + drivers/block/zram/zram_drv.c | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/admin-guide/blockdev/zram.rst index e4551579cb12..699cdbf27e37 100644 --- a/Documentation/admin-guide/blockdev/zram.rst +++ b/Documentation/admin-guide/blockdev/zram.rst @@ -267,6 +267,7 @@ line of text and contains the following stats separated by whitespace: pages_compacted the number of pages freed during compaction huge_pages the number of incompressible pages huge_pages_since the number of incompressible pages since zram set up + objs_moved The number of objects moved during pool compaction ================ ============================================================= File /sys/block/zram/bd_stat diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index aa490da3cef2..3194e9254c6f 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1221,7 +1221,7 @@ static ssize_t mm_stat_show(struct device *dev, max_used = atomic_long_read(&zram->stats.max_used_pages); ret = scnprintf(buf, PAGE_SIZE, - "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n", + "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu %8llu\n", orig_size << PAGE_SHIFT, (u64)atomic64_read(&zram->stats.compr_data_size), mem_used << PAGE_SHIFT, @@ -1230,7 +1230,8 @@ static ssize_t mm_stat_show(struct device *dev, (u64)atomic64_read(&zram->stats.same_pages), atomic_long_read(&pool_stats.pages_compacted), (u64)atomic64_read(&zram->stats.huge_pages), - (u64)atomic64_read(&zram->stats.huge_pages_since)); + (u64)atomic64_read(&zram->stats.huge_pages_since), + (u64)atomic64_read(&pool_stats.objs_moved)); up_read(&zram->init_lock); return ret;