Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp491878pxy; Fri, 30 Apr 2021 09:34:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysIFgdkvlQKrl31OilYW9nAObTHnzsreIZ4CJPzMt/7YTMws88wGwR3sqYg41mA7VQzCHR X-Received: by 2002:a63:ff22:: with SMTP id k34mr5352009pgi.336.1619800444072; Fri, 30 Apr 2021 09:34:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619800444; cv=none; d=google.com; s=arc-20160816; b=vXUdhnShAgzxSjvUMzazev+ugsEhqNDr1xEZjJcK33nJn5h4NF8fSrG7cLnt1IyGyJ Y+cu162zro98MKIeEqzC046h/B1+EY+k/7fFo2rHgaS0GYM1dCqIitz5O3iklpXIZ1JS uugtGIrS6B4SqPuCI0CwdyfecYyWCOcYlw1PnVl55f50SBEW785ZqBXjtKYioRGl+6j6 ENzrTUfq47TNpxSBiXoq1JIN4AUsLogSQYV0/N0AOD4WqCp32EXBgek6CbKT+xeU7Tae xEWjHvsuigKT5thVuQp+cGGjQ2JbWiWBlZtSSciPIBBXj7zwa7RzUmhhlp2kRaYRkX0k Zkig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=b2mLV6on/AScC+J+olqrHsUjmUN/LKY8yJTmwsZh9o8=; b=SVeuoTTjZi1zxDgofor72NJUElazZESOF+CkQajsS1ItTh7+hs2fApXl7sj1VUd+5X zUhhhuWHUUI4ppW0iFQBpFblNYdw1dl0YB+pDj+8JyehayIDfwVokFSbMfKol3VfnCvU h9+n/jCt5/oycJqy2aTo7NZHqSS47/Rjk8LUHWJK+Uead2HYPNzvM0eiHjDeRU7nv+rw iBferpPSUXr+be8MRT8BygiXtyMX4Teue4wyaREZ6IT+fHqSF0DoJt7mo5TwxpdTrDsO Xxq0cf8r1OheIK1MwcVHHUv+6oM4UVuzjn/arVWqujjbb37+X51qW3UHPRhf7jOJIAzC JEhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Ews3etYb; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o34si4931789pgm.247.2021.04.30.09.33.51; Fri, 30 Apr 2021 09:34:04 -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=@gmail.com header.s=20161025 header.b=Ews3etYb; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230196AbhD3Qch (ORCPT + 99 others); Fri, 30 Apr 2021 12:32:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229720AbhD3Qcg (ORCPT ); Fri, 30 Apr 2021 12:32:36 -0400 Received: from mail-yb1-xb35.google.com (mail-yb1-xb35.google.com [IPv6:2607:f8b0:4864:20::b35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E920FC06174A; Fri, 30 Apr 2021 09:31:47 -0700 (PDT) Received: by mail-yb1-xb35.google.com with SMTP id x131so2835783ybg.11; Fri, 30 Apr 2021 09:31:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=b2mLV6on/AScC+J+olqrHsUjmUN/LKY8yJTmwsZh9o8=; b=Ews3etYbRH1SAXxMpCQ3HzT8GqoLm0S6KlnyPkm8GDmGNNs21zoeXWeQB7kVRWCZ78 asA/wpbZBsb96S8dSuoYFtxvNPGHh42qBysRhMcEGCkpk3buxXUi8dCjr3j63c6AVxKc a1quoTAEYQreh7fJuEzUczoZbR4qafpmQTa6bcrGALxjCBsbFYyKQBhx/JYKyAxH5zxl l7PRFyl55cDFErDvY5HKP5Z5KdKPp+VFjHDBZQjgDVUjbLhdELiavNRlJt+GKnVD+hr2 iUX3dZxEMdpmfyHxFrXte61dXoVkPbiKqqCuaIW3LjYQ83+rM4waTff/rm7YBAK0aGxh C0LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=b2mLV6on/AScC+J+olqrHsUjmUN/LKY8yJTmwsZh9o8=; b=B7UXY8mR1G/+lyqZaCTZeSJJTaHxPAW5XcjtLIBiduRn5xP77vPx2CLMINo7e5qmQQ +fPMCTCKmhRIqZH7rBIt7fS2ktKvU+Js8der2TchSO0xXgHlqqsW57U183sYkthcu4eX efN8piGGCNEimyHYwQwOpKeap2iR0hQfUURGeP/jeJBS9gXA/6yUo+Atg1i+WTUOi/hk QpBh6q2L08DcL2eviwRQ7fR9kJag4Iwh2mt9KI1qP/QUC1wYxyKBH+gTmsuHlwVmkQmt eB4q0ql5LhVaRl/V2WA+9WME4kHc3Tc3A91jvfG3cWKJm7af7QfmSGKhAMADKOTTnTmq /DMQ== X-Gm-Message-State: AOAM530X9VsrLMVQQBlWZMwCRy2wGJsUHOhm/nZtLp18+bWV4dApr2Qv xK2akNBQTGAT8XHOOPQV0zgi0uKYXahtfga2MKI= X-Received: by 2002:a25:7507:: with SMTP id q7mr8303198ybc.27.1619800307186; Fri, 30 Apr 2021 09:31:47 -0700 (PDT) MIME-Version: 1.0 References: <20210429130510.1621665-1-jackmanb@google.com> In-Reply-To: <20210429130510.1621665-1-jackmanb@google.com> From: Andrii Nakryiko Date: Fri, 30 Apr 2021 09:31:36 -0700 Message-ID: Subject: Re: [PATCH v2 bpf-next] libbpf: Fix signed overflow in ringbuf_process_ring To: Brendan Jackman Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , open list , KP Singh , Florent Revest Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 29, 2021 at 6:05 AM Brendan Jackman wrote: > > One of our benchmarks running in (Google-internal) CI pushes data > through the ringbuf faster htan than userspace is able to consume > it. In this case it seems we're actually able to get >INT_MAX entries > in a single ringbuf_buffer__consume call. ASAN detected that cnt > overflows in this case. > > Fix by using 64-bit counter internally and then capping the result to > INT_MAX before converting to the int return type. > > Fixes: bf99c936f947 (libbpf: Add BPF ring buffer support) > Signed-off-by: Brendan Jackman > --- > > diff v1->v2: Now we don't break the loop at INT_MAX, we just cap the reported > entry count. > > Note: I feel a bit guilty about the fact that this makes the reader > think about implicit conversions. Nobody likes thinking about that. > > But explicit casts don't really help with clarity: > > return (int)min(cnt, (int64_t)INT_MAX); // ugh > I'd go with if (cnt > INT_MAX) return INT_MAX; return cnt; If you don't mind, I can patch it up while applying? > shrug.. > > tools/lib/bpf/ringbuf.c | 10 ++++++---- > 1 file changed, 6 insertions(+), 4 deletions(-) > > diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c > index e7a8d847161f..2e114c2d0047 100644 > --- a/tools/lib/bpf/ringbuf.c > +++ b/tools/lib/bpf/ringbuf.c > @@ -204,7 +204,9 @@ static inline int roundup_len(__u32 len) > > static int ringbuf_process_ring(struct ring* r) > { > - int *len_ptr, len, err, cnt = 0; > + int *len_ptr, len, err; > + /* 64-bit to avoid overflow in case of extreme application behavior */ > + int64_t cnt = 0; > unsigned long cons_pos, prod_pos; > bool got_new_data; > void *sample; > @@ -240,7 +242,7 @@ static int ringbuf_process_ring(struct ring* r) > } > } while (got_new_data); > done: > - return cnt; > + return min(cnt, INT_MAX); > } > > /* Consume available ring buffer(s) data without event polling. > @@ -263,8 +265,8 @@ int ring_buffer__consume(struct ring_buffer *rb) > } > > /* Poll for available data and consume records, if any are available. > - * Returns number of records consumed, or negative number, if any of the > - * registered callbacks returned error. > + * Returns number of records consumed (or INT_MAX, whichever is less), or > + * negative number, if any of the registered callbacks returned error. > */ > int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms) > { > -- > 2.31.1.498.g6c1eba8ee3d-goog >