public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Donglin Peng <dolinux.peng@gmail.com>
To: ast@kernel.org
Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
	Donglin Peng <dolinux.peng@gmail.com>
Subject: [RFC PATCH v4 0/7] libbpf: BTF performance optimizations with permutation and binary search
Date: Tue,  4 Nov 2025 21:40:26 +0800	[thread overview]
Message-ID: <20251104134033.344807-1-dolinux.peng@gmail.com> (raw)

This patch series introduces significant performance improvements for BTF
type lookups by implementing type permutation and binary search optimizations.

## Overview

The series addresses the performance limitations of linear search in large
BTF instances by:

1. Adding BTF permutation support - Allows rearranging BTF types
2. Implementing binary search optimization - Dramatically improves lookup
   performance for sorted BTF types

## Key Changes

### Patch 1: libbpf: Extract BTF type remapping logic into helper function
- Refactors existing code to eliminate duplication
- Improves modularity and maintainability
- Prepares foundation for permutation functionality

### Patch 2: libbpf: Add BTF permutation support for type reordering
- Introduces `btf__permute()` API for in-place type rearrangement
- Handles both main BTF and extension data
- Maintains type reference consistency after permutation

### Patch 3: libbpf: Optimize type lookup with binary search for sorted BTF
- Implements binary search algorithm for sorted BTF instances
- Maintains linear search fallback for compatibility
- Significant performance improvement for large BTF with sorted types

### Patch 4: libbpf: Implement lazy sorting validation for binary search optimization
- Adds on-demand sorting verification
- Caches results for efficient repeated lookups

### Patch 5: btf: Optimize type lookup with binary search
- Ports binary search optimization to kernel-side BTF implementation
- Maintains full backward compatibility

### Patch 6: btf: Add lazy sorting validation for binary search
- Implements kernel-side lazy sorting detection
- Mirrors user-space implementation for consistency

### Patch 7: selftests/bpf: Add test cases for btf__permute functionality
- Validates both base BTF and split BTF scenarios

## Performance Impact Analysis

Repo: https://github.com/pengdonglin137/btf_sort_test

### 1. Sorting Validation Overhead
Test Environment: Local KVM virtual machine
Results:
- Total BTF types: 143,467
- Sorting validation time: 1.451 ms

*Note: This represents the maximum observed overhead during initial BTF loading.*

### 2. Lookup Performance Comparison
Test Case: Locate all 58,718 named types in vmlinux BTF
Methodology:
./vmtest.sh -- ./test_progs -t btf_permute/perf -v

Results:
| Condition          | Lookup Time | Improvement |
|--------------------|-------------|-------------|
| Unsorted (Linear)  | 17,282 ms   | Baseline    |
| Sorted (Binary)    | 19 ms       | 909x faster |

Analysis:
The binary search implementation reduces lookup time from 17.3 seconds to 19 milliseconds,
achieving a **909x** speedup for large-scale type queries.

## Changelog
v4:
- Abstracted btf_dedup_remap_types logic into a helper function (suggested by Eduard).
- Removed btf_sort.c and implemented sorting separately for libbpf and kernel (suggested by Andrii).
- Added test cases for both base BTF and split BTF scenarios (suggested by Eduard).
- Added validation for name-only sorting of types (suggested by Andrii)
- Refactored btf__permute implementation to reduce complexity (suggested by Andrii)
- Add doc comments for btf__permute (suggested by Andrii)

v3:
https://lore.kernel.org/all/20251027135423.3098490-1-dolinux.peng@gmail.com/
- Remove sorting logic from libbpf and provide a generic btf__permute() interface (suggested
  by Andrii)
- Omitted the search direction patch to avoid conflicts with base BTF (suggested by Eduard).
- Include btf_sort.c directly in btf.c to reduce function call overhead

v2:
https://lore.kernel.org/all/20251020093941.548058-1-dolinux.peng@gmail.com/
- Moved sorting to the build phase to reduce overhead (suggested by Alexei).
- Integrated sorting into btf_dedup_compact_and_sort_types (suggested by Eduard).
- Added sorting checks during BTF parsing.
- Consolidated common logic into btf_sort.c for sharing (suggested by Alan).

v1:
https://lore.kernel.org/all/20251013131537.1927035-1-dolinux.peng@gmail.com/

