BACKPORT: zram: add free space management in backing device
authorMinchan Kim <minchan@kernel.org>
Wed, 6 Sep 2017 23:19:57 +0000 (16:19 -0700)
committerivanmeler <i_ivan@windowslive.com>
Wed, 13 Apr 2022 21:13:24 +0000 (21:13 +0000)
With backing device, zram needs management of free space of backing
device.

This patch adds bitmap logic to manage free space which is very naive.
However, it would be simple enough as considering uncompressible pages's
frequenty in zram.

Link: http://lkml.kernel.org/r/1498459987-24562-6-git-send-email-minchan@kernel.org
Signed-off-by: Minchan Kim <minchan@kernel.org>
Cc: Juneho Choi <juno.choi@lge.com>
Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit 1363d4662a0d28dfdb81ef426c88c9a8dbf7c338)
Signed-off-by: Peter Kalauskas <peskal@google.com>
Bug: 112488418
Change-Id: I37dc98b40bfddceb9eb6d989ca30683dbf89210c

drivers/block/zram/zram_drv.c
drivers/block/zram/zram_drv.h

index 9732218d94a92acba59efc52fdfcf4ec8912ad42..79b5e9c774a7214de13cda4f2dd4c95963b54dd5 100644 (file)
@@ -291,6 +291,9 @@ static void reset_bdev(struct zram *zram)
        zram->backing_dev = NULL;
        zram->old_block_size = 0;
        zram->bdev = NULL;
+
+       kvfree(zram->bitmap);
+       zram->bitmap = NULL;
 }
 
 static ssize_t backing_dev_show(struct device *dev,
@@ -329,10 +332,12 @@ static ssize_t backing_dev_store(struct device *dev,
        struct file *backing_dev = NULL;
        struct inode *inode;
        struct address_space *mapping;
-       unsigned int old_block_size = 0;
+       unsigned int bitmap_sz, old_block_size = 0;
+       unsigned long nr_pages, *bitmap = NULL;
        struct block_device *bdev = NULL;
        int err;
        struct zram *zram = dev_to_zram(dev);
+       gfp_t kmalloc_flags;
 
        file_name = kmalloc(PATH_MAX, GFP_KERNEL);
        if (!file_name)
@@ -368,16 +373,34 @@ static ssize_t backing_dev_store(struct device *dev,
        if (err < 0)
                goto out;
 
+       nr_pages = i_size_read(inode) >> PAGE_SHIFT;
+       bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
+       kmalloc_flags = GFP_KERNEL | __GFP_ZERO;
+       if (bitmap_sz > PAGE_SIZE)
+               kmalloc_flags |= __GFP_NOWARN | __GFP_NORETRY;
+
+       bitmap = kmalloc_node(bitmap_sz, kmalloc_flags, NUMA_NO_NODE);
+       if (!bitmap && bitmap_sz > PAGE_SIZE)
+               bitmap = vzalloc(bitmap_sz);
+
+       if (!bitmap) {
+               err = -ENOMEM;
+               goto out;
+       }
+
        old_block_size = block_size(bdev);
        err = set_blocksize(bdev, PAGE_SIZE);
        if (err)
                goto out;
 
        reset_bdev(zram);
+       spin_lock_init(&zram->bitmap_lock);
 
        zram->old_block_size = old_block_size;
        zram->bdev = bdev;
        zram->backing_dev = backing_dev;
+       zram->bitmap = bitmap;
+       zram->nr_pages = nr_pages;
        up_write(&zram->init_lock);
 
        pr_info("setup backing device %s\n", file_name);
@@ -385,6 +408,9 @@ static ssize_t backing_dev_store(struct device *dev,
 
        return len;
 out:
+       if (bitmap)
+               kvfree(bitmap);
+
        if (bdev)
                blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
 
@@ -398,6 +424,34 @@ out:
        return err;
 }
 
+static unsigned long get_entry_bdev(struct zram *zram)
+{
+       unsigned long entry;
+
+       spin_lock(&zram->bitmap_lock);
+       /* skip 0 bit to confuse zram.handle = 0 */
+       entry = find_next_zero_bit(zram->bitmap, zram->nr_pages, 1);
+       if (entry == zram->nr_pages) {
+               spin_unlock(&zram->bitmap_lock);
+               return 0;
+       }
+
+       set_bit(entry, zram->bitmap);
+       spin_unlock(&zram->bitmap_lock);
+
+       return entry;
+}
+
+static void put_entry_bdev(struct zram *zram, unsigned long entry)
+{
+       int was_set;
+
+       spin_lock(&zram->bitmap_lock);
+       was_set = test_and_clear_bit(entry, zram->bitmap);
+       spin_unlock(&zram->bitmap_lock);
+       WARN_ON_ONCE(!was_set);
+}
+
 #else
 static bool zram_wb_enabled(struct zram *zram) { return false; }
 static inline void reset_bdev(struct zram *zram) {};
index 113a41118918c0e9f0e3dfa9c3e4c55a76e87e82..707aec0a2681d6a4f5124770f8fb5c8764ead329 100644 (file)
@@ -119,6 +119,9 @@ struct zram {
        struct file *backing_dev;
        struct block_device *bdev;
        unsigned int old_block_size;
+       unsigned long *bitmap;
+       unsigned long nr_pages;
+       spinlock_t bitmap_lock;
 #endif
 };
 #endif