fs/afs: use struct_size() in kzalloc() [Linux 5.3]

This Linux kernel change "fs/afs: use struct_size() in kzalloc()" is included in the Linux 5.3 release. This change is authored by Zhengyuan Liu <liuzhengyuan [at] kylinos.cn> on Thu Jun 20 18:12:17 2019 +0100. The commit for this change in Linux stable tree is ee10258 (patch).

fs/afs: use struct_size() in kzalloc()

As Gustavo said in other patches doing the same replace, we can now
use the new struct_size() helper to avoid leaving these open-coded and
prone to type mistake.

Signed-off-by: Zhengyuan Liu <liuzhengyuan@kylinos.cn>
Signed-off-by: David Howells <dhowells@redhat.com>

There are 12 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 fs/afs/dir.c   | 3 +--
 fs/afs/file.c  | 6 ++----
 fs/afs/write.c | 3 +--
 3 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/fs/afs/dir.c b/fs/afs/dir.c
index e8c58c9..5dff607 100644
--- a/fs/afs/dir.c
+++ b/fs/afs/dir.c
@@ -242,8 +242,7 @@ static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
    if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
        nr_inline = 0;

-   req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
-             GFP_KERNEL);
+   req = kzalloc(struct_size(req, array, nr_inline), GFP_KERNEL);
    if (!req)
        return ERR_PTR(-ENOMEM);

diff --git a/fs/afs/file.c b/fs/afs/file.c
index 11e69c5..67cd782 100644
--- a/fs/afs/file.c
+++ b/fs/afs/file.c
@@ -314,8 +314,7 @@ int afs_page_filler(void *data, struct page *page)
        /* fall through */
    default:
    go_on:
-       req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
-                 GFP_KERNEL);
+       req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
        if (!req)
            goto enomem;

@@ -465,8 +464,7 @@ static int afs_readpages_one(struct file *file, struct address_space *mapping,
        n++;
    }

-   req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *) * n,
-             GFP_NOFS);
+   req = kzalloc(struct_size(req, array, n), GFP_NOFS);
    if (!req)
        return -ENOMEM;

diff --git a/fs/afs/write.c b/fs/afs/write.c
index 8bcab95..9cea9c40a 100644
--- a/fs/afs/write.c
+++ b/fs/afs/write.c
@@ -48,8 +48,7 @@ static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
        return 0;
    }

-   req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *),
-             GFP_KERNEL);
+   req = kzalloc(struct_size(req, array, 1), GFP_KERNEL);
    if (!req)
        return -ENOMEM;

Leave a Reply

Your email address will not be published. Required fields are marked *