Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 D419B4BEE53 for ; Fri, 9 Oct 2026 10:48:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791542914; cv=none; b=uXTWKZN1j1tGsMkqO+DYByjlIEAWLpMNbx4Il1aglCSj93Rg+l4FdUV3kIndDToIY1O51Z6yBdCYAuglFBpIBzZVNoBmwo6aqW1ZzOJZxYR7if4yRliLoShzPCPD1LtmjMqR3kNxD6xQlosSf6l1nrVxqpHelrNDJqvV9od5Bi4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791542914; c=relaxed/simple; bh=iqmehQKlLVBGKWn6F6QRxA/TrXyJZ3OUt3YIGM0qR5E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zvw1RfSae+ARE85N5fWKTmDROMeWh2I7uKdE/f59Qs9kcw0trFeSNZ6uCEyGuwYUN3Mv0UaC/e5VKVYRV4PlV/HQKbitc8zobZZHFqoFXfW4k+72+y5/DaXAVnlAGYeUbWDMCTWc2SIt4Hrqm8Uw6PYDqfZq/HCu1XgoNTx4nvM= 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=W7St+O3B; arc=none smtp.client-ip=209.85.215.179 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="W7St+O3B" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-cc1d57602e8so1886547a12.3 for ; Fri, 09 Oct 2026 03:48:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791542902; x=1792147702; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=o9dE2kwaJ+nDLD8FESjFzVFLFMIYXqiE+l8EiYwgpPI=; b=W7St+O3BiUTO3+ImH+sdhs/cF9obcfP9HFHFr08HRNsyGAKPSw4Bz5TqVRnnuItKEn tpW+ooQKawcLNwBVh0LtLvFSFAOWCAKlcp8XKlgrIgLGfbD9AeWgizYGvPdrIa1lonx6 veHBmuP43Z1MiX67dWp/ArKVz4ZLQookmyNAGzQa1lPI8haPwD2gsPcB/Hbt2gicmvyN VZHYzcd0kN7T/grgaUB4iBPZbmHebcZ/2nBN5o+CgBoArHzgIH64QAQuma9+d5rOgcay KG74qYOqwJWdOCBnwTeyYaJtQNrf0ta9t19VMycj+ZGwHno8L++2Z/Jmk9r62vPOGgiV A8tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791542902; x=1792147702; h=content-transfer-encoding:content-type: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 :content-type; bh=o9dE2kwaJ+nDLD8FESjFzVFLFMIYXqiE+l8EiYwgpPI=; b=d5hpxDST9N84TPuC+CJTqZuvk+2eYeLUEviXkX9IJ2eaCVbDlpvxCVR9sgQkhQHqRL MJHlrV2FMrFDhfNXqrf0Mt38ByydihcKpeVpg3FkmJx3XBLjJgdq+qFYEuE7fnhpuKQx qx6q6If+q0onADOFx27zvx0j0IP8zrO8ucjeKHu3y90qyvoy6SJlEN7c7aeGZa08Jl9d ij2vH69l3yHKz5pBtr48mqG6uN0/Wv4QvwVegfy6IyLyi8vq7B4uKGZSTJHMQRn8ofjz s73wicHCFJocCpa/C+S8voaSWGN6hKZSiQDnhoVfixdY7YgcjL7O6mpKI5Ej6/FYE04L hyaw== X-Gm-Message-State: AFq9FYLzs3IhtPou3T0VM07ItF6edNIYfgsJaAJrvoJAWzCcRcaWbpRv xY9XgPxJwLZutrXv7laeLjepwGSr9s95cteglrAyRcCxt9MdqHiSSfDVrZyBuPMY X-Gm-Gg: AYBFou3jHgX0odpD/brr/sdJk3wpvGfJD67BuXFb5+LJJFmbAAypi9l7stP+ROIR1k9 ZxGo7GBlZYMFCY+lxMvUTnfFmuUi9nqj7MJhQCWKfW8VeFvnJXiC87H9Q/iqY42hilXBQcIwN8T BIAdOK9xCkdVgwue66bAtr2bm5jtZp3LVYRA4+5HgMTG9TtldSNzJ12oZkc07wuUBT4PbjTlEoa 0Xzd3pQhLI6epWjfSrfjX1nivCHGKW7Zkm/wHrK0kk5+VKJxC3EBu4W2MNqzYgtobe7FaEOA8ga ZCh3e2Wt2TN4KzRuHGUaSOnx3+sCXDpoWrAq+G3t5JQyrox0CfYPFpyIybVNnYmYZObONUdiR37 8Qoc/JJEoB7u85nDx2UVNeMp6F/im5m3KkLyrBdJbgedfFm8STHHCzVxyK6ZcG6Xsh8Lux7zhaW L2nZJrUsperVp3H9e6gTP9kzh/BeHmiVfAmL1LLBEgC1qWk/20fsiPg1HGmWk2r/xVPnyXcWYlq pScfo5b+rCA+IM= X-Received: by 2002:a17:90b:4fcb:b0:3a6:dc2f:f07f with SMTP id 98e67ed59e1d1-3ab3a609f14mr1392916a91.10.1791542901892; Fri, 09 Oct 2026 03:48:21 -0700 (PDT) Received: from badger ([38.34.87.7]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3ab36ed053csm3050107a91.4.2026.10.09.03.48.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Oct 2026 03:48:20 -0700 (PDT) From: Eduard Zingerman To: bpf@vger.kernel.org, ast@kernel.org Cc: andrii@kernel.org, daniel@iogearbox.net, kernel-team@fb.com, yonghong.song@linux.dev, Eduard Zingerman Subject: [PATCH bpf-next v3 05/51] bpf: summarize may write stack slots in insn_aux_data Date: Fri, 9 Oct 2026 03:47:23 -0700 Message-ID: <20261009-scev-minimal-rebase-v3-5-bcba9bc53ac9@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261009-scev-minimal-rebase-v3-0-bcba9bc53ac9@gmail.com> References: <20261009-scev-minimal-rebase-v3-0-bcba9bc53ac9@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit SCEV needs may-write information to invalidate stack expressions after indirect writes. Liveness tracks this per function instance, but SCEV is not callchain-sensitive and needs a summary across calling contexts. For each instruction, union the current-frame may_write masks from all analyzed instances. Represent 8-byte slots as a summary of constituent halves. Store the summary in insn_aux_data and expose it via a helper. Signed-off-by: Eduard Zingerman --- include/linux/bpf_verifier.h | 6 ++++++ kernel/bpf/liveness.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index eafe79929dfa..4cb36ee3b6f7 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -662,6 +662,11 @@ struct bpf_insn_aux_data { }; struct btf_struct_meta *kptr_struct_meta; u64 map_key_state; /* constant (32 bit) key tracking for maps */ + /* + * Per-instruction summary of stack slots in the current frame + * that this instruction may write to. + */ + DECLARE_BITMAP(may_write_mask, MAX_BPF_STACK_SLOTS); int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ u32 seen; /* this insn was processed by the verifier at env->pass_cnt */ bool nospec; /* do not execute this instruction speculatively */ @@ -1742,6 +1747,7 @@ int bpf_compute_subprog_arg_access(struct bpf_verifier_env *env); int bpf_stack_liveness_init(struct bpf_verifier_env *env); void bpf_stack_liveness_free(struct bpf_verifier_env *env); int bpf_live_stack_query_init(struct bpf_verifier_env *env, struct bpf_verifier_state *st); +const unsigned long *bpf_may_write_mask(struct bpf_verifier_env *env, u32 insn_idx); bool bpf_stack_slot_alive(struct bpf_verifier_env *env, u32 frameno, u32 spi); int bpf_compute_live_registers(struct bpf_verifier_env *env); diff --git a/kernel/bpf/liveness.c b/kernel/bpf/liveness.c index e18d5d86c301..ec9c8beef556 100644 --- a/kernel/bpf/liveness.c +++ b/kernel/bpf/liveness.c @@ -718,6 +718,45 @@ static int cmp_instances(const void *pa, const void *pb) return 0; } +/* OR the 8-byte slots touched by a half-slot (4-byte) mask into @slots. */ +static void half_spis_to_slots(unsigned long *slots, const unsigned long *mask, u32 nbits) +{ + u32 slot; + + for (slot = 0; slot < MAX_BPF_STACK_SLOTS && slot * 2 + 1 < nbits; slot++) + if (test_bit(slot * 2, mask) || test_bit(slot * 2 + 1, mask)) + __set_bit(slot, slots); +} + +/* + * Precompute, for each instruction, the OR of may_write masks over its top + * frame across all func_instances reaching it, stash it in the insn_aux_data. + */ +static void compute_may_write_masks(struct bpf_verifier_env *env) +{ + struct bpf_insn_aux_data *aux = env->insn_aux_data; + struct bpf_liveness *liveness = env->liveness; + struct func_instance *instance; + struct frame_masks *fm; + u32 nbits; + int bkt, i; + + hash_for_each(liveness->func_instances, bkt, instance, hl_node) { + fm = instance->frames[instance->depth]; + if (!fm) + continue; + nbits = frame_mask_bits(fm); + for (i = 0; i < instance->insn_cnt; i++) + half_spis_to_slots(aux[instance->subprog_start + i].may_write_mask, + rel_mask(fm, i, FM_MAY_WRITE), nbits); + } +} + +const unsigned long *bpf_may_write_mask(struct bpf_verifier_env *env, u32 insn_idx) +{ + return env->insn_aux_data[insn_idx].may_write_mask; +} + /* print use/def slots for all instances ordered by callsite first, then by depth */ static int print_instances(struct bpf_verifier_env *env) { @@ -2352,6 +2391,8 @@ int bpf_compute_subprog_arg_access(struct bpf_verifier_env *env) goto out; } + compute_may_write_masks(env); + if (env->log.level & BPF_LOG_LEVEL2) err = print_instances(env); -- 2.53.0