Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2537004yba; Mon, 6 May 2019 07:40:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqzSrVFBlmN1hrbq8vY7FzjzrjqEyn8rQy2rlfDU4LmUmAAmTsdjiWnqMhObNBxnAeuKAgtt X-Received: by 2002:a65:5c89:: with SMTP id a9mr32775908pgt.334.1557153618428; Mon, 06 May 2019 07:40:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557153618; cv=none; d=google.com; s=arc-20160816; b=vx0G23xfYoEMvtwhyx8LHEJyOag6i+QBF8ZT5zzdLgyWZr7DQAqF2qQtMI2NNyIMog oBaLByiaO+x2kowCXWustj+pQMwyxIXIDdArk+5ShDMxx4JwGbhZfxJ4cqqYusSjr3le BWnoiEp6KtJAYGHjI4LmrpvG55xxMxsiZvYBVfyX0XoViRvKxYzGCfkNmXqi1NzAMVs4 hreJlOgGNQouaG8A/K594k2R84d/rD3yPWS7N1Qy220Crr4Uovo0b+jSJyFwExCwWq4T 31I3Aw/pEC4sFcJhE9igy5ijcRJO218HwNhx8KLcfwT8eq4jKgJ4JbJoP+/uNFs7o5Ls hYoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IWhTHd6bpKUNVprG7RaBA8s5sd+xe1ncUy0N80O3gjA=; b=HBpCZdNQuU8egin+L9SchRrwuOZRnkzrwbSphgEYfvJDO3x2bX19Zxd6xixwzIO/yV 3rpgY9viRjeD+5jT0HxAYDdSq/YUP4xXeHWkEJeNHcs4rBdLFKadrFGwVE1BYnWbc1fA vpiHJEHajrwF0dJNppQb62YK78vEuLA6fPRqsNfjwZgIgnJs1pIbqiczlcRgbxxnUTuA fzjDGQ08AQzvA35cibP2NEkich8uyBrnjNVTtIWOjtJRakn0Lr0bJDufdbw5jcjZoZlo Thnj/dm69G48cOCmSK6/qGDqbjKthWKs/32C36C/JnilknrnzhDZIlaldJ5Oi9n0cNOb i3rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=T4gmM8IT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ck7si16990972plb.43.2019.05.06.07.40.03; Mon, 06 May 2019 07:40:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=T4gmM8IT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727323AbfEFOhH (ORCPT + 99 others); Mon, 6 May 2019 10:37:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:57656 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726821AbfEFOhF (ORCPT ); Mon, 6 May 2019 10:37:05 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E3A70204EC; Mon, 6 May 2019 14:37:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557153424; bh=glLK+raQEfAk5jr74YgUHbUWQ+yroWY8+n58sJG65A4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T4gmM8ITZ6GSL1093eAJHQ8ZZOLE9EGQwk+ilWk0WgkqoYv8DAdiQIuavJ7jyIhsQ idnUDw4xTOV49/mfb4j3cJa617MoLEdPSkp/easNKtW0OzPvpACsQLUEy9+QtSosZo 7leNDAdexQDOCvjfUMSLXh1xKxuUMLnfAG+pVBZ4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mike Kravetz , Andrew Morton , Yufen Yu , Linus Torvalds , "Sasha Levin (Microsoft)" Subject: [PATCH 5.0 076/122] hugetlbfs: fix memory leak for resv_map Date: Mon, 6 May 2019 16:32:14 +0200 Message-Id: <20190506143101.684861447@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190506143054.670334917@linuxfoundation.org> References: <20190506143054.670334917@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 58b6e5e8f1addd44583d61b0a03c0f5519527e35 ] 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.com Signed-off-by: Mike Kravetz Reviewed-by: Andrew Morton Reported-by: Yufen Yu Suggested-by: Yufen Yu Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin (Microsoft) --- fs/hugetlbfs/inode.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index a7fa037b876b..a3a3d256fb0e 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -741,11 +741,17 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev) { struct inode *inode; - struct resv_map *resv_map; + struct resv_map *resv_map = NULL; - resv_map = resv_map_alloc(); - if (!resv_map) - return NULL; + /* + * Reserve maps are only needed for inodes that can have associated + * page allocations. + */ + if (S_ISREG(mode) || S_ISLNK(mode)) { + resv_map = resv_map_alloc(); + if (!resv_map) + return NULL; + } inode = new_inode(sb); if (inode) { @@ -780,8 +786,10 @@ 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); + } else { + if (resv_map) + kref_put(&resv_map->refs, resv_map_release); + } return inode; } -- 2.20.1