From nobody Sun Feb 8 07:08:05 2026 Received: from mail-dl1-f44.google.com (mail-dl1-f44.google.com [74.125.82.44]) (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 0B95F350293 for ; Fri, 16 Jan 2026 09:29:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768555770; cv=none; b=kzpywibS9KFkou2INQfgkC5zZNPcn7BFWZuqRJjtrpTevVbAHiaDJaJ/Y0Ok7RRzqfjSI4CiI0QLlKCHybr8IakAm/GKr/YpVf/W7kkXQh1Myd7ftgwaAAQFfIzafnmNUh2sZSrD+3TiO+vC60CZli1PEIhXk4GTTxi5QbEvUw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768555770; c=relaxed/simple; bh=mE9xjNvoz+1IMTysusBHF1ML5m3fOMvLEJBkotKcgdw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=J3lN/ZxKem/dyRnewbZ29k+scKrgQXP46Odb93p7ONOV6OdfzdUnqIxuaDnDXf4of9prq/sh5rdptTdamYMgpOUC0UwUyM/vQkDlJX8Kg5zF1waxrYJeRyhnFvucSoEd3WQHPPlvdmlV+euzKQnRxKi5NV0FQMsvUVtkcJOoayo= 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=UocNpL1M; arc=none smtp.client-ip=74.125.82.44 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="UocNpL1M" Received: by mail-dl1-f44.google.com with SMTP id a92af1059eb24-1232d9f25e9so3395961c88.0 for ; Fri, 16 Jan 2026 01:29:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768555761; x=1769160561; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5idxUqndBr36uoUMlgR0ZpyfBo6cXXyztiYxqo6MKRo=; b=UocNpL1MPuFnxjJVneku0CZ5L/Aqo3+X6T9CTJWIBZhbgvBxoeStueZOsj1GtFhhT+ gOYG/857prCOSVU5RlbeOY7FUUS1TZsWwMTDLaIXzq84nLmiCC9fbDotdX2WsG+EH33q rwVluLplriG4VeJyPt6hacKMlY2sFYSkZsu6BWaZ8dww2vicbZFfOgHZfjvx9YJofwsI 1tCt1XIYSC7ilnvYTfFsWMyo6P/hWgoH7TI+PVcZKTy2f3QI9a8gLC6BYSvrFaosBJCz YjZ0bPOnQ51FfG/Qpmg6MteHOYgo97CgFfu8pgnyG8l+kH1lxV1u2eaFCB96gf+LRMvD +Xxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768555761; x=1769160561; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5idxUqndBr36uoUMlgR0ZpyfBo6cXXyztiYxqo6MKRo=; b=IPDvLrDM0aWdxR3gWRN5BkTjfr80/WdfYYkOZKCBua/H1/5VbKDeLIls0x9yqftqK1 5yHjuQJ5iLjJ378Or9UJUR8JcbjjihIr2x1zNBLS0BSDVH2lDyC2/Bk6dVXLL2EfaI1H wqqQQZ3lB7XcFlJnrtZED1OkNHSwqONmESLCUJ4SGH4pZHjU527OeNCqIOqkEfctbCU3 pdxKd6p5KpYJ4ebDnWPg67HfujslOMPFD5KfIYZtDtHXSL8dHROEEjKdoFCAwQVLvGAd ZOkmdJpDXAY2BbkMITf9jObAm7jixPqak3LKyOH3kFrHJrCmqNlyeTWmIMoGIXITpO8w 5TIA== X-Forwarded-Encrypted: i=1; AJvYcCW7XQaGUE6ukWDjncGyCFUnoPpYza5EHbwL5o899XW+hDb21ygxMFpAJlieqjHU+dQYe8N3tMTSIy8yNY8=@vger.kernel.org X-Gm-Message-State: AOJu0YypUo0DFnu3GEFhxVUphN06E7Cbzrhi9NYbjWA6DhQfOQxSPwph X/C0QRKHlMJq8+QXhqxXDat03/3bCikbJQe9Y49x38WRVyHkb6vWGEh8 X-Gm-Gg: AY/fxX7v99Op2WHAD6iOHodnbs0hLUpFpvY5ucaJilGo6aZ7Cqnrcr4iGPL57ycOVaY JHOITx5MpsK7llKF+HeFWav9WjSyot63C97QPogmHpGmKBY1j9ra7rfLGbFveK+jQnfQ5Egvrk+ xHZNlWKKvfcWQWENxeiXLafwccH0Nq9KB39UogtjC9LyEfZRLqg1yhk4ZucIqbvL2GGuKzCfP2m vbDSA8Vv4iw+R2wKPzIgdg4W/P6kteuDKvzw4hj3sWY7rGp+EXABmarIRWTdqp/E/4GY9wrmsrR 6eid7fiJGiSsx98Gh4QC+QZ/BNZ+yW9TV/KxguXsnw5XAxdmRGQiVnckUv3Si0fGdvSep77ZcZQ ZZSTFCzkhbFFcVgYAJVCxydbXpDY6lWkjePWpkDRXSPCmP9RAdDGJM9YaBSXeECPOY9HszBuITC JFvfa+N08Oc2ct8LGxuWncX3A= X-Received: by 2002:a05:7022:6291:b0:119:e56b:958a with SMTP id a92af1059eb24-1244a72ae2bmr2006030c88.15.1768555760531; Fri, 16 Jan 2026 01:29:20 -0800 (PST) Received: from localhost.localdomain ([74.48.213.230]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1244a8938ddsm1567600c88.0.2026.01.16.01.29.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jan 2026 01:29:20 -0800 (PST) From: Qiliang Yuan To: andrii.nakryiko@gmail.com, eddyz87@gmail.com Cc: andrii@kernel.org, ast@kernel.org, bpf@vger.kernel.org, daniel@iogearbox.net, haoluo@google.com, jolsa@kernel.org, kpsingh@kernel.org, linux-kernel@vger.kernel.org, martin.lau@linux.dev, realwujing@gmail.com, sdf@fomichev.me, song@kernel.org, yonghong.song@linux.dev, yuanql9@chinatelecom.cn Subject: [PATCH v2] bpf/verifier: optimize ID mapping reset in states_equal Date: Fri, 16 Jan 2026 17:29:08 +0800 Message-Id: <20260116092908.32252-1-realwujing@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: References: 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 verifier uses an ID mapping table (struct bpf_idmap) during state equivalence checks. Currently, reset_idmap_scratch performs a full memset on the entire map (~4.7KB) in every call to states_equal. Following suggestions from Eduard Zingerman and Andrii Nakryiko, this patch optimizes the reset logic to avoid unnecessary memory operations: 1. Rename 'map_cnt' to 'cnt' for brevity. 2. Replace the O(N) memset() in reset_idmap_scratch() with a simple O(1) counter reset (idmap->cnt =3D 0). 3. Update check_ids() to search only up to 'idmap->cnt' entries. If no mapping is found, append the new mapping and increment 'cnt'. This ensures that reset overhead is minimal and the search loop is bounded by the number of IDs actually encountered in the current equivalence check. Benchmark results (system-wide 'perf stat' during high-concurrency 'verista= t' stress test, 60s): The following results, captured using perf while running veristat in parall= el across all CPU cores, show a significant reduction in instruction overhead (~9.3%) and branch executions (~11%), confirming that the O(1) reset logic significantly reduces the verifier's workload during state equivalence checks. Metric | Baseline | Patched | Delta Suggested-by: Andrii Nakryiko Suggested-by: Eduard Zingerman ----------------|---------------|---------------|---------- Iterations | 5710 | 5731 | +0.37% Instructions | 1.714 T | 1.555 T | -9.28% Inst/Iter | 300.2 M | 271.3 M | -9.63% Cycles | 1.436 T | 1.335 T | -7.03% Branches | 350.4 B | 311.9 B | -10.99% Migrations | 25,977 | 23,524 | -9.44% Test Command: seq 1 2000000 | sudo perf stat -a -- \ timeout 60s xargs -P $(nproc) -I {} ./veristat access_map_in_map.bpf.o Detailed Performance Stats: Baseline: Performance counter stats for 'system wide': 6,735,538 context-switches # 3505.5 cs/sec = cs_per_second 1,921,431.27 msec cpu-clock # 32.0 CPUs C= PUs_utilized 25,977 cpu-migrations # 13.5 migrati= ons/sec migrations_per_second 7,268,841 page-faults # 3783.0 faults/= sec page_fault_per_second 18,662,357,052 branch-misses # 3.9 % bran= ch_miss_rate (50.14%) 350,411,558,023 branches # 182.4 M/sec = branch_frequency (66.85%) 1,435,774,261,319 cpu-cycles # 0.7 GHz cy= cles_frequency (66.95%) 1,714,154,229,503 instructions # 1.2 instruc= tions insn_per_cycle (66.86%) 429,445,480,497 stalled-cycles-frontend # 0.30 fronten= d_cycles_idle (66.36%) 60.035899231 seconds time elapsed Patched: Performance counter stats for 'system wide': 6,662,371 context-switches # 3467.3 cs/sec = cs_per_second 1,921,497.78 msec cpu-clock # 32.0 CPUs C= PUs_utilized 23,524 cpu-migrations # 12.2 migrati= ons/sec migrations_per_second 7,783,064 page-faults # 4050.5 faults/= sec page_faults_per_second 18,181,655,163 branch-misses # 4.3 % bran= ch_miss_rate (50.15%) 311,865,239,743 branches # 162.3 M/sec = branch_frequency (66.86%) 1,334,859,779,821 cpu-cycles # 0.7 GHz cy= cles_frequency (66.96%) 1,555,086,465,845 instructions # 1.2 instruc= tions insn_per_cycle (66.87%) 407,666,712,045 stalled-cycles-frontend # 0.31 fronten= d_cycles_idle (66.35%) 60.034702643 seconds time elapsed Suggested-by: Eduard Zingerman Suggested-by: Andrii Nakryiko Signed-off-by: Qiliang Yuan --- Hi Eduard and Andrii, Thank you for the feedback on v1. I've optimized the ID mapping reset logic to O(1) and updated the benchmark results as suggested. v1 -> v2: - Rename map_cnt to cnt (Andrii Nakryiko) - Eliminate memset() by using cnt to bound search loop (Andrii Nakryiko) - Remove unnecessary if() check in reset_idmap_scratch() (Eduard Zingerman) - Use full name in Signed-off-by (Eduard Zingerman) include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 130bcbd66f60..8355b585cd18 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -692,6 +692,7 @@ struct bpf_id_pair { =20 struct bpf_idmap { u32 tmp_id_gen; + u32 cnt; struct bpf_id_pair map[BPF_ID_MAP_SIZE]; }; =20 diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 3135643d5695..6ec6d70e5ce7 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -18948,18 +18948,21 @@ static bool check_ids(u32 old_id, u32 cur_id, str= uct bpf_idmap *idmap) if (old_id =3D=3D 0) /* cur_id =3D=3D 0 as well */ return true; =20 - for (i =3D 0; i < BPF_ID_MAP_SIZE; i++) { - if (!map[i].old) { - /* Reached an empty slot; haven't seen this id before */ - map[i].old =3D old_id; - map[i].cur =3D cur_id; - return true; - } + for (i =3D 0; i < idmap->cnt; i++) { if (map[i].old =3D=3D old_id) return map[i].cur =3D=3D cur_id; if (map[i].cur =3D=3D cur_id) return false; } + + /* Reached the end of known mappings; haven't seen this id before */ + if (idmap->cnt < BPF_ID_MAP_SIZE) { + map[idmap->cnt].old =3D old_id; + map[idmap->cnt].cur =3D cur_id; + idmap->cnt++; + return true; + } + /* We ran out of idmap slots, which should be impossible */ WARN_ON_ONCE(1); return false; @@ -19470,8 +19473,10 @@ static bool func_states_equal(struct bpf_verifier_= env *env, struct bpf_func_stat =20 static void reset_idmap_scratch(struct bpf_verifier_env *env) { - env->idmap_scratch.tmp_id_gen =3D env->id_gen; - memset(&env->idmap_scratch.map, 0, sizeof(env->idmap_scratch.map)); + struct bpf_idmap *idmap =3D &env->idmap_scratch; + + idmap->tmp_id_gen =3D env->id_gen; + idmap->cnt =3D 0; } =20 static bool states_equal(struct bpf_verifier_env *env, --=20 2.39.5