From nobody Mon Feb 9 16:32:03 2026 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 1FC6231B816; Fri, 5 Sep 2025 15:13:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757085205; cv=none; b=oGARJlKJxvU6AcC89ZiSabcUYXBEX/e7kBHSAVFP6Cn5H8q6YayGFaT9CL9tJ1RLIKm0lwDh8TJXf4tNwp1Ryak8uWC2XD39pF64aEBdcsOKJ/ks4JZiarZDvBrqZ7KW9Bss4883m6yminnVe42HlyQV8S9Jo8iwyLA3ahv1Gkw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757085205; c=relaxed/simple; bh=NWOuTuw+EJ9IzR6EFtSxI8823k0Bk7UX//cx7V21zC0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fCk3RLEt1mkOllmcdvkT/A16yVxi8nr2KtxMd9KyrEHxJ1utIFP+C4M93JVURLcn1PC/66NZgx6m29S/5TbShq/XoIdJBQVYfqlbd3KEtyMYcYFdtcp6HsWVCogRHv6nba5NZRsbVH17I9ZJOb+84MJ30/AI7ucyXI8d6z0vLRs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4cJKbv5HV9zYQvKm; Fri, 5 Sep 2025 23:13:15 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 3A7AA1A0DBD; Fri, 5 Sep 2025 23:13:14 +0800 (CST) Received: from k-arm6401.huawei.com (unknown [7.217.19.243]) by APP4 (Coremail) with SMTP id gCh0CgDnfYoF_rpoPC72BQ--.54678S3; Fri, 05 Sep 2025 23:13:13 +0800 (CST) From: Xu Kuohai To: bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Stanislav Fomichev , Willem de Bruijn , Jason Xing , Paul Chaignon , Tao Chen , Kumar Kartikeya Dwivedi , Martin Kelly Subject: [PATCH bpf-next v2 1/3] bpf: Add overwrite mode for bpf ring buffer Date: Fri, 5 Sep 2025 23:06:39 +0800 Message-ID: <20250905150641.2078838-2-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250905150641.2078838-1-xukuohai@huaweicloud.com> References: <20250905150641.2078838-1-xukuohai@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgDnfYoF_rpoPC72BQ--.54678S3 X-Coremail-Antispam: 1UD129KBjvAXoWfCw1DAw15Wr4rZr4fZFWkWFg_yoW8uF1DJo WSva1xuF48Cr1UZrWUK3Z7GF1rAryDGFnrCr43uw13CFyDJFZFqry3JFs5W3Z8Xrn8GF1D C3W5Jr15trn8Jr1Un29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UjIYCTnIWjp_UUUOy7kC6x804xWl14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK 8VAvwI8IcIk0rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr 4l82xGYIkIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AK xVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ew Av7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY 6r1j6r4UM4x0Y48IcxkI7VAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7MxkF7I0En4kS14 v26r4a6rW5MxkF7I0Ew4C26cxK6c8Ij28IcwCY02Avz4vEIxC_XrWl42xK82IYc2Ij64vI r41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8Gjc xK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a6rW5MIIYrxkI7VAKI48JMIIF0xvE2Ix0 cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8V AvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E 14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUhWrWUUUUU X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ From: Xu Kuohai When the bpf ring buffer is full, new events can not be recorded util the consumer consumes some events to free space. This may cause critical events to be discarded, such as in fault diagnostic, where recent events are more critical than older ones. So add ovewrite mode for bpf ring buffer. In this mode, the new event overwrites the oldest event when the buffer is full. The scheme is as follows: 1. producer_pos tracks the next position to write new data. When there is enough free space, producer simply moves producer_pos forward to make space for the new event. 2. To avoid waiting for consumer to free space when the buffer is full, a new variable overwrite_pos is introduced for producer. overwrite_pos tracks the next event to be overwritten (the oldest event committed) in the buffer. producer moves it forward to discard the oldest events when the buffer is full. 3. pending_pos tracks the oldest event under committing. producer ensures producers_pos never passes pending_pos when making space for new events. So multiple producers never write to the same position at the same time. 4. producer wakes up consumer every half a round ahead to give it a chance to retrieve data. However, for an overwrite-mode ring buffer, users typically only cares about the ring buffer snapshot before a fault occur= s. In this case, the producer should commit data with BPF_RB_NO_WAKEUP flag to avoid unnecessary wakeups. To make it clear, here are some example diagrams. 1. Let's say we have a ring buffer with size 4096. At first, {producer,overwrite,pending,consumer}_pos are all set to 0 0 512 1024 1536 2048 2560 3072 3584 = 4096 +----------------------------------------------------------------------= -+ | = | | = | | = | +----------------------------------------------------------------------= -+ ^ | | producer_pos =3D 0 overwrite_pos =3D 0 pending_pos =3D 0 consumer_pos =3D 0 2. Reserve event A, size 512. There is enough free space, so A is allocated at offset 0 and producer_= pos is moved to 512, the end of A. Since A is not submitted, the BUSY bit is set. 0 512 1024 1536 2048 2560 3072 3584 = 4096 +----------------------------------------------------------------------= -+ | | = | | A | = | | [BUSY] | = | +----------------------------------------------------------------------= -+ ^ ^ | | | | | producer_pos =3D 512 | overwrite_pos =3D 0 pending_pos =3D 0 consumer_pos =3D 0 3. Reserve event B, size 1024. B is allocated at offset 512 with BUSY bit set, and producer_pos is mov= ed to the end of B. 0 512 1024 1536 2048 2560 3072 3584 = 4096 +----------------------------------------------------------------------= -+ | | | = | | A | B | = | | [BUSY] | [BUSY] | = | +----------------------------------------------------------------------= -+ ^ ^ | | | | | producer_pos =3D 1536 | overwrite_pos =3D 0 pending_pos =3D 0 consumer_pos =3D 0 4. Reserve event C, size 2048. C is allocated at offset 1536 and producer_pos becomes 3584. 0 512 1024 1536 2048 2560 3072 3584 = 4096 +----------------------------------------------------------------------= -+ | | | | = | | A | B | C | = | | [BUSY] | [BUSY] | [BUSY] | = | +----------------------------------------------------------------------= -+ ^ ^ | | | | | producer_pos =3D 3= 584 | overwrite_pos =3D 0 pending_pos =3D 0 consumer_pos =3D 0 5. Submit event A. The BUSY bit of A is cleared. B becomes the oldest event under writing,= so pending_pos is moved to 512, the start of B. 0 512 1024 1536 2048 2560 3072 3584 = 4096 +----------------------------------------------------------------------= -+ | | | | = | | A | B | C | = | | | [BUSY] | [BUSY] | = | +----------------------------------------------------------------------= -+ ^ ^ ^ | | | | | | | pending_pos =3D 512 producer_pos = =3D 3584 | overwrite_pos =3D 0 consumer_pos =3D 0 6. Submit event B. The BUSY bit of B is cleared, and pending_pos is moved to the start of = C, which is the oldest event under writing now. 0 512 1024 1536 2048 2560 3072 3584 = 4096 +----------------------------------------------------------------------= -+ | | | | = | | A | B | C | = | | | | [BUSY] | = | +----------------------------------------------------------------------= -+ ^ ^ ^ | | | | | | | pending_pos =3D 1536 producer_pos = =3D 3584 | overwrite_pos =3D 0 consumer_pos =3D 0 7. Reserve event D, size 1536 (3 * 512). There are 2048 bytes not under writing between producer_pos and pending= _pos, so D is allocated at offset 3584, and producer_pos is moved from 3584 to 5120. Since event D will overwrite all bytes of event A and the begining 512 = bytes of event B, overwrite_pos is moved to the start of event C, the oldest = event that is not overwritten. 0 512 1024 1536 2048 2560 3072 3584 = 4096 +----------------------------------------------------------------------= -+ | | | | = | | D End | | C | D Begi= n| | [BUSY] | | [BUSY] | [BUSY]= | +----------------------------------------------------------------------= -+ ^ ^ ^ | | | | | pending_pos =3D 1536 | | overwrite_pos =3D 1536 | | | producer_pos=3D5120 | consumer_pos =3D 0 8. Reserve event E, size 1024. Though there are 512 bytes not under writing between producer_pos and pending_pos, E can not be reserved, as it would overwrite the first 512 bytes of event C, which is still under writing. 9. Submit event C and D. pending_pos is moved to the end of D. 0 512 1024 1536 2048 2560 3072 3584 = 4096 +----------------------------------------------------------------------= -+ | | | | = | | D End | | C | D Begi= n| | | | | = | +----------------------------------------------------------------------= -+ ^ ^ ^ | | | | | overwrite_pos =3D 1536 | | | producer_pos=3D5120 | pending_pos=3D5120 | consumer_pos =3D 0 The performance data for overwrite mode will be provided in a follow-up patch that adds overwrite mode benchs. A sample of performance data for non-overwrite mode on an x86_64 and arm64 CPU, before and after this patch, is shown below. As we can see, no obvious performance regression occurs. - x86_64 (AMD EPYC 9654) Before: Ringbuf, multi-producer contention =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D rb-libbpf nr_prod 1 13.218 =C2=B1 0.039M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 2 15.684 =C2=B1 0.015M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 3 7.771 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 4 6.281 =C2=B1 0.001M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 8 2.842 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 12 2.001 =C2=B1 0.004M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 16 1.833 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 20 1.508 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 24 1.421 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 28 1.309 =C2=B1 0.001M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 32 1.265 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 36 1.198 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 40 1.174 =C2=B1 0.001M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 44 1.113 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 48 1.097 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 52 1.070 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) After: Ringbuf, multi-producer contention =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D rb-libbpf nr_prod 1 13.751 =C2=B1 0.673M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 2 15.592 =C2=B1 0.008M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 3 7.776 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 4 6.463 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 8 2.883 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 12 2.017 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 16 1.816 =C2=B1 0.004M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 20 1.512 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 24 1.396 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 28 1.303 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 32 1.267 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 36 1.210 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 40 1.181 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 44 1.136 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 48 1.090 =C2=B1 0.001M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 52 1.091 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) - arm64 (HiSilicon Kunpeng 920) Before: Ringbuf, multi-producer contention =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D rb-libbpf nr_prod 1 11.602 =C2=B1 0.423M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 2 9.599 =C2=B1 0.007M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 3 6.669 =C2=B1 0.008M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 4 4.806 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 8 3.856 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 12 3.368 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 16 3.210 =C2=B1 0.007M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 20 3.003 =C2=B1 0.007M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 24 2.944 =C2=B1 0.007M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 28 2.863 =C2=B1 0.008M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 32 2.819 =C2=B1 0.007M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 36 2.887 =C2=B1 0.008M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 40 2.837 =C2=B1 0.008M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 44 2.787 =C2=B1 0.012M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 48 2.738 =C2=B1 0.010M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 52 2.700 =C2=B1 0.007M/s (drops 0.000 =C2=B1 0.000M/s) After: Ringbuf, multi-producer contention =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D rb-libbpf nr_prod 1 11.614 =C2=B1 0.268M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 2 9.917 =C2=B1 0.007M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 3 6.920 =C2=B1 0.008M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 4 4.803 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 8 3.898 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 12 3.426 =C2=B1 0.008M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 16 3.320 =C2=B1 0.008M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 20 3.029 =C2=B1 0.013M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 24 3.068 =C2=B1 0.012M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 28 2.890 =C2=B1 0.009M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 32 2.950 =C2=B1 0.012M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 36 2.812 =C2=B1 0.006M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 40 2.834 =C2=B1 0.009M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 44 2.803 =C2=B1 0.010M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 48 2.766 =C2=B1 0.010M/s (drops 0.000 =C2=B1 0.000M/s) rb-libbpf nr_prod 52 2.754 =C2=B1 0.009M/s (drops 0.000 =C2=B1 0.000M/s) Signed-off-by: Xu Kuohai --- include/uapi/linux/bpf.h | 4 + kernel/bpf/ringbuf.c | 159 +++++++++++++++++++++++++++------ tools/include/uapi/linux/bpf.h | 4 + 3 files changed, 141 insertions(+), 26 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 233de8677382..d3b2fd2ae527 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1430,6 +1430,9 @@ enum { =20 /* Do not translate kernel bpf_arena pointers to user pointers */ BPF_F_NO_USER_CONV =3D (1U << 18), + +/* bpf ringbuf works in overwrite mode? */ + BPF_F_OVERWRITE =3D (1U << 19), }; =20 /* Flags for BPF_PROG_QUERY. */ @@ -6215,6 +6218,7 @@ enum { BPF_RB_RING_SIZE =3D 1, BPF_RB_CONS_POS =3D 2, BPF_RB_PROD_POS =3D 3, + BPF_RB_OVER_POS =3D 4, }; =20 /* BPF ring buffer constants */ diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index 719d73299397..6ca41d01f187 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -13,7 +13,7 @@ #include #include =20 -#define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE) +#define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE | BPF_F_OVERWRITE) =20 /* non-mmap()'able part of bpf_ringbuf (everything up to consumer page) */ #define RINGBUF_PGOFF \ @@ -27,7 +27,8 @@ struct bpf_ringbuf { wait_queue_head_t waitq; struct irq_work work; - u64 mask; + u64 mask:48; + u64 overwrite_mode:1; struct page **pages; int nr_pages; rqspinlock_t spinlock ____cacheline_aligned_in_smp; @@ -72,6 +73,7 @@ struct bpf_ringbuf { */ unsigned long consumer_pos __aligned(PAGE_SIZE); unsigned long producer_pos __aligned(PAGE_SIZE); + unsigned long overwrite_pos; /* to be overwritten in overwrite mode */ unsigned long pending_pos; char data[] __aligned(PAGE_SIZE); }; @@ -166,7 +168,8 @@ static void bpf_ringbuf_notify(struct irq_work *work) * considering that the maximum value of data_sz is (4GB - 1), there * will be no overflow, so just note the size limit in the comments. */ -static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node) +static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node, + int overwrite_mode) { struct bpf_ringbuf *rb; =20 @@ -183,17 +186,25 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t d= ata_sz, int numa_node) rb->consumer_pos =3D 0; rb->producer_pos =3D 0; rb->pending_pos =3D 0; + rb->overwrite_mode =3D overwrite_mode; =20 return rb; } =20 static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr) { + int overwrite_mode =3D 0; struct bpf_ringbuf_map *rb_map; =20 if (attr->map_flags & ~RINGBUF_CREATE_FLAG_MASK) return ERR_PTR(-EINVAL); =20 + if (attr->map_flags & BPF_F_OVERWRITE) { + if (attr->map_type =3D=3D BPF_MAP_TYPE_USER_RINGBUF) + return ERR_PTR(-EINVAL); + overwrite_mode =3D 1; + } + if (attr->key_size || attr->value_size || !is_power_of_2(attr->max_entries) || !PAGE_ALIGNED(attr->max_entries)) @@ -205,7 +216,8 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr= *attr) =20 bpf_map_init_from_attr(&rb_map->map, attr); =20 - rb_map->rb =3D bpf_ringbuf_alloc(attr->max_entries, rb_map->map.numa_node= ); + rb_map->rb =3D bpf_ringbuf_alloc(attr->max_entries, rb_map->map.numa_node, + overwrite_mode); if (!rb_map->rb) { bpf_map_area_free(rb_map); return ERR_PTR(-ENOMEM); @@ -295,11 +307,16 @@ static int ringbuf_map_mmap_user(struct bpf_map *map,= struct vm_area_struct *vma =20 static unsigned long ringbuf_avail_data_sz(struct bpf_ringbuf *rb) { - unsigned long cons_pos, prod_pos; + unsigned long cons_pos, prod_pos, over_pos; =20 cons_pos =3D smp_load_acquire(&rb->consumer_pos); prod_pos =3D smp_load_acquire(&rb->producer_pos); - return prod_pos - cons_pos; + + if (likely(!rb->overwrite_mode)) + return prod_pos - cons_pos; + + over_pos =3D READ_ONCE(rb->overwrite_pos); + return min(prod_pos - max(cons_pos, over_pos), rb->mask + 1); } =20 static u32 ringbuf_total_data_sz(const struct bpf_ringbuf *rb) @@ -402,11 +419,43 @@ bpf_ringbuf_restore_from_rec(struct bpf_ringbuf_hdr *= hdr) return (void*)((addr & PAGE_MASK) - off); } =20 + +static bool bpf_ringbuf_has_space(const struct bpf_ringbuf *rb, + unsigned long new_prod_pos, + unsigned long cons_pos, + unsigned long pend_pos) +{ + /* no space if oldest not yet committed record until the newest + * record span more than (ringbuf_size - 1) + */ + if (new_prod_pos - pend_pos > rb->mask) + return false; + + /* ok, we have space in ovewrite mode */ + if (unlikely(rb->overwrite_mode)) + return true; + + /* no space if producer position advances more than (ringbuf_size - 1) + * ahead than consumer position when not in overwrite mode + */ + if (new_prod_pos - cons_pos > rb->mask) + return false; + + return true; +} + +static u32 ringbuf_round_up_hdr_len(u32 hdr_len) +{ + hdr_len &=3D ~BPF_RINGBUF_DISCARD_BIT; + return round_up(hdr_len + BPF_RINGBUF_HDR_SZ, 8); +} + static void *__bpf_ringbuf_reserve(struct bpf_ringbuf *rb, u64 size) { - unsigned long cons_pos, prod_pos, new_prod_pos, pend_pos, flags; + unsigned long flags; struct bpf_ringbuf_hdr *hdr; - u32 len, pg_off, tmp_size, hdr_len; + u32 len, pg_off, hdr_len; + unsigned long cons_pos, prod_pos, new_prod_pos, pend_pos, over_pos; =20 if (unlikely(size > RINGBUF_MAX_RECORD_SZ)) return NULL; @@ -429,24 +478,39 @@ static void *__bpf_ringbuf_reserve(struct bpf_ringbuf= *rb, u64 size) hdr_len =3D READ_ONCE(hdr->len); if (hdr_len & BPF_RINGBUF_BUSY_BIT) break; - tmp_size =3D hdr_len & ~BPF_RINGBUF_DISCARD_BIT; - tmp_size =3D round_up(tmp_size + BPF_RINGBUF_HDR_SZ, 8); - pend_pos +=3D tmp_size; + pend_pos +=3D ringbuf_round_up_hdr_len(hdr_len); } rb->pending_pos =3D pend_pos; =20 - /* check for out of ringbuf space: - * - by ensuring producer position doesn't advance more than - * (ringbuf_size - 1) ahead - * - by ensuring oldest not yet committed record until newest - * record does not span more than (ringbuf_size - 1) - */ - if (new_prod_pos - cons_pos > rb->mask || - new_prod_pos - pend_pos > rb->mask) { + if (!bpf_ringbuf_has_space(rb, new_prod_pos, cons_pos, pend_pos)) { raw_res_spin_unlock_irqrestore(&rb->spinlock, flags); return NULL; } =20 + /* In overwrite mode, move overwrite_pos to the next record to be + * overwritten if the ring buffer is full + */ + if (unlikely(rb->overwrite_mode)) { + over_pos =3D rb->overwrite_pos; + while (new_prod_pos - over_pos > rb->mask) { + hdr =3D (void *)rb->data + (over_pos & rb->mask); + hdr_len =3D READ_ONCE(hdr->len); + /* since pending_pos is the first record with BUSY + * bit set and overwrite_pos is never bigger than + * pending_pos, no need to check BUSY bit here. + */ + over_pos +=3D ringbuf_round_up_hdr_len(hdr_len); + } + /* smp_store_release(&rb->producer_pos, new_prod_pos) at + * the end of the function ensures that when consumer sees + * the updated rb->producer_pos, it always sees the updated + * rb->overwrite_pos, so when consumer reads overwrite_pos + * after smp_load_acquire(r->producer_pos), the overwrite_pos + * will always be valid. + */ + WRITE_ONCE(rb->overwrite_pos, over_pos); + } + hdr =3D (void *)rb->data + (prod_pos & rb->mask); pg_off =3D bpf_ringbuf_rec_pg_off(rb, hdr); hdr->len =3D size | BPF_RINGBUF_BUSY_BIT; @@ -479,7 +543,50 @@ const struct bpf_func_proto bpf_ringbuf_reserve_proto = =3D { .arg3_type =3D ARG_ANYTHING, }; =20 -static void bpf_ringbuf_commit(void *sample, u64 flags, bool discard) +static __always_inline +bool ringbuf_should_wakeup(const struct bpf_ringbuf *rb, + unsigned long rec_pos, + unsigned long cons_pos, + u32 len, u64 flags) +{ + unsigned long rec_end; + + if (flags & BPF_RB_FORCE_WAKEUP) + return true; + + if (flags & BPF_RB_NO_WAKEUP) + return false; + + /* for non-overwrite mode, if consumer caught up and is waiting for + * our record, notify about new data availability + */ + if (likely(!rb->overwrite_mode)) + return cons_pos =3D=3D rec_pos; + + /* for overwrite mode, to give the consumer a chance to catch up + * before being overwritten, wake up consumer every half a round + * ahead. + */ + rec_end =3D rec_pos + ringbuf_round_up_hdr_len(len); + + cons_pos &=3D (rb->mask >> 1); + rec_pos &=3D (rb->mask >> 1); + rec_end &=3D (rb->mask >> 1); + + if (cons_pos =3D=3D rec_pos) + return true; + + if (rec_pos < cons_pos && cons_pos < rec_end) + return true; + + if (rec_end < rec_pos && (cons_pos > rec_pos || cons_pos < rec_end)) + return true; + + return false; +} + +static __always_inline +void bpf_ringbuf_commit(void *sample, u64 flags, bool discard) { unsigned long rec_pos, cons_pos; struct bpf_ringbuf_hdr *hdr; @@ -495,15 +602,10 @@ static void bpf_ringbuf_commit(void *sample, u64 flag= s, bool discard) /* update record header with correct final size prefix */ xchg(&hdr->len, new_len); =20 - /* if consumer caught up and is waiting for our record, notify about - * new data availability - */ rec_pos =3D (void *)hdr - (void *)rb->data; cons_pos =3D smp_load_acquire(&rb->consumer_pos) & rb->mask; =20 - if (flags & BPF_RB_FORCE_WAKEUP) - irq_work_queue(&rb->work); - else if (cons_pos =3D=3D rec_pos && !(flags & BPF_RB_NO_WAKEUP)) + if (ringbuf_should_wakeup(rb, rec_pos, cons_pos, new_len, flags)) irq_work_queue(&rb->work); } =20 @@ -576,6 +678,8 @@ BPF_CALL_2(bpf_ringbuf_query, struct bpf_map *, map, u6= 4, flags) return smp_load_acquire(&rb->consumer_pos); case BPF_RB_PROD_POS: return smp_load_acquire(&rb->producer_pos); + case BPF_RB_OVER_POS: + return READ_ONCE(rb->overwrite_pos); default: return 0; } @@ -749,6 +853,9 @@ BPF_CALL_4(bpf_user_ringbuf_drain, struct bpf_map *, ma= p, =20 rb =3D container_of(map, struct bpf_ringbuf_map, map)->rb; =20 + if (unlikely(rb->overwrite_mode)) + return -EOPNOTSUPP; + /* If another consumer is already consuming a sample, wait for them to fi= nish. */ if (!atomic_try_cmpxchg(&rb->busy, &busy, 1)) return -EBUSY; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 233de8677382..d3b2fd2ae527 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1430,6 +1430,9 @@ enum { =20 /* Do not translate kernel bpf_arena pointers to user pointers */ BPF_F_NO_USER_CONV =3D (1U << 18), + +/* bpf ringbuf works in overwrite mode? */ + BPF_F_OVERWRITE =3D (1U << 19), }; =20 /* Flags for BPF_PROG_QUERY. */ @@ -6215,6 +6218,7 @@ enum { BPF_RB_RING_SIZE =3D 1, BPF_RB_CONS_POS =3D 2, BPF_RB_PROD_POS =3D 3, + BPF_RB_OVER_POS =3D 4, }; =20 /* BPF ring buffer constants */ --=20 2.43.0 From nobody Mon Feb 9 16:32:03 2026 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 4580F31B809; Fri, 5 Sep 2025 15:13:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757085206; cv=none; b=Rkci3H11Bs037lhT5Z8MwlT/YneLqTtWNusYATovlL6RpE3sk/JAuG+QX/0IjdBC7/R16hzoomcgvq0tcduuuTw/XdHD3A2Apfi6Ts/XlWZi/WWBqr7eX/CqAbufqXomIbhh+h5zEHybE1PPgoAdJCNB4uciQV71HAaBAxI7kY8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757085206; c=relaxed/simple; bh=735ErX0nXQAIiqsum52DgprUGZicLHAW4DORG/hNMwg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sYrP3prXtusJiJqUt6oH4tiknxGsB9bw0As5RwK9mhq8o2Fq45zqE2WANiRHrZ26G6MkNl4TuFnV4tfp+cw3wXVqCrIXVQlQI4dKgyaRFepLoW0Jnx64gcRwoWNTlS4m8zx4j4s6aP2UeFmENx+hATYH9xwC1RTB3WnvydFvNvM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4cJKbw58TzzYQvLp; Fri, 5 Sep 2025 23:13:16 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 334CD1A0F15; Fri, 5 Sep 2025 23:13:15 +0800 (CST) Received: from k-arm6401.huawei.com (unknown [7.217.19.243]) by APP4 (Coremail) with SMTP id gCh0CgDnfYoF_rpoPC72BQ--.54678S4; Fri, 05 Sep 2025 23:13:14 +0800 (CST) From: Xu Kuohai To: bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Stanislav Fomichev , Willem de Bruijn , Jason Xing , Paul Chaignon , Tao Chen , Kumar Kartikeya Dwivedi , Martin Kelly Subject: [PATCH bpf-next v2 2/3] selftests/bpf: Add test for overwrite ring buffer Date: Fri, 5 Sep 2025 23:06:40 +0800 Message-ID: <20250905150641.2078838-3-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250905150641.2078838-1-xukuohai@huaweicloud.com> References: <20250905150641.2078838-1-xukuohai@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgDnfYoF_rpoPC72BQ--.54678S4 X-Coremail-Antispam: 1UD129KBjvJXoW3AFykGFyrGr4ktF15GF4DJwb_yoW3GrWxpa yFgr1YkryI93WFgrWxuFyIvFW8ur4DAw4rKrsrXw1rZr1DuFsxXr1Ikr1Ut3Z8XrW8Xr1Y k34Y9FZ3A3WUGF7anT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQ2b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2AFwI 0_GFv_Wrylc7CjxVAKzI0EY4vE52x082I5MxkIecxEwVCI4VW5WwCF04k20xvY0x0EwIxG rwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4 vE14v26r106r1rMI8E67AF67kF1VAFwI0_GFv_WrylIxkGc2Ij64vIr41lIxAIcVC0I7IY x2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8V AvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E 14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUhNB_UUUUU X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ Content-Type: text/plain; charset="utf-8" From: Xu Kuohai Add test for overwiret mode ring buffer. The test creates a bpf ring buffer in overwrite mode, then repeatlly reserves and commits data to check if the ring buffer works as expected both before and after overwrite happens. Signed-off-by: Xu Kuohai --- tools/testing/selftests/bpf/Makefile | 3 +- .../selftests/bpf/prog_tests/ringbuf.c | 74 ++++++++++++++ .../bpf/progs/test_ringbuf_overwrite.c | 98 +++++++++++++++++++ 3 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_ringbuf_overwrit= e.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests= /bpf/Makefile index 11d2a368db3e..e6c18e201555 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -499,7 +499,8 @@ LINKED_SKELS :=3D test_static_linked.skel.h linked_func= s.skel.h \ LSKELS :=3D fentry_test.c fexit_test.c fexit_sleep.c atomics.c \ trace_printk.c trace_vprintk.c map_ptr_kern.c \ core_kern.c core_kern_overflow.c test_ringbuf.c \ - test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c + test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c \ + test_ringbuf_overwrite.c =20 # Generate both light skeleton and libbpf skeleton for these LSKELS_EXTRA :=3D test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \ diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testi= ng/selftests/bpf/prog_tests/ringbuf.c index d1e4cb28a72c..205a51c725a7 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c @@ -17,6 +17,7 @@ #include "test_ringbuf_n.lskel.h" #include "test_ringbuf_map_key.lskel.h" #include "test_ringbuf_write.lskel.h" +#include "test_ringbuf_overwrite.lskel.h" =20 #define EDONE 7777 =20 @@ -497,6 +498,77 @@ static void ringbuf_map_key_subtest(void) test_ringbuf_map_key_lskel__destroy(skel_map_key); } =20 +static void ringbuf_overwrite_mode_subtest(void) +{ + unsigned long size, len1, len2, len3, len4, len5; + unsigned long expect_avail_data, expect_prod_pos, expect_over_pos; + struct test_ringbuf_overwrite_lskel *skel; + int err; + + skel =3D test_ringbuf_overwrite_lskel__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + size =3D 0x1000; + len1 =3D 0x800; + len2 =3D 0x400; + len3 =3D size - len1 - len2 - BPF_RINGBUF_HDR_SZ * 3; /* 0x3e8 */ + len4 =3D len3 - 8; /* 0x3e0 */ + len5 =3D len3; /* retry with len3 */ + + skel->maps.ringbuf.max_entries =3D size; + skel->rodata->LEN1 =3D len1; + skel->rodata->LEN2 =3D len2; + skel->rodata->LEN3 =3D len3; + skel->rodata->LEN4 =3D len4; + skel->rodata->LEN5 =3D len5; + + skel->bss->pid =3D getpid(); + + err =3D test_ringbuf_overwrite_lskel__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto cleanup; + + err =3D test_ringbuf_overwrite_lskel__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto cleanup; + + syscall(__NR_getpgid); + + ASSERT_EQ(skel->bss->reserve1_fail, 0, "reserve 1"); + ASSERT_EQ(skel->bss->reserve2_fail, 0, "reserve 2"); + ASSERT_EQ(skel->bss->reserve3_fail, 1, "reserve 3"); + ASSERT_EQ(skel->bss->reserve4_fail, 0, "reserve 4"); + ASSERT_EQ(skel->bss->reserve5_fail, 0, "reserve 5"); + + CHECK(skel->bss->ring_size !=3D size, + "check_ring_size", "exp %lu, got %lu\n", + size, skel->bss->ring_size); + + expect_avail_data =3D len2 + len4 + len5 + 3 * BPF_RINGBUF_HDR_SZ; + CHECK(skel->bss->avail_data !=3D expect_avail_data, + "check_avail_size", "exp %lu, got %lu\n", + expect_avail_data, skel->bss->avail_data); + + CHECK(skel->bss->cons_pos !=3D 0, + "check_cons_pos", "exp 0, got %lu\n", + skel->bss->cons_pos); + + expect_prod_pos =3D len1 + len2 + len4 + len5 + 4 * BPF_RINGBUF_HDR_SZ; + CHECK(skel->bss->prod_pos !=3D expect_prod_pos, + "check_prod_pos", "exp %lu, got %lu\n", + expect_prod_pos, skel->bss->prod_pos); + + expect_over_pos =3D len1 + BPF_RINGBUF_HDR_SZ; + CHECK(skel->bss->over_pos !=3D expect_over_pos, + "check_over_pos", "exp %lu, got %lu\n", + (unsigned long)expect_over_pos, skel->bss->over_pos); + + test_ringbuf_overwrite_lskel__detach(skel); +cleanup: + test_ringbuf_overwrite_lskel__destroy(skel); +} + void test_ringbuf(void) { if (test__start_subtest("ringbuf")) @@ -507,4 +579,6 @@ void test_ringbuf(void) ringbuf_map_key_subtest(); if (test__start_subtest("ringbuf_write")) ringbuf_write_subtest(); + if (test__start_subtest("ringbuf_overwrite_mode")) + ringbuf_overwrite_mode_subtest(); } diff --git a/tools/testing/selftests/bpf/progs/test_ringbuf_overwrite.c b/t= ools/testing/selftests/bpf/progs/test_ringbuf_overwrite.c new file mode 100644 index 000000000000..da89ba12a75c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_ringbuf_overwrite.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2025. Huawei Technologies Co., Ltd */ + +#include +#include +#include "bpf_misc.h" + +char _license[] SEC("license") =3D "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(map_flags, BPF_F_OVERWRITE); +} ringbuf SEC(".maps"); + +int pid; + +const volatile unsigned long LEN1; +const volatile unsigned long LEN2; +const volatile unsigned long LEN3; +const volatile unsigned long LEN4; +const volatile unsigned long LEN5; + +long reserve1_fail =3D 0; +long reserve2_fail =3D 0; +long reserve3_fail =3D 0; +long reserve4_fail =3D 0; +long reserve5_fail =3D 0; + +unsigned long avail_data =3D 0; +unsigned long ring_size =3D 0; +unsigned long cons_pos =3D 0; +unsigned long prod_pos =3D 0; +unsigned long over_pos =3D 0; + +SEC("fentry/" SYS_PREFIX "sys_getpgid") +int test_overwrite_ringbuf(void *ctx) +{ + char *rec1, *rec2, *rec3, *rec4, *rec5; + int cur_pid =3D bpf_get_current_pid_tgid() >> 32; + + if (cur_pid !=3D pid) + return 0; + + rec1 =3D bpf_ringbuf_reserve(&ringbuf, LEN1, 0); + if (!rec1) { + reserve1_fail =3D 1; + return 0; + } + + rec2 =3D bpf_ringbuf_reserve(&ringbuf, LEN2, 0); + if (!rec2) { + bpf_ringbuf_discard(rec1, 0); + reserve2_fail =3D 1; + return 0; + } + + rec3 =3D bpf_ringbuf_reserve(&ringbuf, LEN3, 0); + /* expect failure */ + if (!rec3) { + reserve3_fail =3D 1; + } else { + bpf_ringbuf_discard(rec1, 0); + bpf_ringbuf_discard(rec2, 0); + bpf_ringbuf_discard(rec3, 0); + return 0; + } + + rec4 =3D bpf_ringbuf_reserve(&ringbuf, LEN4, 0); + if (!rec4) { + reserve4_fail =3D 1; + bpf_ringbuf_discard(rec1, 0); + bpf_ringbuf_discard(rec2, 0); + return 0; + } + + bpf_ringbuf_submit(rec1, 0); + bpf_ringbuf_submit(rec2, 0); + bpf_ringbuf_submit(rec4, 0); + + rec5 =3D bpf_ringbuf_reserve(&ringbuf, LEN5, 0); + if (!rec5) { + reserve5_fail =3D 1; + return 0; + } + + for (int i =3D 0; i < LEN3; i++) + rec5[i] =3D 0xdd; + + bpf_ringbuf_submit(rec5, 0); + + ring_size =3D bpf_ringbuf_query(&ringbuf, BPF_RB_RING_SIZE); + avail_data =3D bpf_ringbuf_query(&ringbuf, BPF_RB_AVAIL_DATA); + cons_pos =3D bpf_ringbuf_query(&ringbuf, BPF_RB_CONS_POS); + prod_pos =3D bpf_ringbuf_query(&ringbuf, BPF_RB_PROD_POS); + over_pos =3D bpf_ringbuf_query(&ringbuf, BPF_RB_OVER_POS); + + return 0; +} --=20 2.43.0 From nobody Mon Feb 9 16:32:03 2026 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (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 8C59831B81B; Fri, 5 Sep 2025 15:13:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757085208; cv=none; b=kCMqvxyCXe0ldVmISj5oz1IhkcUpBqegczQeZU8dkw7/PBS0+qbx6igH8BpueGLY4nUJtBaA/aIGb2B5hIOvoIvX8pjtlWDV4r1MFx8bbDjltyIcKI6ofBgqy1vHPA3PjYdsCGsm4c58sIgQutTgeovQrGxYziqrjEhH1lHpByA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757085208; c=relaxed/simple; bh=9BUY8LC7egcs0Sdjx0g7bGJ6SO0jrcYCrMB/R/TLrJk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=b01P77v+Kbe2BcZU2GvDzbz5DZMx70pqcCPverTRyFL4zMrwRYMUCeQhBiuIXbKycarAY2rv7KcrD0Q7fgGVOp6P57z6YnDdUvdGYHr0aOEq6AMjkEpxJkYkbOQAMF24YMrOTO136TaAD5CJP7q2nM2xP9Aj6+By0MrE/ZA5WKA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4cJKbx4htmzYQvNb; Fri, 5 Sep 2025 23:13:17 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 2790D1A0F86; Fri, 5 Sep 2025 23:13:16 +0800 (CST) Received: from k-arm6401.huawei.com (unknown [7.217.19.243]) by APP4 (Coremail) with SMTP id gCh0CgDnfYoF_rpoPC72BQ--.54678S5; Fri, 05 Sep 2025 23:13:15 +0800 (CST) From: Xu Kuohai To: bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Yonghong Song , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Stanislav Fomichev , Willem de Bruijn , Jason Xing , Paul Chaignon , Tao Chen , Kumar Kartikeya Dwivedi , Martin Kelly Subject: [PATCH bpf-next v2 3/3] selftests/bpf/benchs: Add producer and overwrite bench for ring buffer Date: Fri, 5 Sep 2025 23:06:41 +0800 Message-ID: <20250905150641.2078838-4-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250905150641.2078838-1-xukuohai@huaweicloud.com> References: <20250905150641.2078838-1-xukuohai@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: gCh0CgDnfYoF_rpoPC72BQ--.54678S5 X-Coremail-Antispam: 1UD129KBjvJXoWfJw15ZF4UZw18uFW5Jw4DCFg_yoWkCw1fpa 93KrWfCF1fAryfWFyvk3yxAry7AwnrZ3W5Cr1xJ3WUZw1UWa1jqr1Ika4jy3W5GrW0yr1S 9a4ktry09w4YywUanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUQIb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUWw A2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1lc7CjxVAaw2AFwI 0_GFv_Wrylc7CjxVAKzI0EY4vE52x082I5MxkIecxEwVCI4VW5WwCF04k20xvY0x0EwIxG rwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4 vE14v26r106r1rMI8E67AF67kF1VAFwI0_GFv_WrylIxkGc2Ij64vIr41lIxAIcVC0I7IY x2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8V AvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E 14v26r4UJVWxJrUvcSsGvfC2KfnxnUUI43ZEXa7IUed9N3UUUUU== X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ From: Xu Kuohai Add rb-prod test for bpf ring buffer to bench producer performance without counsumer thread. And add --rb-overwrite option to bench ring buffer in overwrite mode. For reference, below are bench numbers collected from x86_64 and arm64 CPUs. - AMD EPYC 9654 (x86_64) Ringbuf, overwrite mode with multi-producer contention, no consumer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D rb-prod nr_prod 1 32.295 =C2=B1 0.004M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 2 9.591 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 3 8.895 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 4 9.206 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 8 9.220 =C2=B1 0.002M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 12 4.595 =C2=B1 0.022M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 16 4.348 =C2=B1 0.016M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 20 3.957 =C2=B1 0.017M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 24 3.787 =C2=B1 0.014M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 28 3.603 =C2=B1 0.011M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 32 3.707 =C2=B1 0.011M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 36 3.562 =C2=B1 0.012M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 40 3.616 =C2=B1 0.012M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 44 3.598 =C2=B1 0.016M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 48 3.555 =C2=B1 0.014M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 52 3.463 =C2=B1 0.020M/s (drops 0.000 =C2=B1 0.000M/s) - HiSilicon Kunpeng 920 (arm64) Ringbuf, overwrite mode with multi-producer contention, no consumer =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D rb-prod nr_prod 1 14.687 =C2=B1 0.058M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 2 22.263 =C2=B1 0.007M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 3 5.736 =C2=B1 0.003M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 4 4.934 =C2=B1 0.001M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 8 4.661 =C2=B1 0.001M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 12 3.753 =C2=B1 0.013M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 16 3.706 =C2=B1 0.018M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 20 3.660 =C2=B1 0.015M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 24 3.610 =C2=B1 0.016M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 28 3.238 =C2=B1 0.010M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 32 3.270 =C2=B1 0.018M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 36 2.892 =C2=B1 0.021M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 40 2.995 =C2=B1 0.018M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 44 2.830 =C2=B1 0.019M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 48 2.877 =C2=B1 0.015M/s (drops 0.000 =C2=B1 0.000M/s) rb-prod nr_prod 52 2.814 =C2=B1 0.015M/s (drops 0.000 =C2=B1 0.000M/s) Signed-off-by: Xu Kuohai --- tools/testing/selftests/bpf/bench.c | 2 + .../selftests/bpf/benchs/bench_ringbufs.c | 95 +++++++++++++++++-- .../bpf/benchs/run_bench_ringbufs.sh | 4 + .../selftests/bpf/progs/ringbuf_bench.c | 10 ++ 4 files changed, 103 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/= bpf/bench.c index bd29bb2e6cb5..a98063f6436a 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -541,6 +541,7 @@ extern const struct bench bench_trig_uretprobe_multi_no= p5; =20 extern const struct bench bench_rb_libbpf; extern const struct bench bench_rb_custom; +extern const struct bench bench_rb_prod; extern const struct bench bench_pb_libbpf; extern const struct bench bench_pb_custom; extern const struct bench bench_bloom_lookup; @@ -617,6 +618,7 @@ static const struct bench *benchs[] =3D { /* ringbuf/perfbuf benchmarks */ &bench_rb_libbpf, &bench_rb_custom, + &bench_rb_prod, &bench_pb_libbpf, &bench_pb_custom, &bench_bloom_lookup, diff --git a/tools/testing/selftests/bpf/benchs/bench_ringbufs.c b/tools/te= sting/selftests/bpf/benchs/bench_ringbufs.c index e1ee979e6acc..6d58479fac91 100644 --- a/tools/testing/selftests/bpf/benchs/bench_ringbufs.c +++ b/tools/testing/selftests/bpf/benchs/bench_ringbufs.c @@ -19,6 +19,7 @@ static struct { int ringbuf_sz; /* per-ringbuf, in bytes */ bool ringbuf_use_output; /* use slower output API */ int perfbuf_sz; /* per-CPU size, in pages */ + bool overwrite; } args =3D { .back2back =3D false, .batch_cnt =3D 500, @@ -27,6 +28,7 @@ static struct { .ringbuf_sz =3D 512 * 1024, .ringbuf_use_output =3D false, .perfbuf_sz =3D 128, + .overwrite =3D false, }; =20 enum { @@ -35,6 +37,7 @@ enum { ARG_RB_BATCH_CNT =3D 2002, ARG_RB_SAMPLED =3D 2003, ARG_RB_SAMPLE_RATE =3D 2004, + ARG_RB_OVERWRITE =3D 2005, }; =20 static const struct argp_option opts[] =3D { @@ -43,6 +46,7 @@ static const struct argp_option opts[] =3D { { "rb-batch-cnt", ARG_RB_BATCH_CNT, "CNT", 0, "Set BPF-side record batch = count"}, { "rb-sampled", ARG_RB_SAMPLED, NULL, 0, "Notification sampling"}, { "rb-sample-rate", ARG_RB_SAMPLE_RATE, "RATE", 0, "Notification sample r= ate"}, + { "rb-overwrite", ARG_RB_OVERWRITE, NULL, 0, "Overwrite mode"}, {}, }; =20 @@ -72,6 +76,9 @@ static error_t parse_arg(int key, char *arg, struct argp_= state *state) argp_usage(state); } break; + case ARG_RB_OVERWRITE: + args.overwrite =3D true; + break; default: return ARGP_ERR_UNKNOWN; } @@ -95,8 +102,30 @@ static inline void bufs_trigger_batch(void) =20 static void bufs_validate(void) { - if (env.consumer_cnt !=3D 1) { - fprintf(stderr, "rb-libbpf benchmark needs one consumer!\n"); + bool bench_prod =3D !strcmp(env.bench_name, "rb-prod"); + + if (args.overwrite && !bench_prod) { + fprintf(stderr, "overwite mode only works with benchmakr rb-prod!\n"); + exit(1); + } + + if (bench_prod && env.consumer_cnt !=3D 0) { + fprintf(stderr, "rb-prod benchmark does not need consumer!\n"); + exit(1); + } + + if (bench_prod && args.back2back) { + fprintf(stderr, "back-to-back mode makes no sense for rb-prod!\n"); + exit(1); + } + + if (bench_prod && args.sampled) { + fprintf(stderr, "sampling mode makes no sense for rb-prod!\n"); + exit(1); + } + + if (!bench_prod && env.consumer_cnt !=3D 1) { + fprintf(stderr, "benchmarks excluding rb-prod need one consumer!\n"); exit(1); } =20 @@ -132,8 +161,10 @@ static void ringbuf_libbpf_measure(struct bench_res *r= es) res->drops =3D atomic_swap(&ctx->skel->bss->dropped, 0); } =20 -static struct ringbuf_bench *ringbuf_setup_skeleton(void) +static struct ringbuf_bench *ringbuf_setup_skeleton(int bench_prod) { + __u32 flags; + struct bpf_map *ringbuf; struct ringbuf_bench *skel; =20 setup_libbpf(); @@ -146,12 +177,19 @@ static struct ringbuf_bench *ringbuf_setup_skeleton(v= oid) =20 skel->rodata->batch_cnt =3D args.batch_cnt; skel->rodata->use_output =3D args.ringbuf_use_output ? 1 : 0; + skel->rodata->bench_prod =3D bench_prod; =20 if (args.sampled) /* record data + header take 16 bytes */ skel->rodata->wakeup_data_size =3D args.sample_rate * 16; =20 - bpf_map__set_max_entries(skel->maps.ringbuf, args.ringbuf_sz); + ringbuf =3D skel->maps.ringbuf; + if (args.overwrite) { + flags =3D bpf_map__map_flags(ringbuf) | BPF_F_OVERWRITE; + bpf_map__set_map_flags(ringbuf, flags); + } + + bpf_map__set_max_entries(ringbuf, args.ringbuf_sz); =20 if (ringbuf_bench__load(skel)) { fprintf(stderr, "failed to load skeleton\n"); @@ -171,10 +209,13 @@ static void ringbuf_libbpf_setup(void) { struct ringbuf_libbpf_ctx *ctx =3D &ringbuf_libbpf_ctx; struct bpf_link *link; + int map_fd; =20 - ctx->skel =3D ringbuf_setup_skeleton(); - ctx->ringbuf =3D ring_buffer__new(bpf_map__fd(ctx->skel->maps.ringbuf), - buf_process_sample, NULL, NULL); + ctx->skel =3D ringbuf_setup_skeleton(0); + + map_fd =3D bpf_map__fd(ctx->skel->maps.ringbuf); + ctx->ringbuf =3D ring_buffer__new(map_fd, buf_process_sample, + NULL, NULL); if (!ctx->ringbuf) { fprintf(stderr, "failed to create ringbuf\n"); exit(1); @@ -232,7 +273,7 @@ static void ringbuf_custom_setup(void) void *tmp; int err; =20 - ctx->skel =3D ringbuf_setup_skeleton(); + ctx->skel =3D ringbuf_setup_skeleton(0); =20 ctx->epoll_fd =3D epoll_create1(EPOLL_CLOEXEC); if (ctx->epoll_fd < 0) { @@ -277,6 +318,33 @@ static void ringbuf_custom_setup(void) } } =20 +/* RINGBUF-PRODUCER benchmark */ +static struct ringbuf_prod_ctx { + struct ringbuf_bench *skel; +} ringbuf_prod_ctx; + +static void ringbuf_prod_measure(struct bench_res *res) +{ + struct ringbuf_prod_ctx *ctx =3D &ringbuf_prod_ctx; + + res->hits =3D atomic_swap(&ctx->skel->bss->hits, 0); + res->drops =3D atomic_swap(&ctx->skel->bss->dropped, 0); +} + +static void ringbuf_prod_setup(void) +{ + struct ringbuf_prod_ctx *ctx =3D &ringbuf_prod_ctx; + struct bpf_link *link; + + ctx->skel =3D ringbuf_setup_skeleton(1); + + link =3D bpf_program__attach(ctx->skel->progs.bench_ringbuf); + if (!link) { + fprintf(stderr, "failed to attach program!\n"); + exit(1); + } +} + #define RINGBUF_BUSY_BIT (1 << 31) #define RINGBUF_DISCARD_BIT (1 << 30) #define RINGBUF_META_LEN 8 @@ -540,6 +608,17 @@ const struct bench bench_rb_custom =3D { .report_final =3D hits_drops_report_final, }; =20 +const struct bench bench_rb_prod =3D { + .name =3D "rb-prod", + .argp =3D &bench_ringbufs_argp, + .validate =3D bufs_validate, + .setup =3D ringbuf_prod_setup, + .producer_thread =3D bufs_sample_producer, + .measure =3D ringbuf_prod_measure, + .report_progress =3D hits_drops_report_progress, + .report_final =3D hits_drops_report_final, +}; + const struct bench bench_pb_libbpf =3D { .name =3D "pb-libbpf", .argp =3D &bench_ringbufs_argp, diff --git a/tools/testing/selftests/bpf/benchs/run_bench_ringbufs.sh b/too= ls/testing/selftests/bpf/benchs/run_bench_ringbufs.sh index 91e3567962ff..84ae66beb0ec 100755 --- a/tools/testing/selftests/bpf/benchs/run_bench_ringbufs.sh +++ b/tools/testing/selftests/bpf/benchs/run_bench_ringbufs.sh @@ -49,3 +49,7 @@ for b in 1 2 3 4 8 12 16 20 24 28 32 36 40 44 48 52; do summarize "rb-libbpf nr_prod $b" "$($RUN_RB_BENCH -p$b --rb-batch-cnt 50 = rb-libbpf)" done =20 +header "Ringbuf, overwrite mode with multi-producer contention, no consume= r" +for b in 1 2 3 4 8 12 16 20 24 28 32 36 40 44 48 52; do + summarize "rb-prod nr_prod $b" "$($RUN_BENCH -p$b --rb-batch-cnt 50 --rb-= overwrite rb-prod)" +done diff --git a/tools/testing/selftests/bpf/progs/ringbuf_bench.c b/tools/test= ing/selftests/bpf/progs/ringbuf_bench.c index 6a468496f539..c55282ba4038 100644 --- a/tools/testing/selftests/bpf/progs/ringbuf_bench.c +++ b/tools/testing/selftests/bpf/progs/ringbuf_bench.c @@ -14,9 +14,11 @@ struct { =20 const volatile int batch_cnt =3D 0; const volatile long use_output =3D 0; +const volatile long bench_prod =3D 0; =20 long sample_val =3D 42; long dropped __attribute__((aligned(128))) =3D 0; +long hits __attribute__((aligned(128))) =3D 0; =20 const volatile long wakeup_data_size =3D 0; =20 @@ -24,6 +26,9 @@ static __always_inline long get_flags() { long sz; =20 + if (bench_prod) + return BPF_RB_NO_WAKEUP; + if (!wakeup_data_size) return 0; =20 @@ -47,6 +52,8 @@ int bench_ringbuf(void *ctx) *sample =3D sample_val; flags =3D get_flags(); bpf_ringbuf_submit(sample, flags); + if (bench_prod) + __sync_add_and_fetch(&hits, 1); } } } else { @@ -55,6 +62,9 @@ int bench_ringbuf(void *ctx) if (bpf_ringbuf_output(&ringbuf, &sample_val, sizeof(sample_val), flags)) __sync_add_and_fetch(&dropped, 1); + else if (bench_prod) + __sync_add_and_fetch(&hits, 1); + } } return 0; --=20 2.43.0