Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp648017rwb; Tue, 4 Oct 2022 08:46:18 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Zzq3aB2wcYvAPE/SCg0AzvXCkCRJRCE9erUJt1BvrAaiOwnlqGmmMfiCHE0RD3tCswJSr X-Received: by 2002:a05:6a00:2918:b0:535:ea9:791a with SMTP id cg24-20020a056a00291800b005350ea9791amr28554513pfb.54.1664898378290; Tue, 04 Oct 2022 08:46:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664898378; cv=none; d=google.com; s=arc-20160816; b=x6KTGNzEFsf79h/lKzdlFhCAd4O8A+2pmFDDeYAoIOi9wd1mCF2rH9eXobWsIqblki zUETr+zJnphagkIbSolps/aKmemSJ7+oCe1+alnpFBaDJOprT4d5i8A3LCOaqigcRTza 8kncypPOfVLtRbQVvBWf2I/49sTvlFiZF/1oOF7vbVCM3UWWXL5EZ2s/P5UDDPyL8wRp c62tpCLUYIDy2Jhh4vsJIikPq0lBJPkm1zcj8PwLztp6c2kSoyRFDE+E6vN8WI2sJCB7 4/90TQ9QXrcIadhhWmppcA5qLz9EoXaP27R/nP1rvPXh4J3edBxjIap4Uipi1UfKGqQY Mz1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=FfSJ4aymufDxA8b5eqmbbn2OCM92Q3VRBAEZIvvgymc=; b=o8GmRgP/mSyzi74+PbTzQmU36hjjZD4KgJuuw875iCSEdyJgwo6TuPCTEpwxHot0ny dtjUTpFuu098DpjA9qoInAllQmyXvi1FQSJpBGX79rEx26rhtYBFB/uGIU546bOE8i7f OaBidVTDZ1s4KZB2J6C5uR4x4ZvQ7zgSmm9hxV7zmxTfo2IeVlDHKi+bpqKBvCqeWU0/ adcxwi5owIQYfpatVZz7/G3JlQ+JmkCBT9V04GVMSpdd5LFuOQL2C2dA/itSaP93gj21 Fmzyx7sbKu6dJEoBiX2pj9PPjl9CqwvbICkFYBHb9f2rxTYAe/NPadfOh4nSxQ/PTa+4 WSvA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t17-20020a056a0021d100b0052da2ea956csi13559886pfj.371.2022.10.04.08.46.04; Tue, 04 Oct 2022 08:46:18 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229616AbiJDP3u (ORCPT + 99 others); Tue, 4 Oct 2022 11:29:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229999AbiJDP3n (ORCPT ); Tue, 4 Oct 2022 11:29:43 -0400 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B87152634; Tue, 4 Oct 2022 08:29:41 -0700 (PDT) Received: by mail-qv1-f52.google.com with SMTP id df9so5209470qvb.9; Tue, 04 Oct 2022 08:29:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date; bh=FfSJ4aymufDxA8b5eqmbbn2OCM92Q3VRBAEZIvvgymc=; b=uC/CtMvAUiR5o+DM4lZ6xBxI0LFDMIqZ0eFfyrTi9i4wNz876dORmVtCxJLQ9Bb64D hNYaSigAkNv3mazL8HmKg5rwKSW91Czztn5REndvG8gKmAARZeB9hSep/pv2UfnDQcHD 39TBjGrwsL/gVHfUnUS2knQgg+znAQrhXT1e8raUQBos07H0ajWLqvAHnTDbk/Vkkhx0 q6ut5LCiMXntgXASW0mxfGESIomo2OurDQmAlw2wQp5eDgq14JLAoHFCQyL/8ZqZTiFi nonWEyC6sOD1hxzePKAAp9ybNOBvgo7Lax+dVvMxCnqU8fDhgU5K9VwN/duPeWelYXup CP8w== X-Gm-Message-State: ACrzQf0rA8q2d0QDNaV8WzFvY1P9+9u3mxmtL0eOVyqV2WCipoDxslLu X4gYMIQE8TZASmk4CaG+Aqw= X-Received: by 2002:a05:6214:2588:b0:49e:5dea:8e66 with SMTP id fq8-20020a056214258800b0049e5dea8e66mr20608063qvb.21.1664897380698; Tue, 04 Oct 2022 08:29:40 -0700 (PDT) Received: from maniforge.lan (c-24-15-214-156.hsd1.il.comcast.net. [24.15.214.156]) by smtp.gmail.com with ESMTPSA id bq38-20020a05620a46a600b006a6ebde4799sm14848509qkb.90.2022.10.04.08.29.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 08:29:40 -0700 (PDT) Date: Tue, 4 Oct 2022 10:29:47 -0500 From: David Vernet To: Kumar Kartikeya Dwivedi Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, kernel-team@fb.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, yhs@fb.com, song@kernel.org, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org Subject: Re: [PATCH v2] selftests/bpf: Update map_kptr examples to reflect real use-cases Message-ID: References: <20221002171012.3529521-1-void@manifault.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.7 (2022-08-07) X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no 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 Mon, Oct 03, 2022 at 06:22:55PM +0200, Kumar Kartikeya Dwivedi wrote: [...] > > > > noinline void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) > > > > { > > > > if (!p) > > > > return; > > > > > > > > - refcount_dec(&p->cnt); > > > > + WARN_ON_ONCE(atomic_read(&p->destroyed)); > > > > + if (refcount_dec_and_test(&p->cnt)) > > > > + call_rcu(&p->rcu, delayed_destroy_test_ref_struct); > > > > > > I wonder whether this is ever called, I haven't really given this > > > patch a shot, but I don't see how the refcount can ever drop back to > > > zero. It's initialized as 1, and then only acquired after that, so > > > pairing all releases should still preserve refcount as 1. > > > > Yeah, the call_rcu() path is never hit. If we wanted to update the test > > so that this codepath was actually exercised, I think we'd need to add > > another kfunc that returned a reference to a dynamically allocated > > object rather than using the global, static one. I'm happy to do that if > > we think it's useful. The downside to adding more of these test kfuncs > > is that they actually do add a small bit of runtime overhead to the > > kernel because they're unconditionally registered in the __init function > > for test_run.c. > > > > But that only happens once, right? It still happens, so I don't see > what changes. The idea here would be to return a dynamically allocated object with an initial refcount of 1 that's owned by the _BPF program_, rather than what we have today where the global struct has an initial refcount that's owned by the main kernel and is never expected to go to 0. For all success (i.e. non-fail) testcases that are able to dynamically allocate this object, the refcount should go to 0 for each of them and the object will be destroyed after the current RCU grace period. Please let me know if I've misunderstood your point. > > > Also, even if you made it work, wouldn't you have the warning once you > > > run more selftests using prog_test_run, if you just set the destroyed > > > bit on each test run? > > > > If we want to update the test to have the refcount drop to 0, we would > > probably have to instead use dynamically allocated objects. At that > > point, we'd probably just crash instead of seeing a warning if we > > accidentally let a caller invoke acquire or release after the object had > > been destroyed. Maybe the better thing to do here is to just warn > > unconditionally in the destructor rather than setting a flag? What we > > really want to ensure is that the final refcount that's "owned" by the > > main kernel is never dropped. > > I think the refcount_t API already warns if underflow happens. Right, a warning would probably show up if we did a release that caused an underflow, but it would not for an acquire after the refcount dropped to 0. > To be clear, I don't mind if you want to improve this, it's certainly > a mess right now. Tests can't even run in parallel easily because it's > global. Testing like an actually allocated object might be a good way > to simulate a real scenario. And I totally agree that having a real > example is useful to people who want to add support for more kptrs. Ok, let me update the tests to do two things then: 1. Add a new test kfunc called bpf_kfunc_call_test_alloc() which returns a dynamically allocated instance of a prog_test_ref_kfunc *. This is similar in intention to bpf_xdp_ct_alloc(). 2. Update bpf_kfunc_call_test_acquire() and bpf_kfunc_call_test_release() to take a trusted pointer to that allocated prog_test_ref_kfunc *. 3. Keep the other changes which e.g. use RCU in bpf_kfunc_call_test_kptr_get() to synchronize on getting the kptr. Once the __rcu kptr variant is landed we can get rid of bpf_kfunc_call_test_kptr_get() and make bpf_kfunc_call_test_acquire() require an __rcu pointer. Continuing on point (3) above, we should _probably_ also have an example for an object that isn't RCU-protected? I imagine that we don't want to get rid of KF_KPTR_GET entirely once we have __rcu pointers because some kptr_get() implementations may synchronize in other ways, such as with spinlocks or whatever. Let's leave this until after __rcu lands though. Does this all sound good? > Maybe some comments around the code would also be helpful on what the > expectations are. We should make it easy for people to hook into this > stuff. Nobody reads documentation, people usually only look at > existing examples. For sure, can do.