Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1812220pxf; Fri, 26 Mar 2021 15:52:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+4YoeD7AUMhUFsZf8/x/C+CvsdYZ4FW7SQt5VkVzerMDuD7HVywJqyPENLM7Y056DYINk X-Received: by 2002:a05:6402:438f:: with SMTP id o15mr17690598edc.123.1616799172032; Fri, 26 Mar 2021 15:52:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616799172; cv=none; d=google.com; s=arc-20160816; b=ATaNfpNNP/FgUaPXChHe08BIrbo8CMRmCGojYt2pl79t4yOunQk+sCUtt+y9keSJPV 7ewbHmLtiZ9QXi+FouwvbdeWvJhkDk3hzYjYMAuYGU0aO24t0a5nzbvhtOH6H2RhszcS XKZfnfXAX21fh1BTFEwudPAxRRr1XEjDKMkOMgrwsh1n8aIPJiuvWgVtEarSUfw2QQLX sagId+HFap9d+avblNme5JBkXW1/T3FCZ/RW5hmYnde+pQLuAWC6PW9OtZ/Ra46gO82R jvqlSJ1Hb3f41rWEj0Cjqd2xumLQAFMUUwaeqqqi6LAtLLG3q8qT95pKX3erAY52TYY9 uLTA== 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=wo1xdtPvSfPYNdGvfB/dj7upKQGrr8JQkBYTGwXZk30=; b=Nz0p4vFhpUIbYfQXajkdpyrEJdH/Ao+IHTD7rlXC8N72FFTPGc8POJ2ubIEOMyHoP3 pqiyfIfoR2NcVlqF3BNxcMAq3Z3gCFj3ge6RAlHnhXkgIvrC3hH6iaOcT2rB/innC8y1 M1cL59yXIt3/SpJHOY3fDHerewSD5AWcA6AGQ+FxEHVVS8hI6znlR6xoM2uUECtOIJTy oSsXSioMxTCgE0mFMG3xCB51RK9uWw2lKaorw7EN4gZ7HoLaQ/1I6kJlaVIUFS5JXhub 3xlx++Nv9CxTbsW26U/wVglGGTHblTNuJl2yKQsKGjhg/ts8myly1HsoefWEP0dC21LJ 7MLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=c3A2uDO9; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y5si7652127eda.385.2021.03.26.15.52.29; Fri, 26 Mar 2021 15:52:52 -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=@gmail.com header.s=20161025 header.b=c3A2uDO9; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229986AbhCZWv2 (ORCPT + 99 others); Fri, 26 Mar 2021 18:51:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230299AbhCZWvV (ORCPT ); Fri, 26 Mar 2021 18:51:21 -0400 Received: from mail-yb1-xb2e.google.com (mail-yb1-xb2e.google.com [IPv6:2607:f8b0:4864:20::b2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2179EC0613AA; Fri, 26 Mar 2021 15:51:21 -0700 (PDT) Received: by mail-yb1-xb2e.google.com with SMTP id o66so7372182ybg.10; Fri, 26 Mar 2021 15:51:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wo1xdtPvSfPYNdGvfB/dj7upKQGrr8JQkBYTGwXZk30=; b=c3A2uDO9nuopsC7Bfv6aw/O7cwWYnuEcJlO+eq9xmeGZ6Uubf3GJUMC5LxMHBIKmCD MA3wDQVubMGNWXrFQdDdW89GL5ovBKr3nMZOLe6LlflBPAJQBMj3eRIr0bThl9RM1OYF y091L3UArKWtRl5qM2vvoiMjQJNhafUK9TgDZ8MoE9dCRZ1z7QXP6P6l1gdPAmnDjSmh Gfu80Ul8gHBVK1qQXUu+aoFLUibR/TN120C2BMqFCysff5adwfMPDnCuKkHv+09mcsdE thlUSzg2aglt6Wo5a3oQUegb1ATIxoN1FG6Ry6nhyhF0NjjCZhgHXa8+o9p+EmZMz7Qs SqHQ== 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=wo1xdtPvSfPYNdGvfB/dj7upKQGrr8JQkBYTGwXZk30=; b=lnCjRQcWw2+1f8kYNIaYLrQ1iNdJO4P3VWcPOKf8AYe0RlDo4Wz0MtnmusBUEJQ6bp xbq74QwZhfPzlBR9BC+F7tzeZgT4NMHzYjNI9x6fsJZM5j6sn3LSd2xiHZ2jtJR6w2ny MbPcWVeEQPYX6BBwHLqkn3aTREmuT9OP292L62SKtZyGaQa8icbBjl3+cI6AOZLJIzvl xXulOlIwE2XICQaVFgFe6guwSib68Io0vktQG6YQ6WmV3ATApapCgVX5vajTeEKYxFoN +p+q96KEpXsPDR+J+UoLWrlLX8c6nyGBwXuIvYJ7NX2HQc4GaDzbjQhIAmxVhTfNddbH S/Qg== X-Gm-Message-State: AOAM532J+DK/f2qqmBEMhofuJnvdqpxNDHkJ12UUGrWHSu1Ve3XSn4aQ YGO204NWifGUGvRIK0yDJozouDXCFEkEAqXehYF7nETQAHM= X-Received: by 2002:a25:874c:: with SMTP id e12mr21619164ybn.403.1616799079001; Fri, 26 Mar 2021 15:51:19 -0700 (PDT) MIME-Version: 1.0 References: <20210324022211.1718762-1-revest@chromium.org> <20210324022211.1718762-2-revest@chromium.org> In-Reply-To: From: Andrii Nakryiko Date: Fri, 26 Mar 2021 15:51:08 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2 1/6] bpf: Factorize bpf_trace_printk and bpf_seq_printf To: Florent Revest 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 Fri, Mar 26, 2021 at 2:53 PM Andrii Nakryiko wrote: > > On Tue, Mar 23, 2021 at 7:23 PM Florent Revest wrote: > > > > Two helpers (trace_printk and seq_printf) have very similar > > implementations of format string parsing and a third one is coming > > (snprintf). To avoid code duplication and make the code easier to > > maintain, this moves the operations associated with format string > > parsing (validation and argument sanitization) into one generic > > function. > > > > Unfortunately, the implementation of the two existing helpers already > > drifted quite a bit and unifying them entailed a lot of changes: > > "Unfortunately" as in a lot of extra work for you? I think overall > though it was very fortunate that you ended up doing it, all > implementations are more feature-complete and saner now, no? Thanks a > lot for your hard work! > > > > > - bpf_trace_printk always expected fmt[fmt_size] to be the terminating > > NULL character, this is no longer true, the first 0 is terminating. > > You mean if you had bpf_trace_printk("bla bla\0some more bla\0", 24) > it would emit that zero character? If yes, I don't think it was a sane > behavior anyways. > > > - bpf_trace_printk now supports %% (which produces the percentage char). > > - bpf_trace_printk now skips width formating fields. > > - bpf_trace_printk now supports the X modifier (capital hexadecimal). > > - bpf_trace_printk now supports %pK, %px, %pB, %pi4, %pI4, %pi6 and %pI6 > > - argument casting on 32 bit has been simplified into one macro and > > using an enum instead of obscure int increments. > > > > - bpf_seq_printf now uses bpf_trace_copy_string instead of > > strncpy_from_kernel_nofault and handles the %pks %pus specifiers. > > - bpf_seq_printf now prints longs correctly on 32 bit architectures. > > > > - both were changed to use a global per-cpu tmp buffer instead of one > > stack buffer for trace_printk and 6 small buffers for seq_printf. > > - to avoid per-cpu buffer usage conflict, these helpers disable > > preemption while the per-cpu buffer is in use. > > - both helpers now support the %ps and %pS specifiers to print symbols. > > > > Signed-off-by: Florent Revest > > --- > > This is great, you already saved some lines of code! I suspect I'll > have some complaints about mods (it feels like this preample should > provide extra information about which arguments have to be read from > kernel/user memory, but I'll see next patches first. Disregard the last part (at least for now). I had a mental model that it should be possible to parse a format string once and then remember "instructions" (i.e., arg1 is long, arg2 is string, and so on). But that's too complicated, so I think re-parsing the format string is much simpler. > > See my comments below (I deliberately didn't trim most of the code for > easier jumping around), but it's great overall, thanks! > > > kernel/trace/bpf_trace.c | 529 ++++++++++++++++++--------------------- > > 1 file changed, 244 insertions(+), 285 deletions(-) > > [...] > > +int bpf_printf_preamble(char *fmt, u32 fmt_size, const u64 *raw_args, > > + u64 *final_args, enum bpf_printf_mod_type *mod, > > + u32 num_args) > > +{ > > + struct bpf_printf_buf *bufs = this_cpu_ptr(&bpf_printf_buf); > > + int err, i, fmt_cnt = 0, copy_size, used; > > + char *unsafe_ptr = NULL, *tmp_buf = NULL; > > + bool prepare_args = final_args && mod; > > probably better to enforce that both or none are specified, otherwise > return error it's actually three of them: raw_args, mod, and num_args, right? All three are either NULL or non-NULL. > > > + enum bpf_printf_mod_type current_mod; > > + size_t tmp_buf_len; > > + u64 current_arg; > > + char fmt_ptype; > > + > > + for (i = 0; i < fmt_size && fmt[i] != '\0'; i++) { > [...]