• F
    bpf: Factorize bpf_trace_printk and bpf_seq_printf · 78f26974
    Florent Revest 提交于
    mainline inclusion
    from mainline-5.13-rc1
    commit d9c9e4db
    category: feature
    bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
    CVE: NA
    
    Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d9c9e4db186ab4d81f84e6f22b225d333b9424e3
    
    -------------------------------------------------
    
    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.
    
    The implementation of the two existing helpers already drifted quite a
    bit so unifying them entailed a lot of changes:
    
    - bpf_trace_printk always expected fmt[fmt_size] to be the terminating
      NULL character, this is no longer true, the first 0 is terminating.
    - 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.
    
    The implementation is also moved from bpf_trace.c to helpers.c because
    the upcoming bpf_snprintf helper will be made available to all BPF
    programs and will need it.
    Signed-off-by: NFlorent Revest <revest@chromium.org>
    Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20210419155243.1632274-2-revest@chromium.org
    (cherry picked from commit d9c9e4db)
    Signed-off-by: NWang Yufen <wangyufen@huawei.com>
    78f26974
bpf.h 68.6 KB