Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp580494lql; Mon, 11 Mar 2024 10:56:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVScQBDBSr1RQUVdDloc7UKP54wPq5xwl0ZXvTK1Z3K8DIgabTdHbr8l5/hUIcPQwc2EMtJjH5jSb1jjBBIfv4W6rYVhyTH5xvfnBqO6Q== X-Google-Smtp-Source: AGHT+IH67Txy8tqhny8dGp7VI+1EmG2hX7mWIh1dxZPPcj8tTkXhGjYMEF4s2N5Lo28XENtkZmA/ X-Received: by 2002:a0c:fc41:0:b0:690:7a4f:d670 with SMTP id w1-20020a0cfc41000000b006907a4fd670mr8710851qvp.8.1710179801484; Mon, 11 Mar 2024 10:56:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710179801; cv=pass; d=google.com; s=arc-20160816; b=amctn0jZqN8DzvQMry6rOvS4JEupc8/nwL3vI3UI+hOpNm+WIqtv5eK9YlNKxGey5/ soZKXwSDUENICmmtKAEdAM22AmW01gQgBf2ICIl6nAKFQjx8faF+/ajzBXqQVaZOlGHB VdOZBX+AHf04kvuPbNA5PH6kF4zR38gbplDYSfksqYl1ku99QjtV3ozthnntqqu8ZFUw TagnWtjKRhKbdl0yHm8FUi0A+QTTn1Bdkk/7+nql3Z16aRdkrZ1AUav0i6SweH6ox0ML wNRyMTXSTBGEBKGbn07ArFCzK7BSPtxgXtM6GpGT8fGZGj2QRdHsKFTq8m/bm9cY0Ugs exGg== 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=yA5M5kpASFinQzBucQIe6R0OuQDkEP28n5sfVDn4lPc=; fh=ifz9u3Lwww9XCZ0VBZ/R8AYkt1tv9eyVDIdf+APVCAQ=; b=umeRiGZTkEhJ8SGEIzRKNCd+1rcWVny2FL6UeqsDG3KFo6NAzHw4xzkReBwQxx705Z /JYsbG5NdDGko/5+GIs5lA03aossZqSTHjCLL2q47c2DCeFM8RUZLcTbfdf9R+7F5TON CSZHb7PdxqAiPIU0RWs7lLoLiJcUZ+ufGHDfFc927AI98FBqCW4KwmG/80UQ5RmdoOEY vgPPCrbBj7JshXSU8TofXDOGvsRdvWaV5XUodUVaUKZLqw1URL+iVsD06muKLtT7SNQq xTyxwWgqN2SYBgt5cc5J+nmqBFFSWk78h3WLEWO/VH3FtD2Df3OJV78EbGdhs3DG8INC 7tkA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=N1JQeV7b; 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-99334-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99334-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id gu5-20020a056214260500b0069019f40c93si6294356qvb.22.2024.03.11.10.56.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 10:56:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99334-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=N1JQeV7b; 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-99334-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99334-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id C8E881C21859 for ; Mon, 11 Mar 2024 17:56:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 94B4E535C6; Mon, 11 Mar 2024 17:56:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="N1JQeV7b" Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (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 6F4334C631; Mon, 11 Mar 2024 17:56:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710179771; cv=none; b=bv0KeSgCCJn+FiuRRv4NsBK5uf2SMI2IX/flaXGw4l/HO8ZqPUEkAJKjGGTwTWVBUDGLo5hGDOjobXvVUQIQlUKOco6ER2v9+oWQuAoMTFgqkPyTSfXOVkEH0h8Pe04XYSFhgfWomZX4HaxAILGY7RIbOafb+5o8o89em95txL8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710179771; c=relaxed/simple; bh=2sHTM97y6Ae9lduC17X4CVIj9RmYErbw28IMnyPzK8c=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=YYDf2Xz2FaQnBtqXXDNONAr3odqzh/gs5TVud+BEGKaAXK1oyw6mjBxHzfSZO56GywcaXMSceGhqW5rAYsVuugA18DVfIQQ67b5g9wyYCWHK0r+EYKW7tY3fD2WNpmZhRyn2uKGZ0LhflbcoNdZdzjszChEJ1HB9xVe2RHufNuo= 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=N1JQeV7b; arc=none smtp.client-ip=209.85.215.180 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-pg1-f180.google.com with SMTP id 41be03b00d2f7-5e4613f2b56so3042826a12.1; Mon, 11 Mar 2024 10:56:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710179770; x=1710784570; 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=yA5M5kpASFinQzBucQIe6R0OuQDkEP28n5sfVDn4lPc=; b=N1JQeV7buFwUk5r48st+Ef1KwXH8aIbNNv0Un9zEV4zCtewyMMMEYWMdFQXl7b5rNU TymjHNpn5nAeIWhH51Qj62Px1dr/h22Ozy8kJXRtMszXe9s4w6L1k2O+wsIhQm4STNkT qCeS17FQW0f8HhqVyfm00zKHnvKCgLGXYdrHviO1sY2IV33iQYTqiwYRTO5nK1pliX8I A/hJF8N53Aq9y4u5+1E7YIe0r8Q9+QXcDhjsk8KC8ujVsPZaG3LD6dvCVq6J8wAARMOT Ox5WW0MDb9SzuljqJESN50FSHcXnfIWn8kVhpU4jLev43THqHw2Unr6MWiUTLeFIYnqk EfUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710179770; x=1710784570; 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=yA5M5kpASFinQzBucQIe6R0OuQDkEP28n5sfVDn4lPc=; b=YrFxn6nIc/G59mH3ch/eAfG4nYMzrCQ5GXqDh9tM/mphGTAVqdNTORl68hRPFvfQeJ jVkCaIeaZSlU+FT5O6o7yZZDL/jsUkdWgF8yJbBCtELfshvb3SKGGTzTuDO1UF0BBPmZ b93uLNnPUukqaY3iAM7gvkoCDdoeHW7whYYcfSFdSyFaR7v1gZlPKxfY6sLy7cOWmu1U XA+uVtszxGNb2+zvdcuc6nTu/fNr0teQ49QS+Vow+D2CnNg+d2AjV62DinWCP2vGFiXM yOlKA0FOiji/BqeEL4p5lQs4lQAGqo+AJ1O7GyezpvUecLrM2G7jJnFfM4HagBxvETyT Wl+w== X-Forwarded-Encrypted: i=1; AJvYcCXCBYC9KZHqZdFvmORL9D1cuTxlrIELBPkDH1xY0WxQXICTfijI2Bbiy+hFQeF2HuN6X2/LE74pRlswtqT5kCTRzvhugUwAiwTApHhtEFmavIPpWq7PzhCpKcS6PhVpvID+ X-Gm-Message-State: AOJu0Yxk25N+y6SmuO7BIDf5SF26JiaOMI9M48Fa6g80QKDfEt2gl/Ut bnkSmo76WJKBIKoY6rtEiqMfNZo6cgYLRRCmPHhwec4Nho6yeHZQeHN1nzxOE7x7bN3OF+uUafH 9HHUGji721/Cl5qEGqC6mzyRJk6Q= X-Received: by 2002:a17:90b:4007:b0:29c:e1b:f9e2 with SMTP id ie7-20020a17090b400700b0029c0e1bf9e2mr2261232pjb.25.1710179769736; Mon, 11 Mar 2024 10:56:09 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240310154726.734289-1-andrea.righi@canonical.com> In-Reply-To: <20240310154726.734289-1-andrea.righi@canonical.com> From: Andrii Nakryiko Date: Mon, 11 Mar 2024 10:55:57 -0700 Message-ID: Subject: Re: [PATCH] libbpf: ringbuf: allow to partially consume 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 Sun, Mar 10, 2024 at 8:47=E2=80=AFAM Andrea Righi wrote: > > Instead of always consuming all items from a ring buffer in a greedy > way, allow to stop when the callback returns a value > 0. > > This allows to distinguish between an error condition and an intentional > stop condition by returning a non-negative non-zero value from the ring > buffer callback. > > This can be useful, for example, to consume just a single item from the > ring buffer. > > Signed-off-by: Andrea Righi > --- > tools/lib/bpf/ringbuf.c | 8 ++++++++ > 1 file changed, 8 insertions(+) > > diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c > index aacb64278a01..dd8908eb3204 100644 > --- a/tools/lib/bpf/ringbuf.c > +++ b/tools/lib/bpf/ringbuf.c > @@ -265,6 +265,14 @@ static int64_t ringbuf_process_ring(struct ring *r) > return err; > } > cnt++; > + if (err > 0) { So libbpf already stops at any err < 0 (and sets correct consumer pos). So you could already get desired behavior by just returning your own error code. If you need count, you'd have to count it yourself through custom context, that's a bit of inconvenience. But on the other hand, currently if user callback returns anything > 0 they keep going and that return value is ignored. Your change will break any such user pretty badly. So I'm a bit hesitant to do this. Is there any reason you can't just return error code (libbpf doesn't do anything with it, just passes it back, so it might as well be `-cnt`, if you need that). pw-bot: cr > + /* update consumer pos and return= the > + * total amount of items consumed= . > + */ > + smp_store_release(r->consumer_pos= , > + cons_pos); > + goto done; > + } > } > > smp_store_release(r->consumer_pos, cons_pos); > -- > 2.43.0 > >