VirtualBox

Changeset 83569 in vbox for trunk/src


Ignore:
Timestamp:
Apr 5, 2020 9:23:26 PM (5 years ago)
Author:
vboxsync
Message:

DevVirtioSCSI.cpp: Eliminated two small memory allocations in virtioScsiR3IoReqFinish. bugref:9440

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Storage/DevVirtioSCSI.cpp

    r83568 r83569  
    975975
    976976        /* req datain bytes already in guest phys mem. via virtioScsiIoReqCopyFromBuf() */
    977         /** @todo r=bird: There is too much allocating here and we'll leak stuff if
    978          *        we're low on memory and one of the RTMemAllocZ calls fail! */
    979 
    980         PRTSGBUF pReqSegBuf = (PRTSGBUF)RTMemAllocZ(sizeof(RTSGBUF));
    981         AssertReturn(pReqSegBuf, VERR_NO_MEMORY);
    982 
    983         PRTSGSEG paReqSegs  = (PRTSGSEG)RTMemAllocZ(sizeof(RTSGSEG) * 2);
    984         AssertReturn(paReqSegs, VERR_NO_MEMORY);
    985 
    986         int cSegs = 0;
    987         paReqSegs[cSegs].pvSeg = &respHdr;
    988         paReqSegs[cSegs++].cbSeg = sizeof(respHdr);
    989 
    990         paReqSegs[cSegs].pvSeg = pReq->pbSense;
    991         paReqSegs[cSegs++].cbSeg = pReq->cbSenseAlloc; /* VirtIO 1.0 spec 5.6.4/5.6.6.1 */
    992 
    993         /* Copy segment data to malloc'd memory to avoid stack out-of-scope errors sanitizer doesn't detect */
    994         /** @todo r=bird: The above comment makes zero sense as the memory is freed
    995          *        before we return, so there cannot be any trouble with out-of-scope
    996          *        stuff here. */
    997         for (int i = 0; i < cSegs; i++)
    998         {
    999             void *pv = paReqSegs[i].pvSeg;
    1000             paReqSegs[i].pvSeg = RTMemDup(pv, paReqSegs[i].cbSeg);
    1001             AssertReturn(paReqSegs[i].pvSeg, VERR_NO_MEMORY);
    1002         }
    1003 
    1004         RTSgBufInit(pReqSegBuf, paReqSegs, cSegs);
    1005 
    1006         size_t cbReqSgBuf = RTSgBufCalcTotalLength(pReqSegBuf);
     977        RTSGSEG aReqSegs[2];
     978
     979        paReqSegs[0].pvSeg = &respHdr;
     980        paReqSegs[0].cbSeg = sizeof(respHdr);
     981
     982        paReqSegs[1].pvSeg = pReq->pbSense;
     983        paReqSegs[1].cbSeg = pReq->cbSenseAlloc; /* VirtIO 1.0 spec 5.6.4/5.6.6.1 */
     984
     985        RTSGBUF ReqSgBuf
     986        RTSgBufInit(&ReqSgBuf, aReqSegs, RT_ELEMENTS(aRegSegs));
     987
     988        size_t cbReqSgBuf = RTSgBufCalcTotalLength(&ReqSgBuf);
     989        /** @todo r=bird: Returning here looks a little bogus... */
    1007990        AssertMsgReturn(cbReqSgBuf <= pReq->pDescChain->cbPhysReturn,
    1008                        ("Guest expected less req data (space needed: %d, avail: %d)\n",
    1009                          cbReqSgBuf, pReq->pDescChain->cbPhysReturn),
    1010                        VERR_BUFFER_OVERFLOW);
    1011 
    1012         virtioCoreR3QueuePut(pDevIns, &pThis->Virtio, pReq->qIdx, pReqSegBuf, pReq->pDescChain, true /* fFence TBD */);
     991                       ("Guest expected less req data (space needed: %zu, avail: %u)\n",
     992                        cbReqSgBuf, pReq->pDescChain->cbPhysReturn),
     993                        VERR_BUFFER_OVERFLOW);
     994
     995        virtioCoreR3QueuePut(pDevIns, &pThis->Virtio, pReq->qIdx, &ReqSgBuf, pReq->pDescChain, true /* fFence TBD */);
    1013996        virtioCoreQueueSync(pDevIns, &pThis->Virtio, pReq->qIdx);
    1014 
    1015         for (int i = 0; i < cSegs; i++)
    1016             RTMemFree(paReqSegs[i].pvSeg);
    1017 
    1018         RTMemFree(paReqSegs);
    1019         RTMemFree(pReqSegBuf);
    1020997
    1021998        Log2(("-----------------------------------------------------------------------------------------\n"));
     
    10271004        PDMDevHlpAsyncNotificationCompleted(pDevIns);
    10281005
    1029     return VINF_SUCCESS;
     1006    return rc;
    10301007}
    10311008
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette