From patchwork Mon May 17 09:28:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florent Revest X-Patchwork-Id: 12261307 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96F2CC433B4 for ; Mon, 17 May 2021 09:28:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 71305601FC for ; Mon, 17 May 2021 09:28:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235985AbhEQJaJ (ORCPT ); Mon, 17 May 2021 05:30:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235940AbhEQJaI (ORCPT ); Mon, 17 May 2021 05:30:08 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78325C06174A for ; Mon, 17 May 2021 02:28:52 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id n17-20020a7bc5d10000b0290169edfadac9so4749110wmk.1 for ; Mon, 17 May 2021 02:28:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=UekJLfYsQQ9U7jHP9ExccSivPWVMtsjWSFlJO7W1AFc=; b=KJKsXdQjOO0+jmpc8hEvK/54LqAG2bMlKXcGcaSpRnRl9aPY3ViNRijH2EPhT1uwDJ nixOf6hXly9CfjbDPcjFKfydJMCNHxeXeiY51cbM1hs4PGVvxUOHQuE5cwcqNMuJqaZX m6SvFk+nqadDBxYbp73QHhrzUCngFiWyRwhRU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=UekJLfYsQQ9U7jHP9ExccSivPWVMtsjWSFlJO7W1AFc=; b=PFrrLv8UVZUQr/5T7GLa23Wa3H5pnMju4niJKiJdw6+r6/zHcIlqc63Cim1QAcXkDW mBv5IGFjenUQQgeH6puusUs6Y2fs3b3+tKtHX6acYM9Zj5XiF5W2sAeDrLQxUpb8ncwb d7U8VcWTni6zSLkbuOlv9lqU96U2oQd4cnJ4cBSpk79iPQ12vHsJAk8IF0xFo/Ob/XV2 Ek2xb1SKYsb79mhdeDMVWDhwGOc/1iyxwHpj3H8y8J4OuslduIm3gBNaBGkgX6hHu1eW 240DlTyXz/i//v/3hSdY8ngwzafJ8U7DBMcMxZHYJtxUPiICQnefeY6z5y1bNIIeKJjH Ly+g== X-Gm-Message-State: AOAM533vzAbPtwxAjixKm6A2GVRMgTauqC0NPQ39gWDcrVFEm8BHCk/c MEEzl5ZILWdxOlS2ghbjMvKe6z4gzc7T4Q== X-Google-Smtp-Source: ABdhPJxa+9Z5sLcMkeGMTF4+AK2meXbrrogNh6WPRkBmSXgeC4y9gYxgwoIBeP/8WproL9fM81bSVg== X-Received: by 2002:a05:600c:3545:: with SMTP id i5mr21813454wmq.43.1621243730802; Mon, 17 May 2021 02:28:50 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:61:302:6c6d:556b:3e10:9fd0]) by smtp.gmail.com with ESMTPSA id e8sm16316616wrt.30.2021.05.17.02.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 02:28:50 -0700 (PDT) From: Florent Revest To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kpsingh@kernel.org, jackmanb@google.com, linux-kernel@vger.kernel.org, Florent Revest Subject: [PATCH bpf 1/2] bpf: Clarify a bpf_bprintf_prepare macro Date: Mon, 17 May 2021 11:28:29 +0200 Message-Id: <20210517092830.1026418-1-revest@chromium.org> X-Mailer: git-send-email 2.31.1.751.gd2f1c929bd-goog MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The per-cpu buffers contain bprintf data rather than printf arguments. The macro name and comment were a bit confusing, this rewords them in a clearer way. Signed-off-by: Florent Revest Acked-by: Song Liu --- kernel/bpf/helpers.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index ef658a9ea5c9..3a5ab614cbb0 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -692,13 +692,14 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, return -EINVAL; } -/* Per-cpu temp buffers which can be used by printf-like helpers for %s or %p +/* Per-cpu temp buffers used by printf-like helpers to store the bprintf binary + * arguments representation. */ -#define MAX_PRINTF_BUF_LEN 512 +#define MAX_BPRINTF_BUF_LEN 512 /* Support executing three nested bprintf helper calls on a given CPU */ struct bpf_bprintf_buffers { - char tmp_bufs[3][MAX_PRINTF_BUF_LEN]; + char tmp_bufs[3][MAX_BPRINTF_BUF_LEN]; }; static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs); static DEFINE_PER_CPU(int, bpf_bprintf_nest_level); @@ -761,7 +762,7 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args, if (num_args && try_get_fmt_tmp_buf(&tmp_buf)) return -EBUSY; - tmp_buf_end = tmp_buf + MAX_PRINTF_BUF_LEN; + tmp_buf_end = tmp_buf + MAX_BPRINTF_BUF_LEN; *bin_args = (u32 *)tmp_buf; } From patchwork Mon May 17 09:28:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florent Revest X-Patchwork-Id: 12261309 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4682AC43460 for ; Mon, 17 May 2021 09:28:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 286B4601FC for ; Mon, 17 May 2021 09:28:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235544AbhEQJaK (ORCPT ); Mon, 17 May 2021 05:30:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236001AbhEQJaJ (ORCPT ); Mon, 17 May 2021 05:30:09 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29851C06174A for ; Mon, 17 May 2021 02:28:53 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id r12so5628485wrp.1 for ; Mon, 17 May 2021 02:28:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QvrLOSKHI2fJCJkZwbWCONQINEKi3W3/3dDgjZ+UXwI=; b=WYAd48+QKjaDH4Za9nTjih8+OmyktTPOPPWIQT4yMZmQCxNHFuR4BubBE16LGLQEao kWYJ2ZOTBbQJotgYER3zKidiH+E+bHX94NammSoQUi3H2g81QUBBvkbimcBB4MduAba+ j6/5QNkZ7avwu/ggX5PFmUuuTk6hlgGv0CrZU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QvrLOSKHI2fJCJkZwbWCONQINEKi3W3/3dDgjZ+UXwI=; b=HuH7fUBrE7c0sX/fkiDgDvJyMGkaeMhNTJbyk8EmKtVVtSkmdSRsysWCaFGK1BkEwa FyvZ/jMczZxkpvhQPOkCSux46hOP7KxUW0GAgNqy6RVCp5Qi12DL7fyA5dWPf1FsqBEF gI0aExOKnQkes78VeD5iBl1bqrUoe6fOB5PdU3eNYlasrn0BgKF5tVKLCqdpPQejIokN v8YD7eIQR7dqHRi5AYqto4MQAj+IdBMf8A+bPUjkybRXVtlAxjuPTu0UdhQRvLEuAFA1 k6TxrcoRVwNZOLRIc2emlkOfCYEa9cN1ghKpUk/2GIujYLcygh+6EwYujdpcsRtwZKD+ engw== X-Gm-Message-State: AOAM533dRLabLfCB3N+R0NDveQsZolngQyEUQmFcoyBwypfnev1FlqUF QCMsQDqVE1Ld8qrNzuWGlxh/lyKcPvzmSA== X-Google-Smtp-Source: ABdhPJzPLDy87vJgeW0xxhn0RMCksPboSqnFJDWIeZrZI3CbzDCy9W5urLtEZkYI/bGaCetCj494aA== X-Received: by 2002:adf:dfca:: with SMTP id q10mr28440440wrn.343.1621243731714; Mon, 17 May 2021 02:28:51 -0700 (PDT) Received: from revest.zrh.corp.google.com ([2a00:79e0:61:302:6c6d:556b:3e10:9fd0]) by smtp.gmail.com with ESMTPSA id e8sm16316616wrt.30.2021.05.17.02.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 May 2021 02:28:51 -0700 (PDT) From: Florent Revest To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kpsingh@kernel.org, jackmanb@google.com, linux-kernel@vger.kernel.org, Florent Revest , kernel test robot Subject: [PATCH bpf 2/2] bpf: Avoid using ARRAY_SIZE on an uninitialized pointer Date: Mon, 17 May 2021 11:28:30 +0200 Message-Id: <20210517092830.1026418-2-revest@chromium.org> X-Mailer: git-send-email 2.31.1.751.gd2f1c929bd-goog In-Reply-To: <20210517092830.1026418-1-revest@chromium.org> References: <20210517092830.1026418-1-revest@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The cppcheck static code analysis reported the following error: >> helpers.c:713:43: warning: Uninitialized variable: bufs [uninitvar] if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) { ^ ARRAY_SIZE is a macro that expands to sizeofs, so bufs is not actually dereferenced at runtime, and the code is actually safe. But to keep things tidy, this patch removes the need for a call to ARRAY_SIZE by extracting the size of the array into a macro. Cppcheck should no longer be confused and the code ends up being a bit cleaner. Reported-by: kernel test robot Signed-off-by: Florent Revest Acked-by: Song Liu --- kernel/bpf/helpers.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 3a5ab614cbb0..73443498d88f 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -698,8 +698,9 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, #define MAX_BPRINTF_BUF_LEN 512 /* Support executing three nested bprintf helper calls on a given CPU */ +#define MAX_BPRINTF_NEST_LEVEL 3 struct bpf_bprintf_buffers { - char tmp_bufs[3][MAX_BPRINTF_BUF_LEN]; + char tmp_bufs[MAX_BPRINTF_NEST_LEVEL][MAX_BPRINTF_BUF_LEN]; }; static DEFINE_PER_CPU(struct bpf_bprintf_buffers, bpf_bprintf_bufs); static DEFINE_PER_CPU(int, bpf_bprintf_nest_level); @@ -711,7 +712,7 @@ static int try_get_fmt_tmp_buf(char **tmp_buf) preempt_disable(); nest_level = this_cpu_inc_return(bpf_bprintf_nest_level); - if (WARN_ON_ONCE(nest_level > ARRAY_SIZE(bufs->tmp_bufs))) { + if (WARN_ON_ONCE(nest_level > MAX_BPRINTF_NEST_LEVEL)) { this_cpu_dec(bpf_bprintf_nest_level); preempt_enable(); return -EBUSY;