From nobody Thu Nov 6 08:32:58 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1497524710509846.983883431475; Thu, 15 Jun 2017 04:05:10 -0700 (PDT) Received: from localhost ([::1]:53175 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLSaL-0002PF-7z for importer@patchew.org; Thu, 15 Jun 2017 07:05:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40815) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dLSOz-0008DS-1l for qemu-devel@nongnu.org; Thu, 15 Jun 2017 06:53:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dLSOx-0007XN-ST for qemu-devel@nongnu.org; Thu, 15 Jun 2017 06:53:25 -0400 Received: from mail-wr0-x241.google.com ([2a00:1450:400c:c0c::241]:33444) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dLSOx-0007Wx-Ja for qemu-devel@nongnu.org; Thu, 15 Jun 2017 06:53:23 -0400 Received: by mail-wr0-x241.google.com with SMTP id x23so2767453wrb.0 for ; Thu, 15 Jun 2017 03:53:23 -0700 (PDT) Received: from 640k.lan (94-39-191-51.adsl-ull.clienti.tiscali.it. [94.39.191.51]) by smtp.gmail.com with ESMTPSA id f21sm3258597wra.5.2017.06.15.03.53.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Jun 2017 03:53:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=/wuuYCeN00W1NKfI8UIb0cw6Ys0G+AEkcQs73caMZtQ=; b=ku29pFvJ2NictLgg6M09VBnypFcwPzCFYqD851PLwr9LA3JQDbU2GyZ0835qHHdeVA ub+jrCaXaQq4mXkSW1QwUFKV0lS+59brZPpLDEZptiDQcE7RqX3BmBb9HQGMoVuBAXmT YI2ei9036aodCodtQ2gQVwaucUPoCDpVYlxHMS+HkeygS5qg7LB68z4/hBsQxB9avt2I 1uuHMHpzxhDhfl62kVxhD/PfPwfmN59rrvg0qeqxQ+x8s7NPLtPHF/zqR0cOSWAaGrtJ hsNKHswtAM0+DUAW7P07lj5agrXk9Jd5VpHWHjQ0zyg6xlStx3Ui7/Cxt+udaB53ExcD wU+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=/wuuYCeN00W1NKfI8UIb0cw6Ys0G+AEkcQs73caMZtQ=; b=bc6yiNuSggfihOw5Il+HP/LUZRwURJ6Bf/VlwOY2EiG8ATKZxvV5rC+SB1cEPuQUZI DLq+pa90bkvtNNDuqQOoFMgmbvrQsc4BfX99Nlo+JocVpCpnm0UicbVw0XJTVMYaZv6l Nyz8yroW11cELLq5wrh6UQKpPm4iP/w4ZUsqPK6XdM5Y7kFn3vHNI6Q6uwEX2vDez2Ke e/kj8oq1unvlGBzBn/dV4k5mTFuTGICj1QTLAAyGER6Ul5yDfEI3MotM/CvMwpdnQMPX qhRpZEJnpKMz4VEx16ywFJvSjGKn3z8hlM2iFax5ml9sGAvgyHPaI0zvgn64lDtxmZGS 4rxQ== X-Gm-Message-State: AKS2vOwEZy4GmBEYK1kuii8Aho/DjrVab6oRD3xf/FGAKTHa111p/MsL v8CU4TjLcyau3+2IICw= X-Received: by 10.223.151.44 with SMTP id r41mr3245010wrb.6.1497524002351; Thu, 15 Jun 2017 03:53:22 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 15 Jun 2017 12:52:31 +0200 Message-Id: <1497523981-38449-12-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1497523981-38449-1-git-send-email-pbonzini@redhat.com> References: <1497523981-38449-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::241 Subject: [Qemu-devel] [PULL 11/41] megasas: do not read command more than once from frame X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Avoid TOC-TOU bugs by passing the frame_cmd down, and checking cmd->dcmd_opcode instead of cmd->frame->header.frame_cmd. Signed-off-by: Paolo Bonzini --- hw/scsi/megasas.c | 60 +++++++++++++++++++++++----------------------------= ---- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/hw/scsi/megasas.c b/hw/scsi/megasas.c index a3f75c1..38e0a2f 100644 --- a/hw/scsi/megasas.c +++ b/hw/scsi/megasas.c @@ -1591,12 +1591,13 @@ static int megasas_handle_dcmd(MegasasState *s, Meg= asasCmd *cmd) } =20 static int megasas_finish_internal_dcmd(MegasasCmd *cmd, - SCSIRequest *req) + SCSIRequest *req, size_t resid) { int retval =3D MFI_STAT_OK; int lun =3D req->lun; =20 trace_megasas_dcmd_internal_finish(cmd->index, cmd->dcmd_opcode, lun); + cmd->iov_size -=3D resid; switch (cmd->dcmd_opcode) { case MFI_DCMD_PD_GET_INFO: retval =3D megasas_pd_get_info_submit(req->dev, lun, cmd); @@ -1649,11 +1650,12 @@ static int megasas_enqueue_req(MegasasCmd *cmd, boo= l is_write) } =20 static int megasas_handle_scsi(MegasasState *s, MegasasCmd *cmd, - bool is_logical) + int frame_cmd) { uint8_t *cdb; bool is_write; struct SCSIDevice *sdev =3D NULL; + bool is_logical =3D (frame_cmd =3D=3D MFI_CMD_LD_SCSI_IO); =20 cdb =3D cmd->frame->pass.cdb; =20 @@ -1661,7 +1663,7 @@ static int megasas_handle_scsi(MegasasState *s, Megas= asCmd *cmd, if (cmd->frame->header.target_id >=3D MFI_MAX_LD || cmd->frame->header.lun_id !=3D 0) { trace_megasas_scsi_target_not_present( - mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical, + mfi_frame_desc[frame_cmd], is_logical, cmd->frame->header.target_id, cmd->frame->header.lun_id); return MFI_STAT_DEVICE_NOT_FOUND; } @@ -1671,19 +1673,20 @@ static int megasas_handle_scsi(MegasasState *s, Meg= asasCmd *cmd, =20 cmd->iov_size =3D le32_to_cpu(cmd->frame->header.data_len); trace_megasas_handle_scsi(mfi_frame_desc[cmd->frame->header.frame_cmd], - is_logical, cmd->frame->header.target_id, + trace_megasas_handle_scsi(mfi_frame_desc[frame_cmd], is_logical, + cmd->frame->header.target_id, cmd->frame->header.lun_id, sdev, cmd->iov_si= ze); =20 if (!sdev || (megasas_is_jbod(s) && is_logical)) { trace_megasas_scsi_target_not_present( - mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical, + mfi_frame_desc[frame_cmd], is_logical, cmd->frame->header.target_id, cmd->frame->header.lun_id); return MFI_STAT_DEVICE_NOT_FOUND; } =20 if (cmd->frame->header.cdb_len > 16) { trace_megasas_scsi_invalid_cdb_len( - mfi_frame_desc[cmd->frame->header.frame_cmd], is_logical, + mfi_frame_desc[frame_cmd], is_logical, cmd->frame->header.target_id, cmd->frame->header.lun_id, cmd->frame->header.cdb_len); megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE)); @@ -1703,7 +1706,7 @@ static int megasas_handle_scsi(MegasasState *s, Megas= asCmd *cmd, cmd->frame->header.lun_id, cdb, cmd); if (!cmd->req) { trace_megasas_scsi_req_alloc_failed( - mfi_frame_desc[cmd->frame->header.frame_cmd], + mfi_frame_desc[frame_cmd], cmd->frame->header.target_id, cmd->frame->header.lun_id); megasas_write_sense(cmd, SENSE_CODE(NO_SENSE)); cmd->frame->header.scsi_status =3D BUSY; @@ -1725,11 +1728,11 @@ static int megasas_handle_scsi(MegasasState *s, Meg= asasCmd *cmd, return MFI_STAT_INVALID_STATUS; } =20 -static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd) +static int megasas_handle_io(MegasasState *s, MegasasCmd *cmd, int frame_c= md) { uint32_t lba_count, lba_start_hi, lba_start_lo; uint64_t lba_start; - bool is_write =3D (cmd->frame->header.frame_cmd =3D=3D MFI_CMD_LD_WRIT= E); + bool is_write =3D (frame_cmd =3D=3D MFI_CMD_LD_WRITE); uint8_t cdb[16]; int len; struct SCSIDevice *sdev =3D NULL; @@ -1746,20 +1749,20 @@ static int megasas_handle_io(MegasasState *s, Megas= asCmd *cmd) } =20 trace_megasas_handle_io(cmd->index, - mfi_frame_desc[cmd->frame->header.frame_cmd], + mfi_frame_desc[frame_cmd], cmd->frame->header.target_id, cmd->frame->header.lun_id, (unsigned long)lba_start, (unsigned long)lba_c= ount); if (!sdev) { trace_megasas_io_target_not_present(cmd->index, - mfi_frame_desc[cmd->frame->header.frame_cmd], + mfi_frame_desc[frame_cmd], cmd->frame->header.target_id, cmd->frame->header.lun_id); return MFI_STAT_DEVICE_NOT_FOUND; } =20 if (cmd->frame->header.cdb_len > 16) { trace_megasas_scsi_invalid_cdb_len( - mfi_frame_desc[cmd->frame->header.frame_cmd], 1, + mfi_frame_desc[frame_cmd], 1, cmd->frame->header.target_id, cmd->frame->header.lun_id, cmd->frame->header.cdb_len); megasas_write_sense(cmd, SENSE_CODE(INVALID_OPCODE)); @@ -1781,7 +1784,7 @@ static int megasas_handle_io(MegasasState *s, Megasas= Cmd *cmd) cmd->frame->header.lun_id, cdb, cmd); if (!cmd->req) { trace_megasas_scsi_req_alloc_failed( - mfi_frame_desc[cmd->frame->header.frame_cmd], + mfi_frame_desc[frame_cmd], cmd->frame->header.target_id, cmd->frame->header.lun_id); megasas_write_sense(cmd, SENSE_CODE(NO_SENSE)); cmd->frame->header.scsi_status =3D BUSY; @@ -1799,23 +1802,11 @@ static int megasas_handle_io(MegasasState *s, Megas= asCmd *cmd) return MFI_STAT_INVALID_STATUS; } =20 -static int megasas_finish_internal_command(MegasasCmd *cmd, - SCSIRequest *req, size_t resid) -{ - int retval =3D MFI_STAT_INVALID_CMD; - - if (cmd->frame->header.frame_cmd =3D=3D MFI_CMD_DCMD) { - cmd->iov_size -=3D resid; - retval =3D megasas_finish_internal_dcmd(cmd, req); - } - return retval; -} - static QEMUSGList *megasas_get_sg_list(SCSIRequest *req) { MegasasCmd *cmd =3D req->hba_private; =20 - if (cmd->frame->header.frame_cmd =3D=3D MFI_CMD_DCMD) { + if (cmd->dcmd_opcode !=3D -1) { return NULL; } else { return &cmd->qsg; @@ -1829,7 +1820,7 @@ static void megasas_xfer_complete(SCSIRequest *req, u= int32_t len) =20 trace_megasas_io_complete(cmd->index, len); =20 - if (cmd->frame->header.frame_cmd !=3D MFI_CMD_DCMD) { + if (cmd->dcmd_opcode !=3D -1) { scsi_req_continue(req); return; } @@ -1872,7 +1863,7 @@ static void megasas_command_complete(SCSIRequest *req= , uint32_t status, /* * Internal command complete */ - cmd_status =3D megasas_finish_internal_command(cmd, req, resid); + cmd_status =3D megasas_finish_internal_dcmd(cmd, req, resid); if (cmd_status =3D=3D MFI_STAT_INVALID_STATUS) { return; } @@ -1943,6 +1934,7 @@ static void megasas_handle_frame(MegasasState *s, uin= t64_t frame_addr, { uint8_t frame_status =3D MFI_STAT_INVALID_CMD; uint64_t frame_context; + int frame_cmd; MegasasCmd *cmd; =20 /* @@ -1961,7 +1953,8 @@ static void megasas_handle_frame(MegasasState *s, uin= t64_t frame_addr, s->event_count++; return; } - switch (cmd->frame->header.frame_cmd) { + frame_cmd =3D cmd->frame->header.frame_cmd; + switch (frame_cmd) { case MFI_CMD_INIT: frame_status =3D megasas_init_firmware(s, cmd); break; @@ -1972,18 +1965,15 @@ static void megasas_handle_frame(MegasasState *s, u= int64_t frame_addr, frame_status =3D megasas_handle_abort(s, cmd); break; case MFI_CMD_PD_SCSI_IO: - frame_status =3D megasas_handle_scsi(s, cmd, 0); - break; case MFI_CMD_LD_SCSI_IO: - frame_status =3D megasas_handle_scsi(s, cmd, 1); + frame_status =3D megasas_handle_scsi(s, cmd, frame_cmd); break; case MFI_CMD_LD_READ: case MFI_CMD_LD_WRITE: - frame_status =3D megasas_handle_io(s, cmd); + frame_status =3D megasas_handle_io(s, cmd, frame_cmd); break; default: - trace_megasas_unhandled_frame_cmd(cmd->index, - cmd->frame->header.frame_cmd); + trace_megasas_unhandled_frame_cmd(cmd->index, frame_cmd); s->event_count++; break; } --=20 1.8.3.1