From nobody Sat Feb 7 17:20:06 2026 Received: from mail-dy1-f174.google.com (mail-dy1-f174.google.com [74.125.82.174]) (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 D00A933EAE0 for ; Tue, 6 Jan 2026 20:35:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767731741; cv=none; b=lT+nB5ObV6LJxSVz2mPXayth/vWTQt92hwBtTjRrdbiPrjc4//I0uLol8ytgQDS5lkcgVc52hmTLyXXLLcMhmtyZ2tMvLw1F6myVTH124QH7c6kXvaWO/97ReaR7whXOpPE096ZGawoc1UIiBYIIOkT3ACuAolkOjJR1xJ8tdls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767731741; c=relaxed/simple; bh=D9yQ2o5oNf3ScOdvwc+oEkWVYycM8zjIe/4vKH7dBiY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=uLCKSKkV6/cb0rqs7m88fCtMfOGl2hZ+nR87V+aWtnul09GOSK1bJsS9Sw1XDl8cYfsyFXiu4Wz2uQVv5BG0UrVPnPkpwlHPuEv5qErdy3224WuMl2KmewlmgeFaTb+ULJmUW/kBuSKOW5LQIdJ4Fopx2evdC8+QTe9VUQiUGGc= 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=egowkKl1; arc=none smtp.client-ip=74.125.82.174 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="egowkKl1" Received: by mail-dy1-f174.google.com with SMTP id 5a478bee46e88-2b04fb5c7a7so699781eec.1 for ; Tue, 06 Jan 2026 12:35:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767731738; x=1768336538; 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=5/rnn3ELMzpSfEF1jjvUjW3hY2TqsWtJHmr7YBzUjJw=; b=egowkKl1ALHnKVn97TVWXlm4u8d7KAXQhtnaxT0cSV9+qQaNdCj4a919f/zkHrVDDz Jjyv/p4M+rGFPspY0XhJ6DqIb5Ob21+77sS0MVYyqhRD3/PAesbb6lHRrfU79FQ8aNFu /8roEVzOupnfvO4kBjm8oZTR5qfOwHdj2ih+nSMeGFsClAwhMF+xZT7ZjZddgTGa+UpN fSuuqcZnhAanXE6hEENubxTrH7q/rMxoojEqwk4sfe+B55tLl1vCbqqmt5TNw0sO+Nni CXv+kmld11bwDA/yxfkR0TZ5CNkJL29gwYcyCxVIZnuEnO0H4prxbauL6UYLpeut7G4/ Qw5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767731738; x=1768336538; 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=5/rnn3ELMzpSfEF1jjvUjW3hY2TqsWtJHmr7YBzUjJw=; b=EIPT/vqpw6OHtDfthzxTair+SYvsSSnhc901RWmovvxs8DwU5bS+Y4S9tkmy10cgRi suR/TZGBbWWZBSUM/YAKCAxNWoARTSIPD2fEVSpAl2vNZsdxSGO+FzeWAJR7CIenOCuV E7BvYbSHq7MfEwqkUubxBqVPRfv2271uGw9zRfMQSPvflkki7zXT9547r7yYttd5s359 ys9XqgVYVLEnAaUzLXi/iPzM9v2CO7w7K/hCPA98lMrOEbx0AYc9waMcRn1WPyY+HxbD XUo2VSM2piwd/ia9V1NcpbfkoktRve3J44r/rXxcKSqYhv+OvgCMm8RSjSxxYH2cvNIu 0Urw== X-Forwarded-Encrypted: i=1; AJvYcCWC9g6pMpQ/47sfz7W15MpUATiUAlsh0+a0GXDJ30jhErcBEuYwGm52h3iuCRxDmaOt1mU7rx3lgf7SvZU=@vger.kernel.org X-Gm-Message-State: AOJu0YzjeE22svVUj60SVUCfX2KsJagaPNM7KXjZcWuE0lmatAuNb+Mf AbjY6MkPN3gqsTh4NtZkg6Dvs/3KjCmL9fsV8SfZMP4pjSFy4cMoWWmSAzU0Jg== X-Gm-Gg: AY/fxX74TwoscGGZkk8ZzHsJd+1jwkeBykCvHSPoGLcvhRP4dthk5InGDYLQ+qO0jcJ KUBvJn0u3Ebv5lO6oLpPAMDJ+vqs4rjfg2bpsjrFt9k87dSufu52qJceAQTrMxlmpv2Grm5r7hH w8bwkcgdqLaYlRPZF2wPZOiXT2GzVx0cb5OJoVtYsuqfreQr0i4i9/t1EtN8CQk34uDhV6G1zfD g7e3rpNb1Nx/POiqblczGkzivmIDSMayLJgrZ4I5fi+l8mISSpVy+ePUc4h7vPclRg1BoTkcXDb sU+zFXzPN0VuWZq8tAjkL1HizqrR+PAT8DY2GPZevzplesFJvY1yUamR8XxeGiuBQkiWI3S0P9A HNP0Dr+VzeY+v8mS3kfJXpfXa59mKvfvfOz+HRMU8FTYi3T7skBHkvDj9iQFXjjSqMSbom0tJfa 1OHZKSWp0xwaTxGjZF6/gJmwyi X-Google-Smtp-Source: AGHT+IFiJA+rdlCtKkoxFpOGnLicEwGAcgBp1ff416NZWS9VUF0qprC4FkRzIHCQFO3T97V7ug3zlw== X-Received: by 2002:a05:7300:1829:b0:2b0:5834:44d2 with SMTP id 5a478bee46e88-2b17d34428amr142148eec.39.1767731737566; Tue, 06 Jan 2026 12:35:37 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:da43:aeff:fecc:bfd5]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2b1706a5d3dsm4674177eec.13.2026.01.06.12.35.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jan 2026 12:35:37 -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] ftrace: Do not over-allocate ftrace memory Date: Tue, 6 Jan 2026 12:35:33 -0800 Message-ID: <20260106203533.2896197-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 if too many pages have been allocated to solve the problem. Also use the actual capacity of a page group to determine the number of pages needed to avoid over- allocations in ftrace_allocate_records(). Fixes: 4a3efc6baff93 ("ftrace: Update the mcount_loc check of skipped entri= es") Cc: Steven Rostedt Signed-off-by: Guenter Roeck --- kernel/trace/ftrace.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index ef2d5dca6f70..211ec7a04f7e 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -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: @@ -7308,24 +7308,33 @@ static int ftrace_process_locs(struct module *mod, unsigned long skip; =20 /* Count the number of entries unused and compare it to skipped. */ - pg_remaining =3D (ENTRIES_PER_PAGE << pg->order) - pg->index; + pg_remaining =3D (PAGE_SIZE << pg->order) / ENTRY_SIZE - pg->index; =20 if (!WARN(skipped < pg_remaining, "Extra allocated pages for ftrace")) { + unsigned long space =3D 0; =20 skip =3D skipped - pg_remaining; =20 - for (pg =3D pg_unuse; pg; pg =3D pg->next) + for (pg =3D pg_unuse; pg; pg =3D pg->next) { remaining +=3D 1 << pg->order; + /* + * The capacity of a page group is + * (PAGE_SIZE << order) / ENTRY_SIZE + * Accumulate the total capacity of unused pages. + */ + space +=3D (PAGE_SIZE << pg->order) / ENTRY_SIZE; + } =20 pages -=3D remaining; =20 - 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. + * Check to see if extra pages have been allocated. + * Only warn if the number of unused entries is larger + * than the number of entries per page to avoid false + * positives due to rounding. */ - WARN(skip !=3D remaining, "Extra allocated pages for ftrace: %lu with %= lu skipped", + WARN(space - skip > ENTRIES_PER_PAGE, + "Extra allocated pages for ftrace: %lu with %lu skipped", remaining, skipped); } /* Need to synchronize with ftrace_location_range() */ --=20 2.45.2