Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp655159rdb; Fri, 22 Dec 2023 00:27:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEnYyoPvUe6XwrGvufVcIaWeJfYjuXgJRwCQGqARxcNYj/KP+m6PuhVCzxs8/GnAwIf4IKC X-Received: by 2002:a05:6a20:1192:b0:18f:c76a:993c with SMTP id v18-20020a056a20119200b0018fc76a993cmr602565pze.98.1703233674991; Fri, 22 Dec 2023 00:27:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703233674; cv=none; d=google.com; s=arc-20160816; b=la4D171LN26czqDnQO9UwORoO5htjC1HcWy0im75R2FwErXnMlGp7/s+QEF4CDBpZR 2nt+ftRva9pqsjboOhyUqEYUH6C7ACF1E0bU6GvqF7gqXiQhjWsg6o9YpeAey5PMBvwr JBvGfy/j4SCPQJ/4d3EjWH7kFYAUzQOKUYYBCv9up/06LZ8LOT5gYdA0hWk6BLw09rVh KPeF1Su0juPL47hEgSqiuPihxDzDquSmSJVfNO48olXFaatvNiJemUYNwZXDKSO+mAfi 51ez+PiW9wZd0Vbg33o+uf6dqySNdjznCe0sdKaWN6dLrgJ/T6LPIAfnd3h/jtn619wc PrGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:date:message-id:from :references:cc:to:subject; bh=b3qWl83/MWrQiF8wUH6idI24QkyfzHB9F9zIsw7fTNI=; fh=Gm7RlwEA4YRFaCoAMx5K/2QczN68tW5XW/GNKdViyR0=; b=eArAmn6p9jl392Ggz79RQb+osbMrqFK0SDAciukFv/5Etz+lYVNAgSiuptqxDJqiCN szRye/vLmTHe0zUytmDdqc6W7Z4QuHsLF61GKvpHDVebsdmhhYcSAE66l9fUeysXMO8V iCrso9duj/xcYfe9XtgTIVQZfDluHDXUJuiTmNd+Rt+0TCMm+vFR330LehpKWz44iUZ4 0ySPQTG+3tzq8E+blNJW6vo9H8cwAkWUF6MEFWb4ydH8kQSADzvG5D6Yi1PcrewR5OdA p99njEFkVJad2/J3AYrUi8pDAryHXcsip2xYHXa/UFNVxKtTtOGgGnkwQUB/G4U8q5+J TaBg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-9498-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-9498-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r18-20020a62e412000000b006d09ceb6d1asi2918138pfh.173.2023.12.22.00.27.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Dec 2023 00:27:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-9498-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-9498-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-9498-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A86C4288033 for ; Fri, 22 Dec 2023 08:27:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4313A1863A; Fri, 22 Dec 2023 08:27:31 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27558182C1; Fri, 22 Dec 2023 08:27:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4SxL514k0Sz4f3k68; Fri, 22 Dec 2023 16:27:17 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 1542B1A0B11; Fri, 22 Dec 2023 16:27:19 +0800 (CST) Received: from [10.174.176.73] (unknown [10.174.176.73]) by APP1 (Coremail) with SMTP id cCh0CgCn9QtkSIVlB1VIEQ--.51555S3; Fri, 22 Dec 2023 16:27:18 +0800 (CST) Subject: Re: [PATCH] scsi: sd: unregister device if device_add_disk() failed in sd_probe() To: Luis Chamberlain , linan666@huaweicloud.com Cc: jejb@linux.ibm.com, martin.petersen@oracle.com, linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, linan122@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com, "yukuai (C)" References: <20231208082335.1754205-1-linan666@huaweicloud.com> From: Yu Kuai Message-ID: <78fb6d82-c50a-8fea-ae6d-551fd35656bf@huaweicloud.com> Date: Fri, 22 Dec 2023 16:27:16 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:cCh0CgCn9QtkSIVlB1VIEQ--.51555S3 X-Coremail-Antispam: 1UD129KBjvJXoWxCrWxArW8uw4rXFW5tw1DWrg_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berlain ะด??: > On Fri, Dec 08, 2023 at 04:23:35PM +0800, linan666@huaweicloud.com wrote: >> From: Li Nan >> >> "if device_add() succeeds, you should call device_del() when you want to >> get rid of it." >> >> In sd_probe(), device_add_disk() fails when device_add() has already >> succeeded, so change put_device() to device_unregister() to ensure device >> resources are released. >> >> Fixes: 2a7a891f4c40 ("scsi: sd: Add error handling support for add_disk()") >> Signed-off-by: Li Nan > > Nacked-by: Luis Chamberlain > >> --- >> drivers/scsi/sd.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c >> index 542a4bbb21bc..d81cbeee06eb 100644 >> --- a/drivers/scsi/sd.c >> +++ b/drivers/scsi/sd.c >> @@ -3736,7 +3736,7 @@ static int sd_probe(struct device *dev) >> >> error = device_add_disk(dev, gd, NULL); >> if (error) { >> - put_device(&sdkp->disk_dev); >> + device_unregister(&sdkp->disk_dev); >> put_disk(gd); >> goto out; >> } > > This is incorrect, device_unregister() calls: > > void device_unregister(struct device *dev) > { > pr_debug("device: '%s': %s\n", dev_name(dev), __func__); > device_del(dev); > put_device(dev); > } > > So you're adding what you believe to be a correct missing device_del(). > But what you missed is that if device_add_disk() fails then device_add() > did not succeed because the new code we have in the kernel *today* unwinds > this for us now. I'm confused here, there are two device here, one is 'sdkp->disk_dev', one is gendisk->part0->bd_device, and the order in which they initialize: sd_probe device_add(&sdkp->disk_dev) -> succeed device_add_disk -> failed, and device_add(bd_device) did not succeed put_device(&sdkp->disk_dev) -> device_del is missed I don't see that if device_add_disk() fail, device_del() for 'sdkp->disk_dev'is called from anywhere. Do I missing anything? Thanks, Kuai > > What you missed is that in today's code inside device_add_disk(), if > device_add() succeeeds we now unwind and call device_del() for the > device for you. And so, quoting the next sentence you took from > device_add(): > > "If device_add() has *not* succeeded, use *only* put_device() to drop the > reference count." > > Please do reference in the future a crash dump / or explain how you > reached your conclusions if you do not have a crash dump to prove an > issue. Specially if you are suggesting it Fixes a commit. > > Luis > > . >