From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796387639; b=nxnYe5kfaS1JseMvubNAjOyde7bD1+L+q0Hi5ylvA6+VOVB8y6ec8gw6NobEUXeAnElW 0uFz3+b30qccFzxRuYLVb9wTWdqGVecqCn5RcwrHdm3ek5uSQJzeYFHYfuhVxzBrG6EC8 ZoBbWGODTSWkLDkUHGSZ0qgvxv+OEyS6BiNUNZul1MhmubHyndNy58vMSDUzzqTea4yCD EsC9T6HrR2WJahEsxxqV7h05IJftV+LxvTg4QbdKSv8wYOJL8t60NOYF35i1JXscXDhHZ b6kbHIjaPPIg2g8itTpFrWgzlQ0XcxCYRgVI6uuHQ9Yjf+gqJ76OUaJ2otmtuzw3LqA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387639; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=rmhaljbomJtvMyW3QIsq4AP+wNuf3H0pRccIWaE126o=; b=B5uw1cl5rnHH0oFClLa37vlA3Iejux3dIAGI9YnMv0Xf1Kx5p3Cw2N20oqz3uaAGnLCp Qb0XKgxbseKmJhLt0O2OaAsz+9CceNgpq7EWL9SdMy003EYuEkOYob59NiM7qJjQb+mZn 89PfA/fbwHnHW9mroVF8HmpEf0y75xi1mj6f08cvM/Xacy2vw5n1Ib0k+13qjOT+OHhlW j3Bjg9Z/79U6YP641hiAM9wIs01kjvwJBN4RfqMJa8tv00sg9Bwpf1evXqOpDUomdM/jk XrrJ3JVKRQ7XzHK4lMHTovQGJU8TfB7/oMBHuOpoKVA3IdNR6BQmibtqYWxCpkZdj/A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=bFU0mYY1; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EED3606A335; Fri, 4 Dec 2026 12:33:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=bFU0mYY1 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BB641F000C2; Fri, 4 Dec 2026 12:33:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387639; bh=rmhaljbomJtvMyW3QIsq4AP+wNuf3H0pRccIWaE126o=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bFU0mYY11XB3ZB/omlNj4mh+yjTdKS06D81QhIH8bD9NMCm26mQVP2SX0VdGlfhg/ S9eehdJ8If68ZpuPXMeWxoGv5cY5J/Qn1M/kxrDER0g1uN7yhC0oIPVk3A5jfxFIR2 dzYGiwpCT0u6ZcFx0WKxHdT8sCsFexab6Hce94uI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Damien Le Moal , Niklas Cassel , "Martin K. Petersen (Oracle)" , Sasha Levin Subject: [PATCH 6.1 0329/1059] scsi: scsi_debug: Report the residual of a write Date: Fri, 4 Dec 2026 18:57:40 +0900 Message-ID: <20261204100652.788350643@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Niklas Cassel [ Upstream commit 7c000c4ad2c0a7b2da06ffe28f5a7489fc52ca2d ] Documentation/scsi/scsi_mid_low_api.rst defines the residual as the length of the data buffer less the number of bytes actually transferred, so a write whose data buffer is larger than its transfer length leaves one. resp_read_dt0() and resp_write_tape() report it, but the write paths for a disk do not, so such a write completes with GOOD status and a residual of zero while a READ of the same length into the same buffer reports it correctly. Report it in resp_write_dt0(), resp_write_scat() and resp_atomic_write(). resp_write_scat() differs twice over. Its data-out buffer also holds the parameter list header and the LBA range descriptors, so what the command consumed is sg_off rather than the bytes that the last range transferred; and sg_off counts what was asked for rather than what was transferred, so it can exceed the buffer and needs a guard. A command with no LBA range descriptors or a buffer transfer length of zero transfers nothing, so the whole buffer is residual. An error, real or injected, can end the command after some ranges have been written, so the residual is reported on every exit once the parameter list has been fetched. Assisted-by: LLM Reviewed-by: Damien Le Moal Signed-off-by: Niklas Cassel Link: https://patch.msgid.link/20260929082456.857423-22-cassel@kernel.org Signed-off-by: Martin K. Petersen (Oracle) [ sashal: Adapted to the older base: sdeb_write_unlock() replaces sdeb_meta_write_unlock(), and the missing atomic-write function's hunk was dropped, so only two of three upstream hunks apply. ] Signed-off-by: Sasha Levin --- drivers/scsi/scsi_debug.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c index 6a9a591c5dd3a..5bd748ca1ffd3 100644 --- a/drivers/scsi/scsi_debug.c +++ b/drivers/scsi/scsi_debug.c @@ -3711,6 +3711,9 @@ static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) "%s: write: cdb indicated=%u, IO sent=%d bytes\n", my_name, num * sdebug_sector_size, ret); + if (ret < scsi_bufflen(scp)) + scsi_set_resid(scp, scsi_bufflen(scp) - ret); + if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) && atomic_read(&sdeb_inject_pending))) { if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) { @@ -3778,8 +3781,10 @@ static int resp_write_scat(struct scsi_cmnd *scp, "Unprotected WR to DIF device\n"); } } - if ((num_lrd == 0) || (bt_len == 0)) + if (num_lrd == 0 || bt_len == 0) { + scsi_set_resid(scp, scsi_bufflen(scp)); return 0; /* T10 says these do-nothings are not errors */ + } if (lbdof == 0) { if (sdebug_verbose) sdev_printk(KERN_INFO, scp->device, @@ -3871,6 +3876,8 @@ static int resp_write_scat(struct scsi_cmnd *scp, if (unlikely((sdebug_opts & SDEBUG_OPT_RECOV_DIF_DIX) && atomic_read(&sdeb_inject_pending))) { + /* This range has been written */ + sg_off += num_by; if (sdebug_opts & SDEBUG_OPT_RECOVERED_ERR) { mk_sense_buffer(scp, RECOVERED_ERROR, THRESHOLD_EXCEEDED, 0); atomic_set(&sdeb_inject_pending, 0); @@ -3894,6 +3901,13 @@ static int resp_write_scat(struct scsi_cmnd *scp, } ret = 0; err_out_unlock: + /* + * sg_off counts what the command asked for, which can exceed the + * buffer: lbdof is not validated against it, and a range is counted + * in full even if do_device_access() copied less. + */ + if (scsi_bufflen(scp) > sg_off) + scsi_set_resid(scp, scsi_bufflen(scp) - sg_off); sdeb_write_unlock(sip); err_out: kfree(lrdp); -- 2.53.0