Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp619793rdb; Thu, 21 Dec 2023 22:50:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IFLK6JgGNsA+8aW15xEruNBgn7iB3IZJQZfbVFcz8ne+IkmdrIH6eW+w0OFLB5Lwjjp0DLX X-Received: by 2002:a05:6e02:1a84:b0:35f:cace:45cc with SMTP id k4-20020a056e021a8400b0035fcace45ccmr842320ilv.67.1703227811593; Thu, 21 Dec 2023 22:50:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703227811; cv=none; d=google.com; s=arc-20160816; b=dl/EWf6yl3Gui/FDa85JvjKOp6/DryP6L11zyIuSwTu5D92G6tWH9iBghjZe6fpB+r dZ5Q1fxolK5AsIyMNR1Uc03hWkHbDexMDP3W4T7lJkj1Zt5+czH4TSRBmmN6KKg+JgYD 7SpcRGTyMf7eZicaG5lR26N6USQwa7giQbdgbvG2Z4OECQlxFBm4KUl0Mvxhmhn43MYW TNGPzzqfur0QEb9it05WXL8NgOybY38sWMHPLwlKyNyUXF/oKSp/R9NtjOsRVEeKH6CY qYJW3c3rQeWqU3h+PchlF7t4Z4ZHEwz+i2O64LDQ/HdB5tHnYbAVBXRZ3PMCaHd4YHJt ds0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=Lylqq7ftixmgzmGz5mqZoPbKSpJD7b/C154szLGN3Cg=; fh=/2KH21NPI/bo4nHdD4kLOWrOY3wk6VvIn6fPiWvQ8Hc=; b=FvdwoHh3vxTmP5AYmAJTLVdlf/KOEWjmdW2v1cMm2VPlpi5mLGIvxvp1I1j+6Q04+y JBZ90+vcYGcNjKGnAhOoxzQbPx0/Z88ffwOd6NBQLT/D3ULkKAUxDUmtwE+9EdeV8rbb 2cNdqFefmhXGUwbkLTPMkYwmBOpv4lhTGKPxaeL1IHfiywAq8E29ImwCBrrehlST8t8u 5886x9S2neXuGgtoJDshKE/SuKgtQWzJQMN/4Kff876TNvo0TlMuTYmfo82e3/cAX6rh fYs78usulWawfEvg9Ml0kHXxseOaRgTtOB2X1VUBgh76jZhXdKAGzmFMWXTeOaiQ6NjS sMaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=bAvN7k5z; spf=pass (google.com: domain of linux-kernel+bounces-9422-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-9422-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id ck2-20020a056a02090200b005c6bd30ec26si2933529pgb.449.2023.12.21.22.50.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 22:50:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-9422-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=bAvN7k5z; spf=pass (google.com: domain of linux-kernel+bounces-9422-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-9422-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 46A30B22318 for ; Fri, 22 Dec 2023 06:49:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D91928F6E; Fri, 22 Dec 2023 06:49:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="bAvN7k5z" X-Original-To: linux-kernel@vger.kernel.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 5CDFD8C00; Fri, 22 Dec 2023 06:49:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=Lylqq7ftixmgzmGz5mqZoPbKSpJD7b/C154szLGN3Cg=; b=bAvN7k5zDaRqUU3TXNGvo2oprA 3F3Z3EYrftvBWm9hUR1uK+3xL6tYR9T8crprYsp/P5rHwsfDjMH1oIxBkBN5OrOAUBtJoEje6AqMj r9VDkHnr6hY3XaV3+ZKYoEde1r+k6JqdX/t+exjHGZPAz7ocN7LAq/kpsvGke6SEqFefXWHYySAYx c9zjCUsh8xAhzCdFY2GhiK8K09fIt2YBV1M0O7JtUi4aWaes8KryqKAp13G2zS+Q41RB3nR71Is/E rWGZMqyvVrAWbWljCWaGTBD+iPKm2OEWIGzdH+vA7EsUWAqewuKfpCIY4i8JzCCy0QoyKyJ7n/7Zo YZAqLAmw==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1rGZLJ-0056er-0C; Fri, 22 Dec 2023 06:49:09 +0000 Date: Thu, 21 Dec 2023 22:49:09 -0800 From: Luis Chamberlain To: 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, yukuai3@huawei.com, yi.zhang@huawei.com, houtao1@huawei.com, yangerkun@huawei.com Subject: Re: [PATCH] scsi: sd: unregister device if device_add_disk() failed in sd_probe() Message-ID: References: <20231208082335.1754205-1-linan666@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231208082335.1754205-1-linan666@huaweicloud.com> Sender: Luis Chamberlain 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. 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