Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4924268pxy; Tue, 27 Apr 2021 16:08:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3zmN7LelmJ4oMHkn5DjzYJtakqVm0L8xsEY/EXWIt8Rt6MmZDlXAfonuZV7wUdOnsHRi1 X-Received: by 2002:a65:6a52:: with SMTP id o18mr4820889pgu.17.1619564925429; Tue, 27 Apr 2021 16:08:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619564925; cv=none; d=google.com; s=arc-20160816; b=alpXD65u7+5bH/fozrc0bGC8WuxdYFANz1fVlSiLyWA+iQGUQehse8kWW1nfyDIWcQ yAVmb8tjHWNJoV3HcPKtZGrGR8gYNK9Yryihi0i9LSTqRGWvPhHrCsSWWLOCKMni55xa KZmm0csTZZ1pUaUuX44HxYNOt/3dXxLjcorvnrnjquFBvwbFrq5s7k2GgwCt6KRl9l0M 9oG51urNuT5NDau8XXlCPaMcqkjQ5JghXy2H73Nj4X3iYHOiTz437UEIUo225wP4xpOe D6035S2wg1l8SB/TV1HGLAvMayt3UvVw7FIukk/+nK1O/xRE+VleaBtzV4OQewenTJhT 3nrw== 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=u7UIKcknZgdSq13jCV5rhZAWN1XNlhdgTK1yNKv6YG0=; b=tPSd15kWzpxPUayYtHF1yJyUf+yvoPtgsmvcPo/eKIzxyKQ2F2cJLR9yjWCQ/KIpcZ DdB7MRqSo1SHgYi3HpxQaycUc+9FSi+OYqJiRL9u7krxT8ljXi3qozlV+EIKHR9ywLVr C1VOpJNG0LhlATfaedTuXgqc/L9URndS52jlPLlPUZylTQtKDGQsF/R868DdZGF6uwSH Z46B01V9J1Qrt7wMg2jFLj/TTMc7ithYV+D9HPWHt36e+JiHH1SPYhrjxYP2r2C6r2lp Go/jW3yfdQo6KAjkjslkQpJpo+uEJvtvOSZ38BDWX3mU+gT5uE9PMwjw8blnIOVcuNxN 0n1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=F9g7ogeu; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 8si4777238pfp.229.2021.04.27.16.08.31; Tue, 27 Apr 2021 16:08:45 -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=@kernel.org header.s=k20201202 header.b=F9g7ogeu; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238340AbhD0XGm (ORCPT + 99 others); Tue, 27 Apr 2021 19:06:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:38984 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237670AbhD0XGm (ORCPT ); Tue, 27 Apr 2021 19:06:42 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 56B9F613FB for ; Tue, 27 Apr 2021 23:05:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1619564758; bh=bFq91+wLKJQRSGqSZUDMiKbKQv489DmBnnoPHMaipC0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=F9g7ogeuaeGuK8E6M4wsY/KsStsmoCvnPTUbJxNEnBWJ/hbepbbxwg7rklNeKZay5 y69N2IeNEHdmcvMQuWNRzVInKLsg9eUVyD5uyfvRTuoSbu5707hOsIPY+LvtfPlAJK 0+GJuQCk+K+GJcKcpklvO/0tUkhfDQYrcSKYi/cOiUg+nwtOKTHbFkRLDmAuoQe5ZC XOdQkNdztiqwenZI/2yMVqKq6qlKcI407Qmk55uJj9/vDZRhrVbKchXXWWP4NX7B/J 2H/hFN/ywykL9UE3y7Lbl9CtRTXL9YM+tiigSU266+hhpz6Y/5bk6egP5CLm+MCRC1 2hiA4c6pFprnA== Received: by mail-lf1-f49.google.com with SMTP id x19so65792592lfa.2 for ; Tue, 27 Apr 2021 16:05:58 -0700 (PDT) X-Gm-Message-State: AOAM532Nyx9vAQj91fUbvEn4O99EwJlGhdiSJwLx9vZB2iaYmDK7I42C 1BToHQuEgqZU06R8RXtbJtKQ8CNgS7XUvzGC/fNWGA== X-Received: by 2002:a05:6512:21c2:: with SMTP id d2mr17768323lft.424.1619564756699; Tue, 27 Apr 2021 16:05:56 -0700 (PDT) MIME-Version: 1.0 References: <20210427170859.579924-1-jackmanb@google.com> In-Reply-To: From: KP Singh Date: Wed, 28 Apr 2021 01:05:45 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH bpf-next] libbpf: Fix signed overflow in ringbuf_process_ring To: Andrii Nakryiko Cc: Brendan Jackman , 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 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. > > 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 Disclaimer: I don't know what Brendan's benchmark is actually doing That said, I have seen similar boundaries being reached when doing process monitoring and then a kernel gets compiled (esp. with ccache) and generates a large amount of process events in a very short span of time. Another example is when someone runs a short process in a tight while loop. I agree it's a matter of tuning, but since these corner cases can be easily triggered even on real (non CI) systems no matter how much one tunes, I wouldn't really call it artificial :) - KP > you have more important things to handle :) > > > len_ptr = r->data + (cons_pos & r->mask); > > len = smp_load_acquire(len_ptr); > > > > -- > > 2.31.1.498.g6c1eba8ee3d-goog > >