VirtualBox

Changeset 93632 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Feb 7, 2022 1:15:44 AM (3 years ago)
Author:
vboxsync
Message:

VMM/PDMNetShaper: Tweaked the cTokensAdded and cTokens calculations in PDMNetShaperAllocateBandwidth to avoid overflow problems. bugref:5582

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/VMMAll/PDMAllNetShaper.cpp

    r93631 r93632  
    2727#include <VBox/log.h>
    2828#include <iprt/time.h>
     29#include <iprt/asm-math.h>
    2930
    3031
     
    5960                    /*
    6061                     * Re-fill the bucket first
     62                     *
     63                     * Note! We limit the cTokensAdded calculation to 1 second, since it's really
     64                     *       pointless to calculate much beyond PDM_NETSHAPER_MAX_LATENCY (100ms)
     65                     *       let alone 1 sec.  This makes it possible to use ASMMultU64ByU32DivByU32
     66                     *       as the cNsDelta is less than 30 bits wide now, which means we don't get
     67                     *       into overflow issues when multiplying two 64-bit values.
    6168                     */
    62                     uint64_t const nsNow    = RTTimeSystemNanoTS();
    63                     uint64_t const cNsDelta = nsNow - pGroup->tsUpdatedLast;
    64                     /** @todo r=bird: there might be an overflow issue here if the gap
    65                      *                between two transfers is too large. */
    66                     uint32_t cTokensAdded   = cNsDelta * cbPerSecMax / RT_NS_1SEC;
    67 
     69                    uint64_t const nsNow        = RTTimeSystemNanoTS();
     70                    uint64_t const cNsDelta     = nsNow - pGroup->tsUpdatedLast;
     71                    uint64_t const cTokensAdded = cNsDelta < RT_NS_1SEC
     72                                                ? ASMMultU64ByU32DivByU32(cbPerSecMax, (uint32_t)cNsDelta, RT_NS_1SEC)
     73                                                : cbPerSecMax;
    6874                    uint32_t const cbBucket     = pGroup->cbBucket;
    6975                    uint32_t const cbTokensLast = pGroup->cbTokensLast;
    70                     uint32_t const cTokens      = RT_MIN(cbBucket, cTokensAdded + cbTokensLast);
     76                    uint32_t const cTokens      = (uint32_t)RT_MIN(cbBucket, cTokensAdded + cbTokensLast);
    7177
    7278                    /*
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