From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A825BC48BDF for ; Tue, 15 Jun 2021 15:52:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9469061446 for ; Tue, 15 Jun 2021 15:52:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232926AbhFOPyv (ORCPT ); Tue, 15 Jun 2021 11:54:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:44848 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232267AbhFOPwE (ORCPT ); Tue, 15 Jun 2021 11:52:04 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8A0E7617C9; Tue, 15 Jun 2021 15:49:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1623772199; bh=Y+ABlMGWITX60FZnU8jWzIurpGeNhVGC8AbgKOyWHoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gSrGDeTq9wsn8b//rYk+HfVTRGO7yYNc8byRM+muqePnHSWryTqDUvYH/slo8GFW4 eccR1KrJI2+tmwO/L+Lxxt4D8UMLUENKk0IVhQb+avwPzAuiK+LRbkUWZPtaVDztoI q6xc/5NMYeCHGOz+1CpLYiq5Hlj3wcvXjo3Dy1buAv6tPg+y3pLPmpTKxwLZPCI7kl 9EaiBGJWbnI16epyyN0Op4e8PqAwH7ZdNqDxw2GS8qK8BfvLgUHy1BkzJPQXyjM78s iWmJrCpzmDLuJ/PyBr5cXInxc4diXu0PginzWtkTEkWtLdop+RinGfwZEL91SQxaZ1 DeIVqZ3oyYVUw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ming Lei , Bart Van Assche , John Garry , Hannes Reinecke , "Martin K . Petersen" , Sasha Levin , linux-scsi@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 08/15] scsi: core: Fix error handling of scsi_host_alloc() Date: Tue, 15 Jun 2021 11:49:40 -0400 Message-Id: <20210615154948.62711-8-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210615154948.62711-1-sashal@kernel.org> References: <20210615154948.62711-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ming Lei [ Upstream commit 66a834d092930cf41d809c0e989b13cd6f9ca006 ] After device is initialized via device_initialize(), or its name is set via dev_set_name(), the device has to be freed via put_device(). Otherwise device name will be leaked because it is allocated dynamically in dev_set_name(). Fix the leak by replacing kfree() with put_device(). Since scsi_host_dev_release() properly handles IDA and kthread removal, remove special-casing these from the error handling as well. Link: https://lore.kernel.org/r/20210602133029.2864069-2-ming.lei@redhat.com Cc: Bart Van Assche Cc: John Garry Cc: Hannes Reinecke Tested-by: John Garry Reviewed-by: Bart Van Assche Reviewed-by: John Garry Reviewed-by: Hannes Reinecke Signed-off-by: Ming Lei Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/scsi/hosts.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index 55522b7162d3..b68f29639c10 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -389,8 +389,10 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) mutex_init(&shost->scan_mutex); index = ida_simple_get(&host_index_ida, 0, 0, GFP_KERNEL); - if (index < 0) - goto fail_kfree; + if (index < 0) { + kfree(shost); + return NULL; + } shost->host_no = index; shost->dma_channel = 0xff; @@ -482,7 +484,7 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) shost_printk(KERN_WARNING, shost, "error handler thread failed to spawn, error = %ld\n", PTR_ERR(shost->ehandler)); - goto fail_index_remove; + goto fail; } shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d", @@ -491,17 +493,18 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) if (!shost->tmf_work_q) { shost_printk(KERN_WARNING, shost, "failed to create tmf workq\n"); - goto fail_kthread; + goto fail; } scsi_proc_hostdir_add(shost->hostt); return shost; + fail: + /* + * Host state is still SHOST_CREATED and that is enough to release + * ->shost_gendev. scsi_host_dev_release() will free + * dev_name(&shost->shost_dev). + */ + put_device(&shost->shost_gendev); - fail_kthread: - kthread_stop(shost->ehandler); - fail_index_remove: - ida_simple_remove(&host_index_ida, shost->host_no); - fail_kfree: - kfree(shost); return NULL; } EXPORT_SYMBOL(scsi_host_alloc); -- 2.30.2