Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp984814ybt; Fri, 26 Jun 2020 17:09:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwT8fTswxd/2/nnU91tOcipH9ssfQurCQHnH7TvvANkGAEWILNRAsOxgAW4FwhMv5v9EdMY X-Received: by 2002:a17:906:375a:: with SMTP id e26mr4980132ejc.324.1593216542812; Fri, 26 Jun 2020 17:09:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593216542; cv=none; d=google.com; s=arc-20160816; b=Q7tryrqZ0E3D0Ww+HkkPJ2rCzOeKwYhpUDelyifWUIFiBkixaqf0ET/0n/snr2Y46x x6jU5kPbpHRqiRVpAo1Y8x3lN755zRus0Xdd8KQMZM99P39RzQb77FRMGTA64BHSagfT yJniFPv96w7mOwdp3N868n+/p1lEQlx81t2KqBiSW9dsDgEVgYVf/HzCoE1iakBpdLUO xUNopPo1QwyN2wY0OEd/Dn1wfKPtSWOlIWB18jTg/JxNntlg8j9bESdgPy6Xu09IYeVg lVYu6WyedUhHujSFKZBT7LpAmyaBQDfKzgW7z21u6xfrvHEfIe5bWHNCp8dmlHPBH7O3 clgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=udBllLsYA6lB00cOOJeSI526QRhFyN1UuKw5hU7KkHo=; b=o2QWvhnL4z8nAUGE8vtiwwsc8Nl2BP2/yv+tDnvyK4QhlygTS6vzbS7GDNz/2KSv5U 2SmypQUrK6TlVYE+sl26tOEVCxpSjLvF9f3sz+ZURpa7usIBz1W/dcajRSaRyCS6XLvF H5IQGAblQKboVm4RgYw16mCMefh5QuCfx51QMKZK3a4+2cMzQKGgvmncre5L7/5IgEkM uJaLBNJjJo8ebDoAwIUSp/K9Y1y7Z7LydUkLp6lLMxmjYXzAPOIdHw1eJToXjpQvqjbW LHdLFsJ5+khkzZDBp0A4SYPrqqbp0VLJ0HZirvoK+IO1odn6Y1Lq2cFL1UmJEuce7frm fAbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=febIOE8b; 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 a2si19219090edk.226.2020.06.26.17.08.32; Fri, 26 Jun 2020 17:09:02 -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=febIOE8b; 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 S1726426AbgF0AGh (ORCPT + 99 others); Fri, 26 Jun 2020 20:06:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726086AbgF0AGg (ORCPT ); Fri, 26 Jun 2020 20:06:36 -0400 Received: from mail-qt1-x842.google.com (mail-qt1-x842.google.com [IPv6:2607:f8b0:4864:20::842]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FC77C03E979; Fri, 26 Jun 2020 17:06:36 -0700 (PDT) Received: by mail-qt1-x842.google.com with SMTP id u17so8862412qtq.1; Fri, 26 Jun 2020 17:06:36 -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=udBllLsYA6lB00cOOJeSI526QRhFyN1UuKw5hU7KkHo=; b=febIOE8be8srwpirNRekGtuSASef3Zu8G9ORyVKjmYgcKvf3IAu/EwRI7QJA5s9B/N 9868iMPjM0OFZaxWhKExEh2ykodEtbpFhCfvumLlH+hRxklCxE8AKH4yhK/gAlwnvMWQ kkMhttGQYvOcdHYJis2B87ZT2ajqVkhYVyxFB1GyNfyedwKHVPZT4uWQsNZpAnOfQ5Yc 0zEmUg5QTLwt1TzOPN+WQTqftA+O8mmi7bczGKtOVT9j9A1U7keM7z9PO/RMzAXwvePf b6ZAf+nMvtrtc6lawua5zQLI6kDnEbPrm4ahPvf18ImFrhyqhymGPsYCIwi/gKMwFyiX a+lQ== 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=udBllLsYA6lB00cOOJeSI526QRhFyN1UuKw5hU7KkHo=; b=Mxx+SR86CYUPIg5FSiWSE2csxDHwrXoph9uW6en6XAmNlKH1ajvPnx1BwMQdYvstfE 0I2mtYWqoLtmldPBkihPrEBMswmNS9yZb8T9RMpyH0X/P/MWLU2nrUONIaWSKoi7k4yh HnMGCDKDQnLV23Gf/XUWPqdvncHPBIzJO+lQp2L8Fiel4XlALGZwYy4+vV4sqOHRrIiQ PtMG3I6bpn7ILseOzTVnErxyByaEnIpDjhvHH7cqwaIB6BCfTbDEO+4sgMOJIlXV5I2P KB2nJBlsCId1BmtkRgGyeisJ4kPphaXwM5pYENfxToaMEI2GjJUGOhtctJu42FtZSOHs yLCw== X-Gm-Message-State: AOAM5302MaKWJ8S64nRz55RKec2vrenyO9Jzx6sG0ESKm49CJZtIlUue +M4JK7kkPVbpKIw6ZuUEbyy2Y5WWnIPaOh1WG6Y= X-Received: by 2002:ac8:1991:: with SMTP id u17mr5271867qtj.93.1593216395389; Fri, 26 Jun 2020 17:06:35 -0700 (PDT) MIME-Version: 1.0 References: <20200626001332.1554603-1-songliubraving@fb.com> <20200626001332.1554603-3-songliubraving@fb.com> In-Reply-To: From: Andrii Nakryiko Date: Fri, 26 Jun 2020 17:06:24 -0700 Message-ID: Subject: Re: [PATCH v2 bpf-next 2/4] bpf: introduce helper bpf_get_task_stak() To: Song Liu Cc: bpf , Networking , open list , Peter Ziljstra , Alexei Starovoitov , Daniel Borkmann , Kernel Team , john fastabend , KP Singh Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 26, 2020 at 4:47 PM Song Liu wrote: > > > > > On Jun 26, 2020, at 3:51 PM, Andrii Nakryiko wrote: > > > > On Fri, Jun 26, 2020 at 3:45 PM Song Liu wrote: > >> > >> > >> > >>> On Jun 26, 2020, at 1:17 PM, Andrii Nakryiko wrote: > >>> > >>> On Thu, Jun 25, 2020 at 5:14 PM Song Liu wrote: > >>>> > >>>> Introduce helper bpf_get_task_stack(), which dumps stack trace of given > >>>> task. This is different to bpf_get_stack(), which gets stack track of > >>>> current task. One potential use case of bpf_get_task_stack() is to call > >>>> it from bpf_iter__task and dump all /proc//stack to a seq_file. > >>>> > >>>> bpf_get_task_stack() uses stack_trace_save_tsk() instead of > >>>> get_perf_callchain() for kernel stack. The benefit of this choice is that > >>>> stack_trace_save_tsk() doesn't require changes in arch/. The downside of > >>>> using stack_trace_save_tsk() is that stack_trace_save_tsk() dumps the > >>>> stack trace to unsigned long array. For 32-bit systems, we need to > >>>> translate it to u64 array. > >>>> > >>>> Signed-off-by: Song Liu > >>>> --- > >>> > >>> Looks great, I just think that there are cases where user doesn't > >>> necessarily has valid task_struct pointer, just pid, so would be nice > >>> to not artificially restrict such cases by having extra helper. > >>> > >>> Acked-by: Andrii Nakryiko > >> > >> Thanks! > >> > >>> > >>>> include/linux/bpf.h | 1 + > >>>> include/uapi/linux/bpf.h | 35 ++++++++++++++- > >>>> kernel/bpf/stackmap.c | 79 ++++++++++++++++++++++++++++++++-- > >>>> kernel/trace/bpf_trace.c | 2 + > >>>> scripts/bpf_helpers_doc.py | 2 + > >>>> tools/include/uapi/linux/bpf.h | 35 ++++++++++++++- > >>>> 6 files changed, 149 insertions(+), 5 deletions(-) > >>>> > >>> > >>> [...] > >>> > >>>> + /* stack_trace_save_tsk() works on unsigned long array, while > >>>> + * perf_callchain_entry uses u64 array. For 32-bit systems, it is > >>>> + * necessary to fix this mismatch. > >>>> + */ > >>>> + if (__BITS_PER_LONG != 64) { > >>>> + unsigned long *from = (unsigned long *) entry->ip; > >>>> + u64 *to = entry->ip; > >>>> + int i; > >>>> + > >>>> + /* copy data from the end to avoid using extra buffer */ > >>>> + for (i = entry->nr - 1; i >= (int)init_nr; i--) > >>>> + to[i] = (u64)(from[i]); > >>> > >>> doing this forward would be just fine as well, no? First iteration > >>> will cast and overwrite low 32-bits, all the subsequent iterations > >>> won't even overlap. > >> > >> I think first iteration will write zeros to higher 32 bits, no? > > > > Oh, wait, I completely misread what this is doing. It up-converts from > > 32-bit to 64-bit, sorry. Yeah, ignore me on this :) > > > > But then I have another question. How do you know that entry->ip has > > enough space to keep the same number of 2x bigger entries? > > The buffer is sized for sysctl_perf_event_max_stack u64 numbers. > stack_trace_save_tsk() will put at most stack_trace_save_tsk unsigned > long in it (init_nr == 0). So the buffer is big enough. > Awesome, thanks for clarification! > Thanks, > Song