Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp224957pxb; Wed, 20 Oct 2021 20:47:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyAyYtM79ZQWTWe7FowiFp2wTiWm/nSC3zjmmLLUqmpjh4hwkd9MMtnv+duWzbXS+C0UsCV X-Received: by 2002:a17:90a:d0f:: with SMTP id t15mr246139pja.7.1634788025533; Wed, 20 Oct 2021 20:47:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634788025; cv=none; d=google.com; s=arc-20160816; b=qSu1sbTnoc0nYD7mrKkHtPSsk0HrWosVL7p9jGU4LjpaSjbMdoNyrZv+1/RUT0h35Z gVUoJPV2ug2zyADxJi5MeywqhYt1cNO1w5A7wIE2iyTGJ7czqLUynwpSxNYV8UpvYzg1 EqR25CT3k6Xkcwwo2JtZGq/AqswQHl6tKJfhPv9VtMEh0QYIGGX/ajZLZ9p0xswej14d 1BQJn1mtzxN9zSeEaOc08zQMhIc7IPbkD7fVY5Gz/A9ArdXxWVX+oMV99fdQkjDPDIgK QogC/uJOPbJ47qlOvVDY6uhjNozrxZA+cEH7Zg9GN/e6tKTyO91fCvxKtgHwTpvUz0zM qQNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0QORRqOHwrTwPIk1mXEbyzyPg28H9YEY40+Pmmc8asE=; b=LG687pGosTGN4LqQn+K5rbBLZ4UGSzwpc9732dNXvj+MRCEcE6FzXWZyMEjf1sbaqX sKN4bblUvg6Hrha653OId2DvrBt0w6OGgSIjUFe8SJMKD1IEe0VeekukW/WEOluRjmGt ZEo/94156EYZnjryjoM26AokezuC9Ujze5Ofpt9wJYPNcGwM52mnZaVYeJ1CyEAYBGrQ zBPAQBcV8o4S3DEYo8Ce8XuztS2kC8kFF+9YQYe9iUKWQjd5wssD5s7B1VTM1mYT1hb8 mvfS2WeXe4Z6I9puiJ+YyZaqAy2lZVJPhRDjVZzpxYMfo/i7bc9qbaOQ/gkpeUr2vqxK msmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=V1HK9LH7; 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 g5si5587557plt.215.2021.10.20.20.46.53; Wed, 20 Oct 2021 20:47:05 -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=20210112 header.b=V1HK9LH7; 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 S231740AbhJUDsM (ORCPT + 99 others); Wed, 20 Oct 2021 23:48:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231488AbhJUDrv (ORCPT ); Wed, 20 Oct 2021 23:47:51 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15838C061768; Wed, 20 Oct 2021 20:45:34 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id h193so8968890pgc.1; Wed, 20 Oct 2021 20:45:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0QORRqOHwrTwPIk1mXEbyzyPg28H9YEY40+Pmmc8asE=; b=V1HK9LH74t5jFt8aC0mQynUQ0OO5jCHgGHTNpaWFQ7MvGI6et/N8hKxn+fo0ROPbxI X24+vc6jVTq7SBPM7lpRMdzSkXXDvUvwkAEB9Ca288nYX+jdpV20GIBDRvxFJ3OadNKf cc0c+VBrnYfcHYSUxdpLpvNMRLxVlNewekDxhtpJtvYrKLskikR5v+I2CueBT4+hDeKV nLLDLVzSqdX+/mObPaUOvHrS2E481ot8B5QnnlaEgy7kf0DQZKK3cCnoQsfJgpfCzzSw HXQBpcQxdRBDH5PVrxFQ2PMsS2qk34pUvloDV4jFrL4TDJUkfWEytihrp1r8NFYlEww6 SUog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0QORRqOHwrTwPIk1mXEbyzyPg28H9YEY40+Pmmc8asE=; b=3Uvqyb/GyCAbzq8aRqwoDIMCK8K/9qK1LrRzECf5TniaN5UMVxBVK5LW4N4QpP6DW1 WPSIJ5AzjiRZbCyuBgGbapgUvlVNt5u4MfD4eW7R0ZqJRXnEYiErT8/MsogUBMbxpG6K WWEEOW1rouxwEKrhgasnDrEISYyM1bf4O9yauk7X47f7hVBYuhMO+dWeMOoZcBXE9aiY DYPWr2P3XQfEuovYr64HkWnkP37+vWo2eaAmx+esSD01pR4hOKjlBzOwoBEa5CnMLArK OiFrLWFdshDTpbQCwzmLm/UoPHLU7Eneqt4QDTrGNdXjFDNladSeRgj1i8Fc0BC/DIgG dE+A== X-Gm-Message-State: AOAM530UquiW7qy6BhqIbeV/aLrGhZDJEAKycdcIIZArMPDLx5+uUyU2 rVQKTjSgn4pSwwkKvrblUtQ= X-Received: by 2002:a63:7d55:: with SMTP id m21mr2488194pgn.436.1634787933676; Wed, 20 Oct 2021 20:45:33 -0700 (PDT) Received: from localhost.localdomain ([140.82.17.67]) by smtp.gmail.com with ESMTPSA id bp19sm3651077pjb.46.2021.10.20.20.45.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 20:45:33 -0700 (PDT) From: Yafang Shao To: keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao Subject: [PATCH v5 07/15] samples/bpf/kern: use TASK_COMM_LEN instead of hard-coded 16 Date: Thu, 21 Oct 2021 03:45:14 +0000 Message-Id: <20211021034516.4400-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211021034516.4400-1-laoar.shao@gmail.com> References: <20211021034516.4400-1-laoar.shao@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The linux/sched.h is visible to the bpf kernel modules, so we can use TASK_COMM_LEN_16 to replace the hard-coded 16 in these bpf kernel modules to make it more grepable. In these bpf modules, someone gets task comm via bpf_get_current_comm(), which always get a nul terminated string. While someone gets task comm via bpf_probe_read_kernel(), which is unsafe, we should use bpf_probe_read_kernel_str() instead. Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Kees Cook Cc: Al Viro Cc: Petr Mladek --- samples/bpf/offwaketime_kern.c | 10 +++++----- samples/bpf/test_overhead_kprobe_kern.c | 11 ++++++----- samples/bpf/test_overhead_tp_kern.c | 5 +++-- samples/bpf/tracex2_kern.c | 3 ++- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/samples/bpf/offwaketime_kern.c b/samples/bpf/offwaketime_kern.c index 4866afd054da..c0fd04497eea 100644 --- a/samples/bpf/offwaketime_kern.c +++ b/samples/bpf/offwaketime_kern.c @@ -23,8 +23,8 @@ #define MAX_ENTRIES 10000 struct key_t { - char waker[TASK_COMM_LEN]; - char target[TASK_COMM_LEN]; + char waker[TASK_COMM_LEN_16]; + char target[TASK_COMM_LEN_16]; u32 wret; u32 tret; }; @@ -44,7 +44,7 @@ struct { } start SEC(".maps"); struct wokeby_t { - char name[TASK_COMM_LEN]; + char name[TASK_COMM_LEN_16]; u32 ret; }; @@ -113,11 +113,11 @@ static inline int update_counts(void *ctx, u32 pid, u64 delta) /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args { unsigned long long pad; - char prev_comm[16]; + char prev_comm[TASK_COMM_LEN_16]; int prev_pid; int prev_prio; long long prev_state; - char next_comm[16]; + char next_comm[TASK_COMM_LEN_16]; int next_pid; int next_prio; }; diff --git a/samples/bpf/test_overhead_kprobe_kern.c b/samples/bpf/test_overhead_kprobe_kern.c index f6d593e47037..31e8c5ee0cdc 100644 --- a/samples/bpf/test_overhead_kprobe_kern.c +++ b/samples/bpf/test_overhead_kprobe_kern.c @@ -6,6 +6,7 @@ */ #include #include +#include #include #include #include @@ -22,17 +23,17 @@ int prog(struct pt_regs *ctx) { struct signal_struct *signal; struct task_struct *tsk; - char oldcomm[16] = {}; - char newcomm[16] = {}; + char oldcomm[TASK_COMM_LEN_16] = {}; + char newcomm[TASK_COMM_LEN_16] = {}; u16 oom_score_adj; u32 pid; tsk = (void *)PT_REGS_PARM1(ctx); pid = _(tsk->pid); - bpf_probe_read_kernel(oldcomm, sizeof(oldcomm), &tsk->comm); - bpf_probe_read_kernel(newcomm, sizeof(newcomm), - (void *)PT_REGS_PARM2(ctx)); + bpf_probe_read_kernel_str(oldcomm, sizeof(oldcomm), &tsk->comm); + bpf_probe_read_kernel_str(newcomm, sizeof(newcomm), + (void *)PT_REGS_PARM2(ctx)); signal = _(tsk->signal); oom_score_adj = _(signal->oom_score_adj); return 0; diff --git a/samples/bpf/test_overhead_tp_kern.c b/samples/bpf/test_overhead_tp_kern.c index eaa32693f8fc..a6d5b3301af2 100644 --- a/samples/bpf/test_overhead_tp_kern.c +++ b/samples/bpf/test_overhead_tp_kern.c @@ -4,6 +4,7 @@ * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ +#include #include #include @@ -11,8 +12,8 @@ struct task_rename { __u64 pad; __u32 pid; - char oldcomm[16]; - char newcomm[16]; + char oldcomm[TASK_COMM_LEN_16]; + char newcomm[TASK_COMM_LEN_16]; __u16 oom_score_adj; }; SEC("tracepoint/task/task_rename") diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c index 5bc696bac27d..d70ce59055cb 100644 --- a/samples/bpf/tracex2_kern.c +++ b/samples/bpf/tracex2_kern.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -65,7 +66,7 @@ static unsigned int log2l(unsigned long v) } struct hist_key { - char comm[16]; + char comm[TASK_COMM_LEN_16]; u64 pid_tgid; u64 uid_gid; u64 index; -- 2.17.1