From patchwork Mon May 31 05:07:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 12288659 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=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham 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 BA0CDC4708F for ; Mon, 31 May 2021 05:07:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8E17F610C8 for ; Mon, 31 May 2021 05:07:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230013AbhEaFJ3 (ORCPT ); Mon, 31 May 2021 01:09:29 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:43181 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229730AbhEaFJ2 (ORCPT ); Mon, 31 May 2021 01:09:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622437668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jXZD1O17xzElkKa2DNt6bLljbj9EZi8T5Af4aOsGUWM=; b=aulNjKglP8eKIXmncDA4kmTEJmX9BAJ0FQC+PUGAwBKT+BRDUlUmpPCXOi5SjCWjt3rkRr p3EcIhGRorfP/4oq8g1A/EvRs+EnklFNcC1parpEafJb8L+5KEJtFI1Z2qFoNV4pSc3xoH +upMJ7OBqzsa1T2hzSShEWmBmkDzS2k= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-151-EWZrsa-tMiaheJ7wHZvhrg-1; Mon, 31 May 2021 01:07:44 -0400 X-MC-Unique: EWZrsa-tMiaheJ7wHZvhrg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6B35A180FD65; Mon, 31 May 2021 05:07:43 +0000 (UTC) Received: from localhost (ovpn-12-235.pek2.redhat.com [10.72.12.235]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F3D612C82; Mon, 31 May 2021 05:07:38 +0000 (UTC) From: Ming Lei To: "Martin K . Petersen" , linux-scsi@vger.kernel.org Cc: Ming Lei , Bart Van Assche , John Garry , Hannes Reinecke Subject: [PATCH V3 1/3] scsi: core: use put_device() to release host Date: Mon, 31 May 2021 13:07:25 +0800 Message-Id: <20210531050727.2353973-2-ming.lei@redhat.com> In-Reply-To: <20210531050727.2353973-1-ming.lei@redhat.com> References: <20210531050727.2353973-1-ming.lei@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org 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(). Fixes the issue by replacing kfree(shost) via two put_device() since both .shost_dev and .shost_gendev share same lifetime. Meantime move get_device(shost->shost_gendev) from scsi_add_host_with_dma to scsi_host_alloc(), so that we can grab parent's refcnt explicitly when assigning .shost_dev->parent. With this way code becomes more readable. Also call put_device(dev->parent) in scsi_host_cls_release() so that code readability can be improved. Cc: Bart Van Assche Cc: John Garry Cc: Hannes Reinecke Signed-off-by: Ming Lei --- drivers/scsi/hosts.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index 624e2582c3df..ada11e3ae577 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -55,7 +55,7 @@ static DEFINE_IDA(host_index_ida); static void scsi_host_cls_release(struct device *dev) { - put_device(&class_to_shost(dev)->shost_gendev); + put_device(dev->parent); } static struct class shost_class = { @@ -261,8 +261,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); @@ -473,7 +471,7 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) shost->shost_gendev.type = &scsi_host_type; device_initialize(&shost->shost_dev); - shost->shost_dev.parent = &shost->shost_gendev; + shost->shost_dev.parent = get_device(&shost->shost_gendev); shost->shost_dev.class = &shost_class; dev_set_name(&shost->shost_dev, "host%d", shost->host_no); shost->shost_dev.groups = scsi_sysfs_shost_attr_groups; @@ -503,7 +501,9 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize) fail_index_remove: ida_simple_remove(&host_index_ida, shost->host_no); fail_kfree: - kfree(shost); + put_device(&shost->shost_dev); + put_device(&shost->shost_gendev); + return NULL; } EXPORT_SYMBOL(scsi_host_alloc); From patchwork Mon May 31 05:07:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 12288661 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=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham 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 2805EC47082 for ; Mon, 31 May 2021 05:07:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0984060E0B for ; Mon, 31 May 2021 05:07:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230070AbhEaFJc (ORCPT ); Mon, 31 May 2021 01:09:32 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:50184 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229730AbhEaFJb (ORCPT ); Mon, 31 May 2021 01:09:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622437672; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=11hZ8oqStTDk4oHLoKAP7tJ8yV9mSkaG4BFzoVIlql4=; b=Mt8iSkW6+Q3rk2yuJNHGfSGm3dSQ3NfuM+tjLJLa/qBWlknl4KILOdajV9eUev6YMvBEu2 Ws+6vEHmOhF9nWEciPKli8g6ItOm6F5zZHnYYv5NRogG9TtZET952B+QxsMNi7o6D5EQHK jTxObGlpHL2NI8HksWozMElDTKvtQuY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-433-hUurNkc0OgOJo9P6Q4c4UA-1; Mon, 31 May 2021 01:07:50 -0400 X-MC-Unique: hUurNkc0OgOJo9P6Q4c4UA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 45AA7801107; Mon, 31 May 2021 05:07:49 +0000 (UTC) Received: from localhost (ovpn-12-235.pek2.redhat.com [10.72.12.235]) by smtp.corp.redhat.com (Postfix) with ESMTP id 80E79690EF; Mon, 31 May 2021 05:07:45 +0000 (UTC) From: Ming Lei To: "Martin K . Petersen" , linux-scsi@vger.kernel.org Cc: Ming Lei , Bart Van Assche , John Garry , Hannes Reinecke Subject: [PATCH V3 2/3] scsi: core: fix failure handling of scsi_add_host_with_dma Date: Mon, 31 May 2021 13:07:26 +0800 Message-Id: <20210531050727.2353973-3-ming.lei@redhat.com> In-Reply-To: <20210531050727.2353973-1-ming.lei@redhat.com> References: <20210531050727.2353973-1-ming.lei@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org When scsi_add_host_with_dma() return failure, the caller will call scsi_host_put(shost) to release everything allocated for this host instance. So we can't free allocated stuff in scsi_add_host_with_dma(), otherwise double free will be caused. Strictly speaking, these host resources allocation should have been moved to scsi_host_alloc(), but the allocation may need driver's info which can be built between calling scsi_host_alloc() and scsi_add_host(), so just keep the allocations in scsi_add_host_with_dma(). Fixes the problem by relying on host device's release handler to release everything. Cc: Bart Van Assche Cc: John Garry Cc: Hannes Reinecke Signed-off-by: Ming Lei Reviewed-by: Hannes Reinecke --- drivers/scsi/hosts.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index ada11e3ae577..6cbc3eb16525 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -279,23 +279,22 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, if (!shost->work_q) { error = -EINVAL; - goto out_free_shost_data; + goto out_del_dev; } } error = scsi_sysfs_add_host(shost); if (error) - goto out_destroy_host; + goto out_del_dev; scsi_proc_host_add(shost); scsi_autopm_put_host(shost); return error; - out_destroy_host: - if (shost->work_q) - destroy_workqueue(shost->work_q); - out_free_shost_data: - kfree(shost->shost_data); + /* + * any host allocation in this function will be freed in + * scsi_host_dev_release, so don't free them in the failure path + */ out_del_dev: device_del(&shost->shost_dev); out_del_gendev: @@ -305,7 +304,6 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, pm_runtime_disable(&shost->shost_gendev); pm_runtime_set_suspended(&shost->shost_gendev); pm_runtime_put_noidle(&shost->shost_gendev); - scsi_mq_destroy_tags(shost); fail: return error; } From patchwork Mon May 31 05:07:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 12288663 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=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham 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 B4075C4708F for ; Mon, 31 May 2021 05:07:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92AD1610C8 for ; Mon, 31 May 2021 05:07:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230013AbhEaFJh (ORCPT ); Mon, 31 May 2021 01:09:37 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:20256 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229730AbhEaFJg (ORCPT ); Mon, 31 May 2021 01:09:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622437677; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5UJIi0KGvBaJJMBe4y1OqXoaepCK8zMrucjMhS/LFV0=; b=WtBCbfACmgs+SyRUW2y9a6IAN9JxBi8bjj8yLMty5yNG+b9V9EhBDZAvmL8xIAiFzmkGmi Eam45MN8W5R/ucmYwkyAGeEALBt+bx9fOdwDZA5SpJl2qa6lbkMNu2xSbHwAMxx3iJArke 5xdpeYlIMmj5rd2dzQTRfXPGTlkRppg= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-312-BAu6HjUBNiGmgejvujs9xQ-1; Mon, 31 May 2021 01:07:55 -0400 X-MC-Unique: BAu6HjUBNiGmgejvujs9xQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3D74A8042C9; Mon, 31 May 2021 05:07:54 +0000 (UTC) Received: from localhost (ovpn-12-235.pek2.redhat.com [10.72.12.235]) by smtp.corp.redhat.com (Postfix) with ESMTP id 75A595D740; Mon, 31 May 2021 05:07:51 +0000 (UTC) From: Ming Lei To: "Martin K . Petersen" , linux-scsi@vger.kernel.org Cc: Ming Lei , John Garry , Bart Van Assche , Hannes Reinecke Subject: [PATCH V3 3/3] scsi: core: put ->shost_gendev.parent in failure handling path Date: Mon, 31 May 2021 13:07:27 +0800 Message-Id: <20210531050727.2353973-4-ming.lei@redhat.com> In-Reply-To: <20210531050727.2353973-1-ming.lei@redhat.com> References: <20210531050727.2353973-1-ming.lei@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org get_device(shost->shost_gendev.parent) is called in scsi_add_host_with_dma(), but its counter pair isn't called in the failure path, so fix it by calling put_device(shost->shost_gendev.parent) in its failure path. Reported-by: John Garry Cc: Bart Van Assche Cc: Hannes Reinecke Signed-off-by: Ming Lei --- drivers/scsi/hosts.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c index 6cbc3eb16525..6cc43c51b7b3 100644 --- a/drivers/scsi/hosts.c +++ b/drivers/scsi/hosts.c @@ -298,6 +298,7 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev, out_del_dev: device_del(&shost->shost_dev); out_del_gendev: + put_device(shost->shost_gendev.parent); device_del(&shost->shost_gendev); out_disable_runtime_pm: device_disable_async_suspend(&shost->shost_gendev);