Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp7818129rwp; Wed, 19 Jul 2023 00:13:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlFl13JB3Q8mzjjdEmf7JtJpkidSV3AxvyLTtzA9HNWxx2gbBx6/RPS0iuy9d+5uomjjaV/1 X-Received: by 2002:a17:903:24c:b0:1b8:2c6f:3248 with SMTP id j12-20020a170903024c00b001b82c6f3248mr21740850plh.39.1689750827786; Wed, 19 Jul 2023 00:13:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689750827; cv=none; d=google.com; s=arc-20160816; b=mpSA1eWaJLYgEyqio3NXf+d00v70IG5L33vVGQPBotfqaRLlWtY48kIoJgxmOPSlqX b/7JjT3mKzxoEJKwbDoUJnQ46guJXoYhjagmQY1enOSpv92qPfkHn21K3O4AKRRGtDI/ Khg3c1EXejX1JeI3UhRFu1QW97p5NbQGrezhNc2531ano2k1J8iftOTopXVMfLKduyNo SZlDs0fqPjVymZNyHDYlOBGNKSFUQQzcd9u0Ef7FHUJAKABy4svCVOJW+WZnJqcyd795 HdVKLofyqtKf2WPOyf67AmWNhNhPjdtAOLfL0zJTVeqpCNNmjzon6LlT+eP/qTpcZfO0 smTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=GpJDitm1fqitEh3odC6vYNNE6Ga+U7B/JIkb40KK2kQ=; fh=w3zoRahS6m4ygwHskTaHRTR1ZL4Sy89Cf9j7ffZ40dM=; b=mReHTKgKYwJhzTSspF4P6/LurtBvBQ2A2xFR8cNHsKPSOqfP6FUWc4EsSOjMpqH+Or t2PUrVOFLuTQZfbcnXlX4iGlgHEJ+B3CstYI+Oo65uCh8p4U+YAbUjGmWYTbG7v5WCrk hx5am3ufDM8RmzbpDdprQXzwM/iPoeWwmLaOkbY25nz3N5y/xcSYBWKgqQOfkwJJk0yn PaFkjF3nxyux3fzX5dcM1NjNEL+TjYwNayF/Fvtg/9egr9cuR3Sm7BJCvAK6bJH4ei6N JDGXxEAEv1sAp80tty7w5Y9xQmToyJTe82wU8+ppGjJQJEqBfwCfxUmS5N4qL9QM9Yov LhoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@isovalent.com header.s=google header.b=duYHOVUS; 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 m19-20020a170902c45300b001ab29e00303si2865171plm.426.2023.07.19.00.13.35; Wed, 19 Jul 2023 00:13:47 -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=duYHOVUS; 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 S229471AbjGSHIL (ORCPT + 99 others); Wed, 19 Jul 2023 03:08:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229616AbjGSHIF (ORCPT ); Wed, 19 Jul 2023 03:08:05 -0400 Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3E8CE47 for ; Wed, 19 Jul 2023 00:08:00 -0700 (PDT) Received: by mail-lj1-x22c.google.com with SMTP id 38308e7fff4ca-2b6f0508f54so99674201fa.3 for ; Wed, 19 Jul 2023 00:08:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1689750479; x=1692342479; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=GpJDitm1fqitEh3odC6vYNNE6Ga+U7B/JIkb40KK2kQ=; b=duYHOVUS+HCDLicJtKM/XnF9tEG5HXWvhwV9RmmCXskY7s5lkgq+eYyYjvkQc8AdrL KYKFuSBd9OWJbiIH1pDmY2TcLDoMUkm1Xn3I+oYriCJq5S0znvUFQn29seBDzHqAzV/A xgjjZqdLarvgGUamuxfRTPr9DeA6k0Jmd1U7B79tmtigvxIj3+Yo6F7vDkE/P0z7cLCh +KhQZlm1qnDjZOCZycnpX7wrOmzEkGv66/pMyl3+a96+uVUpGivUc27uFjKEuMc79sRx l/yAv41gUtDSrIERenQbEalLBBzYyNcunfGE9ZYaAWb+hbZR92QqidsLcdtspNxXSUo5 zujg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689750479; x=1692342479; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GpJDitm1fqitEh3odC6vYNNE6Ga+U7B/JIkb40KK2kQ=; b=TSbxOAHzUgqrj9MvLwInZurf4Jvi4GYdKU6HHaMEm1PAocc2wGqhEtuV5jLUOF/UlS yUgFux8wIKHkjG09HfxWxBT7TINxlZpbQJFhwiQQcmXCTYifh1ASQqOx/yvfq0rARo7w ykRjdFXBya5wrM0Z+Zwf0nYerCj0fepi3HGUM8Tn36LifiktTHryqUYVsK1M4VUXlI7N 6QkxGe+s4rmPmtpk8/c3CwLhohBPs4ArsXeIZOjk9LLs9GC7rK+Q9B3OgJCwoIg/qH/I 2mc+SQR3hHOmF9H9U7vK8p+ewqIPjRNVMr4RzKVjYHb6W5xIR8cR2zj0lv8nz+y9NCwo UJYQ== X-Gm-Message-State: ABy/qLZxABwLie6jBteYiUq6dB97loYzWmxop+MTJ8y8XYEPW5TqI0jb 72dExGXxxSdWOFp67JbKhtigow== X-Received: by 2002:a2e:8846:0:b0:2b9:4ac9:6071 with SMTP id z6-20020a2e8846000000b002b94ac96071mr3834706ljj.10.1689750479095; Wed, 19 Jul 2023 00:07:59 -0700 (PDT) Received: from zh-lab-node-5 ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id i7-20020a05600c290700b003fbd597bccesm901661wmd.41.2023.07.19.00.07.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jul 2023 00:07:58 -0700 (PDT) Date: Wed, 19 Jul 2023 07:09:07 +0000 From: Anton Protopopov To: Alexei Starovoitov Cc: 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 , LKML Subject: Re: [PATCH bpf-next 1/2] bpf: fix setting return values for htab batch ops Message-ID: References: <20230717114307.46124-1-aspsk@isovalent.com> <20230717114307.46124-2-aspsk@isovalent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: 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_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 On Tue, Jul 18, 2023 at 05:52:38PM -0700, Alexei Starovoitov wrote: > On Mon, Jul 17, 2023 at 4:42 AM Anton Protopopov wrote: > > > > 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; > > - > > I hear your concern, but I don't think it's a good idea > to return 0 when flags were incorrect. > That will cause more suprises to user space. > I think the code is fine as-is. Yes, thanks, this makes sense. And actually we can do both: max_count = attr->batch.count; put_user(0, &uattr->batch.count); /* check flags */ if (!max_count) return 0; This way we always set the userspace counter to a correct value and also check flags in the right place.