From nobody Thu Dec 18 14:40:46 2025 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (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 DD0CEDDA9 for ; Tue, 25 Mar 2025 03:42:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742874150; cv=none; b=AtdY6F655dCIS/CR/WNMKVUBawBiJt9nxVH4ysgfvm3vf31BamtvRowvJ2+llGHEI+swwp2f0hl4LYKXKgUyxlDe8GwCp5t0Us2qgjxaDJx3GtsMR36FUomhX/W4VRd7ZaWosD5KRRUaGww3TW9mbltGB1EaMYjP/nQgvpsHyfk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742874150; c=relaxed/simple; bh=Ma33962il+p/QhyZvEJJw3lsleTmweRpXA7ldQkwxhs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=AvawU+UVpBfTrNBQMOB8xIHx9y/0T26TVIFSM1PRAPQm7TegF5ADvruXFJ8qWNR7d2macfrr3kMbTnDCBok/quzVEUzbNEyUlmY52leXT5f3UpTNPRieTDcgmWg+MFbgPiEIiqLDCUhnOHyqA28ToKTZ+/RoLX1GVG7G/dgfQ3E= 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=nhs9tKRp; arc=none smtp.client-ip=209.85.216.49 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="nhs9tKRp" Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-301302a328bso9723491a91.2 for ; Mon, 24 Mar 2025 20:42:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1742874148; x=1743478948; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=l6JEESLLV1JCllG84hanAMC518jUUA2QNEhCRHmRkPo=; b=nhs9tKRpk57kOxDoMbaT6wDmFRfFilVPsBrc5kBs10x3G0tH4uJNBVVmBw9mfAPRo+ 6afmkGkMT5psfP/bEt7o3r6qIu6HMyHZs2PbdYscofmExBuRlQ9PAc8HCgtYllkhLy8t VCQ+3q0NywNFcLTgXdWQS6X1pc8FP3bCRkj0w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742874148; x=1743478948; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=l6JEESLLV1JCllG84hanAMC518jUUA2QNEhCRHmRkPo=; b=tGpRBAS6388OIhZcR6LJJFlwYZBYl+z2zxjWooBOpwonbNnVDBpMVlQYG3IBH5tFfz BgXC1F2jAMpbUujHqV+SZHfnXvKk6Ms/drbPBCpcQ2/eV301+DuM/vSTFLkd9VR6pxo/ bfHmCZQAIUvh5vYSnZZwokOoeEx2YGbY17apci7jZN8CxfUTkNVjbh38CtVaGpaHjSt7 PeUUxygwjEU/aMNfaZ/59mOzvyRJ9yTPgttwA2+g8m+siVFJv71wCb+wujJ9ZK6lDCnl kSlN3FevMZAeF/MaEUcO1q7ACzx8b7yygS5wBHduCl+LlCX5FZGFxzwadHtN0BOm6Tgk ksFw== X-Forwarded-Encrypted: i=1; AJvYcCXe1gh/8O9uLQnU9bPJaRLsmguVCKm+g3ZhgYtE1gpRfMKFfynd2tz3IA9KfiDGAq4//XmpaXw99uRILFY=@vger.kernel.org X-Gm-Message-State: AOJu0YyYvN+hT7o9q442wvxXoRDjUBhugaHZIEoi3D/XcJdlI7tD1UNn t54JQzNWCikLqBKWv6DUht0lEHiCgRpeYkPtbreusU6Suq1Mgx4ldey/3ahhrA== X-Gm-Gg: ASbGnctmTGRAhunKxi88c+YfUco5FNM+QgkzoTwJMY28NdrxwCvNoKWQ88XrSB6hSLQ xSLWacMdI53iYUteFm1Sj8uVlepD7IcajxwYGizPOh21bELQnVduaRMRB+reHDkM/3BJe9D1JRm nt/5wdrjQUaUVURtW2R7K70Ry9SIDBoXHQ/nSZAIVWdh6Dh+Xw199U69xEG1BgQzQFPXFJLrfvO +BYWNaJfpD4sdV2iO6iL2VYLqneOLK4AO1QzKtSOHaubPShjOWLrzkimZgHndPrPXBRic5VPAl+ H8cqzvu1OyGaET6YKr7bK8XDqA8PQNfM1aD6fGzWsBiQPxM4KKVNMjcyPtpRdNJrHyxTfuo= X-Google-Smtp-Source: AGHT+IEVH9C89zEjjdOOR4Fn285iF4/gs8w5ibNuGI/A6WU5hES9nEmIT7AkvlUyNMKvqOCPwYBXsg== X-Received: by 2002:a17:90b:4fc6:b0:2ee:45fd:34f2 with SMTP id 98e67ed59e1d1-3030fe5a096mr20199657a91.6.1742874147817; Mon, 24 Mar 2025 20:42:27 -0700 (PDT) Received: from tigerii.tok.corp.google.com ([2401:fa00:8f:203:f107:eb57:41d8:a285]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-227e452ec4asm2642775ad.194.2025.03.24.20.42.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Mar 2025 20:42:27 -0700 (PDT) From: Sergey Senozhatsky To: Andrew Morton , Minchan Kim Cc: Brian Geffon , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCH] zram: modernize writeback interface Date: Tue, 25 Mar 2025 12:42:04 +0900 Message-ID: <20250325034210.3337080-1-senozhatsky@chromium.org> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog 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" The writeback interface supports a page_index=3DN parameter which performs writeback of the given page. Since we rarely need to writeback just one single page, the typical use case involves a number of writeback calls, each performing writeback of one page: echo page_index=3D100 > zram0/writeback ... echo page_index=3D200 > zram0/writeback echo page_index=3D500 > zram0/writeback ... echo page_index=3D700 > zram0/writeback One obvious downside of this is that it increases the number of syscalls. Less obvious, but a significantly more important downside, is that when given only one page to post-process zram cannot perform an optimal target selection. This becomes a critical limitation when writeback_limit is enabled, because under writeback_limit we want to guarantee the highest memory savings hence we first need to writeback pages that release the highest amount of zsmalloc pool memory. This patch adds page_index_range=3DLOW-HIGH parameter to the writeback interface: echo page_index_range=3D100-200 \ page_index_range=3D500-700 > zram0/writeback This gives zram a chance to apply an optimal target selection strategy on each iteration of the writeback loop. Apart from that the patch also unifies parameters passing and resembles other "modern" zram device attributes (e.g. recompression), while the old interface used a mixed scheme: values-less parameters for mode and a key=3Dvalue format for page_index. We still support the "old" value-less format for compatibility reasons. Signed-off-by: Sergey Senozhatsky --- Documentation/admin-guide/blockdev/zram.rst | 11 + drivers/block/zram/zram_drv.c | 321 +++++++++++++------- 2 files changed, 227 insertions(+), 105 deletions(-) diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/ad= min-guide/blockdev/zram.rst index 9bdb30901a93..9dca86365a4d 100644 --- a/Documentation/admin-guide/blockdev/zram.rst +++ b/Documentation/admin-guide/blockdev/zram.rst @@ -369,6 +369,17 @@ they could write a page index into the interface:: =20 echo "page_index=3D1251" > /sys/block/zramX/writeback =20 +In Linux 6.16 this interface underwent some rework. First, the interface +now supports `key=3Dvalue` format for all of its parameters (`type=3Dhuge_= idle`, +etc.) Second, the support for `page_index_range` was introduced, which +specify `LOW-HIGH` range (or ranges) of pages to be written-back. This +reduces the number of syscalls, but more importantly this enables optimal +post-processing target selection strategy. Usage example:: + + echo "type=3Didle" > /sys/block/zramX/writeback + echo "page_index_range=3D1-100 page_index_range=3D200-300" > \ + /sys/block/zramX/writeback + If there are lots of write IO with flash device, potentially, it has flash wearout problem so that admin needs to design write limitation to guarantee storage health for entire product life. diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index fda7d8624889..2c39d12bd2d4 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -734,114 +734,19 @@ static void read_from_bdev_async(struct zram *zram, = struct page *page, submit_bio(bio); } =20 -#define PAGE_WB_SIG "page_index=3D" - -#define PAGE_WRITEBACK 0 -#define HUGE_WRITEBACK (1<<0) -#define IDLE_WRITEBACK (1<<1) -#define INCOMPRESSIBLE_WRITEBACK (1<<2) - -static int scan_slots_for_writeback(struct zram *zram, u32 mode, - unsigned long nr_pages, - unsigned long index, - struct zram_pp_ctl *ctl) +static int zram_writeback_slots(struct zram *zram, struct zram_pp_ctl *ctl) { - for (; nr_pages !=3D 0; index++, nr_pages--) { - bool ok =3D true; - - 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; - - ok =3D place_pp_slot(zram, ctl, index); -next: - zram_slot_unlock(zram, index); - if (!ok) - break; - } - - 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; + unsigned long blk_idx =3D 0; + struct page *page =3D NULL; struct zram_pp_slot *pps; - unsigned long index =3D 0; - struct bio bio; struct bio_vec bio_vec; - struct page *page =3D NULL; - ssize_t ret =3D len; - int mode, err; - unsigned long blk_idx =3D 0; - - if (sysfs_streq(buf, "idle")) - mode =3D IDLE_WRITEBACK; - else if (sysfs_streq(buf, "huge")) - mode =3D HUGE_WRITEBACK; - else if (sysfs_streq(buf, "huge_idle")) - mode =3D IDLE_WRITEBACK | HUGE_WRITEBACK; - else if (sysfs_streq(buf, "incompressible")) - mode =3D INCOMPRESSIBLE_WRITEBACK; - else { - if (strncmp(buf, PAGE_WB_SIG, sizeof(PAGE_WB_SIG) - 1)) - return -EINVAL; - - if (kstrtol(buf + sizeof(PAGE_WB_SIG) - 1, 10, &index) || - index >=3D nr_pages) - return -EINVAL; - - nr_pages =3D 1; - mode =3D PAGE_WRITEBACK; - } - - down_read(&zram->init_lock); - if (!init_done(zram)) { - ret =3D -EINVAL; - goto release_init_lock; - } - - /* Do not permit concurrent post-processing actions. */ - if (atomic_xchg(&zram->pp_in_progress, 1)) { - up_read(&zram->init_lock); - return -EAGAIN; - } - - if (!zram->backing_dev) { - ret =3D -ENODEV; - goto release_init_lock; - } + struct bio bio; + int ret, err; + u32 index; =20 page =3D alloc_page(GFP_KERNEL); - if (!page) { - ret =3D -ENOMEM; - goto release_init_lock; - } - - ctl =3D init_pp_ctl(); - if (!ctl) { - ret =3D -ENOMEM; - goto release_init_lock; - } - - scan_slots_for_writeback(zram, mode, nr_pages, index, ctl); + if (!page) + return -ENOMEM; =20 while ((pps =3D select_pp_slot(ctl))) { spin_lock(&zram->wb_limit_lock); @@ -929,10 +834,216 @@ static ssize_t writeback_store(struct device *dev, =20 if (blk_idx) free_block_bdev(zram, blk_idx); - -release_init_lock: if (page) __free_page(page); + + return ret; +} + +#define PAGE_WRITEBACK 0 +#define HUGE_WRITEBACK (1 << 0) +#define IDLE_WRITEBACK (1 << 1) +#define INCOMPRESSIBLE_WRITEBACK (1 << 2) + +static int parse_page_index(char *val, unsigned long nr_pages, + unsigned long *lo, unsigned long *hi) +{ + int ret; + + ret =3D kstrtoul(val, 10, lo); + if (ret) + return ret; + *hi =3D *lo + 1; + if (*lo >=3D nr_pages || *hi > nr_pages) + return -ERANGE; + return 0; +} + +static int parse_page_index_range(char *val, unsigned long nr_pages, + unsigned long *lo, unsigned long *hi) +{ + char *delim; + int ret; + + delim =3D strchr(val, '-'); + if (!delim) + return -EINVAL; + + *delim =3D 0x00; + ret =3D kstrtoul(val, 10, lo); + if (ret) + return ret; + if (*lo >=3D nr_pages) + return -ERANGE; + + ret =3D kstrtoul(delim + 1, 10, hi); + if (ret) + return ret; + if (*hi >=3D nr_pages || *lo > *hi) + return -ERANGE; + *hi +=3D 1; + return 0; +} + +static int parse_mode(char *val, u32 *mode) +{ + *mode =3D 0; + + if (!strcmp(val, "idle")) + *mode =3D IDLE_WRITEBACK; + if (!strcmp(val, "huge")) + *mode =3D HUGE_WRITEBACK; + if (!strcmp(val, "huge_idle")) + *mode =3D IDLE_WRITEBACK | HUGE_WRITEBACK; + if (!strcmp(val, "incompressible")) + *mode =3D INCOMPRESSIBLE_WRITEBACK; + + if (*mode =3D=3D 0) + return -EINVAL; + return 0; +} + +static int scan_slots_for_writeback(struct zram *zram, u32 mode, + unsigned long lo, unsigned long hi, + struct zram_pp_ctl *ctl) +{ + u32 index =3D lo; + + while (index < hi) { + bool ok =3D true; + + 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; + + ok =3D place_pp_slot(zram, ctl, index); +next: + zram_slot_unlock(zram, index); + if (!ok) + break; + index++; + } + + 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); + u64 nr_pages =3D zram->disksize >> PAGE_SHIFT; + unsigned long lo =3D 0, hi =3D nr_pages; + struct zram_pp_ctl *ctl =3D NULL; + char *args, *param, *val; + ssize_t ret =3D len; + int err, mode =3D 0; + + down_read(&zram->init_lock); + if (!init_done(zram)) { + up_read(&zram->init_lock); + return -EINVAL; + } + + /* Do not permit concurrent post-processing actions. */ + if (atomic_xchg(&zram->pp_in_progress, 1)) { + up_read(&zram->init_lock); + return -EAGAIN; + } + + if (!zram->backing_dev) { + ret =3D -ENODEV; + goto release_init_lock; + } + + ctl =3D init_pp_ctl(); + if (!ctl) { + ret =3D -ENOMEM; + goto release_init_lock; + } + + args =3D skip_spaces(buf); + while (*args) { + args =3D next_arg(args, ¶m, &val); + + /* + * Workaround to support the old writeback interface. + * + * The old writeback interface has a minor inconsistency and + * requires key=3Dvalue only for page_index parameter, while the + * writeback mode is a valueless parameter. + * + * This is not the case anymore and now all parameters are + * required to have values, however, we need to support the + * legacy writeback interface format so we check if we can + * recognize a valueless parameter as the (legacy) writeback + * mode. + */ + if (!val || !*val) { + err =3D parse_mode(param, &mode); + if (err) { + ret =3D err; + goto release_init_lock; + } + + scan_slots_for_writeback(zram, mode, lo, hi, ctl); + break; + } + + if (!strcmp(param, "type")) { + err =3D parse_mode(val, &mode); + if (err) { + ret =3D err; + goto release_init_lock; + } + + scan_slots_for_writeback(zram, mode, lo, hi, ctl); + break; + } + + if (!strcmp(param, "page_index")) { + err =3D parse_page_index(val, nr_pages, &lo, &hi); + if (err) { + ret =3D err; + goto release_init_lock; + } + + scan_slots_for_writeback(zram, mode, lo, hi, ctl); + break; + } + + /* There can be several page index ranges */ + if (!strcmp(param, "page_index_range")) { + err =3D parse_page_index_range(val, nr_pages, &lo, &hi); + if (err) { + ret =3D err; + goto release_init_lock; + } + + scan_slots_for_writeback(zram, mode, lo, hi, ctl); + continue; + } + } + + err =3D zram_writeback_slots(zram, ctl); + if (err) + ret =3D err; + +release_init_lock: release_pp_ctl(zram, ctl); atomic_set(&zram->pp_in_progress, 0); up_read(&zram->init_lock); --=20 2.49.0.395.g12beb8f557-goog