Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1064125ybl; Wed, 14 Aug 2019 10:07:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqwV/uTmELaihWqnFUo53UoSksnuCueU7Bp/il/obkNVnQwOIQEX6Q7+dC/KQ7VBQ7rS8WmT X-Received: by 2002:a17:90a:d146:: with SMTP id t6mr747119pjw.76.1565802478931; Wed, 14 Aug 2019 10:07:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565802478; cv=none; d=google.com; s=arc-20160816; b=i2gYQZLhkw1IXUYGUNk3aNrMcv5UsX5fPyHcLNtfK9iG06HgIYa7ImTcWr94gwpjK7 sr8CxTcUpU0/YMd2NCyJRutBDqB4bBNek6sD6lJS7uyPKTQG5UrLY835stuPr4LjOmSr z4GAZCIlHrK1JiBEENgCJXTKZA5JPL3UoD2IyRza+dNRAguuZOSPqKYg4cQR2FtVzGkn cTS89JpTmX4MZSgfA3er+ysUhJqvDInYtosXMCg+izK7sq8swlq4X/uLW9mwzhsDzQja lMxv6pzVwGZLHL3w60y9t2mgeK9deAF+sM+3DXhVVpqLIeamUhr3NtOsY0Ow8zNDYF3X OfNg== 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=tKu6Dt4bDnqP1sQ3ojcO9D/h2xdAsG/i7ADdcot45WA=; b=rEZY0zEeuDnuy35t4y2y+om58wCiQwpBV/b21P0lzSknHb8TRHG8NFz7O0tgiaQ6La mppWHspFWmLROD7qOgWNN4R+mSF+NVsF1g9kTH0s8dB6N5jWWVt9tc6PtRqulgpNJ0uL ipLyrGd+Gjh9f+O+qemqW+wbI4w0fdNsIFFQurGF+ZhtkuxmNX3GpHz1vmysQeaN6wpV AXygyEkxDquALS6AP1aXNQexd3BG/YyuH4HDBniRvH0UTstclfpFLjpU+6KCglvMhUeP 5UyI2F/eW4omsa8o+LTvC6Ks228REs+T+YWRPRRDcf+AEkeUbiz+KtkOshsipDWNbAaS 4QeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OTNj5xDf; 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 i11si173939pgk.309.2019.08.14.10.07.42; Wed, 14 Aug 2019 10:07:58 -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=OTNj5xDf; 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 S1729199AbfHNRG2 (ORCPT + 99 others); Wed, 14 Aug 2019 13:06:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:55464 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729626AbfHNRGW (ORCPT ); Wed, 14 Aug 2019 13:06:22 -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 77B9A21744; Wed, 14 Aug 2019 17:06:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1565802381; bh=t2XG4CmWsTIyBrHSCGBno53f3jKzblsHT8JKdD8A2O0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OTNj5xDfxHfKjvtkaOaAKbJI9zBtOrAxpGe7IWOwe/Il5WlqzsdaiPX+rvLrcKjGj 8P2/MJ9RErDn3cdvIAr6T2xgCkM18NCvQ0lnRsMR0Fr+Ux0kgDIyE5PqNNkzTvpWOj +pOjIYxvGJ6K/ikFr6994uTAghcHh+x2iVxiav88= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Logan Gunthorpe , Sagi Grimberg , Christoph Hellwig , Sasha Levin Subject: [PATCH 5.2 104/144] nvme: fix memory leak caused by incorrect subsystem free Date: Wed, 14 Aug 2019 19:01:00 +0200 Message-Id: <20190814165804.250036186@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190814165759.466811854@linuxfoundation.org> References: <20190814165759.466811854@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 e654dfd38c1ecf58d8d019f3c053189413484a5b ] When freeing the subsystem after finding another match with __nvme_find_get_subsystem(), use put_device() instead of __nvme_release_subsystem() which calls kfree() directly. Per the documentation, put_device() should always be used after device_initialization() is called. Otherwise, leaks like the one below which was detected by kmemleak may occur. Once the call of __nvme_release_subsystem() is removed it no longer makes sense to keep the helper, so fold it back into nvme_release_subsystem(). unreferenced object 0xffff8883d12bfbc0 (size 16): comm "nvme", pid 2635, jiffies 4294933602 (age 739.952s) hex dump (first 16 bytes): 6e 76 6d 65 2d 73 75 62 73 79 73 32 00 88 ff ff nvme-subsys2.... backtrace: [<000000007d8fc208>] __kmalloc_track_caller+0x16d/0x2a0 [<0000000081169e5f>] kvasprintf+0xad/0x130 [<0000000025626f25>] kvasprintf_const+0x47/0x120 [<00000000fa66ad36>] kobject_set_name_vargs+0x44/0x120 [<000000004881f8b3>] dev_set_name+0x98/0xc0 [<000000007124dae3>] nvme_init_identify+0x1995/0x38e0 [<000000009315020a>] nvme_loop_configure_admin_queue+0x4fa/0x5e0 [<000000001a63e766>] nvme_loop_create_ctrl+0x489/0xf80 [<00000000a46ecc23>] nvmf_dev_write+0x1a12/0x2220 [<000000002259b3d5>] __vfs_write+0x66/0x120 [<000000002f6df81e>] vfs_write+0x154/0x490 [<000000007e8cfc19>] ksys_write+0x10a/0x240 [<00000000ff5c7b85>] __x64_sys_write+0x73/0xb0 [<00000000fee6d692>] do_syscall_64+0xaa/0x470 [<00000000997e1ede>] entry_SYSCALL_64_after_hwframe+0x49/0xbe Fixes: ab9e00cc72fa ("nvme: track subsystems") Signed-off-by: Logan Gunthorpe Reviewed-by: Sagi Grimberg Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- drivers/nvme/host/core.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index 4a1d2ab4d1612..5deb4deb38209 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -2264,17 +2264,15 @@ static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ct memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off); } -static void __nvme_release_subsystem(struct nvme_subsystem *subsys) +static void nvme_release_subsystem(struct device *dev) { + struct nvme_subsystem *subsys = + container_of(dev, struct nvme_subsystem, dev); + ida_simple_remove(&nvme_subsystems_ida, subsys->instance); kfree(subsys); } -static void nvme_release_subsystem(struct device *dev) -{ - __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev)); -} - static void nvme_destroy_subsystem(struct kref *ref) { struct nvme_subsystem *subsys = @@ -2429,7 +2427,7 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id) mutex_lock(&nvme_subsystems_lock); found = __nvme_find_get_subsystem(subsys->subnqn); if (found) { - __nvme_release_subsystem(subsys); + put_device(&subsys->dev); subsys = found; if (!nvme_validate_cntlid(subsys, ctrl, id)) { -- 2.20.1