Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3524931pxf; Mon, 15 Mar 2021 11:29:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5coMmp8GAeKfFn2Mx0aS4Lc2OQirUxvS69hrvrjXcrXuuYh0d15SwTDeWZrR05A4j4I4a X-Received: by 2002:a17:906:4ada:: with SMTP id u26mr25370297ejt.129.1615832960245; Mon, 15 Mar 2021 11:29:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615832960; cv=none; d=google.com; s=arc-20160816; b=ev41deBN2+2hJZWBQRctGWU1Q32BfJfhdOK7QBzYk88jzW3iaz+8UP3O+oIGVzB10f xkFdUilv+7C85T5pgipaxSgdQj4KXaNhnig/p02xbhxU6UBdGoArrLTwRYr6XovJeiri iQwRmbp4uFhxqNZuT9DXyxHcMyHa0RtUpBBvrtP9ns85CIAM8LT2V9c8LSkIvU1RkRDE +l9LXWDDcQL26SIGa7ylc8+Lie8Kse5D7c4FPNcfVOB0Z6dcAthidrp1ayLKzW5Ns8Tz 0jYGJpE99Od93FKQT7QhZbj0wLJHx7/vCDXGS8XN+UojqR1W7m0VSCwNhTINiKUYzVwH N9rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EKHcm5KVoNmTJRy7w+bupxp33xpqgl9NtNpUAIAyLkM=; b=o9ja2tvFLxM3s6z30RH/FQAFd5sZ6p+C8NgpiMFTHSS48KE47EkSL4F4S33wYNhxTE MlrVAZR79zrlhphXKKwqU6v/IcXAFnmd/mLSavnr8oBCQ5CWz/aDMDyZX7N3wDwfAMwW DLdZuRe61WuiW7yaBp+BbrIC1VJnUZ7/Bovh4GhM3rv/HnKZguQwj2gNZSlLV6I87Sys iIS4zsJBcnrfpf297a8qkmCNO0jfPEUIIXgx/Z0Vgb8m6yOL0t98CHGiQQ9Xr0eK0bLf zN1AaEtSeeqPOsmZSZXZlWpyR9ZSRfRvr7vrxLnW/CnhX4RJoN/uS0b+qtIINHIX9RnQ 89FA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=056YbQqz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x20si11158652edq.132.2021.03.15.11.28.49; Mon, 15 Mar 2021 11:29:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=056YbQqz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239941AbhCOOdT (ORCPT + 99 others); Mon, 15 Mar 2021 10:33:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:37836 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233244AbhCOOBN (ORCPT ); Mon, 15 Mar 2021 10:01:13 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 082B564F3A; Mon, 15 Mar 2021 14:00:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816851; bh=Dp2YzIEslFX58KqusQUqq6n8tKRltEpnHGHHvqBAL1E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=056YbQqz6J80u0b9LIvxpfyHB/Z4dZD99pRWcVu4P38/xU+KG0Es6JgycXmOBFXkV npb+Y5VR9F+Ub2GyIIQcFTF705zD9Pnhs3669hMeisU38irRm7fPUy+jP3ytii361f gb8uKe2iVjPqWvNij16wQ2Qo6Dgxqk+yG6F/whdY= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Roman Bolshakov , Bodo Stroesser , Aleksandr Miloserdov , "Martin K. Petersen" , Sasha Levin Subject: [PATCH 5.11 161/306] scsi: target: core: Add cmd length set before cmd complete Date: Mon, 15 Mar 2021 14:53:44 +0100 Message-Id: <20210315135513.081420778@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135507.611436477@linuxfoundation.org> References: <20210315135507.611436477@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman From: Aleksandr Miloserdov [ Upstream commit 1c73e0c5e54d5f7d77f422a10b03ebe61eaed5ad ] TCM doesn't properly handle underflow case for service actions. One way to prevent it is to always complete command with target_complete_cmd_with_length(), however it requires access to data_sg, which is not always available. This change introduces target_set_cmd_data_length() function which allows to set command data length before completing it. Link: https://lore.kernel.org/r/20210209072202.41154-2-a.miloserdov@yadro.com Reviewed-by: Roman Bolshakov Reviewed-by: Bodo Stroesser Signed-off-by: Aleksandr Miloserdov Signed-off-by: Martin K. Petersen Signed-off-by: Sasha Levin --- drivers/target/target_core_transport.c | 15 +++++++++++---- include/target/target_core_backend.h | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/target/target_core_transport.c b/drivers/target/target_core_transport.c index fca4bd079d02..8a4d58fdc9fe 100644 --- a/drivers/target/target_core_transport.c +++ b/drivers/target/target_core_transport.c @@ -879,11 +879,9 @@ void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status) } EXPORT_SYMBOL(target_complete_cmd); -void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length) +void target_set_cmd_data_length(struct se_cmd *cmd, int length) { - if ((scsi_status == SAM_STAT_GOOD || - cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) && - length < cmd->data_length) { + if (length < cmd->data_length) { if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) { cmd->residual_count += cmd->data_length - length; } else { @@ -893,6 +891,15 @@ void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int len cmd->data_length = length; } +} +EXPORT_SYMBOL(target_set_cmd_data_length); + +void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length) +{ + if (scsi_status == SAM_STAT_GOOD || + cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) { + target_set_cmd_data_length(cmd, length); + } target_complete_cmd(cmd, scsi_status); } diff --git a/include/target/target_core_backend.h b/include/target/target_core_backend.h index 6336780d83a7..ce2fba49c95d 100644 --- a/include/target/target_core_backend.h +++ b/include/target/target_core_backend.h @@ -72,6 +72,7 @@ int transport_backend_register(const struct target_backend_ops *); void target_backend_unregister(const struct target_backend_ops *); void target_complete_cmd(struct se_cmd *, u8); +void target_set_cmd_data_length(struct se_cmd *, int); void target_complete_cmd_with_length(struct se_cmd *, u8, int); void transport_copy_sense_to_cmd(struct se_cmd *, unsigned char *); -- 2.30.1