Skip to contentSkip to conversation

zram: fix pool names truncation

PATCH Superseded Sergey Senozhatsky 1 participant 6 messages Aug 11, 2015 – Aug 12, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 zram: fix pool names truncation

Hello,

found this reading the code just now. I do believe we need this patch in 4.2 (before 4.3), because the bug breaks new devices creation under some specific conditions -- new pool names are getting truncated to only 3 digits and, thus, debugfs_create_dir() fails.

The commit message contains more details.

Andrew, can you please pick up this patch?

Sergey Senozhatsky (1):
zram: fix max pool limitation

 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 1 line
2.5.0
#1 lore raw

PATCH 1/1 zram: fix max pool limitation

zram_meta_alloc() constructs a pool name for zs_create_pool() call as
snprintf(pool_name, sizeof(pool_name), "zram%d", device_id);

However, it defines pool name buffer to be only 8 chars long (minus trailing zero), which means that we can have only 1000 pool names: zram0 -- zram999.

With CONFIG_ZSMALLOC_STAT enabled an attempt to create a device zram1000 can fail if device zram100 already exists, because snprintf() will truncate new pool name to zram100 and pass it to debugfs_create_dir(), causing:

debugfs dir <zram100> creation failed
zram: Error creating memory pool

... and so on (zram101 - zram1010, etc).

Fix it by increasing pool_name buffer size to 11:
-- zram prefix (length 4)
-- int device_id can use up to 10 chars
-- terminating zero byte

---
 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +1 -1
