Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp620476pxb; Thu, 15 Apr 2021 02:34:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw0seIldYY5iz7XfCQ8HsdW3kODgMYbBKnFuYzNSI34ELzapExYh8FWjorMOE+VpGAqqNYi X-Received: by 2002:a50:fd99:: with SMTP id o25mr1329859edt.148.1618479257760; Thu, 15 Apr 2021 02:34:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618479257; cv=none; d=google.com; s=arc-20160816; b=N6hUvAC5QcsNgLyKYMuYaaCOmE35Q6XtaInicBTr1FS4TSZ4fWqC7Y4B1Nf3Fu96wG qGyO+mfnBQcBbU1c2ExQdPX1TTg4pB9TtBEvWCt1Y+dxrcaKEnFiwV5H+VXJCpxIsqnK UNuBBP4MbcfnvI0VthUpuJq3pXBWZM5GirVHQrKnCETsQrLtFJfeiH8qJz0G2V9IG0tB 7ciFhEmPds7l81fYwBPGohD6QlmEITEZV0NxcOAkMizXApKoslFjFPQA7giByU/fd9gK CeWUZmOp2QHlwjU2fa7OCsjaqQiwtOMjOSnspPy4ynzU/xJfG9uBnL5OfldWE2ONQ0SL c/UQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ywf0bpiNMcKWdf1xGxMCFgbb7KI06dhEyiPCUR2/0zk=; b=QNTlAzV4PVSsAfrZutaOeSJsQwR68NKiUhMdkkLvqvW3qqJkARr6DSJ9zaSqTDm56F VwcqalcLeVI/x1R3A9s/Vr35JH5+lEootXBxELLFEivDDZTS8qbtFhhR616OUTmK7PvG iJ3Wv9aAEfj2/X3knFcFYFDOmdMfn0IjtLsJV/F1kELyEeHdFxT3XcPT9mpOC879cd4y o0cDGy4UX4JK1Zn4I03Wr8pIa6OFcCkNzp/romz0gO0D2YceNWB+vvws51GBzHdwD8Ru FG6DlpSC2dKvE4hdOGPge8c3TuIlX9oNW1lKUt3So80mKUFKk1zlusv8AVwG70BESZRp bfmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lr3hXR5s; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bl4si1955814ejb.17.2021.04.15.02.33.54; Thu, 15 Apr 2021 02:34:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lr3hXR5s; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231196AbhDOJdZ (ORCPT + 99 others); Thu, 15 Apr 2021 05:33:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230260AbhDOJdZ (ORCPT ); Thu, 15 Apr 2021 05:33:25 -0400 Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEA8BC061574 for ; Thu, 15 Apr 2021 02:33:02 -0700 (PDT) Received: by mail-io1-xd2f.google.com with SMTP id f15so14820591iob.5 for ; Thu, 15 Apr 2021 02:33:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ywf0bpiNMcKWdf1xGxMCFgbb7KI06dhEyiPCUR2/0zk=; b=lr3hXR5sz8eVp9IX8mdziXqSdnbSCOJp2FyjK+KXCGFXrKTvr7pWmQprZk7EhvW6V4 bEH1LwoUa6UCcZnUc2Y61Enr5WSnfL/xCVS1I+cwaPDASIzlEcbwWIFe3SnAnhE+q+Y5 HzJYA6Gf2R0dONiReAoDxOWmONlFHqz8b/K/8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ywf0bpiNMcKWdf1xGxMCFgbb7KI06dhEyiPCUR2/0zk=; b=gJxEcABGXQl5Z7RTuLCneYt4+nkIJZq8OGngflPgOukqOhZA8B2SEJ6yesATZ1XfEl UiMCLgmHK/k+u2DjK5gpdEjWqYdn6RZik/apjD1UKgtM2St3w9DunB2kwhFOIIzmt7xe w2JS8r9K3h4rZbUHmY7Ps1XO/lsrdaWrw1ppqha7+j6Oj9hHvWvpxHhr1YSxjcbzm6sR C8okWH/hX7XSIu4LjWRHjj6j15eNRUHXJ8UJy49mrN3UklPYZdj6bTGPqwhCLeumNEsu y0mTP30BgmPSuKEyi37GDhGbpUnFGx1GV7ABxXFjPiuqh8d8H0U1NiI/L2dqjVYrsINI gZMg== X-Gm-Message-State: AOAM531kzFlMzs+9eVTFgflzbDdRI3uyM2As3bd8qZRt+8qhKuSoqW3T RP1MyTQRAZ+DHfHxr8a6me25qIQtuU4JY+h4vy1jqQ== X-Received: by 2002:a6b:6e17:: with SMTP id d23mr1953060ioh.122.1618479182125; Thu, 15 Apr 2021 02:33:02 -0700 (PDT) MIME-Version: 1.0 References: <20210414185406.917890-1-revest@chromium.org> <20210414185406.917890-2-revest@chromium.org> In-Reply-To: From: Florent Revest Date: Thu, 15 Apr 2021 11:32:51 +0200 Message-ID: Subject: Re: [PATCH bpf-next v4 1/6] bpf: Factorize bpf_trace_printk and bpf_seq_printf To: Andrii Nakryiko Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Yonghong Song , KP Singh , Brendan Jackman , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 15, 2021 at 2:38 AM Andrii Nakryiko wrote: > On Wed, Apr 14, 2021 at 11:54 AM Florent Revest wrote: > > +static int try_get_fmt_tmp_buf(char **tmp_buf) > > +{ > > + struct bpf_printf_buf *bufs; > > + int used; > > + > > + if (*tmp_buf) > > + return 0; > > + > > + preempt_disable(); > > + used = this_cpu_inc_return(bpf_printf_buf_used); > > + if (WARN_ON_ONCE(used > 1)) { > > + this_cpu_dec(bpf_printf_buf_used); > > this makes me uncomfortable. If used > 1, you won't preempt_enable() > here, but you'll decrease count. Then later bpf_printf_cleanup() will > be called (inside bpf_printf_prepare()) and will further decrease > count (which it didn't increase, so it's a mess now). Awkward, yes. :( This code is untested because it only covers a niche preempt_rt usecase that is hard to reproduce but I should have thought harder about these corner cases. > > + i += 2; > > + if (!final_args) > > + goto fmt_next; > > + > > + if (try_get_fmt_tmp_buf(&tmp_buf)) { > > + err = -EBUSY; > > + goto out; > > this probably should bypass doing bpf_printf_cleanup() and > try_get_fmt_tmp_buf() should enable preemption internally on error. Yes. I'll fix this and spend some more brain cycles thinking about what I'm doing. ;) > > -static __printf(1, 0) int bpf_do_trace_printk(const char *fmt, ...) > > +BPF_CALL_5(bpf_trace_printk, char *, fmt, u32, fmt_size, u64, arg1, > > + u64, arg2, u64, arg3) > > { > > + u64 args[MAX_TRACE_PRINTK_VARARGS] = { arg1, arg2, arg3 }; > > + enum bpf_printf_mod_type mod[MAX_TRACE_PRINTK_VARARGS]; > > static char buf[BPF_TRACE_PRINTK_SIZE]; > > unsigned long flags; > > - va_list ap; > > int ret; > > > > - raw_spin_lock_irqsave(&trace_printk_lock, flags); > > - va_start(ap, fmt); > > - ret = vsnprintf(buf, sizeof(buf), fmt, ap); > > - va_end(ap); > > - /* vsnprintf() will not append null for zero-length strings */ > > + ret = bpf_printf_prepare(fmt, fmt_size, args, args, mod, > > + MAX_TRACE_PRINTK_VARARGS); > > + if (ret < 0) > > + return ret; > > + > > + ret = snprintf(buf, sizeof(buf), fmt, BPF_CAST_FMT_ARG(0, args, mod), > > + BPF_CAST_FMT_ARG(1, args, mod), BPF_CAST_FMT_ARG(2, args, mod)); > > + /* snprintf() will not append null for zero-length strings */ > > if (ret == 0) > > buf[0] = '\0'; > > + > > + raw_spin_lock_irqsave(&trace_printk_lock, flags); > > trace_bpf_trace_printk(buf); > > raw_spin_unlock_irqrestore(&trace_printk_lock, flags); > > > > - return ret; > > see here, no + 1 :( I wonder if it's a bug or a feature though. The helper documentation says the helper returns "the number of bytes written to the buffer". I am not familiar with the internals of trace_printk but if the terminating \0 is not outputted in the trace_printk buffer, then it kind of makes sense. Also, if anyone uses this return value, I can imagine that the usecase would be if (ret == 0) assume_nothing_was_written(). And if we suddenly output 1 here, we might break something. Because the helper is quite old, maybe we should improve the helper documentation instead? Your call :)