From patchwork Thu Jun 15 03:48:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13280676 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 CB11EEB64D9 for ; Thu, 15 Jun 2023 03:48:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 010276B0072; Wed, 14 Jun 2023 23:48:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EDB846B0074; Wed, 14 Jun 2023 23:48:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7BBE6B0078; Wed, 14 Jun 2023 23:48:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C429B6B0072 for ; Wed, 14 Jun 2023 23:48:44 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8D59AA0948 for ; Thu, 15 Jun 2023 03:48:44 +0000 (UTC) X-FDA: 80903600568.13.D78C0EC Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf14.hostedemail.com (Postfix) with ESMTP id 00A5F100003 for ; Thu, 15 Jun 2023 03:48:41 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="QuB/9REr"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf14.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686800922; 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:references:dkim-signature; bh=Vbu+qXDQAnJmw8qCM+u2edU9xMFzBPDexgHZg7upYSc=; b=RsJu5Cy2QrpSsP8dgyH9/irQglNFIjw1+hHQlP2N6WmnHw/qjT7FNS115cSaCFVGpLRXIM 1IQa4hHdOxjKclPtpslod93v6D2EpfGzdIl55Ld64EZiaP/FzMHm8Xjdo8JIF9gh0gjlCI zI6VX1rCuoiiYaP+xgXSh4vqMladTnw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="QuB/9REr"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf14.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686800922; a=rsa-sha256; cv=none; b=PYWLPYMO6uqPWfVSQGBfG05z2+zVyD7UbpSuChSHo57StJ4J7IKdJ8p5xYRrJ04JwXKkso XS043lubXTPM2mzUZyirxrQ4UfmvgU3vnml64lUR4AFZ1Zf8KdyI0OCcmcqDMeGCYg1PF5 FakSJuYVe96B0Gln0ceAfQumkffyItA= Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-543d90cc675so4144421a12.3 for ; Wed, 14 Jun 2023 20:48:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1686800919; x=1689392919; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Vbu+qXDQAnJmw8qCM+u2edU9xMFzBPDexgHZg7upYSc=; b=QuB/9REryqq6Mgb8L/e4zMTLqzDoDXfDapT3W0Gx/mxECAPvMPdAXb/7xqeOhW6cOS rDSsPeeYrCYrv5l2lI+PFC6KhRdpynj5Nc5W68ZABRG0SVuM4SYMvOjbDQp9yiwdfuFg c3IlUzDHTM3Fe8qKMpcnHym1vs/Tc9pnPa/Z9VZjvYSWuOV+oc3k2SLfRMFyiHBgDHcf WE/hXQy+tuxSMe7jlfmDYO9nNIjXK8xkQy8hrUeKjS7HgAKMRd7kpWv9b+YE46IvVy/u dilXepFYsW49CWNtr+c9qK0KedUGDaNOr6gL61uSNoBrjIPF6bYFFyLINhJRBq0lk7Pf OxnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686800919; x=1689392919; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Vbu+qXDQAnJmw8qCM+u2edU9xMFzBPDexgHZg7upYSc=; b=SQKpX/ufd+25phs4RDjlupv23689neaxu0aDWxgZzTwE7r1AAPHvwFh6qvn+py3OPH OVHo8Ok8FnQJeaN6bZ7XFaXUpkqwFL3QC5lmRZnRK0/3nsdMwTYbIOWYm1RfKkRusxVv z2x17ibu4FhdnoVu6oSnoUYidMsnGEd8dxb6N+8URZQ7Bq7lVIjlEgblPHr+6lzCZe8n yEDjir8M7TstIRxlTxiBqEyZE9deUQ98pwkNcE3xR+BVUuGJLUNqdpdBCaDRGMyKJjUT 2OEJSylyrRCTnv5VudybmdJpiQvtyR6OeZDFe0qYiuH17EQC6xFm6VbCGESvMgYTD7Z2 cJSQ== X-Gm-Message-State: AC+VfDwHgZlRZj/Ckj0WMAzhVR0f9hJhjb6T7jBTtGfJ/PFxeRMsmnMS 2PvzXloeLFyMDdZAc9fWVUkp0A== X-Google-Smtp-Source: ACHHUZ7IJ/OOff5P+kevIRNHk4rSmF9QFuBJH8tCKcPmy/VcWRp1kVAz+QNpkDSFj/YM7GxNJewtnw== X-Received: by 2002:a05:6a21:29ca:b0:11d:d83b:8d80 with SMTP id tv10-20020a056a2129ca00b0011dd83b8d80mr2265679pzb.46.1686800919047; Wed, 14 Jun 2023 20:48:39 -0700 (PDT) Received: from Tower.bytedance.net ([203.208.167.147]) by smtp.gmail.com with ESMTPSA id 19-20020a170902c11300b001b3fb1119fdsm2751713pli.297.2023.06.14.20.48.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 20:48:38 -0700 (PDT) From: Zhongkun He To: minchan@kernel.org, senozhatsky@chromium.org, mhocko@suse.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Zhongkun He Subject: [RFC PATCH 1/3] zram: charge the compressed RAM to the page's memcgroup Date: Thu, 15 Jun 2023 11:48:30 +0800 Message-Id: <20230615034830.1361853-1-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 00A5F100003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6yxmpj9rq9etkpws899esa7dgbq4bacf X-HE-Tag: 1686800921-32357 X-HE-Meta: U2FsdGVkX1/X+H2SmDqLMF2vbtKcjixvseyLpQPfAUYqP6rHjjebwvjsTRh0ghYK2PMxB1ZRJDON2BZKtYF4rDURB+PLnq9IYKzkjVRbKPexOIbfiaCeFbNgKAbBvVK7Lk5js5fhLcdKAsCWp7pz13iY34jTx7foH7ZASvh0ALHsYz8H0sGyM0C8WpScR8SPYGw09lUQM592Q3kMjkoJ8ns9PVu4C/hrdY5kDJ3nHvgMJOC52q2i1GAzlrGtM//32b+F93bQB8TZ+IbDhipBgvakIrZQ+Ice/FH3MvGQSV43/uMVUx1BysC5Kg8PshZrC47P7Bi1rKLxPdYXAVLRfePSn+gWfNZ+cn5nk7vIHbRMLwgXiRnan3U4nKlseQRaQmjzoDzw+Aqiwb4yIsqxkAJsB6YINbkUBbXgbJmxi8aYln3kA0sJ7dVMGEPMxklOlzq10cRo0TNpc6SXY3h6pmgun6F0Hol6QqENXJsCtcv730Ytqw0oJhZJzq+GuJgvLCXZAZmYuhASVJxfeFVgpK08fzqOC+g0Q++nuvbFNf5pAyCi0DJKJoJoO2JpKZcw8G+NdvAR6jte3J6U/TC6xwDFTPWupwaOGx7qeEwwXuTmNdFk7T6q5Nt8HZLHErax3R53qxlRFJe1IRt0UwfKv2Ydg9Hw64QjlZQAKLjKlfGbawp0KH9Rico7EuRVuI56TFCS1hHP8vopm13M59jY8tEiLaiRBe5IzccFehpCOa5UuCU2lofEAhlAQSH995MfVEygVpy/bE6cAdwwdsEq0GGgfe5gxvsexJQMNGSMgrGm9ai4r4kFFqcuRhZPapRTycyLU7frk7R6PNWjE3W7eJlu83iHaf7r2b3cKeJzOMRBOK1aoZIH51+cmlV2aIN2hlxz7ugGEJg1h72OrNS4r7PL37A7ZcfDVuUx74Bjrn/E40GLVuSu+MN4GpYEyjcUgWznQyfevuRio6dSAJw wqH6FaMV jg8snx+KlReI5AYQ9p76vPYY8LnKMuigb8dfV2pvLTcMNYaADQWJtEhApZrG0Qur9vkUrcwAtEdJjAHbXEdqMCF50rNJJYtTwTjALeaDL0ZTk224OV/QIUVeJV0d4r9CmOtQdReorGRBcIrqTMmwhTKrpkMOse5GxCIwricVlSx5dVMefssIPuuK+3TQk13W3a9NItplJQ6C2jQrHvjpdfJ6uAqAvxhFRw7Wft6cRI8xIIg237IPtIAAp36J3Wt6b1eltONKLEXdexSBx7e6rqz58vfD08opJNK/WUL50h8aX/AzMHJPFppbJtL+R+FbOrO7dNhi9DWuH3/L5Ypl0kbGI5IzVmKdlgKpvpePbWRqa+C4tndyIxf8T8Ms3z1RPfCpHZqERyABqTQIopH+7mXlgPKC+If1JPRNkKRUEap4v9SFly0RsKzEM5g5Td7JRgZSC 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: The compressed RAM is currently charged to kernel, not to any memory cgroup, which is not satisfy our usage scenario. if the memory of a task is limited by memcgroup, it will swap out the memory to zram swap device when the memory is insufficient. In that case, the memory limit will have no effect. So, it should makes sense to charge the compressed RAM to the page's memory cgroup. Signed-off-by: Zhongkun He --- drivers/block/zram/zram_drv.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index f6d90f1ba5cf..03b508447473 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "zram_drv.h" @@ -1419,6 +1420,10 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) struct zcomp_strm *zstrm; unsigned long element = 0; enum zram_pageflags flags = 0; + struct mem_cgroup *memcg, *old_memcg; + + memcg = page_memcg(page); + old_memcg = set_active_memcg(memcg); mem = kmap_atomic(page); if (page_same_filled(mem, &element)) { @@ -1426,7 +1431,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) /* Free memory associated with this sector now. */ flags = ZRAM_SAME; atomic64_inc(&zram->stats.same_pages); - goto out; + goto out_free; } kunmap_atomic(mem); @@ -1440,7 +1445,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); pr_err("Compression failed! err=%d\n", ret); zs_free(zram->mem_pool, handle); - return ret; + goto out; } if (comp_len >= huge_class_size) @@ -1470,8 +1475,10 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) handle = zs_malloc(zram->mem_pool, comp_len, GFP_NOIO | __GFP_HIGHMEM | __GFP_MOVABLE); - if (IS_ERR_VALUE(handle)) - return PTR_ERR((void *)handle); + if (IS_ERR_VALUE(handle)) { + ret = PTR_ERR((void *)handle); + goto out; + } if (comp_len != PAGE_SIZE) goto compress_again; @@ -1491,7 +1498,8 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) if (zram->limit_pages && alloced_pages > zram->limit_pages) { zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); zs_free(zram->mem_pool, handle); - return -ENOMEM; + ret = -ENOMEM; + goto out; } dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO); @@ -1506,7 +1514,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); zs_unmap_object(zram->mem_pool, handle); atomic64_add(comp_len, &zram->stats.compr_data_size); -out: +out_free: /* * Free memory associated with this sector * before overwriting unused sectors. @@ -1531,6 +1539,8 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) /* Update stats */ atomic64_inc(&zram->stats.pages_stored); +out: + set_active_memcg(old_memcg); return ret; }