From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796213689; b=L07RQ47/oR7uNvgM4ZhDsrTCyLHUAwGQmx5kXyaQzf6Y4eG6emC/wd+Tw8mdNGQ1MPPq kgHnG2S3VaAVoTsvOnf1PTu2/6RZ4W/tPmw9Brb1fo5Qzknz1MHzvLbd55+pU9rNbZDNV SbQVfyxo2P6pJH9r7CeX3dp3s1wTT5a/yddhatqGRsDfiSz+q5YB0D99Cr/oDoppj6J+1 mehNPrcu3t9t8c5IdC0AN/+n+xbyprZf+oYiix7Ap6K4/QepeC8z2sMh106eZIEJIQnm0 mF1VhLU+/zsItNvdp+iwUvKIDXwT+wr95i+fbgfiC2LaFcU7GmDpOYYArrhuQFSxMRg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796213689; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=jGPIJwNggDsJU3pu5K2tDbvhugrlRkuqaVgSAMMYFXs=; b=qiDDQlMRfzfZnzp/qVeRkrT1hzTGbIATwl4AHvDQtCmxaNuZpAn41G5MH0Cn8zjM5J/s rjWLYkFHI7aawFi7fq/dCnk9YRR2o8tq6SWsGQatQzTr+/bMP1MQBbEB1xOtiLm2x1lYn htK6IrrmHtjHF8WFmrCgOJahuah9SxvTTDVdB5xL+Pmy9dgw0uXdz512IcYpyNMokmG5p 6AZwrc0sVHZ1bOqB3sDRWqq0ZH9YKAl3RlZS/B22rlsw7KJyktKKhzYvbf+dKtNX5DXZK YubJmKTG7V2iYkUSUerdfIJTcAjGKk+Ult83H/ZIUwsymziGHcAI1B4IbCWkS4s3E+g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="i/zCC+HN"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 697EA606A327 for ; Wed, 2 Dec 2026 12:14:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=i/zCC+HN Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6B1861F000C1; Wed, 2 Dec 2026 12:14:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796213689; bh=jGPIJwNggDsJU3pu5K2tDbvhugrlRkuqaVgSAMMYFXs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=i/zCC+HNlHASlfwyg26l8hQTEY+0Hna8vLfy63qHH1DRF6wc1fvK5HN/jo2iUSQF2 Zx+9i95Aq30ASkd6+OC/AUKC6qkEd8w3xLRTmOSKjSdfr9b/BGnyAXzBbgSBYMIugl BeRJPgsmeBlnZDNBtk4o3xS0qawMTSDpVogzRJWcNX1u29Ioo2zJoGEmT1CaGdPuEZ iUzY3P9dUS3y8sulamsAVNf+LzIDFUp6KzH7nEF07TzRfSBV1Y0XUFgoxfhKmXAsLw WpHpv9/btILVAp6RmBNx3L3mDUgKDnFCxhvLRlI9R4igmpWAY34c52TczNRnj4GOgU AqwgLJK5qGA0w== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo , Athira Rajeev Subject: [PATCH v3 01/18] perf kmem: Use the allocation order when matching page frees Date: Wed, 2 Dec 2026 09:14:17 -0300 Message-ID: <20261202121434.2224-2-acme@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261202121434.2224-1-acme@kernel.org> References: <20261202121434.2224-1-acme@kernel.org> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Arnaldo Carvalho de Melo The page alloc tree is keyed by the values recorded at allocation time, but the free path looks it up with the order from the free event while taking the other key fields from the live page record. When a page is split or reused the orders differ, the lookup misses and the whole stat fails with a misleading -ENOMEM. Use the order from the live page record, as is already done for the gfp flags, migrate type and callsite, so the lookup finds the entry created at allocation time. Subtract the allocation's size when a live entry goes away, not the free event's, and refresh the live record on a new allocation of the same page, so a free that follows a lost free is matched to this allocation. Fixes: 0d68bc92c481 ("perf kmem: Analyze page allocator events also") Reviewed-by: Athira Rajeev Tested-by: Athira Rajeev Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/builtin-kmem.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index 87869c717266815c..a73e2586922ad732 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -873,9 +873,17 @@ static int evsel__process_page_alloc_event(struct perf_sample *sample) if (pstat == NULL) return -ENOMEM; + /* + * A page whose earlier free was not captured is still in the + * live tree: refresh its key with this allocation, so the next + * free is matched to the entry created here. + */ + pstat->order = order; + pstat->gfp_flags = gfp_flags; + pstat->migrate_type = migrate_type; + pstat->callsite = callsite; pstat->nr_alloc++; pstat->alloc_bytes += bytes; - pstat->callsite = callsite; if (!live_page) { pstat = page_stat__findnew_alloc(&this); @@ -905,6 +913,7 @@ static int evsel__process_page_free_event(struct perf_sample *sample) u64 page; unsigned int order = perf_sample__intval(sample, "order"); u64 bytes = kmem_page_size << order; + u64 alloc_size; struct page_stat *pstat; struct page_stat this = { .order = order, @@ -935,9 +944,16 @@ static int evsel__process_page_free_event(struct perf_sample *sample) return 0; } + /* + * The alloc tree is keyed by the values recorded at allocation + * time. Use the order from the live page as well, otherwise a + * free of a split/reused page looks up the wrong entry. + */ + this.order = pstat->order; this.gfp_flags = pstat->gfp_flags; this.migrate_type = pstat->migrate_type; this.callsite = pstat->callsite; + alloc_size = kmem_page_size << this.order; rb_erase(&pstat->node, &page_live_tree); free(pstat); @@ -961,8 +977,12 @@ static int evsel__process_page_free_event(struct perf_sample *sample) pstat->free_bytes += bytes; if (live_page) { + /* + * The entry was credited with the allocation's size, a + * split or coalesced free must not change that. + */ pstat->nr_alloc--; - pstat->alloc_bytes -= bytes; + pstat->alloc_bytes -= alloc_size; if (pstat->nr_alloc == 0) { rb_erase(&pstat->node, &page_caller_tree); -- 2.56.0