index b088ca9..1dec01d 100644
@@ -502,7 +502,7 @@ static void zram_meta_free(struct zram_meta *meta, u64 disksize)
 static struct zram_meta *zram_meta_alloc(int device_id, u64 disksize)
 {
 	size_t num_pages;
-	char pool_name[8];
+	char pool_name[15];
 	struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
 
 	if (!meta)
Signature · 1 line
2.5.0
#2 lore raw

Re: [PATCH] zram: fix max pool limitation

Quoted 13 lines · Sergey Senozhatsky

zram_meta_alloc() constructs a pool name for zs_create_pool() call as
snprintf(pool_name, sizeof(pool_name), "zram%d", device_id);

However, it defines pool name buffer to be only 8 chars long (minus trailing zero), which means that we can have only 1000 pool names: zram0 -- zram999.

With CONFIG_ZSMALLOC_STAT enabled an attempt to create a device zram1000 can fail if device zram100 already exists, because snprintf() will truncate new pool name to zram100 and pass it to debugfs_create_dir(), causing:

Quoted 6 lines

debugfs dir <zram100> creation failed
zram: Error creating memory pool

... and so on (zram101 - zram1010, etc).

Fix it by increasing pool_name buffer size to 11:

oh... don't know how this happened. to 15.

	-ss
Quoted 25 lines

-- zram prefix (length 4)
-- int device_id can use up to 10 chars
-- terminating zero byte

---
 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +1 -1
index b088ca9..1dec01d 100644
@@ -502,7 +502,7 @@ static void zram_meta_free(struct zram_meta *meta, u64 disksize)
 static struct zram_meta *zram_meta_alloc(int device_id, u64 disksize)
 {
 	size_t num_pages;
-	char pool_name[8];
+	char pool_name[15];
 	struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
 
 	if (!meta)
Signature · 1 line
2.5.0
#3 lore raw

PATCH v2 1/1 zram: fix pool name truncation

In reply to Sergey Senozhatsky

zram_meta_alloc() constructs a pool name for zs_create_pool() call as
snprintf(pool_name, sizeof(pool_name), "zram%d", device_id);

However, it defines pool name buffer to be only 8 chars long (minus trailing zero), which means that we can have only 1000 pool names: zram0 -- zram999.

With CONFIG_ZSMALLOC_STAT enabled an attempt to create a device zram1000 can fail if device zram100 already exists, because snprintf() will truncate new pool name to zram100 and pass it to debugfs_create_dir(), causing:

debugfs dir <zram100> creation failed
zram: Error creating memory pool

... and so on (zram101 - zram1010, etc).

Fix it by increasing pool_name buffer size to 15:
-- zram prefix (length 4)
-- int device_id can use up to 10 chars
-- terminating zero byte

---
 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/zram/zram_drv.c +1 -1
index b088ca9..1dec01d 100644
@@ -502,7 +502,7 @@ static void zram_meta_free(struct zram_meta *meta, u64 disksize)
 static struct zram_meta *zram_meta_alloc(int device_id, u64 disksize)
 {
 	size_t num_pages;
-	char pool_name[8];
+	char pool_name[15];
 	struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
 
 	if (!meta)
Signature · 1 line
2.5.0
#4 lore raw
In reply to Sergey Senozhatsky
Quoted 8 lines · Sergey Senozhatsky

found this reading the code just now. I do believe we need this patch in 4.2 (before 4.3), because the bug breaks new devices creation under some specific conditions -- new pool names are getting truncated to only 3 digits and, thus, debugfs_create_dir() fails.

The commit message contains more details.

Hello,

I have an alternative fix; a somewhat better one. We actually don't have to snprintf() there anything. We already have zram->disk->disk_name constructed exactly same way, so we can pass it to zram_meta_alloc() instead of device_id.

I'll send out a patch shortly.

	-ss
Quoted 13 lines

Andrew, can you please pick up this patch?

Sergey Senozhatsky (1):
zram: fix max pool limitation

 drivers/block/zram/zram_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 1 line
2.5.0
#5 lore raw

PATCH v3 1/1 zram: fix pool name truncation

In reply to Sergey Senozhatsky

zram_meta_alloc() constructs a pool name for zs_create_pool() call as
snprintf(pool_name, sizeof(pool_name), "zram%d", device_id);

However, it defines pool name buffer to be only 8 bytes long (minus trailing zero), which means that we can have only 1000 pool names: zram0 -- zram999.

With CONFIG_ZSMALLOC_STAT enabled an attempt to create a device zram1000 can fail if device zram100 already exists, because snprintf() will truncate new pool name to zram100 and pass it debugfs_create_dir(), causing:

debugfs dir <zram100> creation failed
zram: Error creating memory pool

... and so on.

Fix it by passing zram->disk->disk_name to zram_meta_alloc() instead of divice_id. We construct zram%d name earlier and keep it as a ->disk_name, no need to snprintf() it again.

---
 drivers/block/zram/zram_drv.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/block/zram/zram_drv.c +2 -4
index b088ca9..c87d2f6 100644
@@ -499,10 +499,9 @@ static void zram_meta_free(struct zram_meta *meta, u64 disksize)
 	kfree(meta);
 }
 
-static struct zram_meta *zram_meta_alloc(int device_id, u64 disksize)
+static struct zram_meta *zram_meta_alloc(char *pool_name, u64 disksize)
 {
 	size_t num_pages;
-	char pool_name[8];
 	struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
 
 	if (!meta)
@@ -515,7 +514,6 @@ static struct zram_meta *zram_meta_alloc(int device_id, u64 disksize)
 		goto out_error;
 	}
 
-	snprintf(pool_name, sizeof(pool_name), "zram%d", device_id);
 	meta->mem_pool = zs_create_pool(pool_name, GFP_NOIO | __GFP_HIGHMEM);
 	if (!meta->mem_pool) {
 		pr_err("Error creating memory pool\n");
@@ -1033,7 +1031,7 @@ static ssize_t disksize_store(struct device *dev,
 		return -EINVAL;
 
 	disksize = PAGE_ALIGN(disksize);
-	meta = zram_meta_alloc(zram->disk->first_minor, disksize);
+	meta = zram_meta_alloc(zram->disk->disk_name, disksize);
 	if (!meta)
 		return -ENOMEM;
 
Signature · 1 line
2.5.0