From patchwork Wed Dec 18 06:34:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 13913084 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 D844FE77187 for ; Wed, 18 Dec 2024 06:35:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 718746B0085; Wed, 18 Dec 2024 01:35:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C80B6B0088; Wed, 18 Dec 2024 01:35:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56A086B0089; Wed, 18 Dec 2024 01:35:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 35E5D6B0085 for ; Wed, 18 Dec 2024 01:35:27 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A225180BF6 for ; Wed, 18 Dec 2024 06:35:26 +0000 (UTC) X-FDA: 82907117328.26.A9AD1A9 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf05.hostedemail.com (Postfix) with ESMTP id 9C2C310000F for ; Wed, 18 Dec 2024 06:34:24 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=ktac5ihi; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf05.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.179 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734503710; 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:in-reply-to:references:references:dkim-signature; bh=YM5tbrHoFcnr7Yi5hBwjzOD0QAW+fJr+gF9sDLsbf7Q=; b=Hw8AcSzSHn7droUXAC/LHTsAfQ4Y4neEXNamapdMP1Ole67+w6FnU1WUz/oVF6/EheKDFd wHSOq0WVDV30l+f44S75ZIBhj0Jog1Dc3+JUk89v3WuTG7NXkZajGHh9efeWQINu+i5EaD I4lFLIrIauZDdkJthemdm2d2eIbLfEQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734503710; a=rsa-sha256; cv=none; b=k1EgZvqdMPwhqmzaN3vWd6f6MbRcw423JxO1GRYyWyQnjqA4+TMPuTLsdmZ0D3n5AU99PA TSQ+AWJeuMkJ83QnAbPYx1ajR2Qeh1U7ttm50XXbWvOw86l3ad/K9VjGUYQDw+a7W69Sgt bZxtGCLS7Lp/QF3On1fgdL1mnsGEHRE= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=ktac5ihi; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf05.hostedemail.com: domain of senozhatsky@chromium.org designates 209.85.214.179 as permitted sender) smtp.mailfrom=senozhatsky@chromium.org Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-218c8aca5f1so19808275ad.0 for ; Tue, 17 Dec 2024 22:35:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1734503723; x=1735108523; darn=kvack.org; 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=YM5tbrHoFcnr7Yi5hBwjzOD0QAW+fJr+gF9sDLsbf7Q=; b=ktac5ihin0oxfHeRrA//FqOuEuZ/gemxvcSDmgBldZp6fpycO44/nOIKkxo6vttngp Ex1Gf9W1q19dN1M/8LXDx5zMHL8T2MCyNtpjP86DUuwkn/au3NJbanLtPnZPo9cEfEBH OKHuiiLcWb/G439/KQUgUnyM2R8FeEefgRzYA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734503723; x=1735108523; 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=YM5tbrHoFcnr7Yi5hBwjzOD0QAW+fJr+gF9sDLsbf7Q=; b=mkPc3f4OI0LhabkfKkXS0f3vJOdk4yxitFTQPeAA8YQS7STTvL8XHEzmfoEhwv/8ct FfsBTTO6r7emYRd4vdwTrECvCA3mNeHuykjW3o+cHQbNwWBVW1RL0KVqaQN3WkST0NpZ m7pCFEPRDsT9A4juz/HeKUKuxIwA+PGf1dCbxtOQETAWvbQu6qkjADLOdiPdBOi1/juf EHgJ984YUY0oDvFY5TWkCn7i7ilqngyvk+M9FNTnnqHN1tllRhf/PHfBjz5G7M3tCIl2 oHh5Uvw/O2Djw2dzCc+g0cvgVMi8KlJHg6ugV3n1EsHYJU7/wmR4pyJaBYBlx7XKf6XE 76Ag== X-Forwarded-Encrypted: i=1; AJvYcCX08MKZJKMEo59Y5CQs1dP5FBpz2wWhaM5ru5zEQ2EEJRB5uYEApeWSyS6hIuxpat83dDMEeDoXlQ==@kvack.org X-Gm-Message-State: AOJu0YzX6hYfM+EfrahSSnFatlhgYZc6ULAXujk4U23jeRcRzNh0pjl0 MiS/DdHcnvCP9AR1v7CGlbrj3VbIVFd/4pQTpBAKguBuCRIsavjyHbGgQL7fzw== X-Gm-Gg: ASbGncuS4F97jmTMpOaetA+JEn3BtfnhbxBv26TN3v8thuIspDZ/uRx1OcbV0B++0Br mUw8JfdtoUOkMggkSVh1ULtARLeYLMnYOPfeVs6wbm9RilSKCicZkA/+kxQdJ7kpf7ZbAUSpdmo IvYlGRSppx4v+DgAD19Z6NE3qbz6wD9L183//ATbSDW7FFdfv2zyf6iKsXRGpA6aPRNEnI7ECjp TnuBs2BfXvMGu2KpUilTl1FR+CtuIqAQ+kjQD6dZFdGGzBtzF+077o7IXk= X-Google-Smtp-Source: AGHT+IEzRX0s0ipw+MkmY6rxb4hZkmahH0+HA2BPVeSSTzwnMahAGsE7gUMp3e22K+tr163jhvT0PQ== X-Received: by 2002:a17:902:dac6:b0:216:7cd8:e964 with SMTP id d9443c01a7336-218d710417emr25045545ad.22.1734503723387; Tue, 17 Dec 2024 22:35:23 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:3bcc:36cc:b9fe:9379]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-218a1dcb8e9sm69394115ad.83.2024.12.17.22.35.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Dec 2024 22:35:23 -0800 (PST) From: Sergey Senozhatsky To: Andrew Morton Cc: Minchan Kim , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 1/7] zram: free slot memory early during write Date: Wed, 18 Dec 2024 15:34:18 +0900 Message-ID: <20241218063513.297475-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog In-Reply-To: <20241218063513.297475-1-senozhatsky@chromium.org> References: <20241218063513.297475-1-senozhatsky@chromium.org> MIME-Version: 1.0 X-Stat-Signature: yqimt35rrhd1ycqsh6ndz4pajarf7q3d X-Rspamd-Queue-Id: 9C2C310000F X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1734503664-878623 X-HE-Meta: U2FsdGVkX1+SebtzIMswoHSJYPNBZQV+V5egtTSzCe+AgikUa0SMW9LOAHKl4Fszapwi3WEhK6W3YW/uDV45MUMhCuHoJIjd1hIxDxSfFVdOurnwVvRnGxD1quOD6ZBtCFMlJvdAWLcFz8eK49VLyQuHJtjyB9y+qY1GHeUVEKwft+wDrq2G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ity: Ham, tests=bogofilter, spamicity=0.329346, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In the current implementation entry's previously allocated memory is released in the very last moment, when we already have allocated a new memory for new data. This, basically, temporarily increases memory usage for no good reason. For example, consider the case when both old (stale) and new entry data are incompressible so such entry will temporarily use two physical pages - one for stale (old) data and one for new data. We can release old memory as soon as we get a write request for entry. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 45df5eeabc5e..cda0f3fd4058 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1648,6 +1648,11 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) unsigned long element = 0; enum zram_pageflags flags = 0; + /* First, free memory allocated to this slot (if any) */ + zram_slot_lock(zram, index); + zram_free_page(zram, index); + zram_slot_unlock(zram, index); + mem = kmap_local_page(page); if (page_same_filled(mem, &element)) { kunmap_local(mem); @@ -1736,13 +1741,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) zs_unmap_object(zram->mem_pool, handle); atomic64_add(comp_len, &zram->stats.compr_data_size); out: - /* - * Free memory associated with this sector - * before overwriting unused sectors. - */ zram_slot_lock(zram, index); - zram_free_page(zram, index); - if (comp_len == PAGE_SIZE) { zram_set_flag(zram, index, ZRAM_HUGE); atomic64_inc(&zram->stats.huge_pages);