Commit f82da67d authored by Mike Kravetz's avatar Mike Kravetz Committed by Khalid Elmously

hugetlbfs: fix memory leak for resv_map

BugLink: https://bugs.launchpad.net/bugs/1830176

[ Upstream commit 58b6e5e8 ]

When mknod is used to create a block special file in hugetlbfs, it will
allocate an inode and kmalloc a 'struct resv_map' via resv_map_alloc().
inode->i_mapping->private_data will point the newly allocated resv_map.
However, when the device special file is opened bd_acquire() will set
inode->i_mapping to bd_inode->i_mapping.  Thus the pointer to the
allocated resv_map is lost and the structure is leaked.

Programs to reproduce:
        mount -t hugetlbfs nodev hugetlbfs
        mknod hugetlbfs/dev b 0 0
        exec 30<> hugetlbfs/dev
        umount hugetlbfs/

resv_map structures are only needed for inodes which can have associated
page allocations.  To fix the leak, only allocate resv_map for those
inodes which could possibly be associated with page allocations.

Link: http://lkml.kernel.org/r/20190401213101.16476-1-mike.kravetz@oracle.comSigned-off-by: default avatarMike Kravetz <mike.kravetz@oracle.com>
Reviewed-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Reported-by: default avatarYufen Yu <yuyufen@huawei.com>
Suggested-by: default avatarYufen Yu <yuyufen@huawei.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarJuerg Haefliger <juergh@canonical.com>
Signed-off-by: default avatarKleber Sacilotto de Souza <kleber.souza@canonical.com>
parent bf762166
...@@ -745,11 +745,17 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, ...@@ -745,11 +745,17 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb,
umode_t mode, dev_t dev) umode_t mode, dev_t dev)
{ {
struct inode *inode; struct inode *inode;
struct resv_map *resv_map; struct resv_map *resv_map = NULL;
resv_map = resv_map_alloc(); /*
if (!resv_map) * Reserve maps are only needed for inodes that can have associated
return NULL; * page allocations.
*/
if (S_ISREG(mode) || S_ISLNK(mode)) {
resv_map = resv_map_alloc();
if (!resv_map)
return NULL;
}
inode = new_inode(sb); inode = new_inode(sb);
if (inode) { if (inode) {
...@@ -790,8 +796,10 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, ...@@ -790,8 +796,10 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb,
break; break;
} }
lockdep_annotate_inode_mutex_key(inode); lockdep_annotate_inode_mutex_key(inode);
} else } else {
kref_put(&resv_map->refs, resv_map_release); if (resv_map)
kref_put(&resv_map->refs, resv_map_release);
}
return inode; return inode;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment