From patchwork Tue Feb 20 14:06:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 203613 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp420989dyc; Tue, 20 Feb 2024 06:06:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVyc8Ny9wOT0OVRwzL0qoWZTgmaUKVJzMfbVN6NuL9sH9SCgY7s/gByVKF1nQp7oj/5u/L03ImppKuqyPJ715JIn1Nf4A== X-Google-Smtp-Source: AGHT+IEgP544lg7QyQkONpsv715OHy3rSgC5fVvy2ngaKDrGTABND623+fNZ+sCWKgpAaFJ1UqyY X-Received: by 2002:ac8:5ad3:0:b0:42d:b5fd:3fca with SMTP id d19-20020ac85ad3000000b0042db5fd3fcamr15120178qtd.31.1708437960652; Tue, 20 Feb 2024 06:06:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708437960; cv=pass; d=google.com; s=arc-20160816; b=gW3tV5RRwrtMM5VOXqHuLXXRyL0eNsn+OKZ4TMaJXmk4S1AOXRcgAT4UmurOVir2vv wrnfnuLqclnWweE8PKeN24dUY5ENeoq963CCTgNBgWCGsmfFkQxQ2YYJzcluJkRsGN5K nsPl1hPSqBOT1TlmdXEA8qBoBOUEh3FuyNWGZTkAtmvwBW/rg+vfYvHWWadJ3PluFURk ud5sNAVcEEyO1QLUIwA2bRAPLQx5DAeMw8S/3C0Y7gY0d8thzS6cJ53Sfk8F6ZeQGrJm CII4LFbJc1hhRmDoR9OohHMVKe8nXbje8KZ/fvcRghNQvR7EQFbJCcfO38esP4Ntet4T KkEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=pxvPvXOYHL7PMr0uTpzrRBvxEORtLFVwBBsURzr6A6A=; fh=agDjUWYo9fuGLwPVzoFqoP6tQUAdz5VzwVjdQhFyOi4=; b=CuO7KTIyQVXOtYEXpRLSWTtNUdGcWly+48RFIQO1M9r/4BFkq0XU+MZEtM6jHqL7R4 gYjwrg4ySGFsLu8e3EtHFrf5BCW2QIRnUPQ3K1em3wy9GThbzC7KfVFg6HwRLmWtZQw1 /iF1jZMwXKfIzViuQ1jhWeKZO5yHdcQTzvXwI7Smx7VuFieTO9ugkC5PD7l9xFkq4YsQ gipM3ZIFLBJ3JSJHY/tu9QxNXyeBHRvGjSmTBkjNNOo0zsB0tTVvP8D9O0mA5t/Swp8z ExhiV+rMB3kfjMARysAFMMDYaW0iCRpZPVwmwAxla8RAwntVWnJSbG5OxtHiNhqQPFaO BIsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-73136-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73136-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id b22-20020ac844d6000000b0042dd9365466si8542770qto.208.2024.02.20.06.06.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 06:06:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73136-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-73136-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73136-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 3CC031C22855 for ; Tue, 20 Feb 2024 14:05:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A14E96D1C7; Tue, 20 Feb 2024 14:05:19 +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 C30186A8A7; Tue, 20 Feb 2024 14:05:17 +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=1708437917; cv=none; b=trZwdBdgkuLml0RKPInmUGeIGfvrBqx6CKmYa+Csqo2ZQxc1ldzD2DTyIjM1hM6idRmVSttblVHIwL6tNKuuVnVhMN2a3h/nrfl0D6vJzaspKpHQqqK+5jzOnWVewq4kepINsqwyKxl9kWOdk46P7ZSTFfNXSfl6oWmSRVmWRuc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708437917; c=relaxed/simple; bh=4M4JAd9fFYc/39jqZUthmv9OssTIoUBsT1zgu2pl0CI=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=j5STKNBA9F4zUdIfhfDA92yzPLwoihg3saavP5Xxa+O7cXjAR06rwZSEIKzTalItBlO+tT64kn65Livx/1j+2niwBTXMX8kzA1dJknZNKfYThGTuT24JrcB3E6PCUELy3LwEjrBhGGGASrcKAttNIvXK8XEz3Ise/iVpE0Ga4gQ= 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 6C39BC43390; Tue, 20 Feb 2024 14:05:17 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rcQlz-0000000266m-1LAd; Tue, 20 Feb 2024 09:07:03 -0500 Message-ID: <20240220140703.182330529@goodmis.org> User-Agent: quilt/0.67 Date: Tue, 20 Feb 2024 09:06:14 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Tim Chen , Vincent Donnefort , Sven Schnelle , Mete Durlu Subject: [PATCH v4 1/3] tracing: Have saved_cmdlines arrays all in one allocation References: <20240220140613.782679360@goodmis.org> 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: 1791427042794820208 X-GMAIL-MSGID: 1791427042794820208 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/ Fixes: 44dc5c41b5b1 ("tracing: Fix wasted memory in saved_cmdlines logic") Acked-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- Changes since v4: https://lore.kernel.org/linux-trace-kernel/20240216210150.379809261@goodmis.org - Do not kfree() map_cmdline_to_pid now that it is included in the page allocations. kernel/trace/trace.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 8198bfc54b58..52faa30e64ed 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -2325,6 +2325,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]; @@ -2339,7 +2343,6 @@ static void free_saved_cmdlines_buffer(struct saved_cmdlines_buffer *s) { int order = get_order(sizeof(*s) + s->cmdline_num * TASK_COMM_LEN); - kfree(s->map_cmdline_to_pid); kmemleak_free(s); free_pages((unsigned long)s, order); } @@ -2352,7 +2355,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); @@ -2364,16 +2367,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,