From patchwork Sat Apr 6 09:15:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13619699 Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D421383B2 for ; Sat, 6 Apr 2024 09:20:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395222; cv=none; b=TX/BJhxLDHOAEnjLHtkxF+4CIU+9ZBmCaQowCAYYozZR2oiKCpvACHfPVVJjE9NWer+Ldf/hPsgkRMWxG5hboZhqIWkjWlqRrZ5NOTintjBPaiNxcULk13OnwxV8D4CafSgxTlz7ma7125KORfIj1/Mcqiq5lnq31lhCZZGM7e0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395222; c=relaxed/simple; bh=yt9tmTIBJzVdCFZ4PnU57SVH6MzeinR1L4YoDUXNjsE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gd4/YBWKOE0tn9KWR1+wqueKRDEfs5Wx5pKaYs3DofkDaYaHdgEmNKA40uMfAsn+LS1dZCJVaMBBCxJvRzIXJNkF/Ud1EdqHD7RjLv8vnITSN7T6mr0vScmnPFYwSSO4VAoZKyPSb3+R4TLfEKzPWzvjFQqFcde4YKmL61tgl4A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=dOmJwRE8; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="dOmJwRE8" Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 61B2B42495 for ; Sat, 6 Apr 2024 09:20:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1712395216; bh=G6LgQlJD57yHhfXydvKzs8A9bUpRZ0ooDJmyE1zvH0s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dOmJwRE8oTUeXFiopPdOZhTIHljrEg1oNa6fQNSMzZh6tAzjyoAozBGY9xAPb1ZVs WdSHX3/8v9LKdS8sY05r+nyM4A4/cdYp4/30cmpsCgueAeY6vt5GKx8A/vplF3yxGs KbTV/CNFegIF5LT9jhQgEyFOdElabj2P+LUJgIUrRC64AFuTmzgtrQDqqOhbxdb/XI kTc5qrLPqjF38jkMXhWpv66GfIkwQc1/cAM5rWfPMGdOqYMfvygsu1Ocmg8UXQ1p22 hkrhRMW2Pl5IWtj0oCxxIvxexWuoTIv0WXUzFDVofbSX6qhB3W51RyfNFpn6uea0R4 QC8nFzUYu4MUA== Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2d6c94d98afso26915171fa.3 for ; Sat, 06 Apr 2024 02:20:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712395214; x=1713000014; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=G6LgQlJD57yHhfXydvKzs8A9bUpRZ0ooDJmyE1zvH0s=; b=dYpldi7AscDMrQBuMMob0Tql+LjvAkEh32FEr9yrLPCNYszgtcd1GG5aKl8wGwg0Hz dEohg5aA1nt1lQbiWqw0ly6SbM/wCBPG724XhzN4sqICArU+2InZwRxONTEiTGkSCuJA VF6okdLj1DW9pyLpP1QyUXCJ6RKjJfO2QhByf8DhQMLQOEgqKPx1ZPwc92UKEpTvXhTi puQ65haFCWzml2hGs1ZPxqwiCIlbGefbvHuargDVHVqYtwouK64HKsrXahw3pdyZrOxX PwoQKVE45X0EIFSRIG4O4z0DM5dSRlabXeXPQxZ/ycC24glZOLm2QumGbF9jXVuY8+rB L74A== X-Forwarded-Encrypted: i=1; AJvYcCWMjgGxy/8VC23Q6YIMVL+PwkTt78cmUvzubcZmDwNItHNReE2nA1QvW6EgFjHRReDQkz13MqyVBwfN7J+k/hkq59t7D+u+Xp3xw2p2W1os X-Gm-Message-State: AOJu0YwxpMVpqAQenYBPVkeymDbAxfHvJURhUKsBdV6eeLUkfOawXkAB Dt37go4prw8+Bx8Zdizl3o9aBLNKoF2klcN5RV9nmCWRDVyAKlBoZ/gJQu50mbY6XLSPY2HjcdC ckpdgSFLgiVbPGyqLdbNY3nXaTCEkBV/NIrFWYW6q9NlgQtpS6qyHAJD93OczIBnQPA/+QxHr2Y hTRTt7QQ== X-Received: by 2002:a2e:98d9:0:b0:2d6:d351:78ae with SMTP id s25-20020a2e98d9000000b002d6d35178aemr3079660ljj.29.1712395214347; Sat, 06 Apr 2024 02:20:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEC5CR3YqSgu+J4/e2RoJd1lpNEBaMZEgCrY3TxjTsm1+4G8Z80bdHexcZXeNzlB0Tjro1Qiw== X-Received: by 2002:a2e:98d9:0:b0:2d6:d351:78ae with SMTP id s25-20020a2e98d9000000b002d6d35178aemr3079628ljj.29.1712395213639; Sat, 06 Apr 2024 02:20:13 -0700 (PDT) Received: from gpd.station (net-2-39-142-110.cust.vodafonedsl.it. [2.39.142.110]) by smtp.gmail.com with ESMTPSA id l23-20020a7bc457000000b00414924f307csm5779993wmi.26.2024.04.06.02.20.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 02:20:13 -0700 (PDT) From: Andrea Righi To: Andrii Nakryiko , Eduard Zingerman Cc: 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-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/4] libbpf: Start v1.5 development cycle Date: Sat, 6 Apr 2024 11:15:41 +0200 Message-ID: <20240406092005.92399-2-andrea.righi@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240406092005.92399-1-andrea.righi@canonical.com> References: <20240406092005.92399-1-andrea.righi@canonical.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Bump libbpf.map to v1.5.0 to start a new libbpf version cycle. Signed-off-by: Andrea Righi --- tools/lib/bpf/libbpf.map | 3 +++ tools/lib/bpf/libbpf_version.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 51732ecb1385..5dd81a7b96b5 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -416,3 +416,6 @@ LIBBPF_1.4.0 { btf__new_split; btf_ext__raw_data; } LIBBPF_1.3.0; + +LIBBPF_1.5.0 { +} LIBBPF_1.4.0; diff --git a/tools/lib/bpf/libbpf_version.h b/tools/lib/bpf/libbpf_version.h index e783a47da815..d6e5eff967cb 100644 --- a/tools/lib/bpf/libbpf_version.h +++ b/tools/lib/bpf/libbpf_version.h @@ -4,6 +4,6 @@ #define __LIBBPF_VERSION_H #define LIBBPF_MAJOR_VERSION 1 -#define LIBBPF_MINOR_VERSION 4 +#define LIBBPF_MINOR_VERSION 5 #endif /* __LIBBPF_VERSION_H */ From patchwork Sat Apr 6 09:15:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13619701 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70AF53B18D for ; Sat, 6 Apr 2024 09:20:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395237; cv=none; b=Zl6USIEWIBrPEqNJ/qZ+d7rUKu8rWt8/m7/ewc6Cemioj8USEI7GVBNVRJvYo8H5u4byMc/Yns5okRbeYIgpYbCTplr9w2o5DvTUhTgYU3rRtobx84tv2N8p9RMoekXqBhpWErrPTqv+2lW/TwWzuTpjJ2h80YScokDnWmdSoRk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395237; c=relaxed/simple; bh=a78vMWglkqZNxVpizm3bz2mABJk4IrXHntVwOv9hkR0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ROhy7MHCmvPep0PFJp2PFglzyCo3nESy0071/ZJFjNij1KovHXfSLl/RSD63TnjA0seyRE1wXfcG+QwGwKkt9170atqj/7SPghEdJurSoJVkoLRerSpBQVID4rkg1D7EI7HBZzTsK9ZfaOdPLFqYNqTkzuaZ/q4lURCcSgmusxw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=shS/SCUd; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="shS/SCUd" Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id C8E073FE63 for ; Sat, 6 Apr 2024 09:20:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1712395223; bh=U9JiGxHu1Zu7lOUTn6nQmFFrRqhFXaTmBwUIearvaJY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=shS/SCUdkOrlvaMo6v2dMqgIk7vxRz2ZdoXHfzCVrKyAtO2LloW8GQbgK8rXmL912 9Nsgifl3m5sfHdIC3ogxFRoQDyaQ+CLRYLp2M9XQH5vryPG3hEuwzxCYaJmTFEwRDU amRl17WxWsSy2fp7shGUtZE03ENcj2Ec4AWzpiRd+b0Wf4QamaJ4JDHHkxgsy08jTS b2hHb7KZyFagkFEl0quUamCuCbba1gmRMsn8a9CiqJnKvNZLpBbiRPLtm5Eau9JUh8 /cTWNghhiQjlAUtHxCp/kJSZ4pXZJArdErAjGt5jsqMgNy6pzPOt0ySP25IXbGN7kq SD6c0HNO7zX0Q== Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-516d2c322d5so1608636e87.0 for ; Sat, 06 Apr 2024 02:20:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712395216; x=1713000016; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U9JiGxHu1Zu7lOUTn6nQmFFrRqhFXaTmBwUIearvaJY=; b=LKcdiIMIyotqYom/Wkz1ZRQZQJCBlZgIpGWpBq30oDhXKrJmyR9dcWERR3BxXzZVX/ 5DLhxRPH+knXvYuUPJvLM2aYLwsSzZDByYMZONyCcpsPihfNpjC8SxDjDNDgD2UiyAXT ro7mcYMBUJFFKujgrpruKJE4mGlYpVLLpLQkX+egZhio2wKjZwsSQPQSGG9pywMZy+o5 USy5idfg18uo9hnX6jJb1AdyqZEHrH33H8uaAjXhRMYdJA1VuIHplC7xRTaDSTxvNr9x tLubbU0Mo1eLR+GV31M8MQerqZA2zHcAEXSlg35F4Vw2BuLaa/l1jpBxIwcpHA7YND9G C0rg== X-Forwarded-Encrypted: i=1; AJvYcCUUvrTpOdXJFL+0KjvdJdoYem8mWDY9qjWaQ6vDv3OXYqNEKsOu5Liom+CbaE9CqdoSV8po0qcRwQ0RfKld4y5KnDM3VOpMdaVLck0ptrsw X-Gm-Message-State: AOJu0YwMin0UQU3CRPjYhC5grB1Jzgy6sGGbqoqqrHtzJy3KGEkkl9p5 syOlROqNv4AnIqx3PvX3C8LRmMvlMh8OWdrmnjwUr608eSKjXkeyPKTOJ3N2VQ5iSvPCbQR/IY0 NLaLxo3dcZ6guU53VB9/2zceGQtHX34qejBoia+L8HTwSqucr2QChO6oKxNLK76Bcrznu/BovzO pjOPTaYQ== X-Received: by 2002:ac2:5df9:0:b0:515:d50f:c6cc with SMTP id z25-20020ac25df9000000b00515d50fc6ccmr2674257lfq.63.1712395215923; Sat, 06 Apr 2024 02:20:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHS/Wj4fTtj4EhH4SwuK4rc7frejoX2SMVVjV8lwzSRp5JOOCrfUKz3Ui9mytME96ZdhQmWHg== X-Received: by 2002:ac2:5df9:0:b0:515:d50f:c6cc with SMTP id z25-20020ac25df9000000b00515d50fc6ccmr2674231lfq.63.1712395215460; Sat, 06 Apr 2024 02:20:15 -0700 (PDT) Received: from gpd.station (net-2-39-142-110.cust.vodafonedsl.it. [2.39.142.110]) by smtp.gmail.com with ESMTPSA id l23-20020a7bc457000000b00414924f307csm5779993wmi.26.2024.04.06.02.20.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 02:20:14 -0700 (PDT) From: Andrea Righi To: Andrii Nakryiko , Eduard Zingerman Cc: 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-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/4] libbpf: ringbuf: allow to consume up to a certain amount of items Date: Sat, 6 Apr 2024 11:15:42 +0200 Message-ID: <20240406092005.92399-3-andrea.righi@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240406092005.92399-1-andrea.righi@canonical.com> References: <20240406092005.92399-1-andrea.righi@canonical.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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. Signed-off-by: Andrea Righi --- tools/lib/bpf/ringbuf.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index aacb64278a01..2c4031168413 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, size_t n) { int *len_ptr, len, err; /* 64-bit to avoid overflow in case of extreme application behavior */ @@ -268,6 +268,9 @@ static int64_t ringbuf_process_ring(struct ring *r) } smp_store_release(r->consumer_pos, cons_pos); + + if (cnt >= n) + goto done; } } while (got_new_data); done: @@ -287,13 +290,15 @@ int ring_buffer__consume(struct ring_buffer *rb) for (i = 0; i < rb->ring_cnt; i++) { struct ring *ring = rb->rings[i]; - err = ringbuf_process_ring(ring); + err = ringbuf_process_ring(ring, INT_MAX); if (err < 0) return libbpf_err(err); res += err; + if (res > INT_MAX) { + res = INT_MAX; + break; + } } - if (res > INT_MAX) - return INT_MAX; return res; } @@ -314,13 +319,15 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms) __u32 ring_id = rb->events[i].data.fd; struct ring *ring = rb->rings[ring_id]; - err = ringbuf_process_ring(ring); + err = ringbuf_process_ring(ring, INT_MAX); if (err < 0) return libbpf_err(err); res += err; + if (res > INT_MAX) { + res = INT_MAX; + break; + } } - if (res > INT_MAX) - return INT_MAX; return res; } @@ -375,7 +382,7 @@ int ring__consume(struct ring *r) { int64_t res; - res = ringbuf_process_ring(r); + res = ringbuf_process_ring(r, INT_MAX); if (res < 0) return libbpf_err(res); From patchwork Sat Apr 6 09:15:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13619702 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 733B33BBE5 for ; Sat, 6 Apr 2024 09:20:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395239; cv=none; b=TsKlYf66hpQ7M3QyRVC5EbRx22jufv8BhHpIY6lpY9i1DlJkGHiuEYftFjAulVymLzKTsBDFM8pqvOeoI+2m2NibxBvsdEAL8EGaPMB/PXfNiOZ81mvTaHx6IHmypshdN97o9gSoieTrYJWYa97BoDCAgP8V2S24YscJ4BS5O4U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395239; c=relaxed/simple; bh=WxyORd5UGrvrhH3ovdgj29XhigFycIY9JFgRu0PCY/A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UfmQ6FmzeAWwDWjJ/u8CZxgw0cCOMe0V+sIgnH8aAbqnadfMSF46K9kkaaALZtO0UAdDvLP+Uf0tXRiVyyDrMXwuc08pnwivLMLscpDMPoTVMPmO8Mqj0SZsDqseWFkxTBZU4xEuKPab0hD32yjXVSeOCTZBx8ZQrY6vCRDq/zQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=A4hClA6S; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="A4hClA6S" Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id EF9393FE64 for ; Sat, 6 Apr 2024 09:20:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1712395223; bh=OGumsLBWYsNiBqaQkxgfB4Yj3V+oiBGI9bnDO7L7IE8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A4hClA6SGTtBjYX62dfXWjb2uNURMfD/S1PHiJaT7PkqrPUpCoF9nr2cwn+E1bZw0 E0gCxbsa0GKldzg93PMTZhMjv1xQbm8aTsYn0nbJKtC55XK4ZQjM57IVA6RIwx65FL XV6h6qocR4gQZk6UwFL8S2SD8uNhEtyL6UJEHU9Ud8g4GKSnUo8rwFXF7VieBalPWQ 5LCP8d/XhSsOS7s/2t0duYxP86cmn6hpvIdK+bHZiJ0pc0D8UCJYx3O/s8VIdQHbbB ELVC5EB7eBWcYiV+ljm+9V9vxVoSFUjVaVUD00Pb9BFQMAOlka9pF9nbMu7Vtbjqqj bByxtAyLCRo8w== Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4147f17da12so14516785e9.0 for ; Sat, 06 Apr 2024 02:20:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712395218; x=1713000018; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OGumsLBWYsNiBqaQkxgfB4Yj3V+oiBGI9bnDO7L7IE8=; b=bkgJIrJa3zfUOEyeedGw+yNTqhOsag1AC351E5nYA+PkOnxH7Pw+PO6g9Vxg+Sylen 9pdWFjw/KsC77FPUcnxXx0ExUcXMETA7wy+TPGjJ6RZYkgXw25DRLqDZJrHVrR8s8idK QlKN6T/7X6C6pHpX9DFVEo5vtNlISOcCJ7//V0V3P823t4JAK14lFaNLQHMOwB9WKZBj Ej9UFxrinZBa3O4h5hfK4l0/Xcou9byCfafu12kmgPLjOWIH66SCkjec6d9xYPhRhhzv 3bVAhRIDPxyNmc8eH79iktSXcKJ+rJ5UWtatOjoGdsvn/93sV5E/kDyu4wYKR2DoWNWB rqfw== X-Forwarded-Encrypted: i=1; AJvYcCU+RUyjOC5vVxaRdYhVWEbg76olJvNjTHNGWLzTBI+aUGUrT8+WAuvbA88ZMVt+KzcljX6zdx5RqzFZSNwHukMZ/9/6KXPfSKvH7xbpMYsw X-Gm-Message-State: AOJu0YyjA1Xw+FfORQeZXG4BBYCttSkpgOiXytWZ6MO5ZlX1oV4kkjCd gLMd7PMBJ+LxlIZKaPXaSH8nk9UKbKg596QC+dohdx6jK88AC39AuUn6wCWIG6NTyLjo/gWp1ep r5HRcFfqRzXYfRtdWb7Pv1KEb9JyNCdlDw7UTBF/JThSTkCFJ7+TN4Mf2O9ZN3at2hn/EkWI/6N u+f2PhVw== X-Received: by 2002:a05:600c:4e8c:b0:415:6728:a584 with SMTP id f12-20020a05600c4e8c00b004156728a584mr2494536wmq.25.1712395217755; Sat, 06 Apr 2024 02:20:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNXztDhLsiKAKWeuFLp95WeOaSFimKeT6ittgqyCRbNIAyGvgIZJkAN7068+uWqmdKvBO5fg== X-Received: by 2002:a05:600c:4e8c:b0:415:6728:a584 with SMTP id f12-20020a05600c4e8c00b004156728a584mr2494512wmq.25.1712395217054; Sat, 06 Apr 2024 02:20:17 -0700 (PDT) Received: from gpd.station (net-2-39-142-110.cust.vodafonedsl.it. [2.39.142.110]) by smtp.gmail.com with ESMTPSA id l23-20020a7bc457000000b00414924f307csm5779993wmi.26.2024.04.06.02.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 02:20:16 -0700 (PDT) From: Andrea Righi To: Andrii Nakryiko , Eduard Zingerman Cc: 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-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/4] libbpf: Add ring__consume_n / ring_buffer__consume_n Date: Sat, 6 Apr 2024 11:15:43 +0200 Message-ID: <20240406092005.92399-4-andrea.righi@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240406092005.92399-1-andrea.righi@canonical.com> References: <20240406092005.92399-1-andrea.righi@canonical.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce a new API to consume items from a ring buffer, limited to a specified amount, and return to the caller the actual number of items consumed. Link: https://lore.kernel.org/lkml/20240310154726.734289-1-andrea.righi@canonical.com/T Signed-off-by: Andrea Righi --- tools/lib/bpf/libbpf.h | 12 ++++++++++++ tools/lib/bpf/libbpf.map | 3 +++ tools/lib/bpf/ringbuf.c | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index f88ab50c0229..4f775a6dcaa0 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1293,6 +1293,7 @@ LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd, ring_buffer_sample_fn sample_cb, void *ctx); LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb); +LIBBPF_API int ring_buffer__consume_n(struct ring_buffer *rb, size_t n); LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); /** @@ -1367,6 +1368,17 @@ LIBBPF_API int ring__map_fd(const struct ring *r); */ LIBBPF_API int ring__consume(struct ring *r); +/** + * @brief **ring__consume_n()** consumes up to a requested amount of items from + * a ringbuffer without event polling. + * + * @param r A ringbuffer object. + * @param n Maximum amount of items to consume. + * @return The number of items consumed, or a negative number if any of the + * callbacks return an error. + */ +LIBBPF_API int ring__consume_n(struct ring *r, size_t n); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 5dd81a7b96b5..23d82bba021a 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -418,4 +418,7 @@ LIBBPF_1.4.0 { } LIBBPF_1.3.0; LIBBPF_1.5.0 { + global: + ring__consume_n; + ring_buffer__consume_n; } LIBBPF_1.4.0; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 2c4031168413..19cd34883011 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -277,6 +277,33 @@ static int64_t ringbuf_process_ring(struct ring *r, size_t n) return cnt; } +/* Consume available ring buffer(s) data without event polling, up to n + * records. + * + * Returns number of records consumed across all registered ring buffers (or + * n, whichever is less), or negative number if any of the callbacks return + * error. + */ +int ring_buffer__consume_n(struct ring_buffer *rb, size_t n) +{ + int64_t err, res = 0; + int i; + + for (i = 0; i < rb->ring_cnt; i++) { + struct ring *ring = rb->rings[i]; + + err = ringbuf_process_ring(ring, n); + if (err < 0) + return libbpf_err(err); + res += err; + n -= err; + + if (n == 0) + break; + } + return res; +} + /* Consume available ring buffer(s) data without event polling. * Returns number of records consumed across all registered ring buffers (or * INT_MAX, whichever is less), or negative number if any of the callbacks @@ -378,17 +405,22 @@ int ring__map_fd(const struct ring *r) return r->map_fd; } -int ring__consume(struct ring *r) +int ring__consume_n(struct ring *r, size_t n) { - int64_t res; + int res; - res = ringbuf_process_ring(r, INT_MAX); + res = ringbuf_process_ring(r, n); if (res < 0) return libbpf_err(res); return res > INT_MAX ? INT_MAX : res; } +int ring__consume(struct ring *r) +{ + return ring__consume_n(r, INT_MAX); +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { From patchwork Sat Apr 6 09:15:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13619703 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C40943FBAB for ; Sat, 6 Apr 2024 09:20:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395239; cv=none; b=cwr2ICVblGtIe1M/w/bXpdixdmWMhyXudiL1aPMKC9SroNEL7DhjHEU0Yg6UhUsjRYDVN+MX+yUx8hYqOPVzgjvw23ASLUqsHc9shX7wECPp5YzFVUZkpAYv8ibXkqaMzbs9d4X5kafvCVtyVsh899gaTR4XNchxI0NxFhCs58w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395239; c=relaxed/simple; bh=V866d7v0EUjhFe+hV0qrTNaTJ3Gb92BOJZHI+Gz3L24=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FbHvuxh3zV8VQiqTNS3oseXcl2ulAHDncaDX8/VfjV2L4bCLMBkURbu2MPolkeRTA/TpZJf6eyKQJy42RdhihCRohX9abMF38brW2QUX5NhbSCPcEJgTIbHUILDgtMK6WFdRk4oVXt7vlmkaXJbmZ/0iiPuG0iGoWsl52OGF2N0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=mM545qlW; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="mM545qlW" Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id EE38C3FE6E for ; Sat, 6 Apr 2024 09:20:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1712395227; bh=qzaONF2ufkMLEwxVO924M3LwWzApnPaibbBgBkLZGxs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mM545qlW3sBhsM6ijJRuTctVG+fj8CpnjEi8FIU9SrXVgargOI9GX4Q8CyvLf3xJ0 1Ji/SAhIKn7TAWTqMDBlBRWWA7KnBn5/wfDzkNS44/5euaHQMil3PmgD/ilEppj/8/ rZMwKyKggcg1Op7XBK66RB0w3hEwR/XCs2F1BEsSpgXnM/oZu6g4qFQA3OFjV/iCpz YI4TsL7q0Ky/hAwYTw/dSNgTkCRaI3PeH2t2y0bYlZgyOVDNsfhhjr48hoPYLfvqmn rfn2rsLP3mvwUq9Oe+tvM4v4bsP5aRePpejviUqys3C8rTn1EIu+DDI+yS0NEdMtCa vWj057L+JNolQ== Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-516ce32c90aso1968878e87.0 for ; Sat, 06 Apr 2024 02:20:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712395219; x=1713000019; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qzaONF2ufkMLEwxVO924M3LwWzApnPaibbBgBkLZGxs=; b=ElAL7V+K5ZH/vWhY+Kpxtawi8TtIUEc2n6rAQ+GslmSIEttzlUSBDOQ7paFL6q7Cer 0jNkVXoeJr9no52ALFmay2m1Q92quoUyQPCEpRJRhB22fqfNwHSVwrjOQ611zFn3vc2I dLfUYDoMmSsYasF4qQ8ak3WWGb19zLz8AVI8ZYt7wWAIGhTOMuJSMxokyYvOpaIPXiKS Szmf0p6vkc5ptqO9MK1H1HuOX2TAryyzTUZ6I2KKMwwT3/+UW6r3Espy5c0Y/Qz4wlIH fkqzgBFGu2X/XDggtXX4w899xaseux6snnHWU+IdU91sUHOnJoLgWuukgr40OsvysP1a IbGw== X-Forwarded-Encrypted: i=1; AJvYcCU8j6f4iY2h1Y2mciM1Ksv/ldY8K8dYzxEDtizf1LBBbMOuERDkFJ53+n00Sj66JOTcPdX1ULEHGFtqXeKqaDfUOb5R3FeJYE9/NMwrGa5P X-Gm-Message-State: AOJu0Yxhsi/zv3miYVVDm5X+9xVCV6eVrTOYJ6Qs7nTQfy5jmiucIr+T 8Zw6R3OgPd17flMowgqEF9WPb2lITXOhV7eAj0OZ8SWejLSY06kjJdqU8tWGvfB6MHN5UOqBMOX nS2TA74FomFrcB2nuhSmCBW72TPA8u9XavfJbiPHAXptj7/bTczCJBtgTIqvE/jHXelyenJ+ZMd 2Ffxu1ww== X-Received: by 2002:ac2:528f:0:b0:516:ace0:9a75 with SMTP id q15-20020ac2528f000000b00516ace09a75mr2341487lfm.23.1712395219276; Sat, 06 Apr 2024 02:20:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZAyOXnlJzHnF6UAxzVX17DxYL4mhxCDf8C91bLhJCKP1I0WnSc9EcZ/D+EvzXCHwk6NrkuA== X-Received: by 2002:ac2:528f:0:b0:516:ace0:9a75 with SMTP id q15-20020ac2528f000000b00516ace09a75mr2341471lfm.23.1712395218819; Sat, 06 Apr 2024 02:20:18 -0700 (PDT) Received: from gpd.station (net-2-39-142-110.cust.vodafonedsl.it. [2.39.142.110]) by smtp.gmail.com with ESMTPSA id l23-20020a7bc457000000b00414924f307csm5779993wmi.26.2024.04.06.02.20.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 02:20:18 -0700 (PDT) From: Andrea Righi To: Andrii Nakryiko , Eduard Zingerman Cc: 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-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/4] selftests/bpf: Add tests for ring__consume_n and ring_buffer__consume_n Date: Sat, 6 Apr 2024 11:15:44 +0200 Message-ID: <20240406092005.92399-5-andrea.righi@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240406092005.92399-1-andrea.righi@canonical.com> References: <20240406092005.92399-1-andrea.righi@canonical.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add tests for new API ring__consume_n() and ring_buffer__consume_n(). Signed-off-by: Andrea Righi --- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c index 48c5695b7abf..33aba7684ab9 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c @@ -304,10 +304,18 @@ static void ringbuf_subtest(void) err = ring_buffer__consume(ringbuf); CHECK(err < 0, "rb_consume", "failed: %d\b", err); + /* try to consume up to one item */ + err = ring_buffer__consume_n(ringbuf, 1); + CHECK(err < 0 || err > 1, "rb_consume_n", "failed: %d\b", err); + /* also consume using ring__consume to make sure it works the same */ err = ring__consume(ring); ASSERT_GE(err, 0, "ring_consume"); + /* try to consume up to one item */ + err = ring__consume_n(ring, 1); + CHECK(err < 0 || err > 1, "ring_consume_n", "failed: %d\b", err); + /* 3 rounds, 2 samples each */ cnt = atomic_xchg(&sample_cnt, 0); CHECK(cnt != 6, "cnt", "exp %d samples, got %d\n", 6, cnt);