From nobody Sun Feb 8 14:53:03 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41E80EB64DC for ; Mon, 17 Jul 2023 11:42:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230198AbjGQLmO (ORCPT ); Mon, 17 Jul 2023 07:42:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229982AbjGQLmI (ORCPT ); Mon, 17 Jul 2023 07:42:08 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2D0DE56 for ; Mon, 17 Jul 2023 04:42:02 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-3fbf7fbe722so44804945e9.3 for ; Mon, 17 Jul 2023 04:42:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689594121; x=1692186121; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gC3kRXuJb2CDsNb5ofLQfMx3YTx5F9ozdQp3kszCIv0=; b=Auzfuff/BXSj3l/XWHw4L7DXLokGkrcuPtT6rumBdt+l+AJreyG8pQNlDW9mrJ/zuY RD9MbUDYTSGtVNNDwApPqj6652bN9tKXBxFpEo226OSGd3r+c1QM/TVnwM2lfDqeR79v VOHvOTUc1g2XyLYH3A/P3AkxocgTuTlfSp8UqinTrNGIvj8If28VBbtGFBps7MN2rEhN iyiTcaKLGV8t6UxoUnZVmJB/Rv3JDWAQgSa1SokairijDELoRXVzntaEd/Bh03F2hnxV fJllhQGlE/2p/K/QJzacYC0NwRssdnyvEvC0m6Lev9jeExIlfMn4Ag09RoLYT1Xk8Riz k/CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689594121; x=1692186121; 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=gC3kRXuJb2CDsNb5ofLQfMx3YTx5F9ozdQp3kszCIv0=; b=EJxMzG6QFGditn30VE5EaGoePTdtCFI7mzH7NS06AE0fdJA2LDH5ZsCzlLWP2MOGsH FZZ7pbN3PHoGr6jRj+KSmUuvcpjIL+bcRs/Una7vbzg908PyjyoUuYu8DTHEYqszlC6n v3DBG4TG9Fk1+eJZbCYXUJeBRsIiTCxUqwBAQCyZrA92hgVlKaAtvJG2pSSRfeDXUBLF FQs0Gblm1ptwDnFRk52+YRPUCtyMwyf836jW8L9FpZNc7Lmc9M4SBPSEr1OM1bXJgkHU ougm5giazw+z4Gla4gKFe/BvN+DwazfsR9EqWz8/13EXcxW5IilrstVfCKx9+AdVJz00 yyCQ== X-Gm-Message-State: ABy/qLaQ5ybEQVOyHYq48m2IUvVyLa6hZPiX0o1wfFI3cYpM4rvqtLox yjh4bEi320AIGiYCUmKHCaYeLA== X-Google-Smtp-Source: APBJJlGDRgpaqXBnqkzb0WwwhDR/B9BEFzZ16kPCK8IDZLtIAJWhABykrg2Q5mRvQD2QS6IfMIFQGA== X-Received: by 2002:a1c:7c08:0:b0:3fb:424b:ef6e with SMTP id x8-20020a1c7c08000000b003fb424bef6emr11325861wmc.23.1689594120691; Mon, 17 Jul 2023 04:42:00 -0700 (PDT) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id v9-20020a05600c214900b003fbc9371193sm7946725wml.13.2023.07.17.04.41.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 04:42:00 -0700 (PDT) From: Anton Protopopov To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Brian Vazquez , Hou Tao , Joe Stringer , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH bpf-next 1/2] bpf: fix setting return values for htab batch ops Date: Mon, 17 Jul 2023 11:43:06 +0000 Message-Id: <20230717114307.46124-2-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230717114307.46124-1-aspsk@isovalent.com> References: <20230717114307.46124-1-aspsk@isovalent.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The map_lookup{,_and_delete}_batch operations are expected to set the output parameter, counter, to the number of elements successfully copied to the user space. This is also expected to be true if an error is returned and the errno is set to a value other than EFAULT. The current implementation can return -EINVAL without setting the counter to zero, so some userspace programs may confuse this with a [partially] successful operation. Move code which sets the counter to zero to the top of the function so that we always return a correct value. Fixes: 057996380a42 ("bpf: Add batch ops to all htab bpf map") Signed-off-by: Anton Protopopov --- kernel/bpf/hashtab.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index a8c7e1c5abfa..fa8e3f1e1724 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -1692,6 +1692,13 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *m= ap, struct bucket *b; int ret =3D 0; =20 + max_count =3D attr->batch.count; + if (!max_count) + return 0; + + if (put_user(0, &uattr->batch.count)) + return -EFAULT; + elem_map_flags =3D attr->batch.elem_flags; if ((elem_map_flags & ~BPF_F_LOCK) || ((elem_map_flags & BPF_F_LOCK) && !btf_record_has_field(map->record, = BPF_SPIN_LOCK))) @@ -1701,13 +1708,6 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *m= ap, if (map_flags) return -EINVAL; =20 - max_count =3D attr->batch.count; - if (!max_count) - return 0; - - if (put_user(0, &uattr->batch.count)) - return -EFAULT; - batch =3D 0; if (ubatch && copy_from_user(&batch, ubatch, sizeof(batch))) return -EFAULT; --=20 2.34.1 From nobody Sun Feb 8 14:53:03 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB7ADC0015E for ; Mon, 17 Jul 2023 11:42:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230260AbjGQLmS (ORCPT ); Mon, 17 Jul 2023 07:42:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230094AbjGQLmJ (ORCPT ); Mon, 17 Jul 2023 07:42:09 -0400 Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97AF810D8 for ; Mon, 17 Jul 2023 04:42:03 -0700 (PDT) Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-2b6f97c7115so63101171fa.2 for ; Mon, 17 Jul 2023 04:42:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689594122; x=1692186122; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=P7UHhFAQ1FaPs29fvZX7CYf1ico5jMHSKUYpX+oo8ek=; b=Vh2f2nU9VaVBZJD9ooQnVXSm4T5KSE53a1VWB6uNdNOZenJC/gVkO1VjvS6LAPaOmw R2iEjfb6mF8btLJcWWYCXcLMp2pGD5A/N+7NYA6T9pTRwQwP0IBpJgCQ8WiKIBsoJ4EP Wl0ZPZYxoW0iDBPHnJIun3XY302JfupNFwPVlHSRkg0KeXPiyGPoNlfW9jpYwDGBjYhy 2kJpcRyG7wm+qKws+/QLoh3wztG4AzUYb9p/mrPNfL+qf/1jgI46tmpKeA6xyb+EsBQQ Cy1vFb+I1oDViGZJagA1Dl/tjIf0eUNrZrEyiE3LHRraLruiGguoPZ09oNvXeFq8CiI5 beXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689594122; x=1692186122; 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=P7UHhFAQ1FaPs29fvZX7CYf1ico5jMHSKUYpX+oo8ek=; b=Nlbd3eQfKsro+CKxktEWAUcH4qgE0T/IvXAH2B+3/7niLGh7GqgeMqamQDzguHQNmA a7pUq0L5Y+WQIUXjUFcV4CwioKj+KvcJ4z1xhPtakupEUOqWPUXExMdEX93Xo58mmXm+ /BeeUniRqihtPECeBtDpVaKHhvTKjzzvXUBO1s4fSXk6LrfEI8CSd7zq4gvh+C5E1OhR RJix4Jl8YtAYEAdSTaZRUQRN/c75FuW5bZ+VUhsdjW8ShANvF7aCkOfU4TCfQ3ivWW4E lcGJaveFoQH4ZyxLSYlW8maOic8Ay2y8ruO4Q+/sl4adITBLy56wlpVRALRWrC6kYlV/ xhHQ== X-Gm-Message-State: ABy/qLZPm/0acXnz+46PSptcnstwbYqWA3t6NLjL5kkmU+HeQ6nRynkP j95PkoiekB74vl9yuPlvJedTlA== X-Google-Smtp-Source: APBJJlGjRSDk4zBMgAQlu/4kYfZ+mQZFT+iyjVQxWs1snlZC2ZAnxz0Xq1LZL+x1qaKrzrLTAasB5g== X-Received: by 2002:a2e:9ed3:0:b0:2b4:47ad:8c70 with SMTP id h19-20020a2e9ed3000000b002b447ad8c70mr7446271ljk.11.1689594121908; Mon, 17 Jul 2023 04:42:01 -0700 (PDT) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id v9-20020a05600c214900b003fbc9371193sm7946725wml.13.2023.07.17.04.42.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 04:42:01 -0700 (PDT) From: Anton Protopopov To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Brian Vazquez , Hou Tao , Joe Stringer , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH bpf-next 2/2] bpf: update uapi/linux/bpf.h docs on the batch map ops Date: Mon, 17 Jul 2023 11:43:07 +0000 Message-Id: <20230717114307.46124-3-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230717114307.46124-1-aspsk@isovalent.com> References: <20230717114307.46124-1-aspsk@isovalent.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The map_lookup{,_and_delete}_batch operations return same values. Make this clear in documentation. Also, update the comments so that this is more clear that -ENOENT is a valid return value in case of success. (In fact, this is the most common return value, as this is reasonable to do map_lookup_batch(MAX_ENTRIES), which, in case of success, will always return -ENOENT.) Signed-off-by: Anton Protopopov Acked-by: Hou Tao --- include/uapi/linux/bpf.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 600d0caebbd8..9e6e277bedab 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -632,17 +632,19 @@ union bpf_iter_link_info { * returning the lock. This must be specified if the * elements contain a spinlock. * - * On success, *count* elements from the map are copied into the - * user buffer, with the keys copied into *keys* and the values - * copied into the corresponding indices in *values*. - * - * If an error is returned and *errno* is not **EFAULT**, *count* - * is set to the number of successfully processed elements. + * On success, up to *count* elements from the map are copied into + * the user buffer, with the keys copied into *keys* and the + * values copied into the corresponding indices in *values*. * * Return * Returns zero on success. On error, -1 is returned and *errno* * is set appropriately. * + * If an error is returned and *errno* is not **EFAULT**, then + * *count* is set to the number of successfully processed + * elements. In particular, the *errno* may be set to **ENOENT** + * in case of success to indicate that the end of map is reached. + * * May set *errno* to **ENOSPC** to indicate that *keys* or * *values* is too small to dump an entire bucket during * iteration of a hash-based map type. @@ -655,15 +657,15 @@ union bpf_iter_link_info { * **BPF_MAP_LOOKUP_BATCH** with two exceptions: * * * Every element that is successfully returned is also deleted - * from the map. This is at least *count* elements. Note that - * *count* is both an input and an output parameter. + * from the map. The *count* parameter is set to the number of + * returned elements. This value can be less than the actual + * number of deleted elements, see the next item. * * Upon returning with *errno* set to **EFAULT**, up to * *count* elements may be deleted without returning the keys * and values of the deleted elements. * * Return - * Returns zero on success. On error, -1 is returned and *errno* - * is set appropriately. + * Same as the BPF_MAP_LOOKUP_BATCH return values. * * BPF_MAP_UPDATE_BATCH * Description --=20 2.34.1