From nobody Sat Nov 30 16:31:44 2024 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0721315855B for ; Sun, 8 Sep 2024 11:42:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725795763; cv=none; b=mZ1M8TGUPFfa2EKRdXcG569Ig0T7Q0Ej4K9PA97Y1JizdVCW/SbkAz7qVbd891e03EVvhINc7qT7PWwNqel2CZHCouaeopN4m1ISxNbKu6YQ4Q4m1VtcNPomms6WHdCIGUMJ8VdwHblAhBAr3RCWRgmlwQENL0qzLM3Jo8263mE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725795763; c=relaxed/simple; bh=oR3naV05tO9HRz4kvtPKoCFr1CXNpq4dqwybuFPJMjQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DZm8NUN+jqeZTBoQd6BAOMGulOHrUPjOikF6jiNv41ariHZzZaueaZtFstWJnSyzqwMevfP86E/K+ICIP20bx6qHdxFO9yzozxgZrtOBwlI+MrthVLUZW6nScYGcQUUaA29vsfopKieKGIwfHfY8asCotAZ6MdEryQ/L3cGEKck= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=BknI1biw; arc=none smtp.client-ip=209.85.167.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="BknI1biw" Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3e03d17365bso531153b6e.1 for ; Sun, 08 Sep 2024 04:42:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725795761; x=1726400561; darn=vger.kernel.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=IcgEvHbf96KCRkopD9YVwbrf1iPmkYihwVMDRIbnuTE=; b=BknI1biwpa+czDhhK64zrhFYU21Uxl20i7iWfsm17dsqvLmtxFx7ebhDiKNghZqDI6 dN13tyvCq/z57d7p059toMLY9uf289pX76bLM7codRqsRCzUKruOIhX8rbyXGsytCzZM yzCVIZeTQzAh0jX7mz3xm+bAD6Fl7CKGwVELE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725795761; x=1726400561; 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=IcgEvHbf96KCRkopD9YVwbrf1iPmkYihwVMDRIbnuTE=; b=rPlBZnxegSYjdRZMXrikO5bNk/MX4Rvdt7N0bN0+GYkPoKzlPWszL615tYujaQ5/YB xwy/gsrXgMNJY33gNKh6folXFBBYtQ7i/aapp0fyOH+eIT2rrFBfSakCQv3+kl8taFO2 6wqdpjD2WBvbedj8XAEGgyaxn6TawVc2XhUs7ZpVOYmuUperH1XJOcOpQBCuzt459/RQ LKTVbA+DLGNorIXLXkVfAk9t4oZb1E/wt1S6pmF3Jd99aHqRh4Dex70Lsm5o0n9QD0fn XsfhTtvFNP7Q9et7U6GjEVAYb1TMlLOcmxcGheLeXy11GkjJFNv1jdQJIsPWN6zK+6gN yalg== X-Forwarded-Encrypted: i=1; AJvYcCUt8lJrgti8N/5n11MdtpBfSn5WCMoWJninDEqgLfVox9Vaus+Swn09GinFRffOHksQRx9YnTwQvl7IkGM=@vger.kernel.org X-Gm-Message-State: AOJu0YxIANOYwFg+EVWimzm0RpkmQlLIz+ZTalrccJoDl/7FXqyDZF43 pvEqdzPyh1oj5X9R8/gqwOCOJMKLOX463msUR/jH7MIp1zZpo1we0BKw5uuF/w== X-Google-Smtp-Source: AGHT+IHZWrNDRaJJORSIPHd/QO1zw77yK5P7d3v2OMupnQ85Qsrv18YaTtzK9Q1oqdQRxYdguNStqg== X-Received: by 2002:a05:6808:120b:b0:3db:3b15:7e76 with SMTP id 5614622812f47-3e02a028640mr8555169b6e.47.1725795760934; Sun, 08 Sep 2024 04:42:40 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:5879:695a:4656:45ff]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d823cf3a2esm1923029a12.24.2024.09.08.04.42.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Sep 2024 04:42:37 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton Cc: Richard Chang , linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 1/3] zram: introduce ZRAM_PP_SLOT flag Date: Sun, 8 Sep 2024 20:42:01 +0900 Message-ID: <20240908114223.3024872-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240908114223.3024872-1-senozhatsky@chromium.org> References: <20240908114223.3024872-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This flag will indicate that the slot was selected as a candidate slot for post-processing (pp) and was assigned to a pp bucket. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 10 ++++++++-- drivers/block/zram/zram_drv.h | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 1f1bf175a6c3..a14aef6bf634 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -178,6 +178,7 @@ static inline u32 zram_get_priority(struct zram *zram, = u32 index) static void zram_accessed(struct zram *zram, u32 index) { zram_clear_flag(zram, index, ZRAM_IDLE); + zram_clear_flag(zram, index, ZRAM_PP_SLOT); #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME zram->table[index].ac_time =3D ktime_get_boottime(); #endif @@ -659,8 +660,9 @@ static ssize_t writeback_store(struct device *dev, goto next; =20 if (zram_test_flag(zram, index, ZRAM_WB) || - zram_test_flag(zram, index, ZRAM_SAME) || - zram_test_flag(zram, index, ZRAM_UNDER_WB)) + zram_test_flag(zram, index, ZRAM_SAME) || + zram_test_flag(zram, index, ZRAM_PP_SLOT) || + zram_test_flag(zram, index, ZRAM_UNDER_WB)) goto next; =20 if (mode & IDLE_WRITEBACK && @@ -1368,6 +1370,9 @@ static void zram_free_page(struct zram *zram, size_t = index) goto out; } =20 + if (zram_test_flag(zram, index, ZRAM_PP_SLOT)) + zram_clear_flag(zram, index, ZRAM_PP_SLOT); + handle =3D zram_get_handle(zram, index); if (!handle) return; @@ -1927,6 +1932,7 @@ static ssize_t recompress_store(struct device *dev, if (zram_test_flag(zram, index, ZRAM_WB) || zram_test_flag(zram, index, ZRAM_UNDER_WB) || zram_test_flag(zram, index, ZRAM_SAME) || + zram_test_flag(zram, index, ZRAM_PP_SLOT) || zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE)) goto next; =20 diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index b976824ead67..e0578b3542ce 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -50,6 +50,7 @@ enum zram_pageflags { ZRAM_SAME, /* Page consists the same element */ ZRAM_WB, /* page is stored on backing_device */ ZRAM_UNDER_WB, /* page is under writeback */ + ZRAM_PP_SLOT, /* Selected for post-processing */ ZRAM_HUGE, /* Incompressible page */ ZRAM_IDLE, /* not accessed page since last idle marking */ ZRAM_INCOMPRESSIBLE, /* none of the algorithms could compress it */ --=20 2.46.0.469.g59c65b2a67-goog From nobody Sat Nov 30 16:31:44 2024 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5834B15855F for ; Sun, 8 Sep 2024 11:42:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725795766; cv=none; b=u/HHF8aMqUPjKBR+Ht6q0ynf4YamMOZp79vOnWfEbWUTmDoAMlayTzC+qnLpHrogxGWr4GclSDIQPYcKPg6YbHlcmlU5EDu0NhWPZ/bIxwTrV0MDH+VQjuIRrI6y+yHEnuyyqlfd1e0WWAERZ9A78oGr1J9c+akKMg7b3Bzo7uY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725795766; c=relaxed/simple; bh=tp6rTqQQAU4Ay9dSuWGPEHR6UMPSKC8Go5EZXmOpigE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gbEoE1tOXrzX1L+QIYFke/+0+CXsGTMg6KmUm9rzLW+7zg58wXTyqs++uT113tnJo/wwAgw7UYtn6Y2Kt2pG6klrYN/PUpTbodsM99jOyvyFwanTcIsmztpvlECSzT2+oKV1U2DnxhbKi253bzSJLhfT8TqiZ/V6OJDRWGgCXfo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=HuZrwqYi; arc=none smtp.client-ip=209.85.167.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="HuZrwqYi" Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3df0a54623dso2147562b6e.0 for ; Sun, 08 Sep 2024 04:42:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725795763; x=1726400563; darn=vger.kernel.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=AspJWuawxjaqHf9bHwYmf6t2nWc/qRKC7B2AVEtY0Qo=; b=HuZrwqYitvOlhr0iHvA5RfG9qEcnHLHkA73U9o53UtZh13ese054AIr+IRrdPztXbx 2jg3XLdKr3le3YdE07MZZgKUAlrvCFzodwF7OAFOK2Fq+ebl2lvdYQU7TOPTFOom+tBs CQshDO6PS+Zt1vYjoBLMiYnzhxvLknSuHDH9A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725795763; x=1726400563; 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=AspJWuawxjaqHf9bHwYmf6t2nWc/qRKC7B2AVEtY0Qo=; b=PIyK15GMx3qQCXVQHXlu5dZZ8dGUBrmtbdmmzBIFUcuRkSDQj67JDKHtOUoVNKiVPq /2JG3iQbU7+KJrf9j48wlvfTAkrR3tvdCOuMvtCTHS+WfbDG/hZSKkh6Bcf/fcJZzkA3 4XBjB85KxmJNgpQdYxrYUk+auC3nX8KBlDhYuPHRqyvMw9qPujrvvQFxVFsBRSaaF+k1 iYVFWX1ZOIu+6iDFILxaE0COJVio883OmGw9J5LHd9lD9Rer54cRLDNbo6gRFHLXavP+ 2oa/iR+6nXYYKcBiRe7JnqjlxTy8WZMPfPhEo/DDRVf5goq5beaIgtF4CfCJxPeAoggn 5EEQ== X-Forwarded-Encrypted: i=1; AJvYcCVRi2J1OLvebL8mE9rxsmc6KEXWBaRt/wVEx83Qv/TxeZnIOHZuU1QEIkzzzgaxl33IUEFfaFK/IkoPb3U=@vger.kernel.org X-Gm-Message-State: AOJu0Ywkodp6BV+C+KisOKFOuwHnllid+82c1rD6T1s6Zsfiev3opj0+ TK1AKENGugCBIZ8/NW0n/eOO1+j76hbfn3ZuweFGqhYtj1xfilIc5Eq3Ei3AMg== X-Google-Smtp-Source: AGHT+IFHMMtxoKuQmQBP5V6iLmMSspKYeLjyOIpV57Jvx6sPrcz/dLnOiPKr/Qbrwhf/28Du3VUmQw== X-Received: by 2002:a05:6808:3c4b:b0:3d6:5791:a378 with SMTP id 5614622812f47-3e029ce5904mr8745490b6e.17.1725795763353; Sun, 08 Sep 2024 04:42:43 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:5879:695a:4656:45ff]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d823cf3a2esm1923029a12.24.2024.09.08.04.42.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Sep 2024 04:42:42 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton Cc: Richard Chang , linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 2/3] zram: rework recompress target selection strategy Date: Sun, 8 Sep 2024 20:42:02 +0900 Message-ID: <20240908114223.3024872-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240908114223.3024872-1-senozhatsky@chromium.org> References: <20240908114223.3024872-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Target slot selection for recompression is just a simple iteration over zram->table entries (stored pages) from slot 0 to max slot. Given that zram->table slots are written in random order and are not sorted by size, a simple iteration over slots selects suboptimal targets for recompression. This is not a problem if we recompress every single zram->table slot, but we never do that in reality. In reality we limit the number of slots we can recompress (via max_pages parameter) and hence proper slot selection becomes very important. The strategy is quite simple, suppose we have two candidate slots for recompression, one of size 48 bytes and one of size 2800 bytes, and we can recompress only one, then it certainly makes more sense to pick 2800 entry for recompression. Because even if we manage to compress 48 bytes objects even further the savings are going to be very small. Potential savings after good re-compression of 2800 bytes objects are much higher. This patch reworks slot selection and introduces the strategy described above: among candidate slots always select the biggest ones first. For that the patch introduces zram_pp_ctl (post-processing) structure which holds NUM_PP_BUCKETS pp buckets of slots. Slots are assigned to a particular group based on their sizes - the larger the size of the slot the higher the group index. This, basically, sorts slots by size in liner time (we still perform just one iteration over zram->table slots). When we select slot for recompression we always first lookup in higher pp buckets (those that hold the largest slots). Which achieves the desired behavior. TEST =3D=3D=3D=3D A very simple demonstration: zram is configured with zstd, and zstd with dict as a recompression stream. A limited (max 4096 pages) recompression is performed then, with a log of sizes of slots that were recompressed. You can see that patched zram selects slots for recompression in significantly different manner, which leads to higher memory savings (see column #2 of mm_stat output). BASE ---- *** initial state of zram device /sys/block/zram0/mm_stat 1750994944 504491413 514203648 0 514203648 1 0 3420= 4 34204 *** recompress idle max_pages=3D4096 /sys/block/zram0/mm_stat 1750994944 504262229 514953216 0 514203648 1 0 3420= 4 34204 Sizes of selected objects for recompression: ... 45 58 24 226 91 40 24 24 24 424 2104 93 2078 2078 2078 959 154 ... PATCHED ------- *** initial state of zram device /sys/block/zram0/mm_stat 1750982656 504492801 514170880 0 514170880 1 0 3420= 4 34204 *** recompress idle max_pages=3D4096 /sys/block/zram0/mm_stat 1750982656 503716710 517586944 0 514170880 1 0 3420= 4 34204 Sizes of selected objects for recompression: ... 3680 3694 3667 3590 3614 3553 3537 3548 3550 3542 3543 3537 ... Note, pp-slots are not strictly sorted, there is a PP_BUCKET_SIZE_RANGE variation of sizes within particular bucket. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 191 +++++++++++++++++++++++++++++----- 1 file changed, 163 insertions(+), 28 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index a14aef6bf634..026e527ab17d 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -184,6 +184,100 @@ static void zram_accessed(struct zram *zram, u32 inde= x) #endif } =20 +#ifdef CONFIG_ZRAM_MULTI_COMP +struct zram_pp_slot { + unsigned long index; + struct list_head entry; +}; + +/* + * A post-processing bucket is, essentially, a size class, this defines + * the range (in bytes) of pp-slots sizes in particular bucket. + */ +#define PP_BUCKET_SIZE_RANGE 64 +#define NUM_PP_BUCKETS ((PAGE_SIZE / PP_BUCKET_SIZE_RANGE) + 1) + +struct zram_pp_ctl { + struct list_head pp_buckets[NUM_PP_BUCKETS]; +}; + +static struct zram_pp_ctl *init_pp_ctl(void) +{ + struct zram_pp_ctl *ctl; + u32 idx; + + ctl =3D kmalloc(sizeof(*ctl), GFP_KERNEL); + if (!ctl) + return NULL; + + for (idx =3D 0; idx < NUM_PP_BUCKETS; idx++) + INIT_LIST_HEAD(&ctl->pp_buckets[idx]); + return ctl; +} + +static void release_pp_slot(struct zram *zram, struct zram_pp_slot *pps) +{ + zram_slot_lock(zram, pps->index); + if (zram_test_flag(zram, pps->index, ZRAM_PP_SLOT)) + zram_clear_flag(zram, pps->index, ZRAM_PP_SLOT); + zram_slot_unlock(zram, pps->index); + kfree(pps); +} + +static void release_pp_ctl(struct zram *zram, struct zram_pp_ctl *ctl) +{ + u32 idx; + + if (!ctl) + return; + + for (idx =3D 0; idx < NUM_PP_BUCKETS; idx++) { + while (!list_empty(&ctl->pp_buckets[idx])) { + struct zram_pp_slot *pps; + + pps =3D list_first_entry(&ctl->pp_buckets[idx], + struct zram_pp_slot, + entry); + list_del_init(&pps->entry); + release_pp_slot(zram, pps); + } + } + + kfree(ctl); +} + +static void place_pp_slot(struct zram *zram, struct zram_pp_ctl *ctl, + struct zram_pp_slot *pps) +{ + u32 idx; + + idx =3D zram_get_obj_size(zram, pps->index) / PP_BUCKET_SIZE_RANGE; + list_add(&pps->entry, &ctl->pp_buckets[idx]); + + zram_set_flag(zram, pps->index, ZRAM_PP_SLOT); +} + +static struct zram_pp_slot *select_pp_slot(struct zram_pp_ctl *ctl) +{ + struct zram_pp_slot *pps =3D NULL; + s32 idx =3D NUM_PP_BUCKETS - 1; + + /* The higher the bucket id the more optimal slot post-processing is */ + while (idx > 0) { + pps =3D list_first_entry_or_null(&ctl->pp_buckets[idx], + struct zram_pp_slot, + entry); + if (pps) { + list_del_init(&pps->entry); + break; + } + + idx--; + } + return pps; +} +#endif + static inline void update_used_max(struct zram *zram, const unsigned long pages) { @@ -1650,6 +1744,54 @@ static int zram_bvec_write(struct zram *zram, struct= bio_vec *bvec, } =20 #ifdef CONFIG_ZRAM_MULTI_COMP +#define RECOMPRESS_IDLE (1 << 0) +#define RECOMPRESS_HUGE (1 << 1) + +static int scan_slots_for_recompress(struct zram *zram, u32 mode, + struct zram_pp_ctl *ctl) +{ + unsigned long nr_pages =3D zram->disksize >> PAGE_SHIFT; + struct zram_pp_slot *pps =3D NULL; + unsigned long index; + + for (index =3D 0; index < nr_pages; index++) { + if (!pps) + pps =3D kmalloc(sizeof(*pps), GFP_KERNEL); + if (!pps) + return -ENOMEM; + + INIT_LIST_HEAD(&pps->entry); + + zram_slot_lock(zram, index); + if (!zram_allocated(zram, index)) + goto next; + + if (mode & RECOMPRESS_IDLE && + !zram_test_flag(zram, index, ZRAM_IDLE)) + goto next; + + if (mode & RECOMPRESS_HUGE && + !zram_test_flag(zram, index, ZRAM_HUGE)) + goto next; + + if (zram_test_flag(zram, index, ZRAM_WB) || + zram_test_flag(zram, index, ZRAM_UNDER_WB) || + zram_test_flag(zram, index, ZRAM_PP_SLOT) || + zram_test_flag(zram, index, ZRAM_SAME) || + zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE)) + goto next; + + pps->index =3D index; + place_pp_slot(zram, ctl, pps); + pps =3D NULL; +next: + zram_slot_unlock(zram, index); + } + + kfree(pps); + return 0; +} + /* * This function will decompress (unless it's ZRAM_HUGE) the page and then * attempt to compress it using provided compression algorithm priority @@ -1657,7 +1799,7 @@ static int zram_bvec_write(struct zram *zram, struct = bio_vec *bvec, * * Corresponding ZRAM slot should be locked. */ -static int zram_recompress(struct zram *zram, u32 index, struct page *page, +static int recompress_slot(struct zram *zram, u32 index, struct page *page, u64 *num_recomp_pages, u32 threshold, u32 prio, u32 prio_max) { @@ -1677,6 +1819,7 @@ static int zram_recompress(struct zram *zram, u32 ind= ex, struct page *page, return -EINVAL; =20 comp_len_old =3D zram_get_obj_size(zram, index); + /* * Do not recompress objects that are already "small enough". */ @@ -1800,20 +1943,17 @@ static int zram_recompress(struct zram *zram, u32 i= ndex, struct page *page, return 0; } =20 -#define RECOMPRESS_IDLE (1 << 0) -#define RECOMPRESS_HUGE (1 << 1) - static ssize_t recompress_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { u32 prio =3D ZRAM_SECONDARY_COMP, prio_max =3D ZRAM_MAX_COMPS; struct zram *zram =3D dev_to_zram(dev); - unsigned long nr_pages =3D zram->disksize >> PAGE_SHIFT; char *args, *param, *val, *algo =3D NULL; u64 num_recomp_pages =3D ULLONG_MAX; + struct zram_pp_ctl *ctl =3D NULL; + struct zram_pp_slot *pps; u32 mode =3D 0, threshold =3D 0; - unsigned long index; struct page *page; ssize_t ret; =20 @@ -1909,37 +2049,31 @@ static ssize_t recompress_store(struct device *dev, goto release_init_lock; } =20 + ctl =3D init_pp_ctl(); + if (!ctl) { + ret =3D -ENOMEM; + goto release_init_lock; + } + scan_slots_for_recompress(zram, mode, ctl); + ret =3D len; - for (index =3D 0; index < nr_pages; index++) { + while ((pps =3D select_pp_slot(ctl))) { int err =3D 0; =20 if (!num_recomp_pages) break; =20 - zram_slot_lock(zram, index); - - if (!zram_allocated(zram, index)) - goto next; - - if (mode & RECOMPRESS_IDLE && - !zram_test_flag(zram, index, ZRAM_IDLE)) - goto next; - - if (mode & RECOMPRESS_HUGE && - !zram_test_flag(zram, index, ZRAM_HUGE)) + zram_slot_lock(zram, pps->index); + if (!zram_test_flag(zram, pps->index, ZRAM_PP_SLOT)) goto next; =20 - if (zram_test_flag(zram, index, ZRAM_WB) || - zram_test_flag(zram, index, ZRAM_UNDER_WB) || - zram_test_flag(zram, index, ZRAM_SAME) || - zram_test_flag(zram, index, ZRAM_PP_SLOT) || - zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE)) - goto next; - - err =3D zram_recompress(zram, index, page, &num_recomp_pages, - threshold, prio, prio_max); + err =3D recompress_slot(zram, pps->index, page, + &num_recomp_pages, threshold, + prio, prio_max); next: - zram_slot_unlock(zram, index); + zram_slot_unlock(zram, pps->index); + release_pp_slot(zram, pps); + if (err) { ret =3D err; break; @@ -1951,6 +2085,7 @@ static ssize_t recompress_store(struct device *dev, __free_page(page); =20 release_init_lock: + release_pp_ctl(zram, ctl); up_read(&zram->init_lock); return ret; } --=20 2.46.0.469.g59c65b2a67-goog From nobody Sat Nov 30 16:31:44 2024 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95673158D8F for ; Sun, 8 Sep 2024 11:42:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725795768; cv=none; b=AqWOlPWUywAu+aIcGhu4xUHRZk6aaQLmDd1ouy/HzkrbEnTtaAT3eHoVjoAKSYyWxkr3SbLK7CTjbxZdqwquDWu9yRTMzpZF6T2/c/XOUDdgbHIJ6J4pGSuc8nnA5ZBcVeE20585hp18MiY7xERV5oAXmKOB/QWlxCjGcJ7opA4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725795768; c=relaxed/simple; bh=uB2P6UdqMe2nA8UbgN/fT//xtZdaOt0IJFABGdRXRq8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u9hgbiDIMynNWyExHU8UsM2sSdp2erqaFmAyfyeag7rvAGmB1tRp/ftDbkfB6DY5iwZV14YPQKTgQrALPIDFADPBYZhOARGSGSbAHoPtdmkIqivx2oAx0PYPzdjYo7dpjOrL3xnuGhh2TERQdwqEyF7jugqLBb2C1b+LG4hYUCY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=h1sh07l0; arc=none smtp.client-ip=209.85.210.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="h1sh07l0" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-718d606726cso1574000b3a.3 for ; Sun, 08 Sep 2024 04:42:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1725795766; x=1726400566; darn=vger.kernel.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=9TkuowECJ5u49hZzXfADAw0z+4PgfCTTtJrp7ALFdMU=; b=h1sh07l0yUqRYZxf4eJ6TtcAUQpgKJiDdJbycd5iwI1Wmg1QsQYvVSMD+7fDDLolxt 5K+2KftRWvtKA5Oo8ABsbJjAvyjUcNAVKrqZsjPOSdVCdI6fayTtL/ng8s8XjAkUD7RQ Ku+npI6T6WoDToHGYsTH5khJfEflpzpBYQKzY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725795766; x=1726400566; 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=9TkuowECJ5u49hZzXfADAw0z+4PgfCTTtJrp7ALFdMU=; b=UA1aWoOoFbRmWAnb6yVsSC+SuGgzQSIN+KCfwo9S8SoBQOHQhNmNxChv27F6ez0qb7 yj/L2e01CZEP2QREPHVq9NEBA+ifZ5yW62ebLjNnaQJ5N/T8+F5jJbRNIZbSHj4i9ka4 mAfI3kHQOTxqtUDjpFJrmkEgcC+kehTNQYJ0cRSe+fdsUlBeqCXIKieXbLNitrANGab0 Yp6eqkvydSRYb9bmo4bPTDu1arrPynJOkcPVENK91SGUu59LCH3j+0oQdIK7MpxofQjF qwowhxzmDrQuq8EASQxd935LNWjBUC7E9uWJhWm9lspl7MC5K3UOebHi1PSuDIsHtJHd GiNg== X-Forwarded-Encrypted: i=1; AJvYcCW8fVWcTFWymeh3x8rSXaP9qUbcfYeFHldypLIk5b3bSwhseqQGPJ5wKgEyO3N2SIdRB18acU+zuEX3Kt0=@vger.kernel.org X-Gm-Message-State: AOJu0YzoR3P5Rj6aabCiO7yl6sPT3ea2V5ifk+bCdOL8rP1nCa/YggoM ZvNExoy1TYtbsDs5nliIs/+6Qk4ao3ZixzJi5DRmI5LhiDUVdP7bcZwaUxCvVg== X-Google-Smtp-Source: AGHT+IEg5WmRyNMnxMybT5B3YUy42OkXfObdvnmTOJP2nrr4O5qM0PIP4wOHo8+mFs0N3QC0XY61sA== X-Received: by 2002:a05:6a21:392:b0:1cc:e55b:6892 with SMTP id adf61e73a8af0-1cf1d1ed9a9mr8176596637.47.1725795765799; Sun, 08 Sep 2024 04:42:45 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:5879:695a:4656:45ff]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d823cf3a2esm1923029a12.24.2024.09.08.04.42.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Sep 2024 04:42:45 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton Cc: Richard Chang , linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv2 3/3] zram: rework writeback target selection strategy Date: Sun, 8 Sep 2024 20:42:03 +0900 Message-ID: <20240908114223.3024872-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240908114223.3024872-1-senozhatsky@chromium.org> References: <20240908114223.3024872-1-senozhatsky@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Writeback suffers from the same problem as recompression did before - target slot selection for writeback is just a simple iteration over zram->table entries (stored pages) which selects suboptimal targets for writeback. This is especially problematic for writeback, because we uncompress objects before writeback so each of them takes 4K out of limited writeback storage. For example, when we take a 48 bytes slot and store it as a 4K object to writeback device we only save 48 bytes of memory (release from zsmalloc pool). We naturally want to pick the largest objects for writeback, because then each writeback will release the largest amount of memory. This patch applies the same solution and strategy as for recompression target selection: pp control (post-process) with 16 buckets of candidate pp slots. Slots are assigned to pp buckets based on sizes - the larger the slot the higher the group index. This gives us sorted by size lists of candidate slots (in linear time), so that among post-processing candidate slots we always select the largest ones first and maximize the memory saving. TEST =3D=3D=3D=3D A very simple demonstration: zram is configured with a writeback device. A limited writeback (wb_limit 2500 pages) is performed then, with a log of sizes of slots that were written back. You can see that patched zram selects slots for recompression in significantly different manner, which leads to higher memory savings (see column #2 of mm_stat output). BASE ---- *** initial state of zram device /sys/block/zram0/mm_stat 1750327296 619765836 631902208 0 631902208 1 0 3427= 8 34278 *** writeback idle wb_limit 2500 /sys/block/zram0/mm_stat 1750327296 617622333 631578624 0 631902208 1 0 3427= 8 34278 Sizes of selected objects for writeback: ... 193 349 46 46 46 46 852 1002 543 162 107 49 34 34 34 ... PATCHED ------- *** initial state of zram device /sys/block/zram0/mm_stat 1750319104 619760957 631992320 0 631992320 1 0 3427= 8 34278 *** writeback idle wb_limit 2500 /sys/block/zram0/mm_stat 1750319104 612672056 626135040 0 631992320 1 0 3427= 8 34278 Sizes of selected objects for writeback: ... 3667 3580 3581 3580 3581 3581 3581 3231 3211 3203 3231 3246 ... Note, pp-slots are not strictly sorted, there is a PP_BUCKET_SIZE_RANGE variation of sizes within particular bucket. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 88 +++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 20 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 026e527ab17d..d0da6bb4be79 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -184,7 +184,7 @@ static void zram_accessed(struct zram *zram, u32 index) #endif } =20 -#ifdef CONFIG_ZRAM_MULTI_COMP +#if defined CONFIG_ZRAM_WRITEBACK || defined CONFIG_ZRAM_MULTI_COMP struct zram_pp_slot { unsigned long index; struct list_head entry; @@ -682,11 +682,59 @@ static void read_from_bdev_async(struct zram *zram, s= truct page *page, #define IDLE_WRITEBACK (1<<1) #define INCOMPRESSIBLE_WRITEBACK (1<<2) =20 +static int scan_slots_for_writeback(struct zram *zram, u32 mode, + unsigned long nr_pages, + unsigned long index, + struct zram_pp_ctl *ctl) +{ + struct zram_pp_slot *pps =3D NULL; + + for (; nr_pages !=3D 0; index++, nr_pages--) { + if (!pps) + pps =3D kmalloc(sizeof(*pps), GFP_KERNEL); + if (!pps) + return -ENOMEM; + + INIT_LIST_HEAD(&pps->entry); + + zram_slot_lock(zram, index); + if (!zram_allocated(zram, index)) + goto next; + + if (zram_test_flag(zram, index, ZRAM_WB) || + zram_test_flag(zram, index, ZRAM_SAME) || + zram_test_flag(zram, index, ZRAM_PP_SLOT) || + zram_test_flag(zram, index, ZRAM_UNDER_WB)) + goto next; + + if (mode & IDLE_WRITEBACK && + !zram_test_flag(zram, index, ZRAM_IDLE)) + goto next; + if (mode & HUGE_WRITEBACK && + !zram_test_flag(zram, index, ZRAM_HUGE)) + goto next; + if (mode & INCOMPRESSIBLE_WRITEBACK && + !zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE)) + goto next; + + pps->index =3D index; + place_pp_slot(zram, ctl, pps); + pps =3D NULL; +next: + zram_slot_unlock(zram, index); + } + + kfree(pps); + return 0; +} + static ssize_t writeback_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { struct zram *zram =3D dev_to_zram(dev); unsigned long nr_pages =3D zram->disksize >> PAGE_SHIFT; + struct zram_pp_ctl *ctl =3D NULL; + struct zram_pp_slot *pps; unsigned long index =3D 0; struct bio bio; struct bio_vec bio_vec; @@ -732,11 +780,19 @@ static ssize_t writeback_store(struct device *dev, goto release_init_lock; } =20 - for (; nr_pages !=3D 0; index++, nr_pages--) { + ctl =3D init_pp_ctl(); + if (!ctl) { + ret =3D -ENOMEM; + goto release_init_lock; + } + scan_slots_for_writeback(zram, mode, nr_pages, index, ctl); + + while ((pps =3D select_pp_slot(ctl))) { spin_lock(&zram->wb_limit_lock); if (zram->wb_limit_enable && !zram->bd_wb_limit) { spin_unlock(&zram->wb_limit_lock); ret =3D -EIO; + release_pp_slot(zram, pps); break; } spin_unlock(&zram->wb_limit_lock); @@ -745,30 +801,15 @@ static ssize_t writeback_store(struct device *dev, blk_idx =3D alloc_block_bdev(zram); if (!blk_idx) { ret =3D -ENOSPC; + release_pp_slot(zram, pps); break; } } =20 + index =3D pps->index; zram_slot_lock(zram, index); - if (!zram_allocated(zram, index)) + if (!zram_test_flag(zram, index, ZRAM_PP_SLOT)) goto next; - - if (zram_test_flag(zram, index, ZRAM_WB) || - zram_test_flag(zram, index, ZRAM_SAME) || - zram_test_flag(zram, index, ZRAM_PP_SLOT) || - zram_test_flag(zram, index, ZRAM_UNDER_WB)) - goto next; - - if (mode & IDLE_WRITEBACK && - !zram_test_flag(zram, index, ZRAM_IDLE)) - goto next; - if (mode & HUGE_WRITEBACK && - !zram_test_flag(zram, index, ZRAM_HUGE)) - goto next; - if (mode & INCOMPRESSIBLE_WRITEBACK && - !zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE)) - goto next; - /* * Clearing ZRAM_UNDER_WB is duty of caller. * IOW, zram_free_page never clear it. @@ -777,11 +818,14 @@ static ssize_t writeback_store(struct device *dev, /* Need for hugepage writeback racing */ zram_set_flag(zram, index, ZRAM_IDLE); zram_slot_unlock(zram, index); + if (zram_read_page(zram, page, index, NULL)) { zram_slot_lock(zram, index); zram_clear_flag(zram, index, ZRAM_UNDER_WB); zram_clear_flag(zram, index, ZRAM_IDLE); zram_slot_unlock(zram, index); + + release_pp_slot(zram, pps); continue; } =20 @@ -800,6 +844,8 @@ static ssize_t writeback_store(struct device *dev, zram_clear_flag(zram, index, ZRAM_UNDER_WB); zram_clear_flag(zram, index, ZRAM_IDLE); zram_slot_unlock(zram, index); + + release_pp_slot(zram, pps); /* * BIO errors are not fatal, we continue and simply * attempt to writeback the remaining objects (pages). @@ -842,12 +888,14 @@ static ssize_t writeback_store(struct device *dev, spin_unlock(&zram->wb_limit_lock); next: zram_slot_unlock(zram, index); + release_pp_slot(zram, pps); } =20 if (blk_idx) free_block_bdev(zram, blk_idx); __free_page(page); release_init_lock: + release_pp_ctl(zram, ctl); up_read(&zram->init_lock); =20 return ret; --=20 2.46.0.469.g59c65b2a67-goog