From nobody Mon Feb 9 03:27:18 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.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 3C68313C918 for ; Wed, 11 Sep 2024 08:01:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041705; cv=none; b=OmNOk9EihSs1MMc/hzd7EaPzATgk6mZ2rBox05/3t1HER7aLJzbRRYAar7yieeUT+ULKAID3gC/stMYRqrH9f3GI9kQwbrhrTyVkoG+qvamCyjdGKu1akfPiE7GgAXo0bLCAZmTwgclrYRQKrryhCu7bzO7u+pC4yOmrXJpZ/VQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041705; c=relaxed/simple; bh=cMrFZFeYshEhBFOfKSSKpG6P8VFIuhAwYxrlWBPSmzs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ij+5lAhcWjIuilH+HRl3izDSkOEQNNlxz0WAGm38uMg3XdLikRibobeRqEOGXnoBKqtR+5x616KIQD3xgEfugJnrmVCQWiLfsKN1sZeLMrwBto/GVLaB03Yq1rrPkFzy7usL+GAkSM83VPUrVwJWuQALqsgzIPqjgZoFXcBU4fE= 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=Yqkgdkqk; arc=none smtp.client-ip=209.85.214.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="Yqkgdkqk" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-20543fdb7acso43958055ad.1 for ; Wed, 11 Sep 2024 01:01:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726041702; x=1726646502; 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=AwMu+l4YrL6K6neHxIk3gwGHEtDDHlzEJ+d/igKF6jE=; b=YqkgdkqkJ4NF5q9hmWq793l5pcpDCGE+nL9VgpwAHs8mxViMWtui+dC9g8a6+vzhN9 2a9bVvYMmirBDrshovE4xqXyY4MTYnubLBDOxe6GwCM7I4MKahSYpbSx3MWj43ftic+1 S5URBN4ProA1dX99DSSIdeAQh8qNZ1ZIu5uDs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726041702; x=1726646502; 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=AwMu+l4YrL6K6neHxIk3gwGHEtDDHlzEJ+d/igKF6jE=; b=hA7OhLW9nCY7Z+sEe9+CWqVj9gYqUSOWXNHW8oRt3ga0sOzOQbFU+pYXaI8HH965Ra 10Izb+SuiyduphjD1avu1go/3QFPD3howQWaQqbAqvs5OHM2acBg7joxoK12z7B7Dhnq 5Xjc2NWN5es+siL93nP3ALGo5hQrTXG9o8UoabYUjmuNQRo+4VUd+kiSAiNcIVuSVWoY 3Y2pU7Scr6cxXB3ze5N7tj3lYVWCCsogVo8TXQqBYd+aQPpv2wDoY+azEMt9J2pqZJVl +i4+MBzTlZWZfQL6hvQIP8EZV4VN5DTsuLFbbT/AUWSAGjz/6cslxp+7/DoEHTVhMGXc elfw== X-Gm-Message-State: AOJu0Yy1nWXgnGEduwAgCMH2ci5YD541PB+tLKFIuryB2HNinaY/Z1sY +Z+0fG3GSxZhERiL9gNUrhtMzxMmY9GHrHcfSyTvci5Q10SSt8u13CPdxp5bzn5CKQ36u0NZqHi CXA== X-Google-Smtp-Source: AGHT+IEJ5kdKi8ZHj36iu4mabyJVW1yVRBuLobeZh0LrkxNR6h67V1dnTXCqPCDJpMGGRu6vVcoxmQ== X-Received: by 2002:a17:902:c94d:b0:1fb:2bed:6418 with SMTP id d9443c01a7336-2074c79a82amr53297775ad.57.1726041702520; Wed, 11 Sep 2024 01:01:42 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:d828:3442:b431:2762]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20710e37922sm58737795ad.110.2024.09.11.01.01.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 01:01:42 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 1/6] zram: introduce ZRAM_PP_SLOT flag Date: Wed, 11 Sep 2024 17:01:09 +0900 Message-ID: <20240911080130.3766632-2-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911080130.3766632-1-senozhatsky@chromium.org> References: <20240911080130.3766632-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 indicates that the slot was selected as a candidate slot for post-processing (pp) and was assigned to a pp bucket. It does not necessarily mean that the slot is currently under post-processing, but may mean so. The slot can loose its PP_SLOT flag, while still being in the pp-bucket, if it's accessed or slot_free-ed. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 2 ++ drivers/block/zram/zram_drv.h | 1 + 2 files changed, 3 insertions(+) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index ee2a279c5f25..d170bf6cdcd8 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 @@ -1354,6 +1355,7 @@ static void zram_free_page(struct zram *zram, size_t = index) zram_clear_flag(zram, index, ZRAM_INCOMPRESSIBLE); =20 zram_set_priority(zram, index, 0); + zram_clear_flag(zram, index, ZRAM_PP_SLOT); =20 if (zram_test_flag(zram, index, ZRAM_WB)) { zram_clear_flag(zram, index, ZRAM_WB); diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index cfc8c059db63..914cb6629969 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -48,6 +48,7 @@ enum zram_pageflags { ZRAM_SAME =3D ZRAM_FLAG_SHIFT, /* 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.598.g6f2099f65c-goog From nobody Mon Feb 9 03:27:18 2026 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 8E992143888 for ; Wed, 11 Sep 2024 08:01:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041707; cv=none; b=bZPVHe9FWYKSq1vBEZUuXsxvTUgJiOZP79dGacMUmRj4fpDfx6mU8kt0peCcbZEEzCZA+H+Wge6xBTFSTmPNSuxDX4iDrm3g3dVWpRp8iB5gYkCO8u2lScsiU1qBQXP3qj2EBuJBFgBUrMFB6TQvKh2gNlOF2gcxnohJoqYo6Xg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041707; c=relaxed/simple; bh=mVkL7p64iZ9keq4GdB2NspvQm9i3kQpUaGhTZNUXJH4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jBfsP2sDbsa224eUca8bJ2dWVbsWPgV2LPTX27G2zxnWkMd/sqyEAS7q00BnrcDSy9ZGMz/BRpfDClucB0DhGatTaalLnz7DNecDXPy+YKk0vSfzTBNrcQkQHn83b48Sorz9gRZSgSiCmPyeOxa5kKJemXjrBN+MdYw5zVVXfqI= 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=TCjXZcFq; arc=none smtp.client-ip=209.85.214.170 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="TCjXZcFq" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-206aee40676so53348445ad.0 for ; Wed, 11 Sep 2024 01:01:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726041705; x=1726646505; 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=9HLUCUcTOHEbZr3FT9+WxYtkzi909Grd+YuDams/I+k=; b=TCjXZcFqWfjazn4paZ2q9Y45YSescoYW1eYQ7EDFc+2n3Sj0OvCkAy3KFxsuAiBBKf S9uAvxDwKeEcSEVLN1Je+85P/mKcN9I1ZGd+6Tm18OB4dr7Vnna6CnV1CJlgiBI65P+Z hoEonHbUelJlF/0MFuiQnMK++trfZFUc8AJi8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726041705; x=1726646505; 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=9HLUCUcTOHEbZr3FT9+WxYtkzi909Grd+YuDams/I+k=; b=AmzezBvefxbFyjhVg63xLT9iHwtDR0KgAWzeDpMilmxVd7vIBuWLHdhiCcuRm/qKpP kXf5RuvR8evEZP+MWIM3spJ79xd58h7uArA6/z/JIsQ4trLVyMoOn6tw1Kw2bi0Vg4BS nOKz7S0XZT8KmR3dQJniCde8s6RpQtzK5sxONWZ7T4QmwZC3oUDeOlvKJuu/EMNZ9kOu ape8hZeH0IIleLMEC5tP17lbxXnNpZLtXn+9scMT8lv7kZnXL69Gmnh144Pd5KrCz3oh N+D5Z7N8LF5qgYLUBk/Z5DClLWltLutGwWywhw8TdPYxXwVAXCtcfMvEHfI6CXjIIEmW AlOQ== X-Gm-Message-State: AOJu0Yw+Ntl/r4YWzn/VGv8ZlowZosTnne0wuLzMZGQOjYeysEaD5HAA QU4q2DUBaBscI3lh5zbh665naQaHNSgRKREZArqRkLxHslhe4QU+sFvpq+ycGw== X-Google-Smtp-Source: AGHT+IH62pYvbyIT+lENT5afek1422rnWnAov5igigZqz3EY7Vnadfsgb3a2GqvWMmzG4o++Gd/kNA== X-Received: by 2002:a17:903:11d2:b0:206:9536:9778 with SMTP id d9443c01a7336-2074c5d3daamr45477825ad.19.1726041704734; Wed, 11 Sep 2024 01:01:44 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:d828:3442:b431:2762]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20710e37922sm58737795ad.110.2024.09.11.01.01.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 01:01:44 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 2/6] zram: permit only one post-processing operation at a time Date: Wed, 11 Sep 2024 17:01:10 +0900 Message-ID: <20240911080130.3766632-3-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911080130.3766632-1-senozhatsky@chromium.org> References: <20240911080130.3766632-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" Both recompress and writeback soon will unlock slots during processing, which makes things too complex wrt possible race-conditions. We still want to clear PP_SLOT in slot_free, because this is how we figure out that slot that was selected for post-processing has been released under us and when we start post-processing we check if slot still has PP_SLOT set. At the same time, theoretically, we can have something like this: CPU0 CPU1 recompress scan slots set PP_SLOT unlock slot slot_free clear PP_SLOT allocate PP_SLOT writeback scan slots set PP_SLOT unlock slot select PP-slot test PP_SLOT So recompress will not detect that slot has been re-used and re-selected for concurrent writeback post-processing. Make sure that we only permit on post-processing operation at a time. So now recompress and writeback post-processing don't race against each other, we only need to handle slot re-use (slot_free and write), which is handled individually by each pp operation. Having recompress and writeback competing for the same slots is not exactly good anyway (can't imagine anyone doing that). Signed-off-by: Sergey Senozhatsky --- Documentation/admin-guide/blockdev/zram.rst | 2 ++ drivers/block/zram/zram_drv.c | 16 ++++++++++++++++ drivers/block/zram/zram_drv.h | 1 + 3 files changed, 19 insertions(+) diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/ad= min-guide/blockdev/zram.rst index 678d70d6e1c3..714a5171bfc0 100644 --- a/Documentation/admin-guide/blockdev/zram.rst +++ b/Documentation/admin-guide/blockdev/zram.rst @@ -47,6 +47,8 @@ The list of possible return codes: -ENOMEM zram was not able to allocate enough memory to fulfil your needs. -EINVAL invalid input has been provided. +-EAGAIN re-try operation later (e.g. when attempting to run recompress + and writeback simultaneously). =3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 If you use 'echo', the returned value is set by the 'echo' utility, diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index d170bf6cdcd8..c2fa6d3b2183 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -627,6 +627,12 @@ static ssize_t writeback_store(struct device *dev, goto release_init_lock; } =20 + /* Do not permit concurrent post-processing actions. */ + if (atomic_xchg(&zram->pp_in_progress, 1)) { + ret =3D -EAGAIN; + goto release_init_lock; + } + if (!zram->backing_dev) { ret =3D -ENODEV; goto release_init_lock; @@ -753,6 +759,7 @@ static ssize_t writeback_store(struct device *dev, free_block_bdev(zram, blk_idx); __free_page(page); release_init_lock: + atomic_set(&zram->pp_in_progress, 0); up_read(&zram->init_lock); =20 return ret; @@ -1883,6 +1890,12 @@ static ssize_t recompress_store(struct device *dev, goto release_init_lock; } =20 + /* Do not permit concurrent post-processing actions. */ + if (atomic_xchg(&zram->pp_in_progress, 1)) { + ret =3D -EAGAIN; + goto release_init_lock; + } + if (algo) { bool found =3D false; =20 @@ -1950,6 +1963,7 @@ static ssize_t recompress_store(struct device *dev, __free_page(page); =20 release_init_lock: + atomic_set(&zram->pp_in_progress, 0); up_read(&zram->init_lock); return ret; } @@ -2146,6 +2160,7 @@ static void zram_reset_device(struct zram *zram) zram->disksize =3D 0; zram_destroy_comps(zram); memset(&zram->stats, 0, sizeof(zram->stats)); + atomic_set(&zram->pp_in_progress, 0); reset_bdev(zram); =20 comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor); @@ -2383,6 +2398,7 @@ static int zram_add(void) zram->disk->fops =3D &zram_devops; zram->disk->private_data =3D zram; snprintf(zram->disk->disk_name, 16, "zram%d", device_id); + atomic_set(&zram->pp_in_progress, 0); =20 /* Actual capacity set using sysfs (/sys/block/zram/disksize */ set_capacity(zram->disk, 0); diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index 914cb6629969..73a9d47d76ba 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -140,5 +140,6 @@ struct zram { #ifdef CONFIG_ZRAM_MEMORY_TRACKING struct dentry *debugfs_dir; #endif + atomic_t pp_in_progress; }; #endif --=20 2.46.0.598.g6f2099f65c-goog From nobody Mon Feb 9 03:27:18 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 F0C58149DE3 for ; Wed, 11 Sep 2024 08:01:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041709; cv=none; b=RHFpMqdR03glquSydjvIf4m7gx0qLTHnr9mjMs+5tUHRmfiq737uPZXmKputC1M8gfRTLB++BL+rCDVYogPkWJQ1aCckTNhhMk4ziWQ/OuFl1aILPbKrJIVmfjImxQgdSLomIX3BQd1Pw5XBiYRXJyoB0DtZQpCixDdc0OBMRfQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041709; c=relaxed/simple; bh=xIACngLr7OcZKDKLnm9ooJAw8wFrkyh+kwRYIYxgHNI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lmwFntFey0KRU1ckUrRCuRn5kicnfwX08PGFNGCvFVQn/kIXd1YQOvLysH8ufjQX1ZUL+tkA19gxvrFO/oq87GITIyHL3MSrIESpgUEBjr0jZCGSbxA7AfTSjdezdMUwgMnj/zkzA2+G7fsCV8Ye8R5FtOnOlLbFeTfCU1HehGw= 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=CB7Bu537; arc=none smtp.client-ip=209.85.214.175 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="CB7Bu537" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2053525bd90so55790775ad.0 for ; Wed, 11 Sep 2024 01:01:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726041707; x=1726646507; 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=Ap5UaOOLXLlZhsY7B4tvmtUbAeB7owNl4mHv+sKVihQ=; b=CB7Bu537J2DfYqWaIgmoLw+PIi7iFt92ioaYRuWYcrIUJnYw3kihnA0jSUZw/Ud+wD HaqaD4N9FvBM3lDr8LC5Z4Y4KNO5dtfwO6xlGLtCi2is9r/xuiV+n/pc5ZhSR7enZZEy PF+0ZvNum2k3EUXDN51dNDZwTU/LgwbJizOhQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726041707; x=1726646507; 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=Ap5UaOOLXLlZhsY7B4tvmtUbAeB7owNl4mHv+sKVihQ=; b=dGZCKGF6oJXyUClO2NYv1ejMYaGf0pyiDpicbvhTQY9SIWLg1TF/+dnpx9QBQbVhYX S9htAaSJLPLOMNNJTdlTdK3UtFCQeXxdclxrJff9SvX2mnhrJ+elu9FhvW7SvyK5ylnq rPnNVwphfaKzVBud20++mT5MJvV+nJAIEFDT1AJwdprH3kjLiLIki77WIkF8h2XLhI0s cKNgvP/7iBh6G7HkL9dqVMg5iVmbtV75As3CrWVm8A9V5pQN9iuNxEbappBVAlZfh6wz Dv4aDppcO+B1NRNPDmQIxiNC+FXOhUGE4WwyqASldYYPO0D45bPycOQnqUFZJ0okx8lk sCbw== X-Gm-Message-State: AOJu0Yz58Csa+P0xoOBS+YzB3xPU7eUgQQOfGekeWzCNOvOvGgqpxEFy 1qwb4+FIvUNJCeS/vEsa2j7z00vgphEevaoEqeS/UYMm5sbSUtsT8Rx3O+8vKQ== X-Google-Smtp-Source: AGHT+IGRW3jyoEXmIpqgpRqbYFf4IUGBMSy8Z8EI0fdKJP3oFmZ0/WFi3NmS0y/rPt4sctYinYt8uA== X-Received: by 2002:a17:902:d2c5:b0:204:e4c9:ce91 with SMTP id d9443c01a7336-2074c5d3923mr53560395ad.7.1726041706994; Wed, 11 Sep 2024 01:01:46 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:d828:3442:b431:2762]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20710e37922sm58737795ad.110.2024.09.11.01.01.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 01:01:46 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 3/6] zram: rework recompress target selection strategy Date: Wed, 11 Sep 2024 17:01:11 +0900 Message-ID: <20240911080130.3766632-4-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911080130.3766632-1-senozhatsky@chromium.org> References: <20240911080130.3766632-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 | 187 +++++++++++++++++++++++++++++----- 1 file changed, 160 insertions(+), 27 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index c2fa6d3b2183..222e2e74f0da 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -184,6 +184,99 @@ static void zram_accessed(struct zram *zram, u32 index) #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) +{ + list_del_init(&pps->entry); + + zram_slot_lock(zram, pps->index); + 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); + 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) + break; + + idx--; + } + return pps; +} +#endif + static inline void update_used_max(struct zram *zram, const unsigned long pages) { @@ -1657,6 +1750,52 @@ 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_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 @@ -1664,7 +1803,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) { @@ -1807,20 +1946,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 @@ -1922,36 +2058,32 @@ 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)) + zram_slot_lock(zram, pps->index); + if (!zram_test_flag(zram, pps->index, ZRAM_PP_SLOT)) goto next; =20 - 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_SAME) || - 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; @@ -1963,6 +2095,7 @@ static ssize_t recompress_store(struct device *dev, __free_page(page); =20 release_init_lock: + release_pp_ctl(zram, ctl); atomic_set(&zram->pp_in_progress, 0); up_read(&zram->init_lock); return ret; --=20 2.46.0.598.g6f2099f65c-goog From nobody Mon Feb 9 03:27:18 2026 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 1348714A618 for ; Wed, 11 Sep 2024 08:01:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041711; cv=none; b=czibFICQqipghu8UkYgcRfujJwB0WPYyaV8fAnycnXAENVkefd1vHGvYKNAVn0YRRQN8u2ctUXN6UgvWt9e1aHg+e1fSgRKlTxfj2n5JwtyjQd+czgwlbHKJGAnyIS2cJ04wqqVTmJr+a23w4Z4gYRY5IocVv12NlO0Dy7OFY3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041711; c=relaxed/simple; bh=4LokRDtUC8nTDLjyNXXmw8PThG5ovYKVTg9qzeaQWQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WxPbm6UrZrtLyjzQyyYVNeKnj8nquaHPg8UXnWQOQYOZq70o2/gL+bRnomUfB6gtsj0JX7WvlVbPL99DuNaZ8G/jIutLeYHTjkCPbAqtfzTELvVqD6qnFsyilr5xj3SSXkCTn5QzJ0i8gHanVFElO3aCLtp3rrnaaKUtiedrHuU= 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=Mq1Q2dGu; arc=none smtp.client-ip=209.85.214.172 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="Mq1Q2dGu" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-205909af9b5so51501765ad.3 for ; Wed, 11 Sep 2024 01:01:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726041709; x=1726646509; 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=E6AI1g9AMxN+gMC88Xf2+2IG4T4Gob9Vc2tmg27NW6g=; b=Mq1Q2dGuQcI54in6M5jMbeCrNUFWtZbMIaOz5X2wIubCUBrpRJUMiGW7rbmdiEQjPo mk2KjLZj22ry4DcsmdYgPSfwqszRvrU+FmrHg6izXCIOdRsF2DNxFLvUsUxO+N0p1HXn +XGzIZ/nhQGPIwkWjAane6r1cRSmbKPejREmg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726041709; x=1726646509; 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=E6AI1g9AMxN+gMC88Xf2+2IG4T4Gob9Vc2tmg27NW6g=; b=M3XQIk1HALPZa4KAJZy+jcsxq8X6DsuWsOQiwlxDaAOF+dB4jyfaD4cYBS2jxuBOXS VPR3p0pmtyuziGKS2lzkbNTEY0X47kQWUc18DDRuJfe2LS+IXQ/3sU5P54I3nfYGi7ll XOfYKfXLMtkyQydAYwmvOyyhMsg+HItUF0VR4R+Wk1vqSv8NuJykhA0ei0lrHnuFm1do 2OJoFq5m5hAt/SSHU4JOzrJF5549b4czLHxFb0xGS6NTa4wfNUZJjsKnPbg/cw5UbZyK oggrg2b3yelxCcUiBge2Oi6+8HDpjGutSfa6ay0K3WGi6qZCpq1kZYlw08YuJwXM3UAn +eeQ== X-Gm-Message-State: AOJu0YxIv83G08mF+hbL3P/+NI6s9i3L6t3WTMF/iijVde9W6aK98zi5 guy11D+b0C0C1j27xD8WM0+eAsq1ECd3jquUZbHiLj5v5fRFbbhkL5sC3w+ue0kfq6RFXci/Wv8 Oeg== X-Google-Smtp-Source: AGHT+IHZaZmbSbzP7H4h6oxkhZTBl+Qal6cW99hdd9Wu2FustGel7H/11G9rSlFLDzv4UxgKt4rIog== X-Received: by 2002:a17:902:ea04:b0:205:6a9b:7e3e with SMTP id d9443c01a7336-2074c79c496mr54005575ad.56.1726041709172; Wed, 11 Sep 2024 01:01:49 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:d828:3442:b431:2762]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20710e37922sm58737795ad.110.2024.09.11.01.01.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 01:01:48 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 4/6] zram: rework writeback target selection strategy Date: Wed, 11 Sep 2024 17:01:12 +0900 Message-ID: <20240911080130.3766632-5-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911080130.3766632-1-senozhatsky@chromium.org> References: <20240911080130.3766632-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 | 83 +++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 19 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 222e2e74f0da..52cfd36769a6 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; @@ -681,11 +681,57 @@ 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)) + 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; @@ -737,7 +783,15 @@ 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); @@ -754,25 +808,10 @@ static ssize_t writeback_store(struct device *dev, } } =20 + index =3D pps->index; 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_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)) + if (!zram_test_flag(zram, index, ZRAM_PP_SLOT)) goto next; - /* * Clearing ZRAM_UNDER_WB is duty of caller. * IOW, zram_free_page never clear it. @@ -786,6 +825,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); continue; } =20 @@ -804,6 +845,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). @@ -846,12 +889,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); atomic_set(&zram->pp_in_progress, 0); up_read(&zram->init_lock); =20 --=20 2.46.0.598.g6f2099f65c-goog From nobody Mon Feb 9 03:27:18 2026 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 2E7C814D449 for ; Wed, 11 Sep 2024 08:01:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041713; cv=none; b=oedXRaDpcm2puYtL05TIIUlt+qYeASFLJsoM1TK8ry7gGmIzD3I1qWXjYkh6bybvJx45gKtP1pgx6SEbI/Dx+fzcakmSJUlVrY5Df8XUam/7zj9lzcSzCbHE12du2xu+AXEQWRxosg9DfpP6c51y4PwFy/v1lBlFnVI4lQ0tXIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041713; c=relaxed/simple; bh=GpbrAqihpqrGMi6LxZW46ippumBmrcz5UgvWIdCU938=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dIpLtpLI4/f1DNEpPgo7duErXRhWA2NvGR1eDwQwsviIFqjNl0ZuERCi6/cn7b8Q2Qvul0B8k3uu83jn7Plhw7bwUG0FGeJZzELe2W1vYDaNJymlZDodhYtLcXSLflt2K2Bk49Mqc9DHyLwXrLkF9egi0zPmnIIUOEKWGMFtJZQ= 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=S2AHZcme; arc=none smtp.client-ip=209.85.214.173 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="S2AHZcme" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-20551e2f1f8so64719425ad.2 for ; Wed, 11 Sep 2024 01:01:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726041711; x=1726646511; 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=FaaLSaGPvJKGOCkhBt8xB5aM+xySGaG4M/bL3WuizSU=; b=S2AHZcme0hZMjg0UL+X3eed+uffkX12rm2iz5Nt24rPqdSJjoR4eXX1uV5DlCVn13l /zYnXU3GTcldtdeKCIfKzy2uvPmcfRpJvFYJVQ8HOvOXCaGMUWxVQ1mNhcHWXYFp7FoS KPVDpnSay+k/s1dODCDK+9wZEcz3wACvzE9rM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726041711; x=1726646511; 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=FaaLSaGPvJKGOCkhBt8xB5aM+xySGaG4M/bL3WuizSU=; b=CuMlIpO9HSc7YVHJnsZbVLH2eaPGKGZRFT7AT99Ily4eTG5jFQj7rMhLXVcjpH+pah EF4UtenQ8h8Suv8RN+lYkCwwLFgwShIY83wWlI22Vmd6cKV+dk3aDF+oGnWJufNA+1jA rdgxhLSrkVZ32I4OJE/Zd9DSH8Z8o77BzNqXArltFLbyg7ygjet1t29LRXAATUWp4nsN 23SJn8W7MSpnUfHujJLBdo3mv4kojtUBCIt/KLUJOcmavPhrh31KnzxIekFsBas/5UVr Fe30wFtjAh8RO6uHbMIzsmesJ0djQM1jo+d/NI/y5RivxsUCAqz9RQPUZrn01YS6a6ra jeFA== X-Gm-Message-State: AOJu0Yy/IlYq5JHYlGAnB/PGJQh5tqFg2YbQYkuaMv06rp/gkSQ2d1Ym KczftbmbWitycgPPvkz3kjYLvGk+Y7JXT5E209u/aRBjWEATR1ZIcdDLZS8wE3ApHMmj1s8BiPO Yxw== X-Google-Smtp-Source: AGHT+IE/bz2N+M2Pe9WuFvbt+SPPxNYz4eZuwSwr7zTnfxfL6Q8toZHreXBRHLqsu13aIIBX2b2Q7Q== X-Received: by 2002:a17:902:ec8e:b0:205:4885:235e with SMTP id d9443c01a7336-2074c70dac2mr41119555ad.39.1726041711425; Wed, 11 Sep 2024 01:01:51 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:d828:3442:b431:2762]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20710e37922sm58737795ad.110.2024.09.11.01.01.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 01:01:51 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 5/6] zram: do not mark idle slots that cannot be idle Date: Wed, 11 Sep 2024 17:01:13 +0900 Message-ID: <20240911080130.3766632-6-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911080130.3766632-1-senozhatsky@chromium.org> References: <20240911080130.3766632-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" ZRAM_SAME slots cannot be post-processed (writeback or recompress) so do not mark them ZRAM_IDLE. Same with ZRAM_WB slots, they cannot be ZRAM_IDLE because they are not in zsmalloc pool anymore. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 52cfd36769a6..dab5f306af32 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -392,17 +392,28 @@ static void mark_idle(struct zram *zram, ktime_t cuto= ff) /* * Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race. * See the comment in writeback_store. + * + * Also do not mark ZRAM_SAME slots as ZRAM_IDLE, because no + * post-processing (recompress, writeback) happens to the + * ZRAM_SAME slot. + * + * And ZRAM_WB slots simply cannot be ZRAM_IDLE. */ zram_slot_lock(zram, index); - if (zram_allocated(zram, index) && - !zram_test_flag(zram, index, ZRAM_UNDER_WB)) { + if (!zram_allocated(zram, index) || + zram_test_flag(zram, index, ZRAM_WB) || + zram_test_flag(zram, index, ZRAM_UNDER_WB) || + zram_test_flag(zram, index, ZRAM_SAME)) { + zram_slot_unlock(zram, index); + continue; + } + #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME - is_idle =3D !cutoff || ktime_after(cutoff, - zram->table[index].ac_time); + is_idle =3D !cutoff || + ktime_after(cutoff, zram->table[index].ac_time); #endif - if (is_idle) - zram_set_flag(zram, index, ZRAM_IDLE); - } + if (is_idle) + zram_set_flag(zram, index, ZRAM_IDLE); zram_slot_unlock(zram, index); } } --=20 2.46.0.598.g6f2099f65c-goog From nobody Mon Feb 9 03:27:18 2026 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 6A0A214F10F for ; Wed, 11 Sep 2024 08:01:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041715; cv=none; b=tfvTrtvthEn/wVmH8vOaCfrqNmS0ubgOBUIeHm1C33mYEe1ZwvFhyJkzxcEQIfHOx6I/FBOhuRpWjtMUlcIPFhePSqoA0vk1N5ITtefm+78050RJYXQNntm4F7g8Q7x8X79amPpCd2RjUhvM4+4EEuI4tCZzwhYQI2YJXohEJ3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726041715; c=relaxed/simple; bh=+9oLPzwFmt7zWv2BKmL5l8ncYG/9nKsygYhoxabtxq4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j/KIPqc+P+VOkWfR8izjR7uFgIEAVJ1Cg2/JtgEch0Cp8O90/ldyh2f0YT7BIqAdpu2UqMJLDVo+rO+ZzfVez4DDwwjFTvi/y1Md2szeJNop8Dl4YcYXEB6cx+3TRbsO7s8gzECdWo02+sQQ5aaHgkTmrerIQ1WBiN/65ievGwA= 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=iCTgCMd/; arc=none smtp.client-ip=209.85.214.171 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="iCTgCMd/" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2053616fa36so19458175ad.0 for ; Wed, 11 Sep 2024 01:01:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1726041714; x=1726646514; 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=N472WaAGva/om+EmpOL8/0tMAMdGDFbzXUB7x5LuZYc=; b=iCTgCMd/mLt1uDU3xWLawNuv15AcksAK+OO/X2gBmKEVB0ncFhXYtOUcR9f9RB7ZtV U0pt7+9L8CJRjA3HQyQG7qXwPVHhNf89igFqprxfiMizNAd+aNYlfaB12gQgCh48+lJL f2Wi95YDbQINeWafiSVfxtjxomR8Cuuczv7Nw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726041714; x=1726646514; 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=N472WaAGva/om+EmpOL8/0tMAMdGDFbzXUB7x5LuZYc=; b=A2hZunS7Yo/jk26aaLNAGHAR5X/9jHMkqq/CDWY0RH3LNX0BOtrHu5Lca2ucZQN3tZ EXpscmPdfvUoDDYhWzszQTuUlhJkEB68u4M0wGqcssN57Z7r9Gsepsc8FoJ8AoyJXPND YjKnuuD/xqCiZdpCXcE2XEzw/yArs+8ga+3/PEkdLNJ9QkfGbxEHQ79uo23KrSZ98lNo naIkmcrNBYkXu85eajYL5DTLLcEZfZjWLKqOWNFHzbdIUnd4ao0E8VgKWF2RhzqHl56Z mZCCQuCljqISP7NV9AAi0lkGgM+OIXS5R5MB2Mh5uiRa3SYfYdJpvAZyMmoC79d5GtxK D7UQ== X-Gm-Message-State: AOJu0YyW9vTdf5uDASxG7hXIa+f48pijKtmmMSQFD4DPofZXdyf7364y EhgZjt+S7wlmWOPlV6h55P/M/3GQkqQXmTgVbn+3kSwj0nUkf5M14ojtLeoSrQ== X-Google-Smtp-Source: AGHT+IGHbMEPbCVtVRtVcnKyQtuM5pm2H1cCaakAItz+OaoQAJQjImfcc+jE52/MBJUCg9Eq+nMoaA== X-Received: by 2002:a17:902:e548:b0:205:8b84:d5ff with SMTP id d9443c01a7336-207521a5bd3mr24291325ad.26.1726041713657; Wed, 11 Sep 2024 01:01:53 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:d828:3442:b431:2762]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20710e37922sm58737795ad.110.2024.09.11.01.01.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 01:01:53 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCHv3 6/6] zram: reshuffle zram_free_page() flags operations Date: Wed, 11 Sep 2024 17:01:14 +0900 Message-ID: <20240911080130.3766632-7-senozhatsky@chromium.org> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog In-Reply-To: <20240911080130.3766632-1-senozhatsky@chromium.org> References: <20240911080130.3766632-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" Drop some redundant zram_test_flag() calls and re-order zram_clear_flag() calls. Plus two small trivial coding style fixes. No functional changes. Signed-off-by: Sergey Senozhatsky --- drivers/block/zram/zram_drv.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index dab5f306af32..d54502457925 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -1499,20 +1499,17 @@ static void zram_free_page(struct zram *zram, size_= t index) #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME zram->table[index].ac_time =3D 0; #endif - if (zram_test_flag(zram, index, ZRAM_IDLE)) - zram_clear_flag(zram, index, ZRAM_IDLE); + + zram_clear_flag(zram, index, ZRAM_IDLE); + zram_clear_flag(zram, index, ZRAM_INCOMPRESSIBLE); + zram_set_priority(zram, index, 0); + zram_clear_flag(zram, index, ZRAM_PP_SLOT); =20 if (zram_test_flag(zram, index, ZRAM_HUGE)) { zram_clear_flag(zram, index, ZRAM_HUGE); atomic64_dec(&zram->stats.huge_pages); } =20 - if (zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE)) - zram_clear_flag(zram, index, ZRAM_INCOMPRESSIBLE); - - zram_set_priority(zram, index, 0); - zram_clear_flag(zram, index, ZRAM_PP_SLOT); - if (zram_test_flag(zram, index, ZRAM_WB)) { zram_clear_flag(zram, index, ZRAM_WB); free_block_bdev(zram, zram_get_element(zram, index)); @@ -1536,13 +1533,12 @@ static void zram_free_page(struct zram *zram, size_= t index) zs_free(zram->mem_pool, handle); =20 atomic64_sub(zram_get_obj_size(zram, index), - &zram->stats.compr_data_size); + &zram->stats.compr_data_size); out: atomic64_dec(&zram->stats.pages_stored); zram_set_handle(zram, index, 0); zram_set_obj_size(zram, index, 0); - WARN_ON_ONCE(zram->table[index].flags & - ~(1UL << ZRAM_UNDER_WB)); + WARN_ON_ONCE(zram->table[index].flags & ~(1UL << ZRAM_UNDER_WB)); } =20 /* --=20 2.46.0.598.g6f2099f65c-goog