Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3525252pxf; Mon, 15 Mar 2021 11:29:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxGr/6yBiGXiBES2ux8D/11KCS+cm+ha0TU1EnuoHaPjWHLAIIYnVgs0tGFTqvpFkcTzg8P X-Received: by 2002:a17:906:73cd:: with SMTP id n13mr24674072ejl.535.1615832988115; Mon, 15 Mar 2021 11:29:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615832988; cv=none; d=google.com; s=arc-20160816; b=UAXvC5ECXxaph03WeneMyYubbzJsO+mIy/CsaUu1byqp4kV2jubsMN1/BQ8GSFqfhi R31QLw/HPJ1XXmD//UMYQh9b1h4XxQrLAhTQ3QwS953Ks7tvTUidll2aZNCKSTX9BmvC oPuUwYdL+nDE9bVCEYashzgkK7n8BhBLjEDcBcnwMXxU5Ts4HNKE4QSn6Sp149O6QkXR FocSkZ099D2J2Nn63DH8UF1jr0kPCt/o9rBTPGwhBNwi3iMvkTyVhXo04P1gIFUAj4xw 82/sfXlGwcnnLaRxBuZSFRcGIQuADGeo9MAkkAF4X4W5trNOc/U6TWiTK/8H5XwKonhq OKmg== 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=LrA/O4yk1185BkpEu+KCGMH0Lzjn72GVP3e4EYC1Iiw=; b=J738QH5bwdd+KZvpavusS0aH6yec/ny/COO2WzhxzFhaVMCrgJck8Ui41gNjOvijL8 lTgG9nX0N6BlAp4NU7g9QK6IGeuXra1zm3Rh9NcgDkyNuWuUdUKTJ5HyudAjBCobIvAa 7z5a/Xr/14dfx/i6hbZH7Smes+i+Dxss/FRU70Q0KsdrRo4zRdtGOuUU2oJjWeT7OwT2 SPtw9A5fNnlcFy57ZLa4AXJZMel4GsM5ee305NPD930ugNqYfpClASfWThBDOzPNbjwH A/8+D6uEYQ8aMGrBtGJmZAV/ICRRTxHPa0UpnfjZFipN9gIZ125tsURChjzV1w+LKVZs J8+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RKSx1caN; 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 k3si12376798edh.184.2021.03.15.11.29.23; Mon, 15 Mar 2021 11:29:48 -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=RKSx1caN; 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 S236157AbhCOOej (ORCPT + 99 others); Mon, 15 Mar 2021 10:34:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:37522 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233419AbhCOOBk (ORCPT ); Mon, 15 Mar 2021 10:01:40 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1521064EEA; Mon, 15 Mar 2021 14:01:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816877; bh=slnlhpSBqrcz8K5+o2gwuRlOg2HLVJt1EKKH8unuxJQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RKSx1caNA0Sfvu7v6TdSzKyJEGrwpy6bBM+p1VTzvaMKYlwW8SZYWoXPbZNMJPaZI 8eswbuSgVDQLRuTvy7OamYXhOMeF9+eSUMtiv8BLFlkuchBK8htrvXUJ5in0CB7FAI /p+W20f5pGewv48XiddpdVkpqkZCc5fO+DQQkbj0= 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.10 164/290] scsi: target: core: Add cmd length set before cmd complete Date: Mon, 15 Mar 2021 14:54:17 +0100 Message-Id: <20210315135547.447658469@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135541.921894249@linuxfoundation.org> References: <20210315135541.921894249@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 ff26ab0a5f60..484f0ba0a65b 100644 --- a/drivers/target/target_core_transport.c +++ b/drivers/target/target_core_transport.c @@ -873,11 +873,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 { @@ -887,6 +885,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