[PATCH RESEND v2] squashfs: avoid to allocate page actor when to read into the page cache

From: Yue Hu
Date: Thu Jul 18 2019 - 22:07:49 EST


From: Yue Hu <huyue2@xxxxxxxxxx>

Currently, we will allocate page actor in squashfs_readpage_block() via
kmalloc() if enable SQUASHFS_FILE_DIRECT option. However, the actor size
is small and it will be freed finally in this function. So, use stack
memory will be better for that case to avoid out of memory.

Signed-off-by: Yue Hu <huyue2@xxxxxxxxxx>
---
v2: fix commit message

fs/squashfs/file_direct.c | 10 +++-------
fs/squashfs/page_actor.c | 10 ++--------
fs/squashfs/page_actor.h | 4 ++--
3 files changed, 7 insertions(+), 17 deletions(-)

diff --git a/fs/squashfs/file_direct.c b/fs/squashfs/file_direct.c
index a4894cc..98c2dd1 100644
--- a/fs/squashfs/file_direct.c
+++ b/fs/squashfs/file_direct.c
@@ -35,7 +35,7 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
int end_index = start_index | mask;
int i, n, pages, missing_pages, bytes, res = -ENOMEM;
struct page **page;
- struct squashfs_page_actor *actor;
+ struct squashfs_page_actor actor;
void *pageaddr;

if (end_index > file_end)
@@ -51,9 +51,7 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
* Create a "page actor" which will kmap and kunmap the
* page cache pages appropriately within the decompressor
*/
- actor = squashfs_page_actor_init_special(page, pages, 0);
- if (actor == NULL)
- goto out;
+ squashfs_page_actor_init_special(&actor, page, pages, 0);

/* Try to grab all the pages covered by the Squashfs block */
for (missing_pages = 0, i = 0, n = start_index; i < pages; i++, n++) {
@@ -90,7 +88,7 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
}

/* Decompress directly into the page cache buffers */
- res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
+ res = squashfs_read_data(inode->i_sb, block, bsize, NULL, &actor);
if (res < 0)
goto mark_errored;

@@ -116,7 +114,6 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
put_page(page[i]);
}

- kfree(actor);
kfree(page);

return 0;
@@ -135,7 +132,6 @@ int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
}

out:
- kfree(actor);
kfree(page);
return res;
}
diff --git a/fs/squashfs/page_actor.c b/fs/squashfs/page_actor.c
index 520d323..0344806 100644
--- a/fs/squashfs/page_actor.c
+++ b/fs/squashfs/page_actor.c
@@ -78,14 +78,9 @@ static void direct_finish_page(struct squashfs_page_actor *actor)
kunmap_atomic(actor->pageaddr);
}

-struct squashfs_page_actor *squashfs_page_actor_init_special(struct page **page,
- int pages, int length)
+void squashfs_page_actor_init_special(struct squashfs_page_actor *actor,
+ struct page **page, int pages, int length)
{
- struct squashfs_page_actor *actor = kmalloc(sizeof(*actor), GFP_KERNEL);
-
- if (actor == NULL)
- return NULL;
-
actor->length = length ? : pages * PAGE_SIZE;
actor->page = page;
actor->pages = pages;
@@ -94,5 +89,4 @@ struct squashfs_page_actor *squashfs_page_actor_init_special(struct page **page,
actor->squashfs_first_page = direct_first_page;
actor->squashfs_next_page = direct_next_page;
actor->squashfs_finish_page = direct_finish_page;
- return actor;
}
diff --git a/fs/squashfs/page_actor.h b/fs/squashfs/page_actor.h
index 2e3073a..ab9d381 100644
--- a/fs/squashfs/page_actor.h
+++ b/fs/squashfs/page_actor.h
@@ -61,8 +61,8 @@ struct squashfs_page_actor {
};

extern struct squashfs_page_actor *squashfs_page_actor_init(void **, int, int);
-extern struct squashfs_page_actor *squashfs_page_actor_init_special(struct page
- **, int, int);
+extern void squashfs_page_actor_init_special(struct squashfs_page_actor *actor,
+ struct page **page, int pages, int length);
static inline void *squashfs_first_page(struct squashfs_page_actor *actor)
{
return actor->squashfs_first_page(actor);
--
1.9.1