Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp1067027rdg; Fri, 11 Aug 2023 08:42:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH4FhHILKT3OY7BE1K/q/b8Ml0dfp89RFWeaZh8/n4ieh+ewf2cWcXhMl0B/0C5v/Q4fX2E X-Received: by 2002:a17:90b:e8b:b0:262:ea57:43f with SMTP id fv11-20020a17090b0e8b00b00262ea57043fmr1882370pjb.37.1691768552946; Fri, 11 Aug 2023 08:42:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691768552; cv=none; d=google.com; s=arc-20160816; b=un7y3qT9Zof0loU6LLtT/uHdqaEd81ba+SWq58ieunmDHIHoUxf3t7wN/m5t2nKkLA gHHswGtm8SKkSPOyoArOEY22axoDoAT69qF2vD1EXHUoYPu7WZ3HLLYpK3q2mU6eoXyk XigaLZcPralk4qAGhUB0Z/5PZUKAzZIAXeuaqeUw7NEWsUJKa16Nza1MWoqSR5vPCNBf xSoDCsZbvyr87TgTKtDDPgGZCyuRxNUnicQOFQenmmMqkjLX9xjPUrIpoeUKi/tXox7q hAnRHewzHLcbCXCZUpZuxvpi5ih9dMMqXxM4L8WvwWWzqYbgTTRg5842T8hGbC1IPImP 3AJA== 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=R/CYVLCrkE1ApXeKmNRKbHX2DlTn9R8U9zKDOGqrr0o=; fh=ES6OUmPJveZhkDFcJej5SA6j2ZlVfPWRe8rzV8IFquw=; b=AEhpu0qyg5gWmjZQTCVU3KEVQs4f9wxSbbmJIZsy0fXuqs9KryBH78ay1pM0FekLeu wAesfdi/6mLRkKv1ZZpCxtBDbYwn7gyHt7KkCHz/GH95BPciJQLdZAjfEFbNzaHoWB/u tmKr81Z/wXuMbxa2+YRD56fiIcUv9WsITk8DSi1tSN3KRq3wCMrykf+mtlKmUYVGLhtN oeaRYKItxmbRm4J/aUAYpkKSQ1EX+xcHyRTblcSBcFtL+gxzQprikURtvWLTWas2xfuL qqEOxJnV3FdTNda9+JdlGe0LESoJs9AC+S/7DVNHWEHvmHhmm9Rvf3EIButGnHt1WGiW cBWw== 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 nu15-20020a17090b1b0f00b002563db5c4b0si5993624pjb.184.2023.08.11.08.42.20; Fri, 11 Aug 2023 08:42:32 -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 S235715AbjHKPLH (ORCPT + 99 others); Fri, 11 Aug 2023 11:11:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235672AbjHKPLE (ORCPT ); Fri, 11 Aug 2023 11:11:04 -0400 Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B04652D78; Fri, 11 Aug 2023 08:11:03 -0700 (PDT) Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-586a684e85aso23169497b3.2; Fri, 11 Aug 2023 08:11:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691766663; x=1692371463; 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:message-id:reply-to; bh=R/CYVLCrkE1ApXeKmNRKbHX2DlTn9R8U9zKDOGqrr0o=; b=kRCvhsZAsBCSWMyi5V7e/80YKGGNGxk7BiNmxeT6eGbz2KKTVBMAz77FgO2V+H8f++ GlhLCRKGBK9+FuuQmAX2aHg0+dZhWJG4KJxLPUaqvs20HvRfssCeyh7YcS38+MU6Xihi Jiv10QzQcK/tPsCq/j5WxnsYAgTC0rrMkqBh/z5CmSZFE0qnY2sCWnR/sWJzcVzjhT/E dE2bjDGyM3ET5QsBtka9ShjO+/RzCjkIf79xetGbn4MSIZS3G2tWtAeOMlY8lY5e42aj nKW/eqYZG7OXbp50LDFlBwmH8RUSr/WP/JmmH+Z12Z6v/detZp28tZiCXutdZDe1DfIs ea3w== X-Gm-Message-State: AOJu0Yx6h5RLflqeJQ/cZaDVK+HVFTau7UDNu2N1zg92FgGW/J2oZb97 0loqnj3tmG8VzyjfVZAK/x8= X-Received: by 2002:a25:77c9:0:b0:d3c:5c3e:c2a with SMTP id s192-20020a2577c9000000b00d3c5c3e0c2amr1921241ybc.24.1691766662763; Fri, 11 Aug 2023 08:11:02 -0700 (PDT) Received: from maniforge ([24.1.27.177]) by smtp.gmail.com with ESMTPSA id 62-20020a250241000000b00d51f2b8e7fdsm200802ybc.55.2023.08.11.08.11.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 08:11:02 -0700 (PDT) Date: Fri, 11 Aug 2023 10:10:59 -0500 From: David Vernet To: Kui-Feng Lee Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, tj@kernel.org, clm@meta.com, thinker.li@gmail.com Subject: Re: [PATCH bpf-next] bpf: Support default .validate() and .update() behavior for struct_ops links Message-ID: <20230811151059.GB542801@maniforge> References: <20230810220456.521517-1-void@manifault.com> <1fb5d153-7f5d-0024-92e0-8ae75a2eb7cc@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1fb5d153-7f5d-0024-92e0-8ae75a2eb7cc@gmail.com> User-Agent: Mutt/2.2.10 (2023-03-25) 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_BLOCKED,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 Thu, Aug 10, 2023 at 11:22:04PM -0700, Kui-Feng Lee wrote: > Overall, this patch make sense to me. Thanks for the review. > On 8/10/23 15:04, David Vernet wrote: > > Currently, if a struct_ops map is loaded with BPF_F_LINK, it must also > > define the .validate() and .update() callbacks in its corresponding > > struct bpf_struct_ops in the kernel. Enabling struct_ops link is useful > > in its own right to ensure that the map is unloaded if an application > > crashes. For example, with sched_ext, we want to automatically unload > > the host-wide scheduler if the application crashes. We would likely > > never support updating elements of a sched_ext struct_ops map, so we'd > > have to implement these callbacks showing that they _can't_ support > > element updates just to benefit from the basic lifetime management of > > struct_ops links. > > > > Let's enable struct_ops maps to work with BPF_F_LINK even if they > > haven't defined these callbacks, by assuming that a struct_ops map > > element cannot be updated by default. > > > > Signed-off-by: David Vernet > > --- > > kernel/bpf/bpf_struct_ops.c | 17 +++++++++++------ > > 1 file changed, 11 insertions(+), 6 deletions(-) > > > > diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c > > index eaff04eefb31..3d2fb85186a9 100644 > > --- a/kernel/bpf/bpf_struct_ops.c > > +++ b/kernel/bpf/bpf_struct_ops.c > > @@ -509,9 +509,12 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, > > } > > if (st_map->map.map_flags & BPF_F_LINK) { > > - err = st_ops->validate(kdata); > > - if (err) > > - goto reset_unlock; > > + err = 0; > > + if (st_ops->validate) { > > + err = st_ops->validate(kdata); > > + if (err) > > + goto reset_unlock; > > + } > > set_memory_rox((long)st_map->image, 1); > > /* Let bpf_link handle registration & unregistration. > > * > > @@ -663,9 +666,6 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) > > if (attr->value_size != vt->size) > > return ERR_PTR(-EINVAL); > > - if (attr->map_flags & BPF_F_LINK && (!st_ops->validate || !st_ops->update)) > > - return ERR_PTR(-EOPNOTSUPP); > > - > > t = st_ops->type; > > st_map_size = sizeof(*st_map) + > > @@ -838,6 +838,11 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map > > goto err_out; > > } > > + if (!st_map->st_ops->update) { > > + err = -EOPNOTSUPP; > > + goto err_out; > > + } > > + > > We can perform this check before calling mutex_lock(), and > return -EOPNOTSUPP early. Yep, let's do the check outside of that global mutex. Will make that change for v2. Thanks, David