From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 2F5AB1FCF41 for ; Sat, 25 Apr 2026 01:44:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777081463; cv=none; b=HqfdzLIoXYkBlhChOfRCbuHxRNEfg2aksP1uhoprBRYTdauK+HGE16Khn57x0Pkei6e6SKI1Qbb7eJ1+Qykbi4JFydzrR98CMKD0r44BtifeHuKsX9tuMcK3T53Mx62a9ZYdWFGWpmpWD3T7tYj2Foji3AdPHg0fWtGUdUJaG5o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777081463; c=relaxed/simple; bh=WbM8aoFDL1xInf9fk989KxgvWM34KxbOYADLDMviZ7U=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=aKTKhmL51QhNGF1paxcg8P859/+tJ7VUde/Wo8N/2f+BCojM6jWvkot8kTRwSWZ9n4r1kSCNMG+SuRTgjg7ikUQpGkkImmdsfwiJUKYWLiW/Iw7Z0N7ntTHUVE2U7xzjpQz/WYCVP2xEoBM+zXR62JhRsah/lYU0g/6k01wg0Gk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=pHYl6JtV; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com 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="pHYl6JtV" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2adbfab4501so41573355ad.2 for ; Fri, 24 Apr 2026 18:44:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777081461; x=1777686261; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=j1KowH/KQePREcpPaMinSKl3adwCAV357FzMEtXwkvI=; b=pHYl6JtVgluhk/YnrjUZTxkVirDurvSI6PENKtrhUYncW0S9UA5EGQHeWJqjh8YP/o TW+Gklo3Vp4PpcB/0+PVk2A7tHJPEr8RPEG4prEWpSlwWY3fRaLFnshy5uPxZyPjvTHT fXAYc14c6DZF/QUc9Y3T2C6mlr8JcZY/cd2v5Hs0KfbzDQHDDE5TYjSGnydUISJS51DS 82chxJGRMXIwcNtkpOAsY35PLmpC6ub+olGy8gYzI3JtytksaOSnfl8GM1leRxPnrL0V xWIaquYcra4AwK6cCIvU1vFjuKC1cdchkw3HOB2ZcPqMLv/uco5Foa57p7MeaWsu1X74 bjvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777081461; x=1777686261; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=j1KowH/KQePREcpPaMinSKl3adwCAV357FzMEtXwkvI=; b=F9zOIrKpl5UEhET+sssQ/FLNeDvoNMbibRN1LSHv9ofHXoOpg7HUVtbmuKcGOtuR87 VtF/8BFpsR+Gxrgp4EH3CYEsV7Kq4rNq3H8XojhzoUxHDahsmbHp9tAGgN7Mkbv3xfwK VBfBeWwP06cxzASrapVghN4JKU3L/0YHDhtmrA1NYDgNYB0AzYaOB1vKWY4qIrrUm8pl MgBMBhPTP99fWRHSgPIQF1NM6D+nzoy+nbOAaCPHxDeXyV+GA6a/r0NHTzBWkMo1p3ex l1JdLEiKPKN9SXEBre604p/Sna5IUPloEja7Cpz+fSFNX1pjHMg18DgkSE6Qdymarggu x2Ow== X-Forwarded-Encrypted: i=1; AFNElJ8nQgcc0GRn3tR1EQhEDkuPXMbv2b/zQvcXwMRq8xgczFWy9C+WhYC23yzq8D1EdfzR4zDw/8BTwAdDwhE=@vger.kernel.org X-Gm-Message-State: AOJu0Yzo6JPBU1Vn5jBt1j8k4hDh6b0/hx6kumLAHpMa3oZyxVdSox+5 GB26L4nn37+WbfktoOg+u+8wBTpWcEVb+hfaOTMMzsnCSu52R4NlkwZ0 X-Gm-Gg: AeBDiet8RZcuPeme/l8RL8fdau3LF3HG0hGtQwzEk2zxpXpXa/HTeGF7U0Y3qJi/3Hk QW8OiFwvEcQtNpzmHCxVU2CNQN37OBwoV7tZ7aGmtfXn5YIUmRmnrSiUR1cHEtvLObcQzaSaGli zlt5IP32dLPvFTr763W3S38mgJJFhvdagp/IbBiGpmLgiGFvYAtcMVyC930+pVRkQ/nT/TnNqQx xGYtnRaIfbQFifF5Pd7oXdRyh2VnyT1igTKl+Z/VpCV94nQZQqlc/y9XzcXg+PZB1f+GXaKEMKz Pe9yaWihuwppaYy4yO/FrJeubx6dYGD2sfrJEeARIviL2wi6kKTBt4qDMaB1xS82onNa2BPUQ/7 FBmeHWG6Af2bee05Znjen8jqk+gKmnjtvTwLpToVi+5grOdp6URpxDpwpncno0AjueIcpFmZ9tU 4NY3X4KV3caUdWI71t3yXDfdYNOBPypmCwO0J8hkCL2VjTBRK1udj9sNdXXZfbS2116AYJO/16F Nu/NNaZBl8z3PpwwepJ7UddpA== X-Received: by 2002:a17:903:200d:b0:2b2:5597:bad4 with SMTP id d9443c01a7336-2b5f9ffa730mr262055295ad.30.1777081461529; Fri, 24 Apr 2026 18:44:21 -0700 (PDT) Received: from ryzen ([2601:644:8000:5b5d::8bd]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fa9ff918sm254121425ad.13.2026.04.24.18.44.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 18:44:20 -0700 (PDT) From: Rosen Penev To: linux-trace-kernel@vger.kernel.org Cc: Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Kees Cook , "Gustavo A. R. Silva" , linux-kernel@vger.kernel.org (open list:TRACING), linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not covered by other areas):Keyword:\b__counted_by(_le|_be)?\b) Subject: [PATCH] tracing: simplify pages allocation Date: Fri, 24 Apr 2026 18:44:03 -0700 Message-ID: <20260425014403.440786-1-rosenp@gmail.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Change to a flexible array member to allocate together with the array struct. Simplifies code slightly by removing no longer correct null checks for pages and removing kfrees. Signed-off-by: Rosen Penev --- kernel/trace/tracing_map.c | 32 +++++++++++--------------------- kernel/trace/tracing_map.h | 2 +- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c index bf1a507695b6..627cc3fdf69e 100644 --- a/kernel/trace/tracing_map.c +++ b/kernel/trace/tracing_map.c @@ -288,9 +288,6 @@ static void tracing_map_array_clear(struct tracing_map_array *a) { unsigned int i; - if (!a->pages) - return; - for (i = 0; i < a->n_pages; i++) memset(a->pages[i], 0, PAGE_SIZE); } @@ -302,44 +299,37 @@ static void tracing_map_array_free(struct tracing_map_array *a) if (!a) return; - if (!a->pages) - goto free; - for (i = 0; i < a->n_pages; i++) { if (!a->pages[i]) break; kmemleak_free(a->pages[i]); free_page((unsigned long)a->pages[i]); } - - kfree(a->pages); - - free: - kfree(a); } static struct tracing_map_array *tracing_map_array_alloc(unsigned int n_elts, unsigned int entry_size) { struct tracing_map_array *a; + unsigned int entry_size_shift; + unsigned int entries_per_page; + unsigned int n_pages; unsigned int i; - a = kzalloc_obj(*a); + entry_size_shift = fls(roundup_pow_of_two(entry_size) - 1); + entries_per_page = PAGE_SIZE / (1 << entry_size_shift); + n_pages = max(1, n_elts / entries_per_page); + + a = kzalloc_flex(*a, pages, n_pages); if (!a) return NULL; - a->entry_size_shift = fls(roundup_pow_of_two(entry_size) - 1); - a->entries_per_page = PAGE_SIZE / (1 << a->entry_size_shift); - a->n_pages = n_elts / a->entries_per_page; - if (!a->n_pages) - a->n_pages = 1; + a->entry_size_shift = entry_size_shift; + a->entries_per_page = entries_per_page; + a->n_pages = n_pages; a->entry_shift = fls(a->entries_per_page) - 1; a->entry_mask = (1 << a->entry_shift) - 1; - a->pages = kcalloc(a->n_pages, sizeof(void *), GFP_KERNEL); - if (!a->pages) - goto free; - for (i = 0; i < a->n_pages; i++) { a->pages[i] = (void *)get_zeroed_page(GFP_KERNEL); if (!a->pages[i]) diff --git a/kernel/trace/tracing_map.h b/kernel/trace/tracing_map.h index 99c37eeebc16..18a02959d77b 100644 --- a/kernel/trace/tracing_map.h +++ b/kernel/trace/tracing_map.h @@ -167,7 +167,7 @@ struct tracing_map_array { unsigned int entry_shift; unsigned int entry_mask; unsigned int n_pages; - void **pages; + void *pages[] __counted_by(n_pages); }; #define TRACING_MAP_ARRAY_ELT(array, idx) \ -- 2.54.0