Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4816759rwb; Mon, 21 Nov 2022 12:18:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf64NctiPa2zEl/JgoJs95XI1ef6s792AVtMYltI5dmN6UTCXMQMMQeIyh08s2XP69o6Gk5u X-Received: by 2002:a17:90a:1f87:b0:213:7d3d:a769 with SMTP id x7-20020a17090a1f8700b002137d3da769mr27727545pja.22.1669061927850; Mon, 21 Nov 2022 12:18:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669061927; cv=none; d=google.com; s=arc-20160816; b=PrUO9bklhVTP9biUy+U/Kl9xZ+fK11QRQhrglNsPSfusuITey4c0SVoPZkO8FFTh99 RdWd+NkEEehbqvfO7nRkAJi+Fj1+wWLhR8QNqdr3w8qnRl9NsDvv4UMN3FaLEtecEco6 R2NRqIJWXDTYXfcG3oio5P1Sg8VyTB5AASUxTMCBB5aEHTLQduAOWV2GR+Uufjnl+B3o gN5yV7MKhetzQTQCjbN+YpCKRxATPzl0lu48jEjOejJDPBj0ZnkZYqzqiBVLRShoPB/t pBDU85Rg5Jd+DlezN9G8gusF9lTxbkL57xtDBXkEi1MLhh4rKd2zIWA8GuU+pyM5wWEQ gjBg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=94J4V6Ls316AqryyMZmBCDT4NIvXDUVp2JtOV9OlzRc=; b=uTJkk98ATwnHnXyaVnygbyYeikz4FKslEU2zCun54D108msqKsWNmsBBWL7wa0/gcs B9ZIWxOaHhBrjQ7/nBRN7DFWsyB8wbUpm9M3DHtaPGlhRzDLKJf9lwC1uf4qkFA0Sv29 1PrEGQCBX29SjdodRM1SOKQEcQ90syh0X6Al4HEceGWm6RDZvtmzA/b4n2Sr0LFHsjnD ojdcvhbTotuDXKOOFZhY1xZI0KlwQ2YWTpbwGEBGYBFTua0RQ9lklt8bcVAQwCGsqI3G Il85eHahe1lg7644isIoOU26pif5hK9/KRBy7bYUar+irmN+gVDVDMg+hm88XbDSuRcN XuhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sberdevices.ru header.s=mail header.b=aYIyuMIb; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l9-20020a17090270c900b0017123d2abd5si11037197plt.457.2022.11.21.12.18.36; Mon, 21 Nov 2022 12:18:47 -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=@sberdevices.ru header.s=mail header.b=aYIyuMIb; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=sberdevices.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230436AbiKUTBF (ORCPT + 91 others); Mon, 21 Nov 2022 14:01:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229604AbiKUTA6 (ORCPT ); Mon, 21 Nov 2022 14:00:58 -0500 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C794DD0DDA for ; Mon, 21 Nov 2022 11:00:54 -0800 (PST) Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id B44275FD02; Mon, 21 Nov 2022 22:00:51 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1669057251; bh=94J4V6Ls316AqryyMZmBCDT4NIvXDUVp2JtOV9OlzRc=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=aYIyuMIboz0W6DJ6i8lYtxkJL5XEoZPiVXv6TDGwRe7BzvLhJnaPeVxSrAxpc+MIk HzSAFVWWLMqmJaPRxAIeiW3PHoB3vDtsT5/8wukhP9lgr+avn50xU61LvkAMUs+QSE 4QhQF0B4RYPFY7Y2zL+eQHwC6j/2wrRHrJ86WXODWvYxvRQOWqbOcKSFvvU9JdoF2q SAcemz3So2nX20e61dSmi78IdOxrdP74YiFXvEGSKec/5GyHXNNq51cmkUrRu151Zd Cd2Qcm15t/QPFRqwTIDI0K2eGRt/5Hg8iLjlSYwSj6NSQ1neryIKjwGHNexl6llh9L o6ze4UScdUArA== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Mon, 21 Nov 2022 22:00:49 +0300 (MSK) From: Alexey Romanov To: , , , CC: , , , , Alexey Romanov Subject: [RFC PATCH v1 0/4] Introduce merge identical pages mechanism Date: Mon, 21 Nov 2022 22:00:16 +0300 Message-ID: <20221121190020.66548-1-avromanov@sberdevices.ru> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [172.16.1.6] X-ClientProxiedBy: S-MS-EXCH01.sberdevices.ru (172.16.1.4) To S-MS-EXCH01.sberdevices.ru (172.16.1.4) X-KSMG-Rule-ID: 4 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiPhishing: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 1.1.2.30, bases: 2022/11/21 16:41:00 #20594217 X-KSMG-AntiVirus-Status: Clean, skipped X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 Hello! This RFC series adds feature which allows merge identical compressed pages into a single one. The main idea is that zram only stores object references, which store the compressed content of the pages. Thus, the contents of the zsmalloc objects don't change in any way. For simplicity, let's imagine that 3 pages with the same content got into zram: +----------------+ +----------------+ +----------------+ |zram_table_entry| |zram_table_entry| |zram_table_entry| +-------+--------+ +-------+--------+ +--------+-------+ | | | | handle (1) | handle (2) | handle (3) +-------v--------+ +-------v---------+ +--------v-------+ |zsmalloc object| |zsmalloc object | |zsmalloc object| ++--------------++ +-+-------------+-+ ++--------------++ +--------------+ +-------------+ +--------------+ | buffer: "abc"| |buffer: "abc"| | buffer: "abc"| +--------------+ +-------------+ +--------------+ As you can see, the data is duplicated. Merge mechanism saves (after scanning objects) only one zsmalloc object. Here's what happens ater the scan and merge: +----------------+ +----------------+ +----------------+ |zram_table_entry| |zram_table_entry| |zram_tabl _entry| +-------+--------+ +-------+--------+ +--------+-------+ | | | | handle (1) | handle (1) | handle (1) | +--------v---------+ | +-----------> zsmalloc object <-----------+ +--+-------------+-+ +-------------+ |buffer: "abc"| +-------------+ Thus, we reduced the amount of memory occupied by 3 times. This mechanism doesn't affect the perf of the zram itself in any way (maybe just a little bit on the zram_free_page function). In order to describe each such identical object, we (constantly) need sizeof(zram_rbtree_node) bytes. So, for example, if the system has 20 identical buffers with a size of 1024, the memory gain will be (20 * 1024) - (1 * 1024 + sizeof(zram_rbtree_node)) = 19456 - sizeof(zram_rbtree_node) bytes. But, it should be understood, these are counts without zsmalloc data structures overhead. Testing on my system (8GB ram + 1 gb zram swap) showed that at high loads, on average, when calling the merge mechanism, we can save up to 15-20% of the memory usage. This patch serices adds a new sysfs node (trigger merging) and new field in mm_stat (how many pages are merged in zram at the moment): $ cat /sys/block/zram/mm_stat 431452160 332984392 339894272 0 339894272 282 0 51374 51374 0 $ echo 1 > /sys/block/zram/merge $ cat /sys/block/zram/mm_stat 431452160 270376848 287301504 0 339894272 282 0 51374 51374 6593 Alexey Romanov (4): zram: introduce merge identical pages mechanism zram: add merge sysfs knob zram: add pages_merged counter to mm_stat zram: recompression: add ZRAM_MERGED check Documentation/admin-guide/blockdev/zram.rst | 2 + drivers/block/zram/zram_drv.c | 315 +++++++++++++++++++- drivers/block/zram/zram_drv.h | 7 + 3 files changed, 320 insertions(+), 4 deletions(-) -- 2.25.1