Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100204; b=RnAb9j4HsXf5OXHPRCAaxO2eH2ZWoyx3dqPqazdDWD22froNCg7ayL627iPJIhAJAO2a 6SxIwikKCKFeURWeCnI/PszbUlsgmMAEu9ZpdhOObrjvjjaSfQQPQTD5De+eDi6J0XaQU hKZpLczCku1o6C7nss8YNdNPdyU0EHlV8duYEPrNzNCNxC+Q0GSIPS3J9ov5fPRK+ZatP gEq8b2zCJYfd3KAriu59YDDhQTBZyHDQqtDl88vXh4ZHuBJ6k06Uv7UXIRtqWnr6VUqq3 86Mpnnp3YB717aAjeI5sifTUzPDl0j+LXEIWxj1FJiBs8cEI6GlkWPBjtDfM0SppLUg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100204; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: X-Developer-Signature:X-Developer-Key:Content-Transfer-Encoding; bh=fLg1AHcUn4UwX97cls7xz7W9Mb+yex5mtoy1962h2+Y=; b=bl1ol+c6T33nYzHa5Zin81wTzWODcIVArZZRFV7K64hcsZgdTVKrOx22Gvf2pVAJwzt7 1b0d8TqfkVCVGs6uBVHxKDfueDBYEsMpklEHK2xJUToNnm7/QB4ixc3akAdTmzVC3H0ok F/pAGy1h78dnHoUNBlNYoX0zJFlEzhMTpWrP5+iJ5sesD1mnGkQ1LjlqcOafCv60ifcWn kBfSL4BfL5IjhBTqrg4FYa5/ShsLEhUt1JJOCDSiQkUCqzbyyR3H1Lq0NkJvQoH/+Sdg0 iLjFXyBfMlnpVa+wJJB+g/nNNBvHGSmUdEhy7wO4jv2OYKtaKUUbrzjF4vhPV2UNLwQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=VEZkrNB1; arc=none smtp.remote-ip=74.125.231.198 Received: from mail-oi2-f6.google.com (mail-oi2-f6.google.com [74.125.231.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BEB7606A31A for ; Tue, 1 Dec 2026 04:43:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1BEB7606A31A 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.a=rsa-sha256 header.s=20260707 header.b=VEZkrNB1 Received: by mail-oi2-f6.google.com with SMTP id 5614622812f47-545a4fb8d5dso2876931b6e.1 for ; Mon, 30 Nov 2026 20:43:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100203; x=1796705003; 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:content-type; bh=fLg1AHcUn4UwX97cls7xz7W9Mb+yex5mtoy1962h2+Y=; b=VEZkrNB14ZsD+nf3VBrL/69TSlngNysluxzn7irwNT3DaFJVZ/ZX6h2X+XXI6fj1kv XDoCJ/XdIGvMktwdXYrP6r+JN8NknSam7nKvdLiDFhy9WAWrMrrNPn9gYoGDIEgCYEkE y33i38JlegvVrsljXcYe9lrr1TWoX8hTZcxiVgetwaaccNTJZCshQne/f8/6vNf8Ahzv /rfnXkdim6WuNWpIXt2uOhpHrcf0KpBCK8r6l4j7B9yoaAOVaFLo1z/SD6zKfxLLaJiq ih6ekNmlOmSfi+O3HGuLRON80Hr6bgjDN6CvjfUaSCF+0VSnM94fF2TPpGALKQ/dbKA4 M0vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100203; x=1796705003; 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:content-type; bh=fLg1AHcUn4UwX97cls7xz7W9Mb+yex5mtoy1962h2+Y=; b=klGD8IJ/y0op7GmOqowMbfehx8OgIgXfnAD4u1Vm9kQxICbNZiDrxCpI5vrpIew3DR aeS6T4ghJDR2gs8eZGUg7HR2FX4FFPC3NHKse9fcUvXoznv+8ITU1Dj/Xy1jxqtYtKaN dqzUEtr7iLT2YbBKNcH3ZmDy1+ZHJy8InNz99wF/0k6Q0PiWLsPPDYRQHlab542Y25e5 jfiTowuTeCBjS5B6cV8aKe11DiDBTZiK5RoWPed9JXF0W8kogBZKxjSAgWBRNIPble7r qjDJcfjSodYasC7h1RT6YhRVSwxMny/aIWkpPp5RpocD0qbyIB+Xh1nYwnyifMIrii8y FqcA== X-Gm-Message-State: APuU4Z/xVcTnOISadEYz8s0cJLhbEA+RnQiuCXZ714j9NhrsHosFCivM iYbkNo245NoHqPUJ1iI5wZ/osEVepbCjY0pNCrZhTFtiFD4UgvbxNJhHxWHFy6B/iXI= X-Gm-Gg: AZFdTZfJ+E6wyFlCdsxTIjpwvtbCfdCpNcvKKBBJ0UHe5fpRkc/g9gOpKu5KHr0s0o4 fN8KECGI88Gl3epQHGx7zK2322E45/j95sFdsBTmFwm2Ol5jfL/XgOcMdeMGraMAM+c8hq7yhqT prtMFWL94IYMB9rY8sRvC7BOuzfGFak/O+6h2h/Ngvz3rVOrbzVRqtcXcllGEMhgXeBNZs0Sp3C SrptHkgdeOyCr5aynQH61wlGF0kj8zbCEmZK/7PDMvr+bWxZ6Jud/RkxcSe/bI+4PDiS0NuX7fp ssrtd4yc1xUK5NdH8BTUYbu2EpJOFRVwbyOW3LED+9WMW2Txb09Pd6WNefqOxLOn9rXPeQ6QGUq m8HnTNLwJXNJ1JR874f9h3zSadPDZ9YJm1l4zqhASg0rY5yM31XopM3SJn5pezO7kyk8f/jHXpD xU//v/bXyAnmrxbdbVej7cGjlep14/zjIi7lGFSulCQazbtW92xQqS05ndxtbeuYffWFuQ0stG7 LTzrymoKOvOVp3CqyupIQG4/jfqdkX+KuxZHB21PLjUzJYRaT6hFOulzup5KJjl+W1RS2Vv1jbF JLogTFn5Xj+gDoSPl35fMcEri6zNEcW7fhkqSQMU4Wbmf76DcaO0lZU7MVJa2P+rRwtKqf2Xhl2 Yy9tTUMK4Ivte6kXZG3jyC5kV7qND0DuSuytqbVgOJF1XYPDXkzbcjTUwdQ== X-Received: by 2002:a05:6808:1387:b0:564:4f93:709f with SMTP id 5614622812f47-57a0d3eb9c3mr37881419b6e.67.1796100203054; Mon, 30 Nov 2026 20:43:23 -0800 (PST) Received: from localhost ([2a03:2880:10ff:42::]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57a31928e6dsm25632878b6e.2.2026.11.30.20.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:43:21 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com Subject: [PATCH bpf-next v1 64/73] bpf: Find the code that a cleanup pad can reach Date: Tue, 1 Dec 2026 05:39:31 +0100 Message-ID: <7f9d2815cc3feaddff9dc26e1e877edc8331dab3.1796099738.git.memxor@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10841; h=from:subject:message-id; bh=xWhXCq3XsXbppGNc+rQwkt8CYOuaFIZLVwQQFJLkBQM=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb3FQ5dSXk14vP2TpucGl7+yFRx26Mzs6X/+3rLSvK2Vk zt7bUcrCIMbFICumyFLyfx+T8YnK34G2y7hh5rAygQxh4OIUgIlMj2ZkWMHw7PJKjsV9U6RebT+yRe l7Quw3ufy8n7cydL5XlESUqjMyNIXKRInzGodpGlaU3p7jvHjF+/aCz1sLgvMWvliYYR/NCQA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit The "cancelled" word is going to be written only where a frame enters a landing pad of the program: by the stub and by the entry. That is enough only if no frame reaches code that reads the word without passing one of the two. So the verifier has to know which code a frame can run on either side of those two edges. Find that out on the control flow graph, in bpf_find_cleanup_pads(): - Pad code is what can be reached from the first insn of a cleanup pad. A cancelled frame starts there and follows edges of the graph, so it runs pad code and nothing else. - The normal path is what can be reached from the first insn of the function without the jump of an unwind_goto. - The rest is the program's landing pad of a typed catch, until it returns to the normal path or joins pad code. No cancelled frame runs it. call a normal path unwind_goto 1f call b unwind_goto 2f exit 2: if r0 != 7 goto 3f a typed catch r0 = 0 exit 1: call dtor pad code 3: unwind_exit A function is mixed if one of its insns is pad code and on the normal path as well: the normal path falls into a cleanup pad or jumps to it, a cleanup pad jumps back before its call, or the function has no exit. In any other function a frame gets from the normal path into pad code in two ways only: by the jump of an unwind_goto, and through an entry. A tail call, LD_ABS or LD_IND has an edge to the exit insn that stands for its return. That edge is not followed, as in the search for cleanup pads. The exit is often the unwind_exit that the cleanup pads end in, and a tail call on the normal path would make the function mixed for no reason. The result depends on the insns alone. Nothing reads it yet. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 4 ++ kernel/bpf/pads.c | 125 +++++++++++++++++++++++++++++------ 2 files changed, 110 insertions(+), 19 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index aa204e909579..97892d66274e 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1424,6 +1424,8 @@ struct bpf_insn_aux_data { u32 jump_target:1; /* an unwind_goto of the program that leads to a cleanup pad, see bpf_find_cleanup_pads() */ u32 cleanup_pad:1; + /* pad code: a cleanup pad of its function reaches the insn, see bpf_find_cleanup_pads() */ + u32 cleanup_pad_code:1; /* it can read or write the frame of a caller, in a call with a known callee */ u32 reads_outer:1; u32 writes_outer:1; @@ -1713,6 +1715,8 @@ struct bpf_subprog_info { bool arg_refs_differ: 1; /* it has an unwind_goto that leads to a cleanup pad, see bpf_find_cleanup_pads() */ bool has_cleanup_pads: 1; + /* "mixed": an insn of it is pad code and on the normal path, see bpf_find_cleanup_pads() */ + bool cleanup_pads_mixed: 1; /* a cancellation enters one of its cleanup pads, see bpf_mark_subprogs_that_unwind() */ bool enters_cleanup_pads: 1; /* where it is cancelled or unwinds, callees included: bits by enum bpf_site_kind */ diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index 01292cff77c9..7907f59068be 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -4257,6 +4257,104 @@ static bool insn_returns(const struct bpf_insn *insn) (BPF_MODE(insn->code) == BPF_ABS || BPF_MODE(insn->code) == BPF_IND); } +/* + * Find the code that a cancelled frame can run, in the functions that have a cleanup pad. Pad code + * ('cleanup_pad_code') is what can be reached from the first insn of a cleanup pad: a cancelled + * frame starts there and follows edges of the control flow graph. The normal path is what can be + * reached from the first insn of the function without the jump of an unwind_goto. The rest is the + * program's landing pad of a typed catch, which no cancelled frame runs: + * + * call a normal path + * unwind_goto 1f + * call b + * unwind_goto 2f + * exit + * 2: if r0 != 7 goto 3f a typed catch + * r0 = 0 + * exit + * 1: call dtor pad code + * 3: unwind_exit + * + * A function is MIXED ('cleanup_pads_mixed') if one of its insns is pad code and on the normal path + * as well: the normal path falls or jumps into a cleanup pad, a cleanup pad jumps back before its + * call, or the function has no exit. In any other function a frame gets from the normal path into + * pad code in two ways only: by the jump of an unwind_goto, and through an entry. + * + * A function is MIXED, 'cleanup_pads_mixed', if one of its insns is pad code and on the normal path + * as well: a pad that the normal path falls into or jumps to, a pad that goes back in front of its + * call, or to the first insn of its function, any pad of a function that has no exit. In every + * other function the two share no insn, and a frame gets from the normal path into pad code in two + * ways only: by the jump of unwind_goto, when a callee left with unwind_exit, and through the entry + * of a pad, when the kernel unwound the callee. + * + * The exit that stands for the return of a tail call or of LD_ABS is no edge here, as it is none + * where the pads are judged: it can be unwind_exit of the code that resumes, and a tail call on + * the normal path would make every such function mixed. + */ +static int find_pad_code(struct bpf_verifier_env *env) +{ + const struct bpf_insn *insns = env->prog->insnsi, *insn; + struct bpf_insn_aux_data *aux = env->insn_aux_data; + int i, p, t, s, cnt, len = env->prog->len, err = 0; + struct bpf_subprog_info *sub; + unsigned long *pad, *normal; + struct bpf_iarray *succ; + bool changed; + + pad = bitmap_zalloc(len, BPF_VERIFIER_GFP); + normal = bitmap_zalloc(len, BPF_VERIFIER_GFP); + if (!pad || !normal) { + err = -ENOMEM; + goto out; + } + for (s = 0; s < env->subprog_cnt; s++) + if (env->subprog_info[s].has_cleanup_pads) + __set_bit(env->subprog_info[s].start, normal); + for (i = 0; i < len; i++) + if (aux[i].cleanup_pad) + __set_bit(i + 1 + bpf_jmp_offset(&insns[i]), pad); + /* the successors of an insn come before it in the postorder: walk that from its end */ + do { + changed = false; + err = bpf_add_cost_and_check(env, env->cfg.cur_postorder, BPF_COST_SCAN); + if (err) + goto out; + for (p = env->cfg.cur_postorder - 1; p >= 0; p--) { + i = env->cfg.insn_postorder[p]; + if (!test_bit(i, pad) && !test_bit(i, normal)) + continue; + insn = &insns[i]; + succ = bpf_insn_successors(env, i); + cnt = insn_returns(insn) ? min(succ->cnt, 1) : succ->cnt; + for (t = 0; t < cnt; t++) { + if (test_bit(i, pad) && !__test_and_set_bit(succ->items[t], pad)) + changed = true; + /* the second successor of an unwind_goto is its jump */ + if (test_bit(i, normal) && !(t && bpf_is_unwind_goto(insn)) && + !__test_and_set_bit(succ->items[t], normal)) + changed = true; + } + } + } while (changed); + for (s = 0; s < env->subprog_cnt; s++) { + sub = &env->subprog_info[s]; + for (i = sub->start; sub->has_cleanup_pads && i < sub[1].start; i++) { + aux[i].cleanup_pad_code = test_bit(i, pad); + if (sub->cleanup_pads_mixed || !test_bit(i, pad) || !test_bit(i, normal)) + continue; + sub->cleanup_pads_mixed = true; + if (env->log.level & BPF_LOG_LEVEL2) + verbose(env, + "insn %d of func#%d is on the normal path and in a landing pad that only resumes\n", + i, s); + } + } +out: + bitmap_free(pad); + bitmap_free(normal); + return err; +} + /* * Find the cleanup pads of the program on the control flow graph, before the main pass. The landing * pad of an unwind_goto is a cleanup pad ('cleanup_pad' in the aux data of the unwind_goto) if, @@ -4275,23 +4373,11 @@ static bool insn_returns(const struct bpf_insn *insn) * * a cleanup pad returns no unwind_exit * - * only resumes returns no unwind_exit - * - * A typed catch gets to its handler by a branch on what the personality routine of the program - * says, which is a value like any other: the pad returns. Pads that share their last insns, as the - * pads of one function of C++ do, are judged one by one, each from its first insn. - * - * What a frame that the kernel unwinds runs from there on is what the pad gets to, and that need - * not be code of a pad: a pad can jump back in front of its call, or to the first insn of its - * function, and in a function that has no exit every pad only resumes. Such a frame runs the - * normal path of the function once more. What holds for it is not that it runs cleanup code only, - * it is that no insn which it can get to returns to its caller: it is left at unwind_exit or at a - * site, with cancel_exit, and nowhere else. See enter_cleanup_pad() in verifier.c. - * - * The answer is a property of the insns, the same in every pass: 'cleanup_pad' of unwind_goto. The - * kernel enters each of these pads when it unwinds what the call in front of it calls, in a - * function that can unwind, or the program is rejected: see bpf_pads_settle(). - * Dead branches are gone from the graph, see bpf_prune_dead_branches(). + * A typed catch reaches its handler by a branch on a value, so its landing pad returns. Landing + * pads that share their last insns are judged one by one. A cleanup pad can jump back before its + * call, so a cancelled frame need not run cleanup code only. The verifier relies on one thing: no + * insn that the frame can reach returns to the caller. The answer depends on the insns alone, so it + * is the same in every pass. */ int bpf_find_cleanup_pads(struct bpf_verifier_env *env) { @@ -4299,7 +4385,7 @@ int bpf_find_cleanup_pads(struct bpf_verifier_env *env) int i, p, t, pad, cnt, len = env->prog->len, err = 0; unsigned long *returns, *resumes; struct bpf_iarray *succ; - bool changed, ret, res; + bool changed, ret, res, found = false; for (i = 0; i < len && !bpf_is_unwind_goto(&insns[i]); i++) ; @@ -4353,12 +4439,13 @@ int bpf_find_cleanup_pads(struct bpf_verifier_env *env) res = test_bit(pad, resumes); env->insn_aux_data[i].cleanup_pad = !ret && res; if (!ret && res) - bpf_find_containing_subprog(env, i)->has_cleanup_pads = true; + bpf_find_containing_subprog(env, i)->has_cleanup_pads = found = true; if (!(env->log.level & BPF_LOG_LEVEL2)) continue; verbose(env, "landing pad at %d of the call at %d %s\n", pad, i - 1, ret ? "returns" : res ? "only resumes" : "has no unwind_exit"); } + err = found ? find_pad_code(env) : 0; out: bitmap_free(returns); bitmap_free(resumes); -- 2.53.1-Meta