[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v3 2/4] xen/console: correct leaky-bucket rate limiter


  • To: Teddy Astie <teddy.astie@xxxxxxxxxx>
  • From: dmukhin@xxxxxxxx
  • Date: Fri, 17 Jul 2026 19:55:13 -0700
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 205.220.161.53) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=ford.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=ford.com; dkim=pass (signature was verified) header.d=saarlouis.ford.com; dkim=pass (signature was verified) header.d=ford.com; arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=24Xsuhi7oZ6eiVhoWkhf6/X8FCPySQ4HnBJ/b6jNeV0=; b=TO0drwQFTcPFNFpnSmpB//r7ZZoSCU2XZcO0V2YaXrkG+ayVknlbM3Bf+3mIeD+1WaH93bb37lN8Cj6YFzGM8QxaP6qgpoGhrc4rg4KfimBc0TkZvApTtaE4btz00Kyr/kx5zKo09rPWl7uHQjn8ZdqLqnCltpPwUKUfsBLnVzXJQeKIn1Cg68TXuSb8mf953jm8PcPmp3/F16HV8UzU4xBye4UDfo8TMgFvh/ipdNbDpbYBmwoEUsWoKhl8gN+xKHeIUyy2f6bw2V3dOS6SMN1eG4OcvH4t4lt0XGjMJjKOb+gGqPxcNUg4+lbgnANn7qqqMwCaxXkNOpYc9PmA/w==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qdLS81o3mUKvw++7fcFRZ41M/MzAHcWo8WjgZIxWJLbxKle595VaM66R6nb2VWNKN0kvcLdtazUA0sJr4yQtKNsetx7HuRjsuFBc4JRkNmJEvfB7WnV+JJ2uIfWtl0hbV2hL5uTL7Y1YPxiJqUr0yeJzdncnQxesb/TQ6tiB4JYcB6ptgoOvOf2P47ytsK1pV6gMyXyTHSgn81kFjeLNyJLCQbj4afJBL0twptKoNbuTq/cO4tVjF5FBzoYVaM95hCqvCBMMye5rntELHHa3ArcyG5P8v3DgUGOeOmNRfSZQLObM0j7GOmy/l8xT1ktEV/ic7za8qbKTHTNtYgzRZQ==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=ppford header.d=ford.com header.i="@ford.com" header.h="Cc:Content-Transfer-Encoding:Content-Type:Date:From:In-Reply-To:Message-ID:MIME-Version:References:Subject:To"; dkim=fail header.s=selector2-azureford-onmicrosoft-com header.d=azureford.onmicrosoft.com header.i="@azureford.onmicrosoft.com"; dkim=pass header.s=ppserprodsaar header.d=saarlouis.ford.com header.i="@saarlouis.ford.com" header.h="Cc:Content-Transfer-Encoding:Content-Type:Date:From:In-Reply-To:Message-ID:MIME-Version:References:Subject:To"; dkim=pass header.s=ppfserpocford header.d=ford.com header.i="@ford.com" header.h="Cc:Content-Transfer-Encoding:Content-Type:Date:From:In-Reply-To:Message-ID:MIME-Version:References:Subject:To"
  • Cc: dmukhin@xxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx, andrew.cooper3@xxxxxxxxxx, anthony.perard@xxxxxxxxxx, jbeulich@xxxxxxxx, julien@xxxxxxx, michal.orzel@xxxxxxx, roger.pau@xxxxxxxxxx, sstabellini@xxxxxxxxxx
  • Delivery-date: Sat, 18 Jul 2026 02:55:26 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Pser-m365-app: SER-APP

On Thu, Jul 16, 2026 at 11:50:48AM +0200, Teddy Astie wrote:
> Le 15/07/2026 à 22:24, dmukhin@xxxxxxxx a écrit :
> > From: Denis Mukhin <dmukhin@xxxxxxxx>
> > 
> > Use existing printk_ratelimit_ms and printk_ratelimit_burst variables in
> > do_printk_ratelimit() instead of hardcoded values 5000 and 10 respectively.
> > 
> > Ensure rate limiter is disabled if either printk_ratelimit_ms or
> > printk_ratelimit_burst is 0. Make sure no unnecessary initialization is done
> > in the corner case.
> > 
> > Also, simplify the limiter code by using min().
> > 
> > Signed-off-by: Denis Mukhin <dmukhin@xxxxxxxx>
> > ---
> > Changes since v2:
> > - fixed typing and 32-bit integer overflow problem
> > ---
> >   xen/drivers/char/console.c | 21 +++++++++++++--------
> >   1 file changed, 13 insertions(+), 8 deletions(-)
> > 
> > diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
> > index 5d395f882e08..de9f2432445d 100644
> > --- a/xen/drivers/char/console.c
> > +++ b/xen/drivers/char/console.c
> > @@ -1274,21 +1274,26 @@ bool __printk_ratelimit(unsigned int ratelimit_ms,
> >                           unsigned int ratelimit_burst)
> >   {
> >       static DEFINE_SPINLOCK(ratelimit_lock);
> > -    static unsigned long toks = 10 * 5 * 1000;
> > -    static unsigned long last_msg;
> > +    static unsigned long long toks, last_msg;
> >       static unsigned int missed;
> > +    unsigned long long now, limit;
> >       unsigned long flags;
> > -    unsigned long long now = NOW(); /* ns */
> > -    unsigned long ms;
> > +    s_time_t ms;
> > -    do_div(now, 1000000);
> > -    ms = (unsigned long)now;
> > +    if ( !ratelimit_burst || !ratelimit_burst )
> 
> Do you intend here ( !ratelimit_ms || !ratelimit_burst ) ?

Whoops, thanks for the catch!

> 
> > +        return true;
> > +
> > +    limit = min(ratelimit_burst * ratelimit_ms, UINT_MAX);
> 
> That looks no-op (at least at first stance), as UINT_MAX is the largest
> unsigned int value; hence `ratelimit_burst * ratelimit_ms` (both unsigned
> int) can't be larger than it; even if it overflows.
> 
> I think we need to cast both ratelimit_ms and ratelimit_burst to unsigned
> long long before doing the multiply, so that the multiply can't overflow
> (not sure exactly how to write it without being too verbose though).
> 
> https://godbolt.org/z/sxWaPM3cr
> 
> > +    if ( !toks )
> > +        toks = limit;
> > +
> > +    now = NOW(); /* ns */
> > +    ms = do_div(now, MILLISECS(1));
> >       spin_lock_irqsave(&ratelimit_lock, flags);
> >       toks += ms - last_msg;
> >       last_msg = ms;
> > -    if ( toks > (ratelimit_burst * ratelimit_ms))
> > -        toks = ratelimit_burst * ratelimit_ms;
> > +    toks = min(toks, limit);
> >       if ( toks >= ratelimit_ms )
> >       {
> >           unsigned int lost = missed;
> 
> With the if part fixed and the adjustments in the limit computation (cast to
> unsigned long long before the multiply) :
> Reviewed-by: Teddy Astie <teddy.astie@xxxxxxxxxx>
> 
> Teddy








 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.