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=1796384968; b=tF7YfY6OAUnvy2afZPNY/sJvRyF2hjpnLROsjnmgTf9HRVZTKOa/YX84bjiICasaj5AF +8CrWif7gwTxcZsscfPXYwvd+LBo05OnbJ29phCMNgFPEmJ0lDJkRwFWxIfUZsT/v7HwN /ryW0Fx+RxzaRcfGjfJ0HTgoK2EhUJGmFZ8ZMnWPLnhzgE2R7cENN92hY3dOnjGty7c5T oQ6Fv68KlRyn6yqCZLqSJQmN/iLrWo2J1dviKrdWXrCAEEg7W9wlLouwQJdQiAuTUi8f3 LZAaN5xeCspjhF0qecDcX/hPEF3wU0+C++88STTG4KWhWHd3nuhxqUdxQ+4tfIoKTbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384968; 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=kpknKuZSIv20QeTLUt4D4H2Sm6R0/+E/tCA9JHgVwlE=; b=bGpf5HaFWerhs7zaGPqYzJ8q7CGSs045ravxzSCyMyl2/wQupB8lvD+8zQlEHmqAJhYO vt5G4ExrK9SYMLQNn+OQ5w7JzSccFL8mr1BLpB0/nzQOfFW1mw0pRrqNtZoIL7QmnCL8z l3tIe02Rt6Wqvl4Bf3QYZU22Y7t1gK072AhRcd3n8UOApgvyYv4u5kQvI/jP7BaFUqma3 V6dIArakTCrn0F4tlnCCDmn6gVLkCSJRK9b5eI+ZItE4wK0TN0n+AY+RZ2SbaTe1aXdof /MynrFO87DjQKEiWsSV4+bZdjwudazIUGA2aQ31ZBZ62mTdZoNwk9PCl+8qa1sdp/2w== 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="mNEp/OSp"; 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 7E92C606A322; Fri, 4 Dec 2026 11:49:28 +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=mNEp/OSp Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00B8F1F0092C; Fri, 4 Dec 2026 11:49:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384967; bh=kpknKuZSIv20QeTLUt4D4H2Sm6R0/+E/tCA9JHgVwlE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mNEp/OSppuoADM6HX2E2kD7ZtwBsvKXEUugDKr9ixNIunZ38KpwA9AgDAjMNQNHiY q3Cx/mQLrwQYYV5Wd1kKdgGftA5TTL3xrwtLQ+xhKNOEwdZcQdpHIJcpjT3XDbIBTj XjBkZ2DrHTjr1FuvLxNdVTLl2TrBqyYYN4ivrJpM= 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.6 0364/1167] scsi: scsi_debug: Report the residual of a write Date: Fri, 4 Dec 2026 18:56:31 +0900 Message-ID: <20261204100711.465172424@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@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.6-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: Dropped the resp_atomic_write() hunk (function absent), and adapted context to use sdeb_write_unlock() and the older mk_sense_buffer()/THRESHOLD_EXCEEDED names. ] 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 0d5dce648ff33..df9cd0352cb31 100644 --- a/drivers/scsi/scsi_debug.c +++ b/drivers/scsi/scsi_debug.c @@ -3699,6 +3699,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) { @@ -3766,8 +3769,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, @@ -3859,6 +3864,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); @@ -3882,6 +3889,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