Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2725831rdb; Mon, 12 Feb 2024 14:45:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IEM7+pV2X+4g7CrzOpFFFbsPpsGkufpxRIdsWtJyivJmOxyW8TzJWSEelo4/kAQHJPN1PID X-Received: by 2002:a17:903:22ca:b0:1da:16d4:3726 with SMTP id y10-20020a17090322ca00b001da16d43726mr8515775plg.34.1707777900402; Mon, 12 Feb 2024 14:45:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707777900; cv=pass; d=google.com; s=arc-20160816; b=RLocLBK0JX2GsGhcNXfELkTTJ+MKJ4gyeQyXAdt5xnSKjjCBb5eIkyDKToKejQPZD9 fJhkUs2uI9LwPgQvBYe1daGV777hq9ItmZUFKlvbXrXBw/h8X/2IOyPpVXhcUghQe8Ai YClMIz9unfHkNvL47jjjzxjPbk+Uzb8rvwIr24kHiRLMF75GCaG6uWfXpnDuUmxrvDJD aW+vMimohUpqpHI1WRjKkXHWajzY9/mHOq8R7+/cILxnJ0dWvhziDilfPofEmnDLSmL+ UZdQw/wdSaXZIvmyC+0E3HMnSdbZsn6KnF9NnhJpIoWUVZo3CQkWX33TCAFI0CfMHmgU aMBw== 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:references:in-reply-to:message-id :subject:cc:to:from:date; bh=jw2ocZrY3CnzBCPY5cH35g9oEZFvkvm3LwkZ0wRXKeE=; fh=zj8EBAijEGWAufZqZNLoEopoal3Q70U/PccOVtxN6vs=; b=Iy+68dDx2GKYPAf/Pw3FlZdci1+//7FADeWf3ydZD1Arvi5i75tV+aFCzxLyLDftaP 9PiPp5atazkGN1ixU+32gJyOSAfgySLFB7RXlSbaXdyD5d+KwI3BOH9IUzy2q+nUv5wJ fH0ZJEzdtdBKdim9ah9/gk12V9BncUYUUj8ATPHZj/SvnDNLXQT13npEwqA06UMiYlJo Z9xUKCcp3E3hcGcYuPmasnAXldOiZuLK6+poyzOA49TRm6PYitD27Yh0peSG78FoPhLK aPcERp289EogEcjuoWhui8wbxEZYil6xvNBieranYs/gIeANLrNLtiicztibmYfaEt0T zV6Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-62488-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62488-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=2; AJvYcCU/4VpN7UBwz8ZrZh2JC4YmZeDneF8GStuDUlo8x6smf/0HfSGsAy0RMHUYnsTqDkbM5GGRuzVPXVXUeyVDhADrpz7VCZMay2KN8CuC7Q== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id o6-20020a170902d4c600b001d90a15612dsi925274plg.147.2024.02.12.14.44.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 14:45:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62488-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-62488-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62488-linux.lists.archive=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 ECF37B21714 for ; Mon, 12 Feb 2024 22:39:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5B7294EB5B; Mon, 12 Feb 2024 22:39:30 +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 C99734EB3A; Mon, 12 Feb 2024 22:39:29 +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=1707777569; cv=none; b=Xqg5dFoQwzwgBaOi/8S8WyR7JTWl/fa09ZNPwjwtnSjViAsxn1RugObUMLPLOv1NjdpFR/J7eyROgnv5k4GGqzI+mt+w6Nhsvt6dM9O76CmA9tXE3SAda/XrZw+YnVnnobqxmFmZVOOyGMDRRJQVy5TdC3klsU/PoYX4WT9yBro= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707777569; c=relaxed/simple; bh=chCgSI9YJkrGnYJm8WBzd/jHa1AAREfUA3WwG3qlx2U=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OJJwnWzg59I6ME+x/w8mnU0IW/o0QYSKLoVI4Jk7QNLIeOnISZBXyoKypbOxJQEEJikNiVNyLPlZeM9u+ogcDB2J0xH2zy1AFMGUo/PDw0fbJtzqUSz1rXVaO3cKSAzRwvZn9fndDyitU4pFAFseKe4D9QZH6WQvcz9daowqVZU= 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 4D304C433C7; Mon, 12 Feb 2024 22:39:28 +0000 (UTC) Date: Mon, 12 Feb 2024 17:40:11 -0500 From: Steven Rostedt To: Tim Chen Cc: LKML , Linux trace kernel , Masami Hiramatsu , Mathieu Desnoyers , Vincent Donnefort , Sven Schnelle , Mete Durlu , stable Subject: Re: [PATCH] tracing: Fix wasted memory in saved_cmdlines logic Message-ID: <20240212174011.068211d9@gandalf.local.home> In-Reply-To: References: <20240208105328.7e73f71d@rorschach.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 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 12 Feb 2024 14:08:29 -0800 Tim Chen wrote: > > Now, instead of saving only 128 comms by default, by using this wasted > > space at the end of the structure it can save over 8000 comms and even > > saves space by removing the need for allocating the other array. > > The change looks good to me code wise. But it seems like we are still > overallocating as we can now accommodate 8000 comms when 128 > was asked for. That's because that memory is allocated regardless of if we only asked for 128. Why not use it? > > Wonder if we can reduce the default ask to 127 comm so we don't have to > go to the next page order? The internal structure does power of 2 allocations, and is just a little over 128k in size, causing 256k to be allocated, which leaves just under 128k of wasted memory. And on top of that, the old way even allocated another array. So instead, since this is already allocated, we just use it. Now I may add a way to also include the other counter too, so that we can have both, and will make the default slightly smaller. We have three arrays: map_pid_to_cmdline[] which is the bulk of the allocation. map_cmdline_to_pid[] - which is cmdline_num * sizeof(int) saved_cmdlines[] - which is cmdline_num * TASK_COMM_LEN(16). The first one above is a static array, the second is a pointer, and the third is the end of the structure that uses the rest of wasted space. Now, instead of allocating the map_cmdline_to_pid[], we could add that at the end. The following patch drops the default down to 6000 (which is also more than enough) but also saves from allocating another array. -- Steve diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c index e4fbcc3bede5..0c60e4bcdd31 100644 --- a/kernel/trace/trace_sched_switch.c +++ b/kernel/trace/trace_sched_switch.c @@ -201,7 +201,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 * (TASK_COMM_LEN + sizeof(int)); order = get_order(orig_size); size = 1 << (order + PAGE_SHIFT); page = alloc_pages(GFP_KERNEL, order); @@ -212,16 +212,10 @@ 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)) / (TASK_COMM_LEN + sizeof(int)); 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; - } + 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,