From patchwork Fri Feb 16 21:00:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 202356 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp782179dyb; Fri, 16 Feb 2024 13:00:51 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXvhgD5Cp/lYPJMAnHmJ5bdu8edReryK0bmaSFTOtcvxWcQy++vpIh6lK23TuBgVUbdh5gJVsiIU3eTAtR94gT6TvLfOg== X-Google-Smtp-Source: AGHT+IHa8kKf/EBuFEzxqy2OZNDMvNeR90FifV6Mg1ACvAAjDvz0r7yt6cdPmQf4s2TPS8bp0eho X-Received: by 2002:ac2:58f3:0:b0:511:86ce:3920 with SMTP id v19-20020ac258f3000000b0051186ce3920mr4189414lfo.7.1708117251370; Fri, 16 Feb 2024 13:00:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708117251; cv=pass; d=google.com; s=arc-20160816; b=AdQSvZg3NBCwZHOEU5tTlCVx5W4f43UNiy6w6usQUyWsAB8knTwSnRI/FVG/DBBTzL BgmnKRbWolmYf84hMWuTkQN6TblyKX7UkoMY4fSdaxDDk5M6NPjU0HPt9EHqcNVDkuXF cNJALypb4/qjTYgCoZmmFwY9qM06UDQMmV4+UmWVdKbBSw49yNiATlPDPAdJHnGO5qD5 GMwm5xF5efvCJ0wKLdrFkkPrlMs4+tx7XkX2YrTHm8jfcyL+88C/ou87ZTWpDoXScehk FAlvK1O6Y0/T38uL1/MFUGV7SBhxJpfNpeDyRPd2zkh+6NALa16d0KDWT2AayttUpRJm yAVQ== 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=nySu9i46gC+5lgCiDCrSst2NhFDL8xW9M30SKZJZfiI=; fh=agDjUWYo9fuGLwPVzoFqoP6tQUAdz5VzwVjdQhFyOi4=; b=1FRfzW7p7Wp/FR3PGKQeAfQoBLWDYhoj6Im5Sq6Ai+oVafJaZz8R34yWM/bIyRWVsE tbBg7Cqkn/YMw4ycZxEpVQpfk0uWaVzfkAFQ+YUp6l/p3nux2OJi3jcBQQFIiJ6en91U P/MT3WQ7KIemNrNK504556iaUra0I+MicuSn54NV1WNWBcIWYOi38Hep0CY8LwILqYkj emhUoOHXMa2ysTHJ5f+dW/jxFqkDq+sqK4X1L4HaUFKeAFP8UqXBW4VsGOZ/Lo6Y1esJ 64EH9iv7qWdWejJRfaFAAIrdx+uv7e4Ogswnm+ET5Z8lAn8mWipbn8FL78d8c8PYBKAF xDWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-69335-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69335-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n27-20020a170906689b00b00a3e0dc734e2si153731ejr.901.2024.02.16.13.00.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 13:00:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69335-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-69335-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69335-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 am.mirrors.kernel.org (Postfix) with ESMTPS id D5B3F1F2437D for ; Fri, 16 Feb 2024 21:00:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 681D91468ED; Fri, 16 Feb 2024 21:00:15 +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 DD6D5135A74; Fri, 16 Feb 2024 21:00:13 +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=1708117213; cv=none; b=ds378ohJNfO0OVYYFfbt/yqfjBOJHWCJBTx577RVoAb0hSwxjPykkrock6SiIhOmckG/t+yDw5HUEGZOkpjPdrq+7fUDsDmXOzyyWenCAQI6v2OjEdKgqHwEfODzNQY8PgVZztbVdLu3aXXHpYa19ZIzyLVDEdsyj/xwKbHWrng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708117213; c=relaxed/simple; bh=UKERADSm+O7n+aaGmjyDt7o/R4k2K3C1eYdwBNUkpOY=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=aXCo+9cDBDRXQEIilizfribEf5y2trH9MWjGsosYXCt0rtJbNHpxTiye05T1ljgTP1507CuafLYjZzulanUHVeNzx9FpTzlyipTHCvCue59JT1jFTUHp/u4BTPO/6pO5P0HxvNv4vZr2fq8NrnHjEjLDt3ZuZl1GWSPznCTtYQ4= 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 70FD3C433C7; Fri, 16 Feb 2024 21:00:13 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rb5LC-000000013Bp-2B5U; Fri, 16 Feb 2024 16:01:50 -0500 Message-ID: <20240216210150.379809261@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 16 Feb 2024 16:00:48 -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 v3 1/3] tracing: Have saved_cmdlines arrays all in one allocation References: <20240216210047.584712062@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: 1791090755034969714 X-GMAIL-MSGID: 1791090755034969714 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 v3: https://lore.kernel.org/linux-trace-kernel/20240213115232.5fd9e611@gandalf.local.home - Rebased off of Linus's tree as it was originally pushed after the next two patches, but the next two patches were never posted in mainline. I rather have this before the code move to trace_sched_switch.c as it will make it easier to backport (which is why I added a Fixes tag but not a stable tag). kernel/trace/trace.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 1f4140be98af..7a96e5cfe266 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]; @@ -2352,7 +2356,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 +2368,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,