From nobody Sat Feb 7 13:21:13 2026 Received: from mail-dl1-f45.google.com (mail-dl1-f45.google.com [74.125.82.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 916D617B425 for ; Wed, 7 Jan 2026 00:24:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767745475; cv=none; b=QSGik3epMsP5VmkgFMZgGubvvkw80Ki0EiMxwh9cQtfqflmCpqFKmhczyBdYDp+Laf059mAwPJIT96Vq/CV5h6EkT2dI+AHgkKrh0TE4PZ6dZN/6UZ+CDLEsBvJGj29d29whoTxRwRySKytfIyqID8kN3Aluonhv64cK54g4chQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767745475; c=relaxed/simple; bh=sm6kqVtf0K7w9K5dHVkvn/+lh489TTX6PtRzV0xr6ww=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=RV9eLcponuXiMKSeSaq4or+nyAbg+P8XhLVbUJ2pqCxlCqC7G0wm2bmGTfozzNMK8x0Cbbq7C7m1yBUwwQlKB0Uc2R7Gct0pG0FTNkAG2WZZdfqxlBOYuO2C+z34Aj31sCPT/eonrZjI92+RLbIgatlWS+oCCFUHaMVGqCjnrXs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=F0t7zosi; arc=none smtp.client-ip=74.125.82.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F0t7zosi" Received: by mail-dl1-f45.google.com with SMTP id a92af1059eb24-12056277571so1050463c88.1 for ; Tue, 06 Jan 2026 16:24:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767745472; x=1768350272; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=P4NEy/evxLtquvElmXdudeXmemOTXjDwfQllVxs2DqY=; b=F0t7zosiH3izpfMDrfwKcE/IqMHc4OrZGg+iWev4M7ZFNJKaPEEM0ekfL+RjmgYIeZ H82ZC928orKjBvuKI69wxoZ6YQSdEeXHUrAiZ2w32aU6JRHmVoDbF3ftQJ8mGq/flY9B FZECfWrFXFkbQBmLwq8SvspOhmg44gUK3z/ktI4oNbEOy7GIBflkpdoGCmuPiSLPJ/IF nfgmLlVQWWHxbY0aBVIeIEljHbDULDvPz/8bsxA0eq4/5w4XZzawh/zTsPfoJBXN9pUx t9gy7md8QQoT5vvyUjMW/Pi67S2Huy5x7/yt5xXFaaQ7MlMX5vz0G9zFAXVXOFhCo9Er iB6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767745472; x=1768350272; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=P4NEy/evxLtquvElmXdudeXmemOTXjDwfQllVxs2DqY=; b=wdtMe+XptI1sKb5I2s8Zk1fjGkWB75V2PHwGPArmc3kyvCVSfpYcLPdgHNr6BykqzQ AeoYfVoko+q06jNZswHMgmE87CdO4mYST4OrjF+jLoeBkbK193Gk09VW8aAC8kVA21JT ZFaADKWaJDj6rOE0swO19Kj3+UcQLvzVb0EWa/E420aoepMlru3J9y8Y0keASVDRL8A/ cUZ3kQHvzNOcmPktBHuSJXuX4NyY1rzdF6Xx/Xf5qc0z+Bt5SLpMObFxcskc5xecHVAr HZKDDMdRWmIYdDKQwQtNhLLldOzsyOyKLr377CYPR5h+E2gX15fLUlcHyDyRhkVofDEj CSKg== X-Forwarded-Encrypted: i=1; AJvYcCVnZiLVCHC7r5Z5TPI6pPAHHyX3p08WUmB/+ftVYqkPaHCakDj+36ScCDgbUzZZuNKlYyBIzDLuyWP7nFQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwseNGHWAsGo9Pvja+7RQBgou+XuoxBacdjXhkj1CF1zOA6T/Yt wQFHG92eiwXc8Ihb9J/U/peXHY3nX57Q/B1zyi8DBSRzO9WhYCq3URij X-Gm-Gg: AY/fxX5brpd9mauTN+RV11ipNydZtnPxAyMa8YLmIdox1RJywyNruAAdkgTAv+gPnIS EjZHyfAvCpXPg44lMZPOnOJgKOejrxiMPImJjZhcBTMlmNlspxKtQNnqhMwDZmWtt4bf4cfr9ve /0oZ0j7xFMR8Za2F/6hTV3DEOrYt9VKIy6Fh6AK7CMCbBTF96cq6O7v4uLco/MFkhzBPw4PW1DK a7k1JbJ60/CTKK5tqpfbmJhMX/xG1SmWLb3ohY1zAt1QHM9IJl5oJE0FH+Pmzx94hOcDTLOFuuN 4Yt/I6+nqvL0lDspGD00tl+qi5RE2R2upYBCyrtkn0qtHMjHQ4W39vw8EqsiUUDYJK5nzgEIw9/ KHodRuK8Ex5Yxr1JMjIP7AmwH4uXroGtWo8LTMQOTXtKWvg5jyMbS0lKUttJoxQzN3+6Y8HgzDh P+DzTnDd7nVATTuv4RY+jqwdJM X-Google-Smtp-Source: AGHT+IFztVj1GjS3hGk8PSehzOb66RD2mNwcrW4HjOGIQh/h0nLbKrSNKm9X49kajXJ6WyTEXQ7M6w== X-Received: by 2002:a05:7022:41c:b0:11d:bea3:c93d with SMTP id a92af1059eb24-121f8b7931bmr499008c88.29.1767745471463; Tue, 06 Jan 2026 16:24:31 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:da43:aeff:fecc:bfd5]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-121f24985d1sm8240642c88.16.2026.01.06.16.24.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jan 2026 16:24:30 -0800 (PST) Sender: Guenter Roeck From: Guenter Roeck To: Steven Rostedt Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH v2] ftrace: Do not over-allocate ftrace memory Date: Tue, 6 Jan 2026 16:24:28 -0800 Message-ID: <20260107002428.2904852-1-linux@roeck-us.net> X-Mailer: git-send-email 2.45.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The pg_remaining calculation in ftrace_process_locs() assumes that ENTRIES_PER_PAGE multiplied by 2^order equals the actual capacity of the allocated page group. However, ENTRIES_PER_PAGE is PAGE_SIZE / ENTRY_SIZE (integer division). When PAGE_SIZE is not a multiple of ENTRY_SIZE (e.g. 4096 / 24 =3D 170 with remainder 16), high-order allocations (like 256 page= s) have significantly more capacity than 256 * 170. This leads to pg_remaining being underestimated, which in turn makes skip (derived from skipped - pg_remaining) larger than expected, causing the WARN(skip !=3D remaining) to trigger. Extra allocated pages for ftrace: 2 with 654 skipped WARNING: CPU: 0 PID: 0 at kernel/trace/ftrace.c:7295 ftrace_process_locs+0x= 5bf/0x5e0 A similar problem in ftrace_allocate_records() can result in allocating too many pages. This can trigger the second warning in ftrace_process_locs(). Extra allocated pages for ftrace WARNING: CPU: 0 PID: 0 at kernel/trace/ftrace.c:7276 ftrace_process_locs+0x= 548/0x580 Use the actual capacity of a page group to determine the number of pages to allocate. Have ftrace_allocate_pages() return the number of allocated pages to avoid having to calculate it. Drop the code calculating the actual page count since it is no longer needed, and with it the warning backtraces. Also drop the ENTRIES_PER_PAGE definition since it is no longer needed either. Fixes: 4a3efc6baff93 ("ftrace: Update the mcount_loc check of skipped entri= es") Cc: Steven Rostedt Signed-off-by: Guenter Roeck --- v2: Have ftrace_allocate_pages() return the number of allocated pages, and drop the page count calculation code as well as the associated warnings from ftrace_process_locs(). kernel/trace/ftrace.c | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index ef2d5dca6f70..755a11f13808 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -1148,7 +1148,6 @@ struct ftrace_page { }; =20 #define ENTRY_SIZE sizeof(struct dyn_ftrace) -#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) =20 static struct ftrace_page *ftrace_pages_start; static struct ftrace_page *ftrace_pages; @@ -3834,7 +3833,8 @@ static int ftrace_update_code(struct module *mod, str= uct ftrace_page *new_pgs) return 0; } =20 -static int ftrace_allocate_records(struct ftrace_page *pg, int count) +static int ftrace_allocate_records(struct ftrace_page *pg, int count, + unsigned long *num_pages) { int order; int pages; @@ -3844,7 +3844,7 @@ static int ftrace_allocate_records(struct ftrace_page= *pg, int count) return -EINVAL; =20 /* We want to fill as much as possible, with no empty pages */ - pages =3D DIV_ROUND_UP(count, ENTRIES_PER_PAGE); + pages =3D DIV_ROUND_UP(count * ENTRY_SIZE, PAGE_SIZE); order =3D fls(pages) - 1; =20 again: @@ -3859,6 +3859,7 @@ static int ftrace_allocate_records(struct ftrace_page= *pg, int count) } =20 ftrace_number_of_pages +=3D 1 << order; + *num_pages +=3D 1 << order; ftrace_number_of_groups++; =20 cnt =3D (PAGE_SIZE << order) / ENTRY_SIZE; @@ -3887,12 +3888,14 @@ static void ftrace_free_pages(struct ftrace_page *p= ages) } =20 static struct ftrace_page * -ftrace_allocate_pages(unsigned long num_to_init) +ftrace_allocate_pages(unsigned long num_to_init, unsigned long *pages) { struct ftrace_page *start_pg; struct ftrace_page *pg; int cnt; =20 + *pages =3D 0; + if (!num_to_init) return NULL; =20 @@ -3906,7 +3909,7 @@ ftrace_allocate_pages(unsigned long num_to_init) * waste as little space as possible. */ for (;;) { - cnt =3D ftrace_allocate_records(pg, num_to_init); + cnt =3D ftrace_allocate_records(pg, num_to_init, pages); if (cnt < 0) goto free_pages; =20 @@ -7192,8 +7195,6 @@ static int ftrace_process_locs(struct module *mod, if (!count) return 0; =20 - pages =3D DIV_ROUND_UP(count, ENTRIES_PER_PAGE); - /* * Sorting mcount in vmlinux at build time depend on * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in @@ -7206,7 +7207,7 @@ static int ftrace_process_locs(struct module *mod, test_is_sorted(start, count); } =20 - start_pg =3D ftrace_allocate_pages(count); + start_pg =3D ftrace_allocate_pages(count, &pages); if (!start_pg) return -ENOMEM; =20 @@ -7304,30 +7305,6 @@ static int ftrace_process_locs(struct module *mod, =20 /* We should have used all pages unless we skipped some */ if (pg_unuse) { - unsigned long pg_remaining, remaining =3D 0; - unsigned long skip; - - /* Count the number of entries unused and compare it to skipped. */ - pg_remaining =3D (ENTRIES_PER_PAGE << pg->order) - pg->index; - - if (!WARN(skipped < pg_remaining, "Extra allocated pages for ftrace")) { - - skip =3D skipped - pg_remaining; - - for (pg =3D pg_unuse; pg; pg =3D pg->next) - remaining +=3D 1 << pg->order; - - pages -=3D remaining; - - skip =3D DIV_ROUND_UP(skip, ENTRIES_PER_PAGE); - - /* - * Check to see if the number of pages remaining would - * just fit the number of entries skipped. - */ - WARN(skip !=3D remaining, "Extra allocated pages for ftrace: %lu with %= lu skipped", - remaining, skipped); - } /* Need to synchronize with ftrace_location_range() */ synchronize_rcu(); ftrace_free_pages(pg_unuse); --=20 2.45.2