Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp5472302rwp; Mon, 17 Jul 2023 04:59:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlFO+ZwVnPqSXvClnwlBzGTTdtHrzsZRysWnnP8c7L5nHRyYogQyu68Cuurb5KEzsUyFQ/g4 X-Received: by 2002:a17:906:225a:b0:98e:16b7:e038 with SMTP id 26-20020a170906225a00b0098e16b7e038mr12353324ejr.23.1689595167541; Mon, 17 Jul 2023 04:59:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689595167; cv=none; d=google.com; s=arc-20160816; b=BDPFd5xdIgPAjXH39YWtm95vc8JkPVXeAScLmfsM3jWH/OJXjmyTr+3UREFiRlh4mH ZnIYrrlccM6Id5D2n2W+DrLVkBQ+z4fa3XBjGwFuD8Xsx5TAEz3xhClxhQCbQ8oQMWje HlMSeS72fqZB6DwPapsZJhSGQC/bjm0eoFAZXLlh+tkLSfIUUfpFtmg3PpoydfWBbFfG rlOSinoitEO0xe7diDJAp0+b2nWc0THxYG5rebp6LgiojDbL2PS7F8/cxS8/rvx7sYxl 1c4Z8hHLLsINwmv0Arz6px5N4GL5hy0XtOdrjVSeo1UaorWqJk56hen7L4DLe2oCNgR2 JSZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gC3kRXuJb2CDsNb5ofLQfMx3YTx5F9ozdQp3kszCIv0=; fh=YdAhxi0bx/OySu0JO8LdM4GJk2EzSySwAjphKrwyQzo=; b=Ngsa3nlkM5mXdF6MONoN/x/faGog4xjnNu95xVILDcty7TMDGeCpHW18MsbBOzB0p1 2A0xu0PBWWa4u5bG7662w3SKFgyMKu82aDN+ThrdKEFtCNuEZtYW5qNl2H3WmsQ4JaYL KuQszYI+45XIB52d4FLgsRwZxMJoz4rxpIl+5hE4NCw1d85ws/XCJvm7/0na6YKblLfd Q0nWSIUnlmqOfP1HB32QW5DaiIMAKTR6jX+NlO7Fy7FjRBDj3bLJJzFVRIu4RlOfVNp0 G52YIIA4Pfk/lXAQooQWvRJcllj69yMbKX5hspj5bYO7OgV0z9eTla7unTM8fai9OtIP 3RLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@isovalent.com header.s=google header.b="Auzfuff/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=isovalent.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f24-20020a170906495800b00987acac4c40si6885283ejt.596.2023.07.17.04.59.01; Mon, 17 Jul 2023 04:59:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@isovalent.com header.s=google header.b="Auzfuff/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=isovalent.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230062AbjGQLmO (ORCPT + 99 others); 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-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: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 *map, struct bucket *b; int ret = 0; + max_count = attr->batch.count; + if (!max_count) + return 0; + + if (put_user(0, &uattr->batch.count)) + return -EFAULT; + elem_map_flags = 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 *map, if (map_flags) return -EINVAL; - max_count = attr->batch.count; - if (!max_count) - return 0; - - if (put_user(0, &uattr->batch.count)) - return -EFAULT; - batch = 0; if (ubatch && copy_from_user(&batch, ubatch, sizeof(batch))) return -EFAULT; -- 2.34.1