From nobody Sat Feb 7 18:21:03 2026 Received: from mail-dl1-f50.google.com (mail-dl1-f50.google.com [74.125.82.50]) (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 2455230DED5 for ; Thu, 15 Jan 2026 14:49:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768488599; cv=none; b=arqeLcPt0Ik9x5be6Z7de9LU8TfxRlK7AtA4zHGIqo3jJ4d+m3cJU3jH9pC9Nr7+e2G9+ObZMy3dtsfddCtDHC67DAfgtu3rhAoSNq+pemeCHVLH2IYZB6lVr9hnwH619K4mMRXME8lCtDzsT2LnrQUN4pSF8ak1OvmwLNIh52Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768488599; c=relaxed/simple; bh=RW55d/JaaWFpEiXYObuDlyFwRLauudhCxr+54ALHDYI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=fm2tLJ0SGD98YOEcz+MzPcA6jhdZaZx0fepZfALDTt/n6Zu24EDd6oQCzkdIvhJ+BzbvTk/630wqRRZJU/+8uAf0Gpao+qW7SUlo9j5h7ZIFT1Dvuk6EAAtZKKDZwET7JZtjzXOxgxXKWdNeR1C8Rh0F30k4eOkd60mo1KsXINc= 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=JyQ8gMyl; arc=none smtp.client-ip=74.125.82.50 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="JyQ8gMyl" Received: by mail-dl1-f50.google.com with SMTP id a92af1059eb24-12448c4d404so166269c88.1 for ; Thu, 15 Jan 2026 06:49:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768488597; x=1769093397; 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=ayyj+smXP+IDsPB6AjrrQ2LVZ2utyXTn+MR3JfEzhYE=; b=JyQ8gMylPaAYIOY4/qT6p4nWDBkdeOy2VIHh+5qj0ayVQKj4Z/xqnNJmIcYq6okHj4 g4Ahl+4lG4Kp0xp9IePw5ifDP+pAeGLQqFPCW+xxz1f2mXa15LdYjqFztyf63pDMLgHd 6WCZZNQxStKIjpViTD9dMTCAWUGAis3+SbDYFCsn9fMLS5KkYINpVgduS03vaHWZmeBi sXjfCunst5yJG43XGdVbsR5FoWXcm76IAdKkJ0mWBQ7xB6zbBDpfwIL/HpKsTCDJP1qE JxCl+BFhJsMRIaznGMeNy+nH4VovNRPltks69Nk7eqgBr+4A02FBWzB0V/XnXh8/BsEV px7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768488597; x=1769093397; 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=ayyj+smXP+IDsPB6AjrrQ2LVZ2utyXTn+MR3JfEzhYE=; b=E/XkYeTK1FlM+qoMxPhVicKrs5mPMUyquA6Qx0GQUTsnfrmPYH4YjPCIWiuz9U9EFR IO0A92ek2655uzCuvkTD3Bq3VGViBztfoGpLYKVwwV7HRgh3Q7ieWLdqagnoh8+lE54k lejJJSZ/CI6lT5qxNQa5NEIjxaBVFCbOPnV1JcZC3y/QZxBcqA3lmHhYWKfO6m6d7Rio xG2yCMz2y9fLw1jg9U220EW7O23Vx/wu7kx6ZfO0eVQGYY8j23zaVxh/Hq5Rj1tOYCjL YBTyMHb5T7OEkQYw8HdZezpWdHAxWBEA27+8Y5uOGjfYSa2N5X6jKzzm60RtHCAJsG0/ LnUw== X-Forwarded-Encrypted: i=1; AJvYcCUVWB73cRzc861jK0WpS0yH21oQdC+Ong/5gu8+eolMJq1ApOJdJN0LZPzNIqBRzzdOJYzXfGJTd/cGakY=@vger.kernel.org X-Gm-Message-State: AOJu0Yzw68zPu2KoHNyIMYZ3t4sFr961k+yf63szINOhSynBD4k77ry6 7jYs5Zn+YB3nsECabMbc689leTWWX7+O8SazGBxCkcKFJSbttuZoq273 X-Gm-Gg: AY/fxX69NBWM/IyPoZWkYL87WAMKwRZDctbRtBgsHIdVbY7iW+apYeEoQiPytGsRaEY +c3F0P5TPau2E2c6VXFwYXnOI0v6qZ7EgRi+iPTwE43XCuuOpKF+bojAE6FxgZbjaWn51IlNoxJ FfBkzdw1ppRkJQbYktvo8jpAHR/UKRrJJkPFdbZ+3gRhKgw3OjwFhPTBskuuqhYitKU9ZdUdRBd 5HxSl1UnIaWHk6x4/Kzbcgq6bDUGBeAAwORbfFKeLcaG6Ok+V8jGKgZMp3IyGsXDn7FX+X8EQcT rJiSq0UJxSlCisW+p5pMQ8sKqbNnOWoDPo/lLSM1rzZ6tN4m6Mnaw3m8Cs5U8HHukqb6kzcWlZn k+nTKttdsMIr5iw6wU8OoIBWShuePv3jWoDuw23N+4palZCwx9lSkq1OGrdXV406YCF8cwgLJrN A8hTzqwPqn9MZ3ZmUvKsBwpYc= X-Received: by 2002:a05:7022:e889:b0:119:e55a:9bf6 with SMTP id a92af1059eb24-12336a4514bmr5934658c88.18.1768488596841; Thu, 15 Jan 2026 06:49:56 -0800 (PST) Received: from localhost.localdomain ([74.48.213.230]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-121f24985d1sm33835539c88.16.2026.01.15.06.49.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jan 2026 06:49:56 -0800 (PST) From: wujing To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, wujing , Qiliang Yuan Subject: [PATCH] bpf/verifier: optimize ID mapping reset in states_equal Date: Thu, 15 Jan 2026 22:49:46 +0800 Message-Id: <20260115144946.439069-1-realwujing@gmail.com> X-Mailer: git-send-email 2.39.5 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 in every call. The table size is exactly 4800 bytes (approx. 4.7KB), calculated as: - MAX_BPF_REG =3D 11 - MAX_BPF_STACK =3D 512 - BPF_REG_SIZE =3D 8 - MAX_CALL_FRAMES =3D 8 - BPF_ID_MAP_SIZE =3D (11 + 512 / 8) * 8 =3D 600 entries - Each entry (struct bpf_id_pair) is 8 bytes (two u32 fields) - Total size =3D 600 * 8 =3D 4800 bytes For complex programs with many pruning points, this constant large memset introduces significant CPU overhead and cache pressure, especially when only a few IDs are actually used. This patch optimizes the reset logic by: 1. Adding 'map_cnt' to bpf_idmap to track used slots. 2. Updating 'map_cnt' in check_ids to record the high-water mark. 3. Making reset_idmap_scratch perform a partial memset based on 'map_cnt'. This improves pruning performance and reduces redundant memory writes. Signed-off-by: wujing Signed-off-by: Qiliang Yuan --- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 130bcbd66f60..562f7e63be29 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 map_cnt; struct bpf_id_pair map[BPF_ID_MAP_SIZE]; }; =20 diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 37ce3990c9ad..6220dde41107 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -18954,6 +18954,7 @@ static bool check_ids(u32 old_id, u32 cur_id, struc= t bpf_idmap *idmap) /* Reached an empty slot; haven't seen this id before */ map[i].old =3D old_id; map[i].cur =3D cur_id; + idmap->map_cnt =3D i + 1; return true; } if (map[i].old =3D=3D old_id) @@ -19471,8 +19472,13 @@ 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; + if (idmap->map_cnt) { + memset(idmap->map, 0, idmap->map_cnt * sizeof(struct bpf_id_pair)); + idmap->map_cnt =3D 0; + } } =20 static bool states_equal(struct bpf_verifier_env *env, --=20 2.39.5