Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp944520ybt; Fri, 26 Jun 2020 15:51:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyklMlB1kOm/MLW4PQ3fqI0VZtmN1yGIf2PtrNQtPwZUqQ+j5lmF/kOFqNsL6xEh8/aqf7/ X-Received: by 2002:a17:906:7694:: with SMTP id o20mr4521417ejm.289.1593211912439; Fri, 26 Jun 2020 15:51:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593211912; cv=none; d=google.com; s=arc-20160816; b=Wl/8V25zRNfk75oBPd85RB6jqllY9+fR+zKhE38oGsC8RqUjQnYWQ71i/zrdNW3tKY rTN9Q8REyruToIwExXo1nFFRaQhJqn+WkIp6AOZTUp3k1tUKxNwtWwX9EeETXjFRkjBy Cz/pa5oz7bVxrtoT3Bp6LfpvEwJ1VRIgswXWVC2VY2suu8mK5O2Y4udjEQxEAJoaQMCK +YwfP05fHBlDzs8/v+6SUHwcFUoELNpI4F/gfLiguZtKnq+Lyz6EDkyz3qpQyIvvcBqC b9WeACp/+flPBOAFv8TeuXhSp6XcLHoVsnQ2nYXoUT9hwcmdkmXre7O87gacuf6ZUaDa jLIQ== 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=8HWO0SS+0P6uz8QKcTGg5GM74omnlEcycJKWzTuQqgw=; b=mpMB65sZr6OMdSecM1jAbJI3efGuMOiFFs1xGW3q9hNbqRJqbnHOqCNkDobiNoNuv8 uo2qYf49fjB/cYHz4wC65zb3xKh7MuLIoVGFlbzDzMQDlmCli/zc65K0TTIXYRvw0oOR dQk2H3mUJ23A4E0aC8di2qiazuA61hrGiG2CvmP8rcUwJhRcUEms5bUjgXx9H2mmbt5P aPyfQ5UIPmxj006VqR4Hvv3sk/xHVsWv/j4WGK7SU2Mlr2Y1pmopWWKKZ11VfNMOGnMD kHXIOuNO+A8HXP1wN8k5lTIRtVJHsVMdUHuPvIkX4x3nOCzsEP7Uk01STtrza4gQeiRK T1pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=JIF9V2ne; 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 d21si1462061eje.437.2020.06.26.15.51.28; Fri, 26 Jun 2020 15:51: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=JIF9V2ne; 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 S1725971AbgFZWvV (ORCPT + 99 others); Fri, 26 Jun 2020 18:51:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725883AbgFZWvU (ORCPT ); Fri, 26 Jun 2020 18:51:20 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 821F7C03E979; Fri, 26 Jun 2020 15:51:20 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id h23so8769276qtr.0; Fri, 26 Jun 2020 15:51:20 -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=8HWO0SS+0P6uz8QKcTGg5GM74omnlEcycJKWzTuQqgw=; b=JIF9V2neAo3LDYaCCpQHM7tlzFYnCVM6vrrwKOwdygR9cYlw9fFpc1I+ByHQ7ggp1h wOxdl5nacpcSjF9pEmq7bohWDFU6y4iO2mY7OVe0vmmdXzht2Mc82LD8/g+5YwqJtVxi +xQ5TzaQDkEzOLHLjD/eISW23FIoftXnaueWUrJJ5g8pO36wlOjoc8jAJR3ToOVDlWra OBGfbY7vxOZAhaGoPnPkTki3lsaw8JPkFxqLEuKsOys93f0KupsXHS4KFFIKVfM5knYX GCia7wp7cLOAa71CK+GTY2RjEXJgQ4tutKVD4H5DRxhCMGMOF3Jr9ViS9dFXHTDtwho0 BYwg== 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=8HWO0SS+0P6uz8QKcTGg5GM74omnlEcycJKWzTuQqgw=; b=dR1L4InE+E7pqYtVAIF53O4gON3PUqS7ON6DIeJ0wIzCGIFnp64VmnJbtacEowOMez yy+ShEv9LpZNeoHQr3aicnRI4kzs3zJYJsJrJ55mAz+MDqeQAMTUxWJgoT8r7qSTzj3G NCfXcOGkNDTWJzFt2FzgUBeruu2LihntoV4w7EdRa8rKualllm+12C0j2DWcGfX8lw1Z 62xaNCDBAFSO4q2XLqVmEhZ8ja4zJytGytpAHo8ItUEpeh/tmQnT+q8PsTWa+Blg0F0Z uUxzj5qrxWkJrCL8OrZua21o8D+WoiN7CyijHbG6UXHAwbUu5zipXWUf7/8jdwHsHaT2 Qfig== X-Gm-Message-State: AOAM530KGxC1dQ8Hskk/zgTlOUY+DH/+jxG9/85jWUDd/4VXl1Gh0SVx HG1GwZrqKVp2JYOO2Dr1+01ySWCSxtdY9svMeGA= X-Received: by 2002:ac8:1991:: with SMTP id u17mr5021097qtj.93.1593211879622; Fri, 26 Jun 2020 15:51:19 -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 15:51:08 -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 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? > > > > >> + } > >> + > >> +exit_put: > >> + put_callchain_entry(rctx); > >> + > >> + return entry; > >> +} > >> + > > > > [...] > > > >> +BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf, > >> + u32, size, u64, flags) > >> +{ > >> + struct pt_regs *regs = task_pt_regs(task); > >> + > >> + return __bpf_get_stack(regs, task, buf, size, flags); > >> +} > > > > > > So this takes advantage of BTF and having a direct task_struct > > pointer. But for kprobes/tracepoint I think it would also be extremely > > helpful to be able to request stack trace by PID. How about one more > > helper which will wrap this one with get/put task by PID, e.g., > > bpf_get_pid_stack(int pid, void *buf, u32 size, u64 flags)? Would that > > be a problem? > > That should work. Let me add that in a follow up patch. >