Received: by 2002:a05:7208:3003:b0:81:def:69cd with SMTP id f3csp4236210rba; Tue, 2 Apr 2024 10:58:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXzotErqS3VVa84r2rcwjsh/UDyXM0p/oisnGNt5ylPzld5JcWAYTi1xTw0w7rwHfnTHs2GTeKtDGbyJ029e/ie+wOWX75Qlt8nhWgRwg== X-Google-Smtp-Source: AGHT+IH2dENTbUSqVX+YjmTVmd7J72emSya+Ouk8saKrzYo/D/yWGnpc+VsAAayijPe3WT6jHt2N X-Received: by 2002:ac2:4145:0:b0:516:9f4d:1fc4 with SMTP id c5-20020ac24145000000b005169f4d1fc4mr5832616lfi.49.1712080739551; Tue, 02 Apr 2024 10:58:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712080739; cv=pass; d=google.com; s=arc-20160816; b=a8Co5Cc5iHK/bR+ZLM0Z4zPr7QjofJv4KsE1aXPqQBtTdbak4TosIouxiXJYSiBCvn VrsU8K33/B7F1Rv2zOBKPcyDFf98RplBu02ftaPumT+lg/44aCsW2SUtpd5Q9XylmbN9 MVgjB3Wg6XLXjCkPtYtxQjzE6J/YuVKde8NcsISroMLeefpk+3R1/bZ4ABXeSt8U11Fq mpa/yAolbCJnM6BodOIK4KVuon9hPNRl4XWSa2nb5IaaJYbhNASe5WS3pGQ+TGaIbcR/ 7Gaos+rBWl8O7YpKRRDmcFetIRgCtEYtUkmVKg164jZgtUYMmV4gfFDZGIUDTw1+47sh gA0A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=E+eeJaOo2zbz2WTbCF4Si6uwF5B92wp+mvI31Obgdqw=; fh=Zu3UI2i0fNH0S3PbtDehg3hr8b8t3bmbEqwpjvQJncc=; b=wG8wRVRSqeh9DVsUkbAvEb/kjIgF1/XD6s0y5gFttrlxSZLmQx51YlcIR8G2gn8utw bJSHTYdI6rW5srFU6PW8YGZrJPfCdOEjlO49Ys8zu9nRXP29S+SjwHgsv4xyGTyByLji N+tWmLT5Z9CuivoGKCDGj7K63s3fle8VNlrtoi2Xr5VD+pytAQoetdRGz7tFmdenvNlH jTkX3EiJZPNjnw9vE9DhnFi1w5EcizgdYBkjBuV73xAU3cuq9RsMwp2TjZY5PmNjL4Ll vD6nldGMcoiaU+CFJsk5eJZnpZQs/JT5vTa+PZ+4qbkf+e7IXEGiN+XvWizfZvqEJnuu A+bw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=er8760qY; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-128471-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128471-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id hd10-20020a170907968a00b00a4e8a8092d8si914063ejc.292.2024.04.02.10.58.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 10:58:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-128471-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=er8760qY; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-128471-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128471-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 1575D1F221CB for ; Tue, 2 Apr 2024 17:58:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5B81E15AAD0; Tue, 2 Apr 2024 17:58:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="er8760qY" Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 08B2215AABA; Tue, 2 Apr 2024 17:58:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712080728; cv=none; b=YNCD6oS/KB8esx8wtTailQnHp/TtMasd/bBl/umF+Q0l9mVPdwHrRIG6CSXKtEN+uw6zQxkqRwUNhJOzI6DvB5IuLqjk1riPMQD/NHw6tb5xrhMURVRC+gdJy6cJZtF59r5qXS22ZYaBVj1lXdliwF73NSNf2Zqv3HKBF2wVfNQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712080728; c=relaxed/simple; bh=CckikFIEdka9zckJdLB6X9WogvFT5kc+ADx3ZTqUgSw=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=p/HnpoHWaNRleN0Xz9XpckIvvBTDZz5ClB+WHb8n1qUTslstQ6hsj9h0AgFe8Hvlbr0BsSwZRSHGQv025OLgVYSzQmbrsbsh4F26B2Us81/5LAp0hhNFjQmN7CGnPta6CCl0dhz9bCrsHnUoGZSebBWF6aZkTVq5rG3izm6eNzI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=er8760qY; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1e244c7cbf8so23258395ad.0; Tue, 02 Apr 2024 10:58:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712080726; x=1712685526; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=E+eeJaOo2zbz2WTbCF4Si6uwF5B92wp+mvI31Obgdqw=; b=er8760qYfH0a5PhmgOhiseQXy7e6PZ4NpjCOWvntt0y4YG1ATppHrjIlmHD7K5lKXS 1HHfAyz/Xu2A5+ZP79V/JuAPdzeJRG8l2EzBI+vrOOapSzTU3rj8P3lFjg56Tl7HfcaM 3+cuViVlruX+4UZflTt3nN6wTxPAmfmnTd5YsEu9s58hac7jj+g1iLiwG0qYlqgcthU2 D4FS1MsElurmDuD3M5Mb4noO6bU3V5NXt84uGOx/yruecYQzQ3Wmrmwq5zXmB1WjzF27 ufnR76aQJKoZ+33m6u7bAlMi7lGVRlYyuW80kQlSVV8jZsK30xxfBFEubGdU/H+ksdDF 7tJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712080726; x=1712685526; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=E+eeJaOo2zbz2WTbCF4Si6uwF5B92wp+mvI31Obgdqw=; b=WoyBfM0Q77HRUPIGtLSbRQgG0uK1yCGVcrMwFlcyFhTUH53IxSwB3JeVJzMGEBencW KOccZJKR/pzBKMHGXyrKy0daULX/i4FLTCmoUqbwobmxauafRHQT/iwBWXBmKaj5tGus KwPgSlBbIGYsXh/pmX2q6/LDvMotCQ+JRg/xnypFoaWXpfBR9GdwQIXRtnXrGaktRU3c 8XQHVXE8j6XN34jNvKDqIr5hsiG/jX3fH61SLJyZQsh/0zb5pNHRyX7Sku89CLFCTTIz o1dabC2yW1eJVtoHpY2JyCElbcqpRWBI0/zsUTHARt9lx+X0PMNg8NLaiemzouMy2YYM fkSg== X-Forwarded-Encrypted: i=1; AJvYcCXTjDZCbqUrN3FdudjRaX2RGWei4GVOHXsUQBqqBUc2C7zmmKVK0Tvsafd5B0FxXh4zAhwSl214aidCWO+WlL4+B/O+jqatxhtXOcRZt6Nxpvi3b2FeXO5XFsj2GbXO5u3x X-Gm-Message-State: AOJu0YzW/M3sWR6UxfCYgJ7h0gPByIXgn5pdrb+2yH1gyQyWzoip4kVz wNg47n31yrmFfajaPzu9f1SSQTn4xG0WDRRJsPRFu59412/kO5AK4J1AMtsN98LQDa7zsx5ZBz+ Icodn2LPS3tBoxTipkJHBU48bZak= X-Received: by 2002:a17:902:f606:b0:1e2:88fd:c220 with SMTP id n6-20020a170902f60600b001e288fdc220mr425236plg.44.1712080725946; Tue, 02 Apr 2024 10:58:45 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240401073159.16668-1-andrea.righi@canonical.com> <20240401073159.16668-2-andrea.righi@canonical.com> In-Reply-To: <20240401073159.16668-2-andrea.righi@canonical.com> From: Andrii Nakryiko Date: Tue, 2 Apr 2024 10:58:33 -0700 Message-ID: Subject: Re: [PATCH 1/2] libbpf: ringbuf: allow to consume up to a certain amount of items To: Andrea Righi Cc: Andrii Nakryiko , Eduard Zingerman , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Vernet , Tejun Heo , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Apr 1, 2024 at 12:32=E2=80=AFAM Andrea Righi wrote: > > In some cases, instead of always consuming all items from ring buffers > in a greedy way, we may want to consume up to a certain amount of items, > for example when we need to copy items from the BPF ring buffer to a > limited user buffer. > > This change allows to set an upper limit to the amount of items consumed > from one or more ring buffers. > > Link: https://lore.kernel.org/lkml/20240310154726.734289-1-andrea.righi@c= anonical.com/T > Signed-off-by: Andrea Righi > --- > tools/lib/bpf/ringbuf.c | 29 +++++++++++++++++------------ > 1 file changed, 17 insertions(+), 12 deletions(-) > > diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c > index aacb64278a01..81df535040d1 100644 > --- a/tools/lib/bpf/ringbuf.c > +++ b/tools/lib/bpf/ringbuf.c > @@ -231,7 +231,7 @@ static inline int roundup_len(__u32 len) > return (len + 7) / 8 * 8; > } > > -static int64_t ringbuf_process_ring(struct ring *r) > +static int64_t ringbuf_process_ring(struct ring *r, int64_t max_items) > { > int *len_ptr, len, err; > /* 64-bit to avoid overflow in case of extreme application behavi= or */ > @@ -264,7 +264,14 @@ static int64_t ringbuf_process_ring(struct ring *r) > cons_pos); > return err; > } > - cnt++; > + if (++cnt >=3D max_items) { > + /* update consumer pos and return= the > + * total amount of items consumed= . > + */ > + smp_store_release(r->consumer_pos= , > + cons_pos); Does this fit on a single line under 100 characters? If yes, please keep it as a single line but actually it seems cleaner to keep cnt++ intact, let smp_store_release() below happen, and then check the exit condition. Were you afraid to do unnecessary checks on discarded samples? I wouldn't worry about that. > + goto done; > + } > } > > smp_store_release(r->consumer_pos, cons_pos); > @@ -281,19 +288,18 @@ static int64_t ringbuf_process_ring(struct ring *r) > */ > int ring_buffer__consume(struct ring_buffer *rb) > { > - int64_t err, res =3D 0; > + int64_t err, res =3D 0, max_items =3D INT_MAX; I'm wondering if it might be better to have a convention that zero means "no limit", which might allow the compiler to eliminate the condition in ringbuf_process_ring altogether due to constant propagation. WDYT? > int i; > > for (i =3D 0; i < rb->ring_cnt; i++) { > struct ring *ring =3D rb->rings[i]; > > - err =3D ringbuf_process_ring(ring); > + err =3D ringbuf_process_ring(ring, max_items); > if (err < 0) > return libbpf_err(err); > res +=3D err; > + max_items -=3D err; > } > - if (res > INT_MAX) > - return INT_MAX; > return res; > } [...]