[PATCH] zram: Always expose rw_page

Brian Geffon posted 1 patch 1 year, 6 months ago
There is a newer version of this series
drivers/block/zram/zram_drv.c | 26 +++-----------------------
1 file changed, 3 insertions(+), 23 deletions(-)
[PATCH] zram: Always expose rw_page
Posted by Brian Geffon 1 year, 6 months ago
Currently zram will adjust its fops to a version which does not
contain rw_page when a backing device has been assigned. This is
done to prevent upper layers from assuming a synchronous operation
when a page may have been written back. This forces every operation
through bio which has overhead associated with bio_alloc/frees.

The code can be simplified to always expose a rw_page method and
only in the rare event that a page is written back we instead will
return -EOPNOTSUPP forcing the upper layer to fallback to bio.

Signed-off-by: Brian Geffon <bgeffon@google.com>
---
 drivers/block/zram/zram_drv.c | 26 +++-----------------------
 1 file changed, 3 insertions(+), 23 deletions(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 226ea76cc819..b9646886be0f 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -52,9 +52,6 @@ static unsigned int num_devices = 1;
 static size_t huge_class_size;
 
 static const struct block_device_operations zram_devops;
-#ifdef CONFIG_ZRAM_WRITEBACK
-static const struct block_device_operations zram_wb_devops;
-#endif
 
 static void zram_free_page(struct zram *zram, size_t index);
 static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
@@ -543,17 +540,6 @@ static ssize_t backing_dev_store(struct device *dev,
 	zram->backing_dev = backing_dev;
 	zram->bitmap = bitmap;
 	zram->nr_pages = nr_pages;
-	/*
-	 * With writeback feature, zram does asynchronous IO so it's no longer
-	 * synchronous device so let's remove synchronous io flag. Othewise,
-	 * upper layer(e.g., swap) could wait IO completion rather than
-	 * (submit and return), which will cause system sluggish.
-	 * Furthermore, when the IO function returns(e.g., swap_readpage),
-	 * upper layer expects IO was done so it could deallocate the page
-	 * freely but in fact, IO is going on so finally could cause
-	 * use-after-free when the IO is really done.
-	 */
-	zram->disk->fops = &zram_wb_devops;
 	up_write(&zram->init_lock);
 
 	pr_info("setup backing device %s\n", file_name);
@@ -1267,6 +1253,9 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
 		struct bio_vec bvec;
 
 		zram_slot_unlock(zram, index);
+		/* If we don't have a bio we came via rw_page, we must fallback to bio */
+		if (!bio)
+			return -EOPNOTSUPP;
 
 		bvec.bv_page = page;
 		bvec.bv_len = PAGE_SIZE;
@@ -1848,15 +1837,6 @@ static const struct block_device_operations zram_devops = {
 	.owner = THIS_MODULE
 };
 
-#ifdef CONFIG_ZRAM_WRITEBACK
-static const struct block_device_operations zram_wb_devops = {
-	.open = zram_open,
-	.submit_bio = zram_submit_bio,
-	.swap_slot_free_notify = zram_slot_free_notify,
-	.owner = THIS_MODULE
-};
-#endif
-
 static DEVICE_ATTR_WO(compact);
 static DEVICE_ATTR_RW(disksize);
 static DEVICE_ATTR_RO(initstate);
-- 
2.38.0.rc1.362.ged0d419d3c-goog
Re: [PATCH] zram: Always expose rw_page
Posted by Sergey Senozhatsky 1 year, 6 months ago
On (22/09/30 15:52), Brian Geffon wrote:
> Currently zram will adjust its fops to a version which does not
> contain rw_page when a backing device has been assigned. This is
> done to prevent upper layers from assuming a synchronous operation
> when a page may have been written back. This forces every operation
> through bio which has overhead associated with bio_alloc/frees.
> 
> The code can be simplified to always expose a rw_page method and
> only in the rare event that a page is written back we instead will
> return -EOPNOTSUPP forcing the upper layer to fallback to bio.

Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>

> @@ -1267,6 +1253,9 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
>  		struct bio_vec bvec;
>  
>  		zram_slot_unlock(zram, index);
> +		/* If we don't have a bio we came via rw_page, we must fallback to bio */
> +		if (!bio)
> +			return -EOPNOTSUPP;

The comment is above 80 cols.
Re: [PATCH] zram: Always expose rw_page
Posted by Brian Geffon 1 year, 6 months ago
On Sun, Oct 2, 2022 at 10:59 PM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> On (22/09/30 15:52), Brian Geffon wrote:
> > Currently zram will adjust its fops to a version which does not
> > contain rw_page when a backing device has been assigned. This is
> > done to prevent upper layers from assuming a synchronous operation
> > when a page may have been written back. This forces every operation
> > through bio which has overhead associated with bio_alloc/frees.
> >
> > The code can be simplified to always expose a rw_page method and
> > only in the rare event that a page is written back we instead will
> > return -EOPNOTSUPP forcing the upper layer to fallback to bio.
>
> Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>

Thank you.

>
> > @@ -1267,6 +1253,9 @@ static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index,
> >               struct bio_vec bvec;
> >
> >               zram_slot_unlock(zram, index);
> > +             /* If we don't have a bio we came via rw_page, we must fallback to bio */
> > +             if (!bio)
> > +                     return -EOPNOTSUPP;
>
> The comment is above 80 cols.

Fixed in a new patch.