Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp768608pxy; Wed, 28 Apr 2021 13:54:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxoufrG7cniESVfz+jNXLL5hULtsDDM9XglllJiyE0IucoaGXDs1XZO9+TkoTzPQNOmd+z3 X-Received: by 2002:a17:906:74c6:: with SMTP id z6mr13769957ejl.13.1619643269272; Wed, 28 Apr 2021 13:54:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619643269; cv=none; d=google.com; s=arc-20160816; b=USaEwNHNYkNjGNumUr94fmtnH0w4sGk1gAXuQEiTYtjfVOQ9DzNaa/5boaZ+FaLSvr yUiz/4xM0jaAF771xeANDPozDe215zrerF//A1lYJcH5pfaCeuzilcM0woqKOJ3nfgAW yIBXIrq3Gp0BZZITK6Ji2FbiIU0EL/5nHtzrn6ueO5YdhbrBoTxkAVSldt7cf3toEqJ8 uAttAXVCjYo7OQkP4Rf3/mE1QUXRr5D2RHhoQtzspuWqb0Zojo7DRaVed2C3RclBTWG4 r5vPQghZqPhGl+N5she2jJ99d6n02aL64kfPSbt/BnXjT3LIhCLyx6thomVlnLmksAjD BENA== 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=yOBofwvLv9zf8YC0Zy32OgrcWp0knjz3rutihNNwWNI=; b=z+xAYAzQoWLi+MyQw0ahynTtXP+N/jmV23qIGUC6Pgp+XjVo+aCin4JavCquGYtN6h pH1wY4hZ4d3oAXg63aDlei1OD7uWq9SEP5KBTZp8EHKxZMlk1YJmz3tPAFUo+Bzjb+/W g4lpIYOnWa1Jw039N2LiVCwA0iZmgO51kRRw9Id9NSg8cPDkWNmDlUK9Fb0Sx916/BH+ X+M4maDWEGt3AVg9QhQVDjQcNDSEOGXVj+u6D38ZOdYzsrnsMCnRJpULuTcf0VHs/+Pf 9b2SYxfFtFSgIKyrYyalOLD+C6M7si0G10Qvt+hh3DEg2Udl9Dr7sblIjtpqZop+QkAD KWDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=TqSG1JPI; 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 j4si887063edk.420.2021.04.28.13.54.06; Wed, 28 Apr 2021 13:54:29 -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=TqSG1JPI; 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 S243605AbhD1SOc (ORCPT + 99 others); Wed, 28 Apr 2021 14:14:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240111AbhD1SOb (ORCPT ); Wed, 28 Apr 2021 14:14:31 -0400 Received: from mail-yb1-xb36.google.com (mail-yb1-xb36.google.com [IPv6:2607:f8b0:4864:20::b36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AA55C061573; Wed, 28 Apr 2021 11:13:46 -0700 (PDT) Received: by mail-yb1-xb36.google.com with SMTP id p202so31433126ybg.8; Wed, 28 Apr 2021 11:13:46 -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=yOBofwvLv9zf8YC0Zy32OgrcWp0knjz3rutihNNwWNI=; b=TqSG1JPI7Ay2Z9T/58cWhm29Dop0mBA8wysl0/1OHbrmkteoHRXE2k19L9LqhKSAUZ FDzpgoKg6VrmM5iq3Ltk7JAnUhHXLXvm+NNHShmTtWqTRsIivEm9ltss2eZRu/5MR/RR R9Ckt+GEyEmmafmTzfU3IKcITK5ZFazRMpS/7dWgmm4vbJOH+Af1nAJnQMdAw+CYXsK1 ZDuheRVpNwMHJAZbZGC6HlwwJh5pw+n6PrFz9dftLJewuyTknVk1MaUGwSxLQ2r0Nrws HFPqzHKzBUErEdb36sAr2Af5QYCIzOn4xruJyVNf6NEh3ScSgh6ogM5TvTftYafuk2ZA AwAg== 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=yOBofwvLv9zf8YC0Zy32OgrcWp0knjz3rutihNNwWNI=; b=Z+lPCnN/Y1dK+HhLsMvqpWh24gNJIgX7jx8KCSfQa6sIeVOLCUr6ArX+8hwByndav0 b9Ucbed+2lFVTxrRie2jwoT8zG0/yLl/QgzW3y4Ynx5dyZ7QLkB/mmIqqoV6KgqnsX/Y chQokrVRs+R/gjxpLlhKUb9F7YsREuW2y23r8hW+YCNOV907LFNnpfwQsOjVino9qlJo Ty4pUIXAeuSpDZueydmCnO6hrLxWhGwY6KihZCdbBDMaCIs15Ut9/YiKtsdTrKP3cPzZ +1g1E1BnpcvXJRgazfMMlw9zAAOeQEyEAyciPqFCTb1dMTZHfaDz6E6cwjorN7pyLeX4 1yHA== X-Gm-Message-State: AOAM533YvL4mMgongQHt5E5Cjro4AuZxzwDy+BDkGhHK7IkyvNwQQ8t6 MDLJ7oZJNRQm3qY3cwRfodYikjyrE80XO7sOlxU6aAzuNf0= X-Received: by 2002:a25:c4c5:: with SMTP id u188mr42036749ybf.425.1619633625302; Wed, 28 Apr 2021 11:13:45 -0700 (PDT) MIME-Version: 1.0 References: <20210427170859.579924-1-jackmanb@google.com> In-Reply-To: From: Andrii Nakryiko Date: Wed, 28 Apr 2021 11:13:34 -0700 Message-ID: Subject: Re: [PATCH bpf-next] libbpf: Fix signed overflow in ringbuf_process_ring To: Brendan Jackman Cc: KP Singh , bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 28, 2021 at 1:18 AM Brendan Jackman wrote: > > On Wed, 28 Apr 2021 at 01:19, Andrii Nakryiko wrote: > > > > On Tue, Apr 27, 2021 at 4:05 PM KP Singh wrote: > > > > > > On Tue, Apr 27, 2021 at 11:34 PM Andrii Nakryiko > > > wrote: > > > > > > > > On Tue, Apr 27, 2021 at 10:09 AM Brendan Jackman wrote: > > > > > > > > > > One of our benchmarks running in (Google-internal) CI pushes data > > > > > through the ringbuf faster 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 just setting a limit on the number of entries that can be > > > > > consumed. > > > > > > > > > > Fixes: bf99c936f947 (libbpf: Add BPF ring buffer support) > > > > > Signed-off-by: Brendan Jackman > > > > > --- > > > > > tools/lib/bpf/ringbuf.c | 3 ++- > > > > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > > > > > > > diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c > > > > > index e7a8d847161f..445a21df0934 100644 > > > > > --- a/tools/lib/bpf/ringbuf.c > > > > > +++ b/tools/lib/bpf/ringbuf.c > > > > > @@ -213,8 +213,8 @@ static int ringbuf_process_ring(struct ring* r) > > > > > do { > > > > > got_new_data = false; > > > > > prod_pos = smp_load_acquire(r->producer_pos); > > > > > - while (cons_pos < prod_pos) { > > > > > + /* Don't read more than INT_MAX, or the return vale won't make sense. */ > > > > > + while (cons_pos < prod_pos && cnt < INT_MAX) { > > > > > > > > ring_buffer__pool() is assumed to not return until all the enqueued > > > > messages are consumed. That's the requirement for the "adaptive" > > > > notification scheme to work properly. So this will break that and > > > > cause the next ring_buffer__pool() to never wake up. > > Ah yes, good point, thanks. > > > > > We could use __u64 internally and then cap it to INT_MAX on return > > > > maybe? But honestly, this sounds like an artificial corner case, if > > > > you are producing data faster than you can consume it and it goes > > > > beyond INT_MAX, something is seriously broken in your application and > > Yes it's certainly artificial but IMO it's still highly desirable for > libbpf to hold up its side of the bargain even when the application is > behaving very strangely like this. One can also argue that if application consumed more than 2 billion messages in one go, that's an error. ;-P But of course that is not great. > > [...] > > > I think we have two alternatives here: > > 1) consume all but cap return to INT_MAX > > 2) consume all but return long long as return result > > > > Third alternative is to have another API with maximum number of > > samples to consume. But then user needs to know what they are doing > > (e.g., they do FORCE on BPF side, or they do their own epoll_wait, or > > they do ring_buffer__poll with timeout = 0, etc). > > > > I'm just not sure anyone would want to understand all the > > implications. And it's easy to miss those implications. So maybe let's > > do long long (or __s64) return type instead? > > Wouldn't changing the API to 64 bit return type break existing users > on some ABIs? > Yes, it might, not perfect. > I think capping the return value to INT_MAX and adding a note to the > function definition comment would also be fine, it doesn't feel like a > very complex thing for the user to understand: "Returns number of > records consumed (or INT_MAX, whichever is less)". Yep, let's cap. But to not penalize a hot loop with extra checks. Let's use int64_t internally for counting and only cap it before the return.