Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1375614lql; Tue, 12 Mar 2024 15:43:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVedMXKHBbgAMkrDqzsRjp1UPuRLZQbgagy/HGVMDGeinREnvz5DecVYlX/HANkhDna93BnizdGg9C5+andiahWSicTNok+l0QAoYGj+A== X-Google-Smtp-Source: AGHT+IH5VbXthvszAGFnJ4T+uRs6K/0ZGGYiZcWAZffVFXNQ4a2zDZwgvFJRhx7uu3m+mG+bh+Xc X-Received: by 2002:a05:6870:e415:b0:21e:7e5d:4fde with SMTP id n21-20020a056870e41500b0021e7e5d4fdemr11661878oag.52.1710283384848; Tue, 12 Mar 2024 15:43:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710283384; cv=pass; d=google.com; s=arc-20160816; b=zuD+wIv+oldzmAyDUsprn/amphgGBxu4PefCTcIj+OmpmfFoL64zhFbMRH/X7/w/P+ XbiGOzPAuG+eqNalEdjub2XwN8WHUh0kFGz1lUSIv8iPuXvUfFdCuFS/N93vLk5JVCPo Z1pdW/dKl27S6pLPq2fKYyR8dHhUiLd9+gk4uJoTD3KUO6DpiCyRN4WiKLpN4nL1b9lg w465KKBIowA/KSkjLxXdDaBxoU0lVRbT32V52t83NDGeHNJzNeoTkurVuyyMDGhS2oYb kIfvTsja2ChsNgAnNxK6nfvDafBTY+45b0WRNeUfabQNoEgEpnlGbKAUZeLGI1CiEET9 t55w== 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=GW5vj3ww6Q8f0lmgbHjuCxUYGiTO32XJbsJws7w3Yo4=; fh=OsAF0Yb3YqpM3YOImVuDHTzUtGP19xUmitaB3c6wpGU=; b=V99/4i+OMAqWrDaKmnvV/QYVpoBkvEA26pVk8xuQw4i8ESS3XhgH8WM3CQ+iMpDtHO BrRHMpu7yMjkk0WrgcKGRlJh5qHpmDNfdo9iZc+BQYxSsqMKmg7pDj9rsXUTGpGtc0O+ SzXzb02zTOLjnazFc7ngyfrLj1QG46oYFyJI6HVDERogks7VxUi6Mpge91wi0HwBbB+B dxOHGGPxmwEASBoyJqP9yl5XehRqu1NwIVXrautCFGGadDx6lG98R5ttYeNZcY16+4EA 4/4JaE7qhW0gTxPIfP/0bSm0Y4vatm+Q0qAwnjy4V408KH5Vgc5zefoCNMxb+p4lK4tt Cbmw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TIVWnHwx; 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-100902-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100902-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 b12-20020a05620a0ccc00b007883241f9f5si8171598qkj.383.2024.03.12.15.43.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 15:43:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-100902-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=TIVWnHwx; 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-100902-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-100902-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 44F261C219CE for ; Tue, 12 Mar 2024 22:43:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BB87F3BB43; Tue, 12 Mar 2024 22:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TIVWnHwx" Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 6475B14293; Tue, 12 Mar 2024 22:42:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710283375; cv=none; b=U3OwTx5tK3FhJ7FX55og/DhytODestP7hdIo+o9CI08l41BKfQUqj/wvcMxm+lzV2j7gLbJJQQQlWlSQcGByvfJ5r0L3GgzVjdyE4Mdq3rhnhiTHbbuccMM1ChGy/XlR0q2YMldlGkD2ouFImQFXdzjy3REr3MJG23OoKa0gC6g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710283375; c=relaxed/simple; bh=itk3+aa121hQohCaZWXLyFbk44xpebuGh6lznsJnwBI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=A9XYgKaknHZp7ZvtC5i8IWfUvneyQ4nAXbD8In/SBGVXGAp79so+4cvgq9bZpoX0Qsfz2EqLvjP7BmYcP6eFhFyUYrgSTyTTH4mLYwwLFH9yXmJQiXBQ7Lo/Q1DC3ovrfuXapaNoAzht05YCWjk3P1kR3v2VyAcgGaL2+46qa1o= 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=TIVWnHwx; arc=none smtp.client-ip=209.85.216.46 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-pj1-f46.google.com with SMTP id 98e67ed59e1d1-29c23f53eabso1549045a91.0; Tue, 12 Mar 2024 15:42:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710283373; x=1710888173; 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=GW5vj3ww6Q8f0lmgbHjuCxUYGiTO32XJbsJws7w3Yo4=; b=TIVWnHwx2/uuaE5pqmD2H4Zq4GtrGGAv9o1jEyq+jWiz695Tdfqjb3hNuw+6L5VESc h6q5O0Ws3FdBatlX/jJZrYnUUs6g0s5swQbVyEfTmhNV0r7oPyDf76PYJNZMtlpG1jbp LszeYIwvCHrdvmgwTeDTG4Tml2/RcV9EH29XoPt1ZM9sk3oCZaBIe47JvW1YbEOLfFOl 5qx+JDPk7OvptZh/ilhvSHCZXxFxzNvg12bPx0+pgi/8xdFl3AmuNPxpaa8YPFXq6lN0 D+DraiunRv+7IQFpZRiwf8VtYJYSYhYSgs25BiiUtjb6DzKRANmgv/1jNlPnr1k4h9TM Q/gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710283373; x=1710888173; 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=GW5vj3ww6Q8f0lmgbHjuCxUYGiTO32XJbsJws7w3Yo4=; b=nv/uLKVqUryeMsdpKK/qxNzQ/caZFbHOaZ127BlHHA1Km5WIJS5ANmiwJbKTfL76Qz o4ekB8wDEGf5MRX6ouPpzhKUZ8rsQvRGmhrLjlOEd7P51hhVFiH+Y44QtdnDrc+g5Cd8 TU/Na6NCedPs2M/66xbUiCU5z46/8Iq7bmnP03pyjDWkj0dPk2AVHgvilj/sFI/cyQQo mKVOcnSkrHxjwKLh0jqlyuJgRsNCXdf5PlfnpsZIz9NtE+uvcs2Tp9pUSBZwdkEW2O9G WjQhleJ9XxCqjmIqjLJ1duxioPr/XK+OqK+Va5yrrvppekvQxBfJJwZu04GNzZ1gTnQ8 t3Vw== X-Forwarded-Encrypted: i=1; AJvYcCV4wvqi1iXkoJxZPlfcCjf29srXTWVoWgDjvZyVRCUUuk48s5q+iAn9710d0gQ6kq6fYuOIv7Fyo4aiHHpi87ALLBN4dm7wXVMxYDqB26NlADT47nLZzf/+a1eqfVbvtcPj X-Gm-Message-State: AOJu0Yw9zujFMc+g9lbeSQLCWICORsGFXZmOHrWZpGhGvWyoxKV3fAhF Ycl+enBoE8UMgf9afSblOMBwFiKmQ840trmzHjkb+PtyhASOylsDC+WusrbmpmghzXtuSnlA2SM z2CdMHXNus7UwXBgrfNxAJnzWriI= X-Received: by 2002:a17:90a:bb8a:b0:29b:a793:e3cb with SMTP id v10-20020a17090abb8a00b0029ba793e3cbmr8207504pjr.22.1710283373514; Tue, 12 Mar 2024 15:42:53 -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: From: Andrii Nakryiko Date: Tue, 12 Mar 2024 15:42:41 -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 Mon, Mar 11, 2024 at 1:25=E2=80=AFPM Andrea Righi wrote: > > On Mon, Mar 11, 2024 at 10:55:57AM -0700, Andrii Nakryiko wrote: > > 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 intentio= nal > > > stop condition by returning a non-negative non-zero value from the ri= ng > > > buffer callback. > > > > > > This can be useful, for example, to consume just a single item from t= he > > > 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. > > Yep, that's exactly what I'm doing right now. > > To give more context, here's the code: > https://github.com/sched-ext/scx/blob/b7c06b9ed9f72cad83c31e39e9c4e2cfd86= 83a55/rust/scx_rustland_core/src/bpf.rs#L217 > cool, great that you at least have a work-around > > > > 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. > > So, returning a value > 0 should have the same behavior as returning 0? yes, that's what the contract is right now > Why any user callback would return > 0 then? this is not the code I can control and ringbuf API was like that for a long time, which is why I'm saying I'm hesitant to make these changes > > > > > 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). > > Sure, I can keep using my special error code to stop. It won't be a > problem for my particular use case. > > Actually, one thing that it would be nice to have is a way to consume up > to a certain amount of items, let's say I need to copy multiple items > from the ring buffer to a limited user buffer. But that would require a > new API I guess, in order to pass the max counter... right? Yes, definitely a new API, but that's not a big problem. Though I'm wondering if ring_buffer__consume_one() would be a more flexible API, where user would have more flexible control. Either way libbpf is doing smp_store_release() after each consumed element, so I don't think it will have any downsides in terms of performance. So please consider contributing a patch for this new API. > > Thanks, > -Andrea > > > > > pw-bot: cr > > > > > + /* update consumer pos and re= turn the > > > + * total amount of items cons= umed. > > > + */ > > > + smp_store_release(r->consumer= _pos, > > > + cons_pos); > > > + goto done; > > > + } > > > } > > > > > > smp_store_release(r->consumer_pos, cons_pos); > > > -- > > > 2.43.0 > > > > > >