Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp29150835rwd; Wed, 5 Jul 2023 07:54:55 -0700 (PDT) X-Google-Smtp-Source: APBJJlGwni3iVqG171atkPAKFj+tTaPfK48SUcQ4B8NKYav12gZZRH5TOKrxlKZ5+dADGoDVgU1+ X-Received: by 2002:a05:6870:32ca:b0:1b3:c5ac:cefa with SMTP id r10-20020a05687032ca00b001b3c5accefamr7481756oac.49.1688568895502; Wed, 05 Jul 2023 07:54:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688568895; cv=none; d=google.com; s=arc-20160816; b=DGvomb1h3PsH627+X697f9y5tI++fJUFs9xr5sgR0yHIFY7Jh1VeQjYsG6xlwSoOKs tJli3DL01GfoKk7Lz3u7t1idtSEM5A7beNS7ainYMoAY8wjAdZPmFRDtceCPtToX3me4 fOL0XdydpxkCdMz8kpVbK89SkhjD0xUQ5fJBA1KfsntnDJbpNM0+165lvMsskB00MCUP N9o12ScIJlmP7uozcYKUgCRfu2mqVZ00iwyyBOSn/AYsF9iXjZ8rwWhYLs+JpT6AK3z3 5Upz2cJMwt6XYpDWf3ZTVj3o8uDbzADaCpyaS/ogAX6a23z0crZHvtFTsrAQ1AYZO/U9 iaHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=a3Rf4+roGcyzICjP/y+mzVOY5fi2qEb4QAdDmwYjSrY=; fh=9jYmK9+v8PCoYLpRldsqUbhsElZy0v7GQsRga67YxUc=; b=io0z0KF1HXDZ8/CC6V+x6lUQhxhJvEa00M/qNJ3GM1IkonVrneAmkuh8VSE55oEBX9 Jb49oqe10FWjKGd/Kdxf2+ZvBKmEB3g/HL9130aJJemDtljl3dyOALlYBR/pOXTTfpId zwT0GM/uQXUi+PX9VtJfTmXJ0iEdVLJGmDfr0FrdiFe3piPuby/XzWGLwuqdq8/vkQsD 2pvmJ4JrbCjKwoGg4vELs+Sh8RP1FC1K8fP1G6ukcZOnNWKr0es8PEbNZecCDjfptkg7 fwWYJcIheoB/Zg4fI9CrFzYC7YtdIrQG2mnY89jvZFqM8YuIqUziM+N/mP60st56QUxH f4zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@iogearbox.net header.s=default2302 header.b=aX26S1+S; 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=REJECT sp=NONE dis=NONE) header.from=iogearbox.net Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h9-20020a17090ac38900b0024e4ace1a41si1797302pjt.183.2023.07.05.07.54.42; Wed, 05 Jul 2023 07:54:55 -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=@iogearbox.net header.s=default2302 header.b=aX26S1+S; 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=REJECT sp=NONE dis=NONE) header.from=iogearbox.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232515AbjGEOjo (ORCPT + 99 others); Wed, 5 Jul 2023 10:39:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231565AbjGEOjn (ORCPT ); Wed, 5 Jul 2023 10:39:43 -0400 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C7A9113; Wed, 5 Jul 2023 07:39:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=iogearbox.net; s=default2302; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=a3Rf4+roGcyzICjP/y+mzVOY5fi2qEb4QAdDmwYjSrY=; b=aX26S1+S9ndT/vzQo02Vq2zdDI I4us3iH1FQtMqBOG3NELuDdA0BgZ09qftjKvCJGWMrEA9O3B9EXoue6iuiLiUVTGDLaGqWfu2/O4P V5UrbWTffRe0hcrmWUaXEVBesPRuoUmNpSkFqfcymn3vdLfhLzl0KdjbBwW5GqQde8U5EdAQ5mocw gs7C2U3udmVDnzsnVcPsF/0Ku4yB5ZAIsPl+GHDYHJc/luD8Fefdbz+LXLr/0hzWm0n315ly9CNwt S3u8GDF3jlGxuFYXKwBKuU2fP1DLWXBztGs/ABsJ7btY7lA+Kg/v7RFWMVV/1wtOF5tjGHGdnC78T a37xe0Sw==; Received: from sslproxy03.your-server.de ([88.198.220.132]) by www62.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qH3fD-000Db4-9D; Wed, 05 Jul 2023 16:39:27 +0200 Received: from [178.197.249.31] (helo=linux.home) by sslproxy03.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qH3fC-000GRN-EA; Wed, 05 Jul 2023 16:39:26 +0200 Subject: Re: [PATCH bpf-next] bpf: Introduce bpf generic log To: Leon Hwang , ast@kernel.org Cc: john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, hawk@kernel.org, tangyeechou@gmail.com, kernel-patches-bot@fb.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org References: <20230705132058.46194-1-hffilwlqm@gmail.com> From: Daniel Borkmann Message-ID: <1a205a85-ebf2-6d90-468d-4fd63ce3dd0f@iogearbox.net> Date: Wed, 5 Jul 2023 16:39:25 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20230705132058.46194-1-hffilwlqm@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.8/26960/Wed Jul 5 09:29:05 2023) X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,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 7/5/23 3:20 PM, Leon Hwang wrote: > Currently, excluding verifier, users are unable to obtain detailed error > information when issues occur in BPF syscall. > > To overcome this limitation, bpf generic log is introduced to provide > error details similar to the verifier. This enhancement will enable the > reporting of error details along with the corresponding errno in BPF > syscall. > > Essentially, bpf generic log functions as a mechanism similar to netlink, > enabling the transmission of error messages to user space. This > mechanism greatly enhances the usability of BPF syscall by allowing > users to access comprehensive error messages instead of relying solely > on errno. > > This patch specifically addresses the error reporting in dev_xdp_attach() > . With this patch, the error messages will be transferred to user space > like the netlink approach. Hence, users will be able to check the error > message along with the errno. > > Signed-off-by: Leon Hwang > --- > include/linux/bpf.h | 30 ++++++++++++++++++++++++++++++ > include/uapi/linux/bpf.h | 6 ++++++ > kernel/bpf/log.c | 33 +++++++++++++++++++++++++++++++++ > net/core/dev.c | 11 ++++++++++- > tools/include/uapi/linux/bpf.h | 6 ++++++ > 5 files changed, 85 insertions(+), 1 deletion(-) > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > index f58895830..fd63f4a76 100644 > --- a/include/linux/bpf.h > +++ b/include/linux/bpf.h > @@ -3077,4 +3077,34 @@ static inline gfp_t bpf_memcg_flags(gfp_t flags) > return flags; > } > > +#define BPF_GENERIC_TMP_LOG_SIZE 256 > + > +struct bpf_generic_log { > + char kbuf[BPF_GENERIC_TMP_LOG_SIZE]; > + char __user *ubuf; > + u32 len_used; > + u32 len_total; > +}; > + > +__printf(2, 3) void bpf_generic_log_write(struct bpf_generic_log *log, > + const char *fmt, ...); > +static inline void bpf_generic_log_init(struct bpf_generic_log *log, > + const struct bpf_generic_user_log *ulog) > +{ > + log->ubuf = (char __user *) (unsigned long) ulog->log_buf; > + log->len_total = ulog->log_size; > + log->len_used = 0; > +} > + > +#define BPF_GENERIC_LOG_WRITE(log, ulog, fmt, ...) do { \ > + const char *____fmt = (fmt); \ > + bpf_generic_log_init(log, ulog); \ > + bpf_generic_log_write(log, ____fmt, ##__VA_ARGS__); \ > +} while (0) > + > +#define BPF_GENERIC_ULOG_WRITE(ulog, fmt, ...) do { \ > + struct bpf_generic_log ____log; \ > + BPF_GENERIC_LOG_WRITE(&____log, ulog, fmt, ##__VA_ARGS__); \ > +} while (0) > + Could we generalize the bpf_verifier_log infra and reuse bpf_log() helper instead of adding something new? > #endif /* _LINUX_BPF_H */ > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > index 60a9d59be..34fa33493 100644 > --- a/include/uapi/linux/bpf.h > +++ b/include/uapi/linux/bpf.h > @@ -1318,6 +1318,11 @@ struct bpf_stack_build_id { > }; > }; > > +struct bpf_generic_user_log { > + __aligned_u64 log_buf; /* user supplied buffer */ > + __u32 log_size; /* size of user buffer */ > +}; > + > #define BPF_OBJ_NAME_LEN 16U > > union bpf_attr { > @@ -1544,6 +1549,7 @@ union bpf_attr { > }; > __u32 attach_type; /* attach type */ > __u32 flags; /* extra flags */ > + struct bpf_generic_user_log log; /* user log */ You cannot add this here, this breaks user space, you would have to ad this under a xdp specific section inside the union. > union { > __u32 target_btf_id; /* btf_id of target to attach to */ > struct { > diff --git a/kernel/bpf/log.c b/kernel/bpf/log.c > index 850494423..be56b153b 100644 > --- a/kernel/bpf/log.c > +++ b/kernel/bpf/log.c > @@ -325,3 +325,36 @@ __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, > va_end(args); > } > EXPORT_SYMBOL_GPL(bpf_log); > + > +static inline void __bpf_generic_log_write(struct bpf_generic_log *log, const char *fmt, > + va_list args) > +{ > + unsigned int n; > + > + n = vscnprintf(log->kbuf, BPF_GENERIC_TMP_LOG_SIZE, fmt, args); > + > + WARN_ONCE(n >= BPF_GENERIC_TMP_LOG_SIZE - 1, > + "bpf generic log truncated - local buffer too short\n"); > + > + n = min(log->len_total - log->len_used - 1, n); > + log->kbuf[n] = '\0'; > + > + if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) > + log->len_used += n; > + else > + log->ubuf = NULL; > +} > + > +__printf(2, 3) void bpf_generic_log_write(struct bpf_generic_log *log, > + const char *fmt, ...) > +{ > + va_list args; > + > + if (!log->ubuf || !log->len_total) > + return; > + > + va_start(args, fmt); > + __bpf_generic_log_write(log, fmt, args); > + va_end(args); > +} > +EXPORT_SYMBOL_GPL(bpf_generic_log_write); > diff --git a/net/core/dev.c b/net/core/dev.c > index 69a3e5446..e933809c0 100644 > --- a/net/core/dev.c > +++ b/net/core/dev.c > @@ -9409,12 +9409,20 @@ static const struct bpf_link_ops bpf_xdp_link_lops = { > .update_prog = bpf_xdp_link_update, > }; > > +static inline void bpf_xdp_link_log(const union bpf_attr *attr, struct netlink_ext_ack *extack) > +{ > + const struct bpf_generic_user_log *ulog = &attr->link_create.log; > + > + BPF_GENERIC_ULOG_WRITE(ulog, extack->_msg); > +} > + > int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) > { > struct net *net = current->nsproxy->net_ns; > struct bpf_link_primer link_primer; > struct bpf_xdp_link *link; > struct net_device *dev; > + struct netlink_ext_ack extack; > int err, fd; > > rtnl_lock(); > @@ -9440,12 +9448,13 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) > goto unlock; > } > > - err = dev_xdp_attach_link(dev, NULL, link); > + err = dev_xdp_attach_link(dev, &extack, link); > rtnl_unlock(); > > if (err) { > link->dev = NULL; > bpf_link_cleanup(&link_primer); > + bpf_xdp_link_log(attr, &extack); > goto out_put_dev; > } Agree that this is a useful facility to have and propagate back here. > diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h > index 60a9d59be..34fa33493 100644 > --- a/tools/include/uapi/linux/bpf.h > +++ b/tools/include/uapi/linux/bpf.h > @@ -1318,6 +1318,11 @@ struct bpf_stack_build_id { > }; > }; > > +struct bpf_generic_user_log { > + __aligned_u64 log_buf; /* user supplied buffer */ > + __u32 log_size; /* size of user buffer */ > +}; > + > #define BPF_OBJ_NAME_LEN 16U > > union bpf_attr { > @@ -1544,6 +1549,7 @@ union bpf_attr { > }; > __u32 attach_type; /* attach type */ > __u32 flags; /* extra flags */ > + struct bpf_generic_user_log log; /* user log */ > union { > __u32 target_btf_id; /* btf_id of target to attach to */ > struct { >