Re: [PATCH] hugetlbfs: make BUG_ON(!inode) takes effect in hugetlbfs_setattr

From: Muchun Song
Date: Mon Jan 18 2021 - 03:02:14 EST


On Sat, Jan 16, 2021 at 5:11 PM Miaohe Lin <linmiaohe@xxxxxxxxxx> wrote:
>
> When we reach here with inode = NULL, we should have crashed as inode has
> already been dereferenced via hstate_inode. In order to make BUG_ON(!inode)
> takes effect, we should defer initializing hstate until we really need it.
> Also do this for hugetlbfs_inode_info as it's only used when ia_valid is
> verified with ATTR_SIZE.
>
> Signed-off-by: Miaohe Lin <linmiaohe@xxxxxxxxxx>
> ---
> fs/hugetlbfs/inode.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> index 740693d7f255..9b221b87fbea 100644
> --- a/fs/hugetlbfs/inode.c
> +++ b/fs/hugetlbfs/inode.c
> @@ -755,10 +755,8 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
> static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
> {
> struct inode *inode = d_inode(dentry);
> - struct hstate *h = hstate_inode(inode);

If inode == NULL, it will panic here when we access inode->i_sb.
I have seen many other file system implementations about setattr,
they all think that d_inode() will return a non-NULL pointer. So
I prefer to just remove the BUG_ON().

Thanks.


> int error;
> unsigned int ia_valid = attr->ia_valid;
> - struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
>
> BUG_ON(!inode);
>
> @@ -767,6 +765,8 @@ static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
> return error;
>
> if (ia_valid & ATTR_SIZE) {
> + struct hstate *h = hstate_inode(inode);
> + struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
> loff_t oldsize = inode->i_size;
> loff_t newsize = attr->ia_size;
>
> --
> 2.19.1
>