From patchwork Fri Nov 20 21:34:12 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 7671391 Return-Path: X-Original-To: patchwork-linux-scsi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C41859F2EC for ; Fri, 20 Nov 2015 21:34:22 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 941B020499 for ; Fri, 20 Nov 2015 21:34:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6A19E20497 for ; Fri, 20 Nov 2015 21:34:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757174AbbKTVeT (ORCPT ); Fri, 20 Nov 2015 16:34:19 -0500 Received: from mail-bn1on0096.outbound.protection.outlook.com ([157.56.110.96]:51040 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751747AbbKTVeS (ORCPT ); Fri, 20 Nov 2015 16:34:18 -0500 Received: from BY2PR02CA0042.namprd02.prod.outlook.com (10.141.216.32) by CY1PR0201MB1580.namprd02.prod.outlook.com (10.163.140.14) with Microsoft SMTP Server (TLS) id 15.1.325.17; Fri, 20 Nov 2015 21:34:14 +0000 Received: from BY2FFO11FD037.protection.gbl (2a01:111:f400:7c0c::187) by BY2PR02CA0042.outlook.office365.com (2a01:111:e400:2c40::32) with Microsoft SMTP Server (TLS) id 15.1.331.20 via Frontend Transport; Fri, 20 Nov 2015 21:34:14 +0000 Authentication-Results: spf=pass (sender IP is 63.163.107.173) smtp.mailfrom=sandisk.com; infradead.org; dkim=none (message not signed) header.d=none;infradead.org; dmarc=bestguesspass action=none header.from=sandisk.com; Received-SPF: Pass (protection.outlook.com: domain of sandisk.com designates 63.163.107.173 as permitted sender) receiver=protection.outlook.com; client-ip=63.163.107.173; helo=milsmgep12.sandisk.com; Received: from milsmgep12.sandisk.com (63.163.107.173) by BY2FFO11FD037.mail.protection.outlook.com (10.1.14.222) with Microsoft SMTP Server id 15.1.331.11 via Frontend Transport; Fri, 20 Nov 2015 21:34:13 +0000 Received: from MILHUBIP03.sdcorp.global.sandisk.com ( [172.22.12.162]) by milsmgep12.sandisk.com (Symantec Messaging Gateway) with SMTP id A0.1F.32178.5D19F465; Fri, 20 Nov 2015 13:34:13 -0800 (PST) Received: from milsmgip12.sandisk.com (10.177.8.100) by MILHUBIP03.sdcorp.global.sandisk.com (10.177.9.96) with Microsoft SMTP Server id 14.3.248.2; Fri, 20 Nov 2015 13:34:13 -0800 X-AuditID: ac160a69-f79f76d000007db2-40-564f91d51c5a Received: from [10.60.52.33] ( [10.177.8.100]) by milsmgip12.sandisk.com (Symantec Messaging Gateway) with SMTP id 22.38.04661.4D19F465; Fri, 20 Nov 2015 13:34:12 -0800 (PST) Subject: [PATCH 2/2] Fix a memory leak in scsi_host_dev_release() To: James Bottomley References: <564F910E.9070306@sandisk.com> CC: "Martin K. Petersen" , Christoph Hellwig , Hannes Reinecke , "linux-scsi@vger.kernel.org" From: Bart Van Assche Message-ID: <564F91D4.2010003@sandisk.com> Date: Fri, 20 Nov 2015 13:34:12 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <564F910E.9070306@sandisk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrDLMWRmVeSWpSXmKPExsWyRoxnke7Vif5hBk0P9Cz2LJrEZHF6wiIm i439HBbd13ewWSw//o/JgdVj2qRTbB6bV2h5fHx6i8Vj8+lqj8+b5AJYo7hsUlJzMstSi/Tt ErgyLh6awFjwQb/iwe69LA2M+zS6GDk4JARMJE5OV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FsvOT93EyM4aDmj djBen2h+iJGJg1OqgdGvaPJR/5reSvUp1xX+R56Ye63MehXbrwXt3N8u/Xj8I3wyA1vDwz2H t0wyLvsgz/B+2eQVbixG6TMVPzYekBbafGta+4QZVleTVk6+6n/t/MVd0pN3y5t3ZPg+elPt J2TQzym9O3FFumxn0dxZjwtXy54KKxA2/LF/DpvqhV1x51in2AQVSpcrsRRnJBpqMRcVJwIA QzeG0AoCAAA= X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BY2FFO11FD037; 1:BcfMSJGTKzMnTnYAVqNHVEd35t//Ri1wYXZgkjGk0LsnNenWPirN8BUaOjLKGTRe4rzP48R/bgnqy6nHMcR8VEqa4kbh+oUsHz1z+oWz5ck06rxeFf0YpvjV1+rZ0W6wrikELo1vDKCovoiCyMT1qdlTB6rJbZoxB2AEgaOkeeCFNGF2xFQVBOGqcF2ShF416Z5mUB39tihB6m6K99utTuZKOtm0/quLHo1tEFxkTEIMdtk1LVboxmEQli2tAIq2Ow/eKGn5Q7E6d/P/oJw3vkqipxQA+y0sv04/vXikFSEzbJxvYUUaY3S4rdIUOuE4onG/u41AoyGTzEF0/8h45DvteK5iBXUNeCZmG/ghMV9BnCe+ILmj7L5Fwnt+5Pyxfi4cF8dE+Yy5uCm3S/kbXw== X-Forefront-Antispam-Report: CIP:63.163.107.173; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(438002)(199003)(189002)(69596002)(106466001)(575784001)(65816999)(77096005)(54356999)(33656002)(87936001)(86362001)(5008740100001)(80316001)(64126003)(50986999)(36756003)(11100500001)(92566002)(19580405001)(110136002)(50466002)(4001350100001)(230700001)(586003)(47776003)(5001960100002)(5007970100001)(5001920100001)(59896002)(2950100001)(189998001)(19580395003)(65956001)(23676002)(65806001)(76176999)(87266999)(97736004)(229853001)(83506001)(81156007); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1PR0201MB1580; H:milsmgep12.sandisk.com; FPR:; SPF:Pass; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; CY1PR0201MB1580; 2:r1R7/oQWXRCIkqqgv82WYNCLtteNNtb6OumGLHKF0v8Df4ixrMGsn9Xz3EEeXL9Tvr3t7rgvBq2t4Zbdg3k2w0v6CxY9XsMz5tCJewwrzSzQhcJ0uh+zaFWGDgLo7Rl9mV7483m8NsWkC01hK1XAqOH0HqnZZMAHmHjftTU4l/8=; 3:XPvKz4jVBAqu7vqxz8Z1BqpAyj5gyRNPTJqYmc9+hlKd8TSgzjTGvOvdrhfskZmCynsmloLW3PzHnnb8ChSzJaHLHiUnoObzYy7vNRbLgQ/PRfaTwPmUC+k+N+f/lN9cXnCdHHcp+54SA/HiVlAHVQMLpYFjp820in+Fy6W+KrKnS5XlnXimRQCTgNxjuCgKdT3LrpwC2IbGHb4Bb66XNVIPbI6gCKG77AAuO5JRe9Nhy8xY2YCsxmkhP9q3xSv7LyG4PKKdrCUGi6nD9ohh9g==; 25:G14ZEfQ/CCgIMgmraNA5pYO0g+hkxiQ/+lyOGrc8labxjubmmQA8ITBiDLf6PEjVpTCBW+R5x839pHzMwjCCt5+eoyJivgdhhie09u+ADdPJ6Jknrq6Wy6d5l8dXkNWkBUw0qSVavupG4kQg3o/taZyzzkADjQURoIJ0ojjyVPxc+8zJCjqwnGq53be0jysEFXETmuaNLkzNeCE4Fs7S8+wy8EFHVBkm9R7sTsSXRETSaJSIlrQpWXJcRXPvmmRngosJXW09xYOS8eaptUEsiQ== X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(8251501001); SRVR:CY1PR0201MB1580; X-Microsoft-Exchange-Diagnostics: 1; CY1PR0201MB1580; 20:nuyjE3iUfd2/0iKmYPnCiJwMbckmdH6sx4JRFjOUB4uGjQqdt5qWeqFMmR9Lp2UuLMwB7W6YBKRbnPuxGw9kqawMRdh+t4DJ2Eb6QfhMHjMRvyV7WwaUMg7LydQGYgwttdzcRKxairXEPQYhKq0bNlpsXZBmK6BDPdkAhy9sEJ9iXgLJnW1A0VBx4PzKF8EQhpnYaYdw/YwVzwg5ez6pmCfBGhrTq0QUVtqH4cLnLd6Q1ias5+BMpA5D3Hc9HXKUyZQgQSy5FZvTLErOIB/xDe42GM9UIgZrJOB0Yihsp/4yupNp84xmggchIpPjCb8/YJ6ILpKubNLZB1emtwqdMm64SmylHaAb/p1B7RKyLRhfB2s2N+cRHTnu36iC17AtSCp8P/u6boC/BZ0Q7Fjbgx9kBfXEET7kX6VJBA07U3awhCW26SB5ERKBxcLwE48a9olf+l0qnoyCBCWah+d52nS6o0zsujxffQ7gnN7MTUZnbRreQAaC6esjFjhGB01r; 4:vtcn1qrdvNQZAU3C9awGMOLQzqtAN7M9E3Qq7tJCABWtPjqi+pbWGx7FZstskfII6GWQR1GRU8tfWwC9YPbTLyCFQvLZpufeSgaf6RnCUDqYFqkqo9ApEV9+hxwLtjzs4U18x42Sj1Xhj1dQu8CvCjWP5fPrdPcGTFrU4fL+HbOpvs1LeDHgtuURBPYpXMmR53Aq1I9r/axTdgc2koqHfyu2Jt1V+RZWLbgl9YlBPJtQT06wJO/0GzJAv1AJsHYNoZa/H4EakqKU4ZGctJAoE/GfReE80tdqO2NOki5/5V/gNhjoqZ9iS32wI94JnFF8bYr440NohlUhX4rnh49VujiV/tdU+YtajK35o4XCMVLLKVhFc2qXynwkuarWzqUPN0pCAR4zGjucn9zE4iZ62Q== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(42932892334569); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(8121501046)(520078)(5005006)(10201501046)(3002001); SRVR:CY1PR0201MB1580; BCL:0; PCL:0; RULEID:; SRVR:CY1PR0201MB1580; X-Forefront-PRVS: 07665BE9D1 X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtDWTFQUjAyMDFNQjE1ODA7MjM6YzZrZmtZbTBKZkcxazFHWFhyVn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s8ZVnakWy0gSdSY5clAJaf8HP4m4zAIHIFU8UZ7RCCRe0IISw8dCh/ZKNFoBKBOVW57wu2XVRkI=; 20:nHsCX2IicNLnUUhhkmOqNLqQfWL6RsuvYlFRoqU6Se0DIb6u3aUCfzI4XbTruEQD2OW1Wkpqx9mFIHijmeVsj59z9xFfx8BAW9m+c8pZFckEDo3bFcYo0CDOZ7Tc/EhLLFlODOoL6x1mNBzzORb3kw5Jqjkrod2Yfzy7hXOCZw7Gna81cX8cHBFtKwcrkSMjG/qzdH50aIUoPKYuyw47oRd3crldblNaF2ud9ewe/c60vPDmKD1gOjAFBxq2BCpU SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: sandisk.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Nov 2015 21:34:13.8989 (UTC) X-MS-Exchange-CrossTenant-Id: fcd9ea9c-ae8c-460c-ab3c-3db42d7ac64d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=fcd9ea9c-ae8c-460c-ab3c-3db42d7ac64d; Ip=[63.163.107.173]; Helo=[milsmgep12.sandisk.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR0201MB1580 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Fix a memory leak that occurs if a SCSI LLD calls scsi_host_alloc() and scsi_host_put() but neither scsi_host_add() nor scsi_host_remove(). This leak is fixed by ensuring that put_device(&shost->shost_dev) is always called. This patch also removes the get_device() call from scsi_add_host*() and the put_device() call from scsi_remove_host() since these calls are no longer needed. The following shell command triggers the scenario described above: for ((i=0; i<2; i++)); do srp_daemon -oac | while read line; do echo $line >/sys/class/infiniband_srp/srp-mlx4_0-1/add_target done done The kmemleak report is as follows: unreferenced object 0xffff88021b24a220 (size 8): comm "srp_daemon", pid 56421, jiffies 4295006762 (age 4240.750s) hex dump (first 8 bytes): 68 6f 73 74 35 38 00 a5 host58.. backtrace: [] kmemleak_alloc+0x7a/0xc0 [] __kmalloc_track_caller+0xfe/0x160 [] kvasprintf+0x5b/0x90 [] kvasprintf_const+0x8d/0xb0 [] kobject_set_name_vargs+0x3c/0xa0 [] dev_set_name+0x3c/0x40 [] scsi_host_alloc+0x327/0x4b0 [] srp_create_target+0x4e/0x8a0 [ib_srp] [] dev_attr_store+0x1b/0x20 [] sysfs_kf_write+0x4a/0x60 [] kernfs_fop_write+0x14e/0x180 [] __vfs_write+0x2f/0xf0 [] vfs_write+0xa4/0x100 [] SyS_write+0x54/0xc0 [] entry_SYSCALL_64_fastpath+0x12/0x6f Signed-off-by: Bart Van Assche Cc: Christoph Hellwig Cc: Hannes Reinecke Cc: stable --- drivers/scsi/hosts.c | 57 +++++++++++++++++++++++++++++++++--------------- include/scsi/scsi_host.h | 5 +++++ 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index 323982f..b9fa1f9 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -45,16 +45,6 @@ static atomic_t scsi_host_next_hn = ATOMIC_INIT(0); /* host_no for next new host */ -static void scsi_host_cls_release(struct device *dev) -{ - put_device(&class_to_shost(dev)->shost_gendev); -} - -static struct class shost_class = { - .name = "scsi_host", - .dev_release = scsi_host_cls_release, -}; - /** * scsi_host_set_state - Take the given host through the host state model. * @shost: scsi host to change the state of. @@ -180,7 +170,7 @@ void scsi_remove_host(struct Scsi_Host *shost) spin_unlock_irqrestore(shost->host_lock, flags); transport_unregister_device(&shost->shost_gendev); - device_unregister(&shost->shost_dev); + device_del(&shost->shost_dev); device_del(&shost->shost_gendev); } EXPORT_SYMBOL(scsi_remove_host); @@ -263,8 +253,6 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, if (error) goto out_del_gendev; - get_device(&shost->shost_gendev); - if (shost->transportt->host_size) { shost->shost_data = kzalloc(shost->transportt->host_size, GFP_KERNEL); @@ -311,10 +299,10 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, } EXPORT_SYMBOL(scsi_add_host_with_dma); -static void scsi_host_dev_release(struct device *dev) +static void scsi_host_free(struct kref *kref) { - struct Scsi_Host *shost = dev_to_shost(dev); - struct device *parent = dev->parent; + struct Scsi_Host *shost = container_of(kref, typeof(*shost), kref2); + struct device *parent = shost->shost_gendev.parent; struct request_queue *q; void *queuedata; @@ -349,6 +337,35 @@ static void scsi_host_dev_release(struct device *dev) kfree(shost); } +/* Called if shost_gendev refcnt drops to zero. */ +static void scsi_host_dev_release(struct device *dev) +{ + struct Scsi_Host *shost = dev_to_shost(dev); + + kref_put(&shost->kref2, scsi_host_free); +} + +/* Called if shost_dev refcnt drops to zero. */ +static void scsi_host_cls_release(struct device *dev) +{ + struct Scsi_Host *shost = class_to_shost(dev); + + kref_put(&shost->kref2, scsi_host_free); +} + +static struct class shost_class = { + .name = "scsi_host", + .dev_release = scsi_host_cls_release, +}; + +static void scsi_host_release(struct kref *kref) +{ + struct Scsi_Host *shost = container_of(kref, typeof(*shost), kref1); + + put_device(&shost->shost_gendev); + put_device(&shost->shost_dev); +} + static int shost_eh_deadline = -1; module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR); @@ -467,6 +484,10 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) shost->use_blk_mq = scsi_use_blk_mq && !shost->hostt->disable_blk_mq; + kref_init(&shost->kref1); + kref_init(&shost->kref2); + kref_get(&shost->kref2); + device_initialize(&shost->shost_gendev); dev_set_name(&shost->shost_gendev, "host%d", shost->host_no); shost->shost_gendev.bus = &scsi_bus_type; @@ -571,7 +592,7 @@ EXPORT_SYMBOL(scsi_host_lookup); struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost) { if ((shost->shost_state == SHOST_DEL) || - !get_device(&shost->shost_gendev)) + !kref_get_unless_zero(&shost->kref1)) return NULL; return shost; } @@ -583,7 +604,7 @@ EXPORT_SYMBOL(scsi_host_get); **/ void scsi_host_put(struct Scsi_Host *shost) { - put_device(&shost->shost_gendev); + kref_put(&shost->kref1, scsi_host_release); } EXPORT_SYMBOL(scsi_host_put); diff --git a/include/scsi/scsi_host.h b/include/scsi/scsi_host.h index ed52712..4f32cf3 100644 --- a/include/scsi/scsi_host.h +++ b/include/scsi/scsi_host.h @@ -707,6 +707,11 @@ struct Scsi_Host { enum scsi_host_state shost_state; + /* refcnt manipulated by scsi_host_get() / scsi_host_put() */ + struct kref kref1; + /* refcnt that tracks existence of shost_gendev and shost_dev */ + struct kref kref2; + /* ldm bits */ struct device shost_gendev, shost_dev;