Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 1A41B51C050 for ; Tue, 13 Oct 2026 15:30:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791905451; cv=none; b=rcfGf8BfSZUZygdoBYp6oOcOiCTnNRnJCi4k4CTkBxUKl1qPX5F8WzmYf7e68IXzJjCcc3e3dA/pOO64F9YX67MnCenOmMOd9toARDQmv4svOHGHWvoQbNzjjLS8RqO7DpDGEMnUDzxrMDcXPK1YBiRhqbfMvPLiEMfpimLerv0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791905451; c=relaxed/simple; bh=OQdmbk2E/we57Ljm1R8wCio0epZXMvfe696itQiVgFU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S1YyzOF/ZV3uyNUDp/A3JPBYFJ4cRf0eUySITpzBBJ2KcPaMMd6vrAOzjrvZ8c0zNGbkkeF0+JEsnK5riz4i7o5wCkgvYFjK3KoUbQL5ajcsdOOX1pkdDpatTN1XBkoNzr/MDIfIfKkXT1VEnKqFzbrbdk1PZy9mXGtI/1bysRA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cHPfK/mP; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cHPfK/mP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6CD6A1F000FF; Tue, 13 Oct 2026 15:30:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1791905450; bh=tmMtlHsi+G0nvYLKZT7CzUlLR2jgP4gIQCmP8BP44vs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cHPfK/mPWW4qazPo1mIvcWraNa4pjVAYrizaR3G6PMTv7rjl9V59dtN7Q2KSm0DVq Ae++19MRROfMwcsGevX3hg6e4O7LH1AtAjOEpw+gHGNnegDsnaevoXDHUDhqOSL/rH NdDuHi1pTaepTCGcJP8HqyEI6BEuQNMGL6jyyz3aw08OhVlnnZ5HEgx40ugXpPLT10 1D9ls0jTzzHPKq4Pi8QVyWzhAYpTK1YfteiE6OD1kCBFujPiLXjtstaXfsO06jiwxl b6xlVXymzYT4gzGLRtFNl83VtQJcTRXNDR74HB7omulJ00mgkQndGDNd2W1udAS5Su glIYBsQVH0kag== From: Sasha Levin To: stable@vger.kernel.org Cc: Ahmed Abdelhaleem Ahmed , Laurence Oberman , "Martin K. Petersen (Oracle)" , Sasha Levin Subject: [PATCH 6.18.y] scsi: ch: Do not keep references to data transfer element devices Date: Tue, 13 Oct 2026 11:30:48 -0400 Message-ID: <20261013153026.104382-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026101234-harmonics-pampers-6f11@gregkh> References: <2026101234-harmonics-pampers-6f11@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ahmed Abdelhaleem Ahmed [ Upstream commit 27c0f718d3b3c99e6d315308e95d987d0382a8a8 ] ch_readconfig() looks up the scsi_device of every data transfer element whose SCSI id the changer reports in READ ELEMENT STATUS, and stores it in ch->dt[]. scsi_device_lookup() takes a reference, and nothing ever drops it: ch_destroy() frees the array with kfree(). ch->dt[] is read nowhere else - it only supplies the vendor, model and revision printed in the same loop. Once such a drive is removed, its scsi_device can never be released. It stays on the host's device list at its address, so a new device there is refused by anything that walks the list - target_core_pscsi reports "scsi_device_get() failed for H:C:T:L" - and the low-level driver's module can no longer be unloaded. Only a reboot recovers. It shows with any changer that reports its drives' ids; with the mhvtl virtual library (IBM 3573-TL personality), each create and remove of a library with four drives leaves four references behind, counted by the module's use count in lsmod. With ch not bound the count is unchanged, and with this patch applied it is unchanged too. Drop the reference as soon as the name has been printed, and remove the now unused dt[] array. That also removes the array leaked when ch_probe() fails after ch_readconfig(). The same leak was reported with an RFC patch in 2022, which was not merged. Fixes: daa6eda65a53 ("[SCSI] add scsi changer driver") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/linux-scsi/20220719075442.6215-1-yanghao_ht@163.com/ Signed-off-by: Ahmed Abdelhaleem Ahmed Reviewed-by: Laurence Oberman Fixes: daa6eda65a53 ("[SCSI] add scsi changer driver") Link: https://patch.msgid.link/20260929-ch-dt-leak-v2-1-ddda8d635dca@gmail.com Signed-off-by: Martin K. Petersen (Oracle) [ sashal: Adapted allocation removal from kzalloc_objs() to the existing two-line kcalloc() call. ] Signed-off-by: Sasha Levin --- drivers/scsi/ch.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/drivers/scsi/ch.c b/drivers/scsi/ch.c index f2b63e4b9b99e..95f3894c0acd2 100644 --- a/drivers/scsi/ch.c +++ b/drivers/scsi/ch.c @@ -112,7 +112,6 @@ typedef struct { int minor; char name[8]; struct scsi_device *device; - struct scsi_device **dt; /* ptrs to data transfer elements */ u_int firsts[CH_TYPES]; u_int counts[CH_TYPES]; u_int voltags; @@ -363,16 +362,10 @@ ch_readconfig(scsi_changer *ch) vendor_labels[i]); } - /* look up the devices of the data transfer elements */ - ch->dt = kcalloc(ch->counts[CHET_DT], sizeof(*ch->dt), - GFP_KERNEL); - - if (!ch->dt) { - kfree(buffer); - return -ENOMEM; - } - + /* report the devices of the data transfer elements */ for (elem = 0; elem < ch->counts[CHET_DT]; elem++) { + struct scsi_device *sdev; + id = -1; lun = 0; if (elem < CH_DT_MAX && -1 != dt_id[elem]) { @@ -388,10 +381,8 @@ ch_readconfig(scsi_changer *ch) VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]); if (data[6] & 0x80) { VPRINTK(KERN_CONT, "not this SCSI bus\n"); - ch->dt[elem] = NULL; } else if (0 == (data[6] & 0x30)) { VPRINTK(KERN_CONT, "ID/LUN unknown\n"); - ch->dt[elem] = NULL; } else { id = ch->device->id; lun = 0; @@ -401,18 +392,16 @@ ch_readconfig(scsi_changer *ch) } if (-1 != id) { VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun); - ch->dt[elem] = - scsi_device_lookup(ch->device->host, - ch->device->channel, - id,lun); - if (!ch->dt[elem]) { + sdev = scsi_device_lookup(ch->device->host, + ch->device->channel, + id, lun); + if (!sdev) { /* should not happen */ VPRINTK(KERN_CONT, "Huh? device not found!\n"); } else { VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n", - ch->dt[elem]->vendor, - ch->dt[elem]->model, - ch->dt[elem]->rev); + sdev->vendor, sdev->model, sdev->rev); + scsi_device_put(sdev); } } } @@ -576,7 +565,6 @@ static void ch_destroy(struct kref *ref) scsi_changer *ch = container_of(ref, scsi_changer, ref); ch->device = NULL; - kfree(ch->dt); kfree(ch); } -- 2.53.0