Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp1694770pxy; Thu, 6 May 2021 13:32:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJygxXWFtu99HqCnO7M0oSCOsCqGAZll8AGgQDrVM3bIouHYI8qPiSW6+/dyehcCcbsnQNqR X-Received: by 2002:a63:5004:: with SMTP id e4mr6051951pgb.61.1620333124687; Thu, 06 May 2021 13:32:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620333124; cv=none; d=google.com; s=arc-20160816; b=wQ9k0nm26yTYUO4MV+M4qj70OI4Of1J3mD4BBMkWX0bevk4d+QHzxKrGJ6El/2OJAK K5mA3JDoe22wgR9w4CBad7F8185LWHOUqKPtOaev1bkH1p2iV3x+dvHNoWcW+Rp8FMbs 39sYSCAsp+9a1bJJ8TiUtTpp8adT4SMxsxjcGCP27sZeRUeG3TsNGi6i/5BcvNqQBHYg bS0yAjlVD/GTgQg2xpQgP/v2Ubt+v9OjecZUV6qGhTC4gs9ULRfZYmQFu0z4wym+cPEh BxeVEN682W/TaUZPJvdNLRwZWc/Fb3nvnzX8H+Pi3hhD9iErARIa8UpyHkEioPAnxpu+ qzGg== 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=9FpP0oYJ5o5WVaMKta2Gw4ucGc3wzpzA9zy0oojQ72k=; b=MF6qzJMMj/z15dgRnZgTeyZwnQnDOiA0wzgonrTr3gDyEVT7Pc8dq52r2ZvVYtZWcx ICwYsKauwD12gV6J7anH5SsLMFkb/MRuXD2tEAJSS7BrMlCTtN7qL7wLvyPoB4H17PTN yQdjLU6QhCgFpbu0HHC4DTE/F9CQNOqTmiSrz1Pum1+1uzznJZtfihtM/Tfmd/rdDqch UF64oaiG5nnC095hyaInmFeuzuWmpOsR3NRB5FgJG1b4WGvGdm6zYfbHlQkBINWdBcN/ UKLMPmLha0dGUkmEZSW+9EwUkoQfhJ52xuleWlUz4KMtQOfGpZX4twT2AOHpdSJidzjO cFSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cVRBVTdI; 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 e7si1386207pfi.290.2021.05.06.13.31.51; Thu, 06 May 2021 13:32:04 -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=cVRBVTdI; 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 S233730AbhEFUTH (ORCPT + 99 others); Thu, 6 May 2021 16:19:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233714AbhEFUTF (ORCPT ); Thu, 6 May 2021 16:19:05 -0400 Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B045C061574 for ; Thu, 6 May 2021 13:18:07 -0700 (PDT) Received: by mail-io1-xd29.google.com with SMTP id z24so5987775ioj.7 for ; Thu, 06 May 2021 13:18:07 -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=9FpP0oYJ5o5WVaMKta2Gw4ucGc3wzpzA9zy0oojQ72k=; b=cVRBVTdIe2G3kEyjvc30fD55v7pIgnGqkAdDAuF8SLTWZ5d6SygwRGMbmQH2+zfG59 M+DLsIJSLgq5ec6kQZI4yAaUaE/xcdKWyxfVHH/1yKTbCTcI7TBq4YGrQFMsU2Pt4OPo WfsO8GItV90u8aye4WD+uDdHHD6LrTbZdY9JM= 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=9FpP0oYJ5o5WVaMKta2Gw4ucGc3wzpzA9zy0oojQ72k=; b=rvzEfwkZiAxGZ41ovaphrVgEpVMws2qN5LhNXouJi1OY5w2/Gwj1QgGIkzIwHjNT2D BL26uYIoaYCVsIb7qwjIPqOpgjwR+ZyszZSE/p8cJQBt4PooucHweOgBF+lesSHwKRmb 1yAXgTu7ZVN5GGIgdBFHpyNWFKU9x6rD5J+bFBQ/ptDeIktGz2+nklVamSKHq2guuzie HxfMImucrePfe8nj3BQOcpvunfatZ3vrR5o6zzLcqabWDzP+6ZMLyRFOweyMhV6jbVrR Hj9s5N8khxwrU//d3IWdZCv1uIkhepnf6hkijLxd05gIV9hwq/LY6nPQT1phTSj3647V ojbQ== X-Gm-Message-State: AOAM531PC6r7XYMlf744Kw6wHCrqKvdeCdRyfYL0ShkIey6HCOiSFFrA OmsShhtRLlRKMi42J/5UIMM70q2cEmmEZYbCZ2dXXw== X-Received: by 2002:a6b:dc06:: with SMTP id s6mr4938895ioc.130.1620332286076; Thu, 06 May 2021 13:18:06 -0700 (PDT) MIME-Version: 1.0 References: <20210505162307.2545061-1-revest@chromium.org> In-Reply-To: From: Florent Revest Date: Thu, 6 May 2021 22:17:54 +0200 Message-ID: Subject: Re: [PATCH bpf] bpf: Don't WARN_ON_ONCE in bpf_bprintf_prepare To: Andrii Nakryiko Cc: Daniel Borkmann , bpf , Alexei Starovoitov , Andrii Nakryiko , KP Singh , Brendan Jackman , Stanislav Fomichev , open list , syzbot Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 6, 2021 at 8:52 PM Andrii Nakryiko wrote: > > On Wed, May 5, 2021 at 3:29 PM Florent Revest wrote: > > > > On Wed, May 5, 2021 at 10:52 PM Andrii Nakryiko > > wrote: > > > > > > On Wed, May 5, 2021 at 1:48 PM Andrii Nakryiko > > > wrote: > > > > > > > > On Wed, May 5, 2021 at 1:00 PM Daniel Borkmann wrote: > > > > > > > > > > On 5/5/21 8:55 PM, Andrii Nakryiko wrote: > > > > > > On Wed, May 5, 2021 at 9:23 AM Florent Revest wrote: > > > > > >> > > > > > >> The bpf_seq_printf, bpf_trace_printk and bpf_snprintf helpers share one > > > > > >> per-cpu buffer that they use to store temporary data (arguments to > > > > > >> bprintf). They "get" that buffer with try_get_fmt_tmp_buf and "put" it > > > > > >> by the end of their scope with bpf_bprintf_cleanup. > > > > > >> > > > > > >> If one of these helpers gets called within the scope of one of these > > > > > >> helpers, for example: a first bpf program gets called, uses > > > > > > > > > > > > Can we afford having few struct bpf_printf_bufs? They are just 512 > > > > > > bytes, so can we have 3-5 of them? Tracing low-level stuff isn't the > > > > > > only situation where this can occur, right? If someone is doing > > > > > > bpf_snprintf() and interrupt occurs and we run another BPF program, it > > > > > > will be impossible to do bpf_snprintf() or bpf_trace_printk() from the > > > > > > second BPF program, etc. We can't eliminate the probability, but > > > > > > having a small stack of buffers would make the probability so > > > > > > miniscule as to not worry about it at all. > > > > > > > > > > > > Good thing is that try_get_fmt_tmp_buf() abstracts all the details, so > > > > > > the changes are minimal. Nestedness property is preserved for > > > > > > non-sleepable BPF programs, right? If we want this to work for > > > > > > sleepable we'd need to either: 1) disable migration or 2) instead of > > > > > > > > oh wait, we already disable migration for sleepable BPF progs, so it > > > > should be good to do nestedness level only > > > > > > actually, migrate_disable() might not be enough. Unless it is > > > impossible for some reason I miss, worst case it could be that two > > > sleepable programs (A and B) can be intermixed on the same CPU: A > > > starts&sleeps - B starts&sleeps - A continues&returns - B continues > > > and nestedness doesn't work anymore. So something like "reserving a > > > slot" would work better. > > > > Iiuc try_get_fmt_tmp_buf does preempt_enable to avoid that situation ? > > > > > > > > > > > > assuming a stack of buffers, do a loop to find unused one. Should be > > > > > > acceptable performance-wise, as it's not the fastest code anyway > > > > > > (printf'ing in general). > > > > > > > > > > > > In any case, re-using the same buffer for sort-of-optional-to-work > > > > > > bpf_trace_printk() and probably-important-to-work bpf_snprintf() is > > > > > > suboptimal, so seems worth fixing this. > > > > > > > > > > > > Thoughts? > > > > > > > > > > Yes, agree, it would otherwise be really hard to debug. I had the same > > > > > thought on why not allowing nesting here given users very likely expect > > > > > these helpers to just work for all the contexts. > > > > > > > > > > Thanks, > > > > > Daniel > > > > What would you think of just letting the helpers own these 512 bytes > > buffers as local variables on their stacks ? Then bpf_prepare_bprintf > > would only need to write there, there would be no acquire semantic > > (like try_get_fmt_tmp_buf) and the stack frame would just be freed on > > the helper return so there would be no bpf_printf_cleanup either. We > > would also not pre-reserve static memory for all CPUs and it becomes > > trivial to handle re-entrant helper calls. > > > > I inherited this per-cpu buffer from the pre-existing bpf_seq_printf > > code but I've not been convinced of its necessity. > > I got the impression that extra 512 bytes on the kernel stack is quite > a lot and that's why we have per-cpu buffers. Especially that > bpf_trace_printk() can be called from any context, including NMI. Ok, I understand. What about having one buffer per helper, synchronized with a spinlock? Actually, bpf_trace_printk already has that, not for the bprintf arguments but for the bprintf output so this wouldn't change much to the performance of the helpers anyway: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/kernel/trace/bpf_trace.c?id=9d31d2338950293ec19d9b095fbaa9030899dcb4#n385 These helpers are not performance sensitive so a per-cpu stack of buffers feels over-engineered to me (and is also complexity I feel a bit uncomfortable with).