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, From patchwork Tue Feb 20 14:06:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 203624 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp430413dyc; Tue, 20 Feb 2024 06:18:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWkRLbLVNk89MEAYDMwWXI9Kqc1mntVqyrMheYNHHJkZqfvsATavShWTA+hrq6rZxYZFAIMgrCH/W9Y2um5dNaws+50aA== X-Google-Smtp-Source: AGHT+IEaghj5+rLzNYjv7qlSGmksAKzwi7tZjzG36JiAVntDWkAKSu0ymTZWxqBchyS++GxVwxaM X-Received: by 2002:a05:6a00:180d:b0:6e4:68fa:f1f9 with SMTP id y13-20020a056a00180d00b006e468faf1f9mr6179706pfa.2.1708438732268; Tue, 20 Feb 2024 06:18:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708438732; cv=pass; d=google.com; s=arc-20160816; b=B79pRI1oleVj9KTQQXX5U54uzrlLmYmPGX2kU3g+1CuFS6BOZqi0nYmBZro1hfrmRo O7daqQTkXgkV9tjDalg2E7R0quocx2z0VGzV+30ZkiKU8Ssyb8QkENChttMkBIc2L1zn tzZVjx4LOYwQP9s1Ihb8J9fzJwuq3VP+6my5Q8AZH4AS8QKeCkW2sD4YpfhtphO5Lqi+ qAk80M6K5RYGYCCYTEk06x80olxNeGyg0PEAWeleJ1n4VSi35yw6NbtNarvPlsVDU1LF fFA3M6foXB8h5uNB0wr9ekMw7DiZvmYJCAT+KAxa9V3wG7bk2UDsMisbLZaBGdURtBP9 m7xQ== 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=ytkQHM3rN7f64VSnUC6Ez+L8HS49rIF6r1MpiCj79AM=; fh=agDjUWYo9fuGLwPVzoFqoP6tQUAdz5VzwVjdQhFyOi4=; b=zhBy8iZdpS/lTrn2xkjDLy/Jl8L/thHMDZkuhWcTm7Dx4v3A7+4KE+cWJc5Vfnh7Jp i/qSgDimbbe0VlxeKeTDe6S5z9RY1VmXRlQdaiJ+yNUgg5PAhggBRU7uhvY+toN5UYQx fMMaDC2mP3QX/KT/iqKpRNVLgCvVrbR1Qki3j8Scq1/rvx8TOODrOaKAjaOwDxiQ3QaL LgK4K2S9DfDJVVHtz8Z49yelj4pU2zah5i0ieJxs7gTEBVjBnDXxE2GVGAuyy7mjIXT6 ANrkLZeQjM+E6FFYObUsdLvpznIR7Gcq/rieKb2Jt1x9EMsFphTixr89xc5rIAR0Uis7 Kx4g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-73138-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73138-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id t13-20020aa78f8d000000b006de378fb498si6238155pfs.249.2024.02.20.06.18.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 06:18:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73138-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-73138-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73138-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id E4A5AB23B6F for ; Tue, 20 Feb 2024 14:05:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 55B536F074; Tue, 20 Feb 2024 14:05:20 +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 04F136A351; 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=1708437918; cv=none; b=KXzEcOQ2QkRfSQXNHorzL+eTEHZQgI+DD1QZIh2q8AgeSadVpF8ekiF/xxBJ7aQsQruc4TsADEI5wv0DXD8/VgEO3RY2JiFV/QWOQMnV8SY1v7wiLViVb8ppYvr2fVeDLlcyJlQRR/Wnfe3tSfDDwOPJvEQbjs9GuzLPYE5MVQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708437918; c=relaxed/simple; bh=3QSNHN93sGXVCGki/9m6nhWZGDsN8nXFzji0P87AlhE=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=BglWZvpVx/4hDPZGZ67qvey6P/MbLug91D3mx49Y7vkK48QY6spfZS0MiFGlP7VrbGJ5LdOLZSvBvrNGq+Dsuca0qS1xaZERkndFHZooApuDnQwwJi4cP8ZKs71KhHkxxofS3bNWcBmjWBSz5mvaMDplVWGsLYoD8L5dsnsJOPM= 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 B0B1BC433A6; Tue, 20 Feb 2024 14:05:17 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rcQlz-0000000267G-1zMG; Tue, 20 Feb 2024 09:07:03 -0500 Message-ID: <20240220140703.338116216@goodmis.org> User-Agent: quilt/0.67 Date: Tue, 20 Feb 2024 09:06:15 -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 2/3] tracing: Move open coded processing of tgid_map into helper function 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: 1791427852013028451 X-GMAIL-MSGID: 1791427852013028451 From: "Steven Rostedt (Google)" In preparation of moving the saved_cmdlines logic out of trace.c and into trace_sched_switch.c, replace the open coded manipulation of tgid_map in set_tracer_flag() into a helper function trace_alloc_tgid_map() so that it can be easily moved into trace_sched_switch.c without changing existing functions in trace.c. No functional changes. Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 52faa30e64ed..06c593fc93d0 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -5432,10 +5432,31 @@ int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set) return 0; } -int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) +static int trace_alloc_tgid_map(void) { int *map; + if (tgid_map) + return 0; + + tgid_map_max = pid_max; + map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map), + GFP_KERNEL); + if (!map) + return -ENOMEM; + + /* + * Pairs with smp_load_acquire() in + * trace_find_tgid_ptr() to ensure that if it observes + * the tgid_map we just allocated then it also observes + * the corresponding tgid_map_max value. + */ + smp_store_release(&tgid_map, map); + return 0; +} + +int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) +{ if ((mask == TRACE_ITER_RECORD_TGID) || (mask == TRACE_ITER_RECORD_CMD)) lockdep_assert_held(&event_mutex); @@ -5458,20 +5479,7 @@ int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled) trace_event_enable_cmd_record(enabled); if (mask == TRACE_ITER_RECORD_TGID) { - if (!tgid_map) { - tgid_map_max = pid_max; - map = kvcalloc(tgid_map_max + 1, sizeof(*tgid_map), - GFP_KERNEL); - - /* - * Pairs with smp_load_acquire() in - * trace_find_tgid_ptr() to ensure that if it observes - * the tgid_map we just allocated then it also observes - * the corresponding tgid_map_max value. - */ - smp_store_release(&tgid_map, map); - } - if (!tgid_map) { + if (trace_alloc_tgid_map() < 0) { tr->trace_flags &= ~TRACE_ITER_RECORD_TGID; return -ENOMEM; }