Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp8451160rwp; Wed, 19 Jul 2023 09:58:47 -0700 (PDT) X-Google-Smtp-Source: APBJJlGjA+ToVNe+R7YH6KjAgQdzI0qBUr5z3s59UAf8ltJv31QVfXfQ6qpCT6mq1TrlJ9MaWtm+ X-Received: by 2002:aa7:d50b:0:b0:521:a86d:d596 with SMTP id y11-20020aa7d50b000000b00521a86dd596mr3215208edq.7.1689785927590; Wed, 19 Jul 2023 09:58:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689785927; cv=none; d=google.com; s=arc-20160816; b=hLk81KbqcLgzLctuHYElFL9+9H4l6XK3hvb+nMHi4/puNBC26BhMaaOhsP4THvyPLE P4NIBUfY0sPrQ18jYbncRSQwmkM91v9FF5U6nQKW6VoAR8ZMXSkQ3e+K596ukdv0RuiI 7xzlaFr3oPcUoFHVCrth8ch8Z+BMj0zjmhvOO968fn1jq6GCTNsm4mCREt3S1JIdm0XQ 2rMjxmGooHqifx7Qjpz0hE7O2JuU4KldmAxj3Fh29QeXRDyIrAd8RFiTCBQF9BampVZC fPOKpBFzOAmjwU1gdBEu5Pg7PAPmogGwRzT6FPnrtEws/WzuE8pIJfK90ltypNUm6H3y VMOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=99yK8+KnHqRGqbqnzMVhOVnI/JP3FUcwgMY39whDxlg=; fh=4r5DVzhSEySWr1UuY8p89p2tu6HjJd2UmPkYa5NXgQE=; b=ujwvN9eq7jIqlZ+g9di9cpdRjMLFy3QsUAcfnTWCHUkUHPrvs0RLMLk9uUzp8edgRn NkyEalswKWGV4uXpDQPpcrkh77Hd0NHvZ8tz91DQNPioJ/QsEdOdwae7dcLlTF8Clp8P 4fi8MRrVsn8y1LjDzOSE7EN0Dec/8+2XnAkoXgznMQ95qY+wkATjhWFoZX6lsYcETIau 7Cv7FebMiuDLRPLF6Pb+POxYLePmcLlV+DP3Sg5BhHvT9349AkULRNSTr+LZE+995Uam P1OGEr+x2XA9dEFCT68Ox3ROBbIaFSiU1hJ4Ai9OsHJ0jfOmQblvttVAcBc7PHV6TvQ6 C0pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=UnxyuKFJ; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g17-20020aa7c851000000b0051e1662942esi3133075edt.136.2023.07.19.09.58.23; Wed, 19 Jul 2023 09:58: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=@gmail.com header.s=20221208 header.b=UnxyuKFJ; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229731AbjGSQ2q (ORCPT + 99 others); Wed, 19 Jul 2023 12:28:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229447AbjGSQ2o (ORCPT ); Wed, 19 Jul 2023 12:28:44 -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 B26F21BB; Wed, 19 Jul 2023 09:28:43 -0700 (PDT) Received: by mail-lj1-x22c.google.com with SMTP id 38308e7fff4ca-2b933bbd3eeso75686911fa.1; Wed, 19 Jul 2023 09:28:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689784122; x=1692376122; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=99yK8+KnHqRGqbqnzMVhOVnI/JP3FUcwgMY39whDxlg=; b=UnxyuKFJOvvgGrb4VavlzqnHs+xZ0waNJYz6YIk1Z3rxBTeE+/umfOLQ10Ti0TYDDH 7HWt/AUWYYelCkjGH3s31LtmKjZrII6YN5+gNJZaMXhbcu/fe/aaoZLE7gsmKJysFlty wfs5w+RPI4B8bxLJt/PwRAudU9x/9MiXeHegGa2oTcSSQXsU13GEE88Y6s+/fnUCTkR1 lAPjqaKgISJE2Acl6hitprBwz+/fMXvY5xwZb1AR6T/QmO4EBq/UsJfxEjxp3YILVwoM flWP9Kde14eyM8Nbh2d9XrhosU/8bAav+TzRpkMwQlkVxu2PJ816+Gl5xxOmSSTJvIW0 WcsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689784122; x=1692376122; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=99yK8+KnHqRGqbqnzMVhOVnI/JP3FUcwgMY39whDxlg=; b=EvC6z/VzG4kELyYJZOSnVRw4nlr9ZtjLsMY8iH6hQOCTtM6El/K6vDJ4bg9VK+h6uj ZQ3uADpasow+3ol0a41oX3RasgKsoVL2dVL1S759VnTFDz9iQF2af36XD5Ez5zAATtkH lQowOwXWtbfglAf4+DvznOiel6TnxehVvlP9RG1V3kvHu0ZsYc9S1zauTZfjVDJFXm+2 NP8/B8i/q0SlC1lp/pAtmwdW2SNOuOUupudpMn/B/ckbeuiYVvBPKDcZjI0KH/0+vuCJ aeDJHWzJOHNTpaw3S/mV97GIvE81+nyh+a2pUBCMh9wt2iByyXxRCTxdZ7otH7k1X6PM 0cBg== X-Gm-Message-State: ABy/qLaZrCamcyzWIXYFdp0LNODCEuaK1qbM553kdH7Cfi+1Be4J9smy K1hfOULZ0FU6m6HBjDeFSkYXRNgXsnjaEgQPI9ousv6H X-Received: by 2002:a2e:7805:0:b0:2b9:55c9:c21d with SMTP id t5-20020a2e7805000000b002b955c9c21dmr322674ljc.33.1689784121432; Wed, 19 Jul 2023 09:28:41 -0700 (PDT) MIME-Version: 1.0 References: <20230717114307.46124-1-aspsk@isovalent.com> <20230717114307.46124-2-aspsk@isovalent.com> In-Reply-To: From: Alexei Starovoitov Date: Wed, 19 Jul 2023 09:28:30 -0700 Message-ID: Subject: Re: [PATCH bpf-next 1/2] bpf: fix setting return values for htab batch ops To: Anton Protopopov 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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 On Wed, Jul 19, 2023 at 12:07=E2=80=AFAM Anton Protopopov wrote: > > On Tue, Jul 18, 2023 at 05:52:38PM -0700, Alexei Starovoitov wrote: > > On Mon, Jul 17, 2023 at 4:42=E2=80=AFAM Anton Protopopov wrote: > > > > > > The map_lookup{,_and_delete}_batch operations are expected to set the > > > output parameter, counter, to the number of elements successfully cop= ied > > > 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 curre= nt > > > implementation can return -EINVAL without setting the counter to zero= , so > > > some userspace programs may confuse this with a [partially] successfu= l > > > 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 =3D 0; > > > > > > + 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(m= ap->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 =3D 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 =3D 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. Looks too convoluted to me. I think concerns over user space always assuming batch.count is updated with zero even when it calls api incorrectly are overblown.