Donglin Peng (7):
  libbpf: Extract BTF type remapping logic into helper function
  libbpf: Add BTF permutation support for type reordering
  libbpf: Optimize type lookup with binary search for sorted BTF
  libbpf: Implement lazy sorting validation for binary search
    optimization
  btf: Optimize type lookup with binary search
  btf: Add lazy sorting validation for binary search
  selftests/bpf: Add test cases for btf__permute functionality

 kernel/bpf/btf.c                              | 177 ++++++-
 tools/lib/bpf/btf.c                           | 436 +++++++++++++++---
 tools/lib/bpf/btf.h                           |  34 ++
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/lib/bpf/libbpf_internal.h               |   1 +
 .../selftests/bpf/prog_tests/btf_permute.c    | 142 ++++++
 6 files changed, 728 insertions(+), 63 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_permute.c

-- 
2.34.1


             reply	other threads:[~2025-11-04 13:40 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-04 13:40 Donglin Peng [this message]
2025-11-04 13:40 ` [RFC PATCH v4 1/7] libbpf: Extract BTF type remapping logic into helper function Donglin Peng
2025-11-04 23:16   ` Eduard Zingerman
2025-11-05  0:11   ` Andrii Nakryiko
2025-11-05  0:36     ` Eduard Zingerman
2025-11-05  0:57       ` Andrii Nakryiko
2025-11-05  1:23         ` Eduard Zingerman
2025-11-05 18:20           ` Andrii Nakryiko
2025-11-05 19:41             ` Eduard Zingerman
2025-11-06 17:09               ` Andrii Nakryiko
2025-11-04 13:40 ` [RFC PATCH v4 2/7] libbpf: Add BTF permutation support for type reordering Donglin Peng
2025-11-04 23:45   ` Eduard Zingerman
2025-11-05 11:31     ` Donglin Peng
2025-11-05  0:11   ` Andrii Nakryiko
2025-11-05  0:16     ` Eduard Zingerman
2025-11-05  1:04       ` Andrii Nakryiko
2025-11-05  1:20         ` Eduard Zingerman
2025-11-05 13:19           ` Donglin Peng
2025-11-05 18:32             ` Andrii Nakryiko
2025-11-05 18:23           ` Andrii Nakryiko
2025-11-05 19:23             ` Eduard Zingerman
2025-11-06 17:21               ` Andrii Nakryiko
2025-11-07  2:36             ` Donglin Peng
2025-11-07 17:43               ` Andrii Nakryiko
2025-11-05 12:52     ` Donglin Peng
2025-11-05 18:29       ` Andrii Nakryiko
2025-11-06  7:31         ` Donglin Peng
2025-11-06 17:12           ` Andrii Nakryiko
2025-11-07  1:39             ` Donglin Peng
2025-11-04 13:40 ` [RFC PATCH v4 3/7] libbpf: Optimize type lookup with binary search for sorted BTF Donglin Peng
2025-11-04 14:15   ` bot+bpf-ci
2025-11-05  0:06   ` Eduard Zingerman
2025-11-05  0:11   ` Andrii Nakryiko
2025-11-05  0:19     ` Eduard Zingerman
2025-11-05  0:54       ` Andrii Nakryiko
2025-11-05  1:17         ` Eduard Zingerman
2025-11-05 13:48           ` Donglin Peng
2025-11-05 16:52             ` Eduard Zingerman
2025-11-06  6:10               ` Donglin Peng
2025-11-05 18:11             ` Andrii Nakryiko
2025-11-06  7:49               ` Donglin Peng
2025-11-06 17:31                 ` Andrii Nakryiko
2025-11-07  4:57                   ` Donglin Peng
2025-11-07 17:01                     ` Andrii Nakryiko
2025-11-10  2:04                       ` Donglin Peng
2025-11-04 13:40 ` [RFC PATCH v4 4/7] libbpf: Implement lazy sorting validation for binary search optimization Donglin Peng
2025-11-05  0:29   ` Eduard Zingerman
2025-11-04 13:40 ` [RFC PATCH v4 5/7] btf: Optimize type lookup with binary search Donglin Peng
2025-11-04 17:14   ` Alexei Starovoitov
2025-11-05 13:22     ` Donglin Peng
2025-11-04 13:40 ` [RFC PATCH v4 6/7] btf: Add lazy sorting validation for " Donglin Peng
2025-11-04 13:40 ` [RFC PATCH v4 7/7] selftests/bpf: Add test cases for btf__permute functionality Donglin Peng
2025-11-05  0:41   ` Eduard Zingerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251104134033.344807-1-dolinux.peng@gmail.com \
    --to=dolinux.peng@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox