From patchwork Tue Feb 13 16:52:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 200523 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp667655dyb; Tue, 13 Feb 2024 08:52:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IHtXLgkLUnEThj+YF7BZtR8aboy7d816Is8vg9ZPHHOsnVMgY/98+WcTkYyx3rHTtiKno3I X-Received: by 2002:ac8:5806:0:b0:42d:b60c:f5be with SMTP id g6-20020ac85806000000b0042db60cf5bemr541321qtg.64.1707843138944; Tue, 13 Feb 2024 08:52:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707843138; cv=pass; d=google.com; s=arc-20160816; b=fctshs6w/mjr8X2mahCTocb7cE7ofsOEZ4dtgQf0DgNu1WdzpbxpPA+lVPrqTxCugW bRRz6+a06xtY+yc3PycCzWnoCa+ejI4YAcICZ9D2NLFORSTfSiI18H9NSNLs8U7Otg6L zQiLQpGQc/lbC3W+5hMAfmno5QEVnrwzeuvyIghI9DhXUf6VOJ3/WgSPGX3+qab1rgon t7cAqCbQBnQVT3kRSrAC7rvHhvLtowWNhffJBgC9mQYhedO+d92lWwguodZdoJeCTjJg Rhjwy44NhO4v9jtdtzh5c+QoE2AaF8z/t6XZyXKz/XeawVxoqn/lSYfFLPK/uhF89REV SQ8Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:subject:cc:to:from :date; bh=kJxahoG8HrQTyswV2v/+gaL3z5epO6twwsGaPWTfqJg=; fh=CzDjxWAWdgL6V/3RayszfouUEkp5qaZm0B/MvkU3qSc=; b=FzNWfNkByW4i/gobgqDeBOVYGDs/aHLZ7r5OssYLg2n/ZC7PhqIW/m2Bt3ipZ5f38f buR6w7sgiYKZAfEHRFk4OMVVwzUqp8h+IN2xEZe5qLX9a0x0Sdg7zMzZJep7ntcyfuop dPYC2WUkeEz82xOlkOiqwcmGgRSTpccIwBQOEcBMljeFExcpLOAL6GkI8WRPy4e5k5B4 TkkOhmZj5jKLfLOjrHGomMBsOyx6Nixd7cJmDDaRiHHJDA0E52nA830oaHxCPWlYfHya +WevdSzAz010GbZ6ycnxPXIVtkTQsSDCLgD4HXRBNHj2fbCCW6zPvL+HWSJHb7AZd8eA O7EA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-63941-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63941-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCUlFNJ3CLx3A4eM4CqIt3n+Kq7EXfd4zplV0J9Bv0aPpNnXP5kxhznQjFq9Oy04JrjghnqPNJWfjgk4hSF39pwCMZTwYQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id wg21-20020a05620a569500b00785ca61c6casi7636264qkn.309.2024.02.13.08.52.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 08:52:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63941-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-63941-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63941-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id A6FFB1C24B0F for ; Tue, 13 Feb 2024 16:52:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D45875FDA1; Tue, 13 Feb 2024 16:51:56 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EAA05FB90; Tue, 13 Feb 2024 16:51:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707843115; cv=none; b=fYH2UMgqGlLEJxwMGyCHz2yqTIv4nMkDlmgoyuCvJ8VIy4UWtCv+Loy3IPR+KxDMoyBcJVqbpJCQTKXmj6dwOiC7JaAOBRGbGli0y1KQLrRpur16lZSUeasBdQie1egyi8dSK+07x8YTM4z+gMGFzqqN2x8oIaEnXIk2PW1Zmsc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707843115; c=relaxed/simple; bh=pDNrTzFYVWPEbMC4V/M9gXVJh6i7WKDwFhKiB2AoIz0=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=iY+3DlZ8ipU1TY56c92vOQe5VlMmSvatVAtFINe7g1RNo9AMhudyMWUNNQGGLFaXRLJTh8izLLNiL9L42DzFd3LQNzQZ4b730wwWOBnAwiTyUpy2sCZny2JHLQbRloy6pwKPwcmFyJSZXZrbAMBj+cxe2MRTTfTB6uKDB1O8Woo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE644C433C7; Tue, 13 Feb 2024 16:51:49 +0000 (UTC) Date: Tue, 13 Feb 2024 11:52:32 -0500 From: Steven Rostedt To: LKML , Linux Trace Kernel Cc: Masami Hiramatsu , Mathieu Desnoyers , Mark Rutland , Tim Chen , Vincent Donnefort , Sven Schnelle , Mete Durlu Subject: [PATCH v2] tracing: Have saved_cmdlines arrays all in one allocation Message-ID: <20240213115232.5fd9e611@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790803327245202895 X-GMAIL-MSGID: 1790803327245202895 From: "Steven Rostedt (Google)" The saved_cmdlines have three arrays for mapping PIDs to COMMs: - map_pid_to_cmdline[] - map_cmdline_to_pid[] - saved_cmdlines The map_pid_to_cmdline[] is PID_MAX_DEFAULT in size and holds the index into the other arrays. The map_cmdline_to_pid[] is a mapping back to the full pid as it can be larger than PID_MAX_DEFAULT. And the saved_cmdlines[] just holds the COMMs associated to the pids. Currently the map_pid_to_cmdline[] and saved_cmdlines[] are allocated together (in reality the saved_cmdlines is just in the memory of the rounding of the allocation of the structure as it is always allocated in powers of two). The map_cmdline_to_pid[] array is allocated separately. Since the rounding to a power of two is rather large (it allows for 8000 elements in saved_cmdlines), also include the map_cmdline_to_pid[] array. (This drops it to 6000 by default, which is still plenty for most use cases). This saves even more memory as the map_cmdline_to_pid[] array doesn't need to be allocated. Link: https://lore.kernel.org/linux-trace-kernel/20240212174011.068211d9@gandalf.local.home/ Signed-off-by: Steven Rostedt (Google) Acked-by: Masami Hiramatsu (Google) --- Changes since v1: https://lore.kernel.org/linux-trace-kernel/20240212180941.379c419b@gandalf.local.home/ -- Added SAVED_CMDLINE_MAP_ELEMENT_SIZE helper macro. kernel/trace/trace_sched_switch.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c index e4fbcc3bede5..5f3e9bc87079 100644 --- a/kernel/trace/trace_sched_switch.c +++ b/kernel/trace/trace_sched_switch.c @@ -175,6 +175,10 @@ struct saved_cmdlines_buffer { }; static struct saved_cmdlines_buffer *savedcmd; +/* Holds the size of a cmdline and pid element */ +#define SAVED_CMDLINE_MAP_ELEMENT_SIZE(s) \ + (TASK_COMM_LEN + sizeof((s)->map_cmdline_to_pid[0])) + static inline char *get_saved_cmdlines(int idx) { return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN]; @@ -201,7 +205,7 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) int order; /* Figure out how much is needed to hold the given number of cmdlines */ - orig_size = sizeof(*s) + val * TASK_COMM_LEN; + orig_size = sizeof(*s) + val * SAVED_CMDLINE_MAP_ELEMENT_SIZE(s); order = get_order(orig_size); size = 1 << (order + PAGE_SHIFT); page = alloc_pages(GFP_KERNEL, order); @@ -212,16 +216,11 @@ static struct saved_cmdlines_buffer *allocate_cmdlines_buffer(unsigned int val) memset(s, 0, sizeof(*s)); /* Round up to actual allocation */ - val = (size - sizeof(*s)) / TASK_COMM_LEN; + val = (size - sizeof(*s)) / SAVED_CMDLINE_MAP_ELEMENT_SIZE(s); s->cmdline_num = val; - s->map_cmdline_to_pid = kmalloc_array(val, - sizeof(*s->map_cmdline_to_pid), - GFP_KERNEL); - if (!s->map_cmdline_to_pid) { - free_saved_cmdlines_buffer(s); - return NULL; - } + /* Place map_cmdline_to_pid array right after saved_cmdlines */ + s->map_cmdline_to_pid = (unsigned *)&s->saved_cmdlines[val * TASK_COMM_LEN]; s->cmdline_idx = 0; memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,