When .mknod create a block device file in hugetlbfs, it will allocate an inode, and kmalloc a 'struct resv_map' in resv_map_alloc(). For now, inode->i_mapping->private_data is used to point the resv_map. However, when open the device, bd_acquire() will set i_mapping as bd_inode->imapping, result in resv_map memory leak. We fix it by waiting until a call to hugetlb_reserve_pages() to allocate the inode specific resv_map. We could then remove the resv_map allocation at inode creation time. Programs to reproduce: mount -t hugetlbfs nodev hugetlbfs mknod hugetlbfs/dev b 0 0 exec 30<> hugetlbfs/dev umount hugetlbfs/ Signed-off-by: Yufen Yu <yuyufen@xxxxxxxxxx> --- fs/hugetlbfs/inode.c | 10 ++-------- mm/hugetlb.c | 6 ++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index a7fa037b876b..eb87cfb80eb9 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -741,11 +741,6 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev) { struct inode *inode; - struct resv_map *resv_map; - - resv_map = resv_map_alloc(); - if (!resv_map) - return NULL; inode = new_inode(sb); if (inode) { @@ -757,7 +752,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, &hugetlbfs_i_mmap_rwsem_key); inode->i_mapping->a_ops = &hugetlbfs_aops; inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); - inode->i_mapping->private_data = resv_map; + inode->i_mapping->private_data = NULL; info->seals = F_SEAL_SEAL; switch (mode & S_IFMT) { default: @@ -780,8 +775,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, break; } lockdep_annotate_inode_mutex_key(inode); - } else - kref_put(&resv_map->refs, resv_map_release); + } return inode; } diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 8dfdffc34a99..088fd1239d49 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4462,6 +4462,12 @@ int hugetlb_reserve_pages(struct inode *inode, */ if (!vma || vma->vm_flags & VM_MAYSHARE) { resv_map = inode_resv_map(inode); + if (!resv_map) { + resv_map = resv_map_alloc(); + if (!resv_map) + return -ENOMEM; + inode->i_mapping->private_data = resv_map; + } chg = region_chg(resv_map, from, to); -- 2.16.2.dirty