From patchwork Mon Nov 21 19:00:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Romanov X-Patchwork-Id: 13051554 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 15E6EC4332F for ; Mon, 21 Nov 2022 19:00:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B22EF6B007B; Mon, 21 Nov 2022 14:00:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AA8388E0005; Mon, 21 Nov 2022 14:00:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74EF46B007D; Mon, 21 Nov 2022 14:00:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6082E8E0002 for ; Mon, 21 Nov 2022 14:00:55 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 37B9D120CA8 for ; Mon, 21 Nov 2022 19:00:55 +0000 (UTC) X-FDA: 80158366470.27.EF76114 Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by imf02.hostedemail.com (Postfix) with ESMTP id E6CFF8001C for ; Mon, 21 Nov 2022 19:00:53 +0000 (UTC) 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 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669057254; a=rsa-sha256; cv=none; b=7tbH8fFS6fm3qAeOc/pvVJxNMnRDtrK8dPDgTL7sH/2OxeQvE/ZONYwWtSUvPJ+XNpEKrF i3fAcC8AO4YDmBRMbDkCqEH/DCMMR6JngMlJ1AY/Vdp2OgeV5mcPUNUMZociR2yKR73FmM jmzMmpxtJbKPftAMnykwbY6RFG/SeyQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=sberdevices.ru header.s=mail header.b=aYIyuMIb; spf=pass (imf02.hostedemail.com: domain of AVRomanov@sberdevices.ru designates 45.89.227.171 as permitted sender) smtp.mailfrom=AVRomanov@sberdevices.ru; dmarc=pass (policy=quarantine) header.from=sberdevices.ru ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669057254; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=94J4V6Ls316AqryyMZmBCDT4NIvXDUVp2JtOV9OlzRc=; b=ZFX/zO/4Zwoy0v4RNDNt+f+S4/XkM7KXJh5uMlq/1oP+iCPReV6l1i2WF6HykmMUcvylnu yZTgcrSwPvdOVssdOePjbc90vKisXQYM9O5vOpe1kQyU0GPvUJkWzvukLZ93VBI8HDXTXa gY7OwAA87dctl+zPRxMgUqTdI6GX1lw= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E6CFF8001C Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=sberdevices.ru header.s=mail header.b=aYIyuMIb; spf=pass (imf02.hostedemail.com: domain of AVRomanov@sberdevices.ru designates 45.89.227.171 as permitted sender) smtp.mailfrom=AVRomanov@sberdevices.ru; dmarc=pass (policy=quarantine) header.from=sberdevices.ru X-Rspam-User: X-Stat-Signature: 8hi1hf6d7p47o7o1suw5kxj5bcw3azs6 X-HE-Tag: 1669057253-835291 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000015, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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(-)