From patchwork Tue Apr 18 06:24:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Romanov X-Patchwork-Id: 13215070 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 21ACAC77B75 for ; Tue, 18 Apr 2023 06:25:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 91BDA8E0003; Tue, 18 Apr 2023 02:25:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A4688E0002; Tue, 18 Apr 2023 02:25:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 71D818E0003; Tue, 18 Apr 2023 02:25:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 5AEDB8E0002 for ; Tue, 18 Apr 2023 02:25:17 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 239B0C0149 for ; Tue, 18 Apr 2023 06:25:17 +0000 (UTC) X-FDA: 80693524674.13.85D185E Received: from mx.sberdevices.ru (mx.sberdevices.ru [45.89.227.171]) by imf25.hostedemail.com (Postfix) with ESMTP id 9112DA000F for ; Tue, 18 Apr 2023 06:25:14 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=sberdevices.ru header.s=mail header.b=hZA5i7iC; spf=pass (imf25.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=1681799115; 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=Fse+MPOrkQw272jW64QlMpSMJAmRuGF8pomAFgTEHuE=; b=nnjOPXyT8ofn/bSquw+ox9UpogcKiFaaeEi+6co/4cTIaGNjWxf1KvXgvgyxdKF2EhylBj dy6+cNygbdvb+Jm+Bkuz7S4xcIo0X68L3BC5/nbxhBiz9vNQXbEj6TGa5DNh8ntzuSFiKV YWKTaVLNzVzvy3nANXprDtPjC7Ca39M= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=sberdevices.ru header.s=mail header.b=hZA5i7iC; spf=pass (imf25.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681799115; a=rsa-sha256; cv=none; b=lFr60zyGiURLQ+66//T5PPokAvuuy3u0nnJX/BmG5I+8IFxaMwJfE88NBDdf24ih3o54Rr fR3qHloRaz5nM0eKfo0+aRn2ktTTa/QcuozK4TuENM8JVdtqOX05nRdXKAgeiyucADVjJ1 SV5lvAqa+uG+2iuu9HnI9rgJIg4Ct9Y= Received: from s-lin-edge02.sberdevices.ru (localhost [127.0.0.1]) by mx.sberdevices.ru (Postfix) with ESMTP id 7493D5FD02; Tue, 18 Apr 2023 09:25:12 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sberdevices.ru; s=mail; t=1681799112; bh=Fse+MPOrkQw272jW64QlMpSMJAmRuGF8pomAFgTEHuE=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=hZA5i7iCjFc/tKT3rPkI4USwY//pOo/cnffBym9jGgM50MwyUjdHPt3CKY+nkdck7 18Q/R6NVSISemgJNhsRPONl0XlRFTYa0gERJxmvDBRNqpjtXR/899VJNg0Qr/WG2kg t5NB83Ioh5dzeCdWIPEZEjEzjTEXmNtkQmf3PaRGOltOxke0zECiwvryQkKsvE7WnO VgiBLmR+AGTedzfEcCgIXibEZ5F0neD/819/sHGew355vgmqf0EpI3uZZhg6wtHgpJ SjmvMJFTAhZEOXX8FqLEsccEWCnQcs8NgPv+VLzTBfK6FzcLdSMwnrN2TCBTxZyyr2 YGMFbWI1OU3Mw== Received: from S-MS-EXCH01.sberdevices.ru (S-MS-EXCH01.sberdevices.ru [172.16.1.4]) by mx.sberdevices.ru (Postfix) with ESMTP; Tue, 18 Apr 2023 09:25:10 +0300 (MSK) From: Alexey Romanov To: , , CC: , , , Alexey Romanov Subject: [RFC PATCH v1 0/5] Introduce objects folding mechanism Date: Tue, 18 Apr 2023 09:24:58 +0300 Message-ID: <20230418062503.62121-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-EXCH02.sberdevices.ru (172.16.1.5) 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: 2023/04/18 02:02:00 #21122658 X-KSMG-AntiVirus-Status: Clean, skipped X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9112DA000F X-Stat-Signature: org9b681cq4exz6ydo9umocxgfifprn7 X-HE-Tag: 1681799114-632796 X-HE-Meta: U2FsdGVkX1+qyxKGKDgySmuyAfdm6EJJ75rSUuyMmvwiwEZ1FXcWvJimbhYhSXu2yLrrkkmp7IDtsDdqJEa4nhftQO+2OmuZ2j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Pi3aGXxusEI33RixkL+hM+A5NJf04Hxr46AxCDeRRkIgA4rI1uJJEP9V2HcV+lH1D/70vmwneLk3QQTTm7QDdvjDzHUFvTimUITdkPxW20XVT80Noa9J6JTITtuoIQbjUwXR4FmeenszQLu0tEsqeuUOC+jrfOtz80s+HomF3XS848MJnbXgsw2FTrVohlMY2S/9FpZuv4c7ouQzJqUpvE= 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: Hello! This RFC series adds feature which allows fold identical zsmalloc objects into a single one. Based on ZRAM version: https://lore.kernel.org/lkml/Y3w8%2Fq%2FHoSbqamoD@google.com/t/ Let's imagine that 3 objects with the same content got into zsmalloc: +----------------+ +----------------+ +----------------+ | 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. Fold mechanism saves (after scanning objects) only one zsmalloc object. Here's what happens after the scan and fold: +----------------+ +----------------+ +----------------+ | handle 1 | | handle 2 | | handle 3 | +-------+--------+ +-------+--------+ +--------+-------+ | | | | | | | +--------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 zsmalloc itself in any way (maybe just a little bit on the zs_free() function). In order to describe each such identical object, we (constantly) need sizeof(fold_rbtree_node) bytes. Also, all struct size_class now have new field struct rb_root fold_rbtree. Testing on my system (8GB RAM + 1Gb ZRAM SWAP) showed that at high loads, on average, when calling the fold mechanism, we can save up to 15-20% of the memory usage. This patch series adds a new sysfs node into ZRAM - trigger folding and provides new field in mm_stat. This field shows how many pages freed during folding: $ cat /sys/block/zram0/mm_stat 431452160 332984392 339894272 0 339894272 282 0 51374 51374 0 $ echo 1 > /sys/block/zram0/fold $ cat /sys/block/zram/mm_stat 431452160 270376848 287301504 0 339894272 282 0 51374 51374 6593 Alexey Romanov (5): mm/zsmalloc: use ARRAY_SIZE in isolate_zspage() mm/zsmalloc: get rid of PAGE_MASK mm/zsmalloc: introduce objects folding mechanism zram: add fold sysfs knob zram: add pages_folded to stats Documentation/admin-guide/blockdev/zram.rst | 2 + drivers/block/zram/zram_drv.c | 30 +- include/linux/zsmalloc.h | 4 + mm/Kconfig | 9 + mm/zsmalloc.c | 484 +++++++++++++++++++- 5 files changed, 513 insertions(+), 16 deletions(-)