Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4985663pxy; Tue, 27 Apr 2021 17:53:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1PROMK+g1dyTkUFUkkQOJ2QQeQ8amqSoPINNFaP11pZPnAMAVUrtaIG6X4ZtPJvJ3WbAN X-Received: by 2002:a17:907:2d86:: with SMTP id gt6mr14854143ejc.368.1619571200686; Tue, 27 Apr 2021 17:53:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619571200; cv=none; d=google.com; s=arc-20160816; b=mJhqiusBv7mv9CSViF9tM7+87P3x7XHh/2s6Pm2F/kJt8jx+UjQFCbiRGE4sI/vr6g sxekg60J/PGnu3dc9+PDfVob4tJ/NRkZZGjhyP6hsi8BMs5Xxq5nJR3jJyrBdHNcKbZt IYthETx2p9XMeqyvhszpWJUqrq4HxhC7p/7MUyJQqtX9GKPp8vh//whI8Hwdtshaxmvp knRTmiK6YiyVlkgK9DOJQiv6AxXVj3q1DMarxMEnX5nM+MAwYfzrsIkr6QtXNid8g9R/ 3pnf08JKbnPV3L6kLTbALu3aWaZxxP2vEurnkUx+hhGE/o0Ne6SiWBdauVTsuIa0k1w+ xkjw== 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=UBG7XS9thzjZcMvWkPqDMf/eiNI69sVC30py45fCdXE=; b=TYRhFWG60STJ26oQQS1u1DXZ2bUjfOE0gFAQNw47CY2q+3o9zx6p/UNS6netj6cUpb FO7yZhoIgZhalzSarXe8C1EodyBB6wBmyJK/Ft9Ekbnit6/EVoyC/5pOv1NM2dnXEF+G 7mtNh9NUwbQuY1ks33GKsuJZ5sFCLYvuyjvZjzut2MGPo91JcEq+X9vawo6q4mQO02sy vW+n37pjiJY6O3VN3Mv92O3f2T1sGU7U69wUexqiNicRHZ/+G5ca6dr9CWXV67RN2T9z dF29EEB5PyDQLFtt427e9JW2nMM1Y4iHVWgC1IIWB4IcG5oZRTpECUTJPrOhiC2bJSfH F98w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JM1K2BEP; 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 c95si4400873edf.542.2021.04.27.17.52.56; Tue, 27 Apr 2021 17:53:20 -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=JM1K2BEP; 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 S235797AbhD1Awo (ORCPT + 99 others); Tue, 27 Apr 2021 20:52:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230368AbhD1Awo (ORCPT ); Tue, 27 Apr 2021 20:52:44 -0400 Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E080CC061574; Tue, 27 Apr 2021 17:51:58 -0700 (PDT) Received: by mail-lf1-x12c.google.com with SMTP id 4so36568527lfp.11; Tue, 27 Apr 2021 17:51:58 -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=UBG7XS9thzjZcMvWkPqDMf/eiNI69sVC30py45fCdXE=; b=JM1K2BEPc84C9uenaUaHtI3qRtkTe4U0B85lbrMxvrg1MMEIFDxXtLzsjICYtqvYKE 3ooPpj7sIHGwNfLHWav6hEg0WCbBuvFwHcoa8mlh7aqoXCpZqqy7vnmST95GUpoIg+eq PvrSdyjHx0tClpZgo12DU5m/PigWYPEAHIvfAXFQmT4rPBgPo6aGBpC29loG6ka+ViTO D/8cq1qJM/hdC78dYWR/KmqnmzBbHngIySIa7jGI5g7OCrkLOHJPnkGpDBE+jZr0Hrur WAwIOdEsLYCT2p+pEAGgABfLYo45rVasO8+dmvivw6QQJ3Yw90TWJ2B4jtpucbx8VdUt M1sA== 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=UBG7XS9thzjZcMvWkPqDMf/eiNI69sVC30py45fCdXE=; b=CIQRaimee0F80CNixhgfQ+wVum/zmyuYHaN5sheQY9SkAr7DXn0TyOxP2BTnaRwOdA 7xrTqm/RyijQCssSzCrW1IWEHXFefABTK2oevXDInd86G26KL4PruQC3Sbx3Ne01ntRl wxphQ6emw4wu/W3Tyrshc0pgeazXMNKdiAwlytI5OMYXCxOqXorgiWW8d1wFLP/EteAa 3i7YdBOhyGNk6E7HFQ8htfRZXGJ8s3SnkCrrTVk/GBIjdXcfvkYhtqGxHcXbrtLP46Gb HEuVA37IkWfQ5LLxmOcU1NwjQHohTwxltvdoqDhqeX3TshmLEyYx+CcOe00ryPP37BCR MdZA== X-Gm-Message-State: AOAM531bAOsWKJ1hiyTWSmKa98P4cN0Ydv+oA7V+dXQr7UU369bq4mA8 Q7OR8C57l/tbgKkpP5ECp8LGCOS4kg23sqNSBejHcJ8V X-Received: by 2002:a05:6512:3f93:: with SMTP id x19mr18568740lfa.182.1619571117078; Tue, 27 Apr 2021 17:51:57 -0700 (PDT) MIME-Version: 1.0 References: <20210427174313.860948-1-revest@chromium.org> <20210427174313.860948-3-revest@chromium.org> In-Reply-To: From: Alexei Starovoitov Date: Tue, 27 Apr 2021 17:51:45 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2 2/2] bpf: Implement formatted output helpers with bstr_printf To: Florent Revest Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , KP Singh , Brendan Jackman , Rasmus Villemoes , Steven Rostedt , LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 27, 2021 at 5:20 PM Florent Revest wrote: > > On Wed, Apr 28, 2021 at 1:46 AM Alexei Starovoitov > wrote: > > > > On Tue, Apr 27, 2021 at 10:43 AM Florent Revest wrote: > > > + if (fmt[i + 1] == 'B') { > > > + if (tmp_buf) { > > > + err = snprintf(tmp_buf, > > > + (tmp_buf_end - tmp_buf), > > > + "%pB", > > ... > > > + if ((tmp_buf_end - tmp_buf) < sizeof_cur_ip) { > > > > I removed a few redundant () like above > > Oh, sorry about that. > > > and applied. > > Nice! :) > > > > if (fmt[i] == 'l') { > > > - cur_mod = BPF_PRINTF_LONG; > > > + sizeof_cur_arg = sizeof(long); > > > i++; > > > } > > > if (fmt[i] == 'l') { > > > - cur_mod = BPF_PRINTF_LONG_LONG; > > > + sizeof_cur_arg = sizeof(long long); > > > i++; > > > } > > > > This bit got me thinking. > > I understand that this is how bpf_trace_printk behaved > > and the sprintf continued the tradition, but I think it will > > surprise bpf users. > > The bpf progs are always 64-bit. The sizeof(long) == 8 > > inside any bpf program. So printf("%ld") matches that long. > > Yes, this also surprised me. > > > The clang could even do type checking to make sure the prog > > is passing the right type into printf() if we add > > __attribute__ ((format (printf))) to bpf_helper_defs.h > > But this sprintf() implementation will trim the value to 32-bit > > to satisfy 'fmt' string on 32-bit archs. > > So bpf program behavior would be different on 32 and 64-bit archs. > > I think that would be confusing, since the rest of bpf prog is > > portable. The progs work the same way on all archs > > (except endianess, of course). > > I'm not sure how to fix it though. > > The sprintf cannot just pass 64-bit unconditionally, since > > bstr_printf on 32-bit archs will process %ld incorrectly. > > The verifier could replace %ld with %Ld. > > The fmt string is a read only string for bpf_snprintf, > > but for bpf_trace_printk it's not and messing with it at run-time > > is not good. Copying the fmt string is not great either. > > Messing with internals of bstr_printf is ugly too. > > Indeed, none of these solutions are satisfying. Maybe Daniel has other ideas? > > Maybe we just have to live with this quirk ? > > If we were starting from scratch, maybe just banning %ld could have > been an option, but now that bpf_trace_printk has been behaving like > this for a while, I think it might be best to just keep the behavior > as it is. > > > Just add a doc to uapi/bpf.h to discourage %ld and be done? > > More doc is always good. Something like "Note: %ld behaves differently > depending on the host architecture, it is recommended to avoid it and > use %d or %lld instead" in the helper description of the three > helpers? If you don't have the time to do it today, I can send a patch > tomorrow. bpf_trace_printk was like this for a long time, so there is no rush. Pls wait until everything comes back to bpf tree and send a patch against it. bpf_trace_printk comment in uapi/bpf.h is outdated too. Would be good to document the latest behavior for them all.