Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100214; b=5E74nsjHSDqDvmC5HXm4/ks5j5VitZOCWPUequ7gbfeXCg9WlY0YENHnjkySTARUvKk7 08Jm1L8wDehGDkros4YHcUeSd9vi/2CH0WKb0p4CG5CsMeqnC5eMDJlfTqV9GD6FJ9udv o79c/sBpLzAbopBSE//zpMlAWK083JxbbObyURNJdNkkDLVbibYHy96LDhmT3GwwYT847 hE84uYipc/scV5tP8gS78Lw2ijo0ojpoAgDVoQpVXjyUlUU2c+DcQ0fM+ERzKuvpoCXPu NLUhTJnPzF7YU7a7DTI6A2Gygjwgy2uB4lheiuG6CXBa38LEkO6443Pcfom3PPHxTfA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100214; 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=XQc99gcAwfb9j+Sszj7rv3OUGFplb1Oau/oZYprUics=; b=WxHZzOvgXXNgU0Amre2hc8iM0ReLrnCEE0u1HbT6+QnAnt+0J+sdZnkAgnH6auCNQl1U iPhmGvR3bOViXvhVsO8G6EUsLbkiMl4TLtN2fLtQcU/ZAIuWmKteplO34bqLW2KuUC7LF s9Y6SGacvSsBMjRUVMM6CbLOUBRhX7FzMIJEU1wSqXv4Ku3DG9OpKt6g5Nr7gPjHBi2cy O+uQBfR69iXV29+4Rf1u39ewvm+uEI6Igwjw94P9ic58GCDziFftPrB/Z17mJhhlkE7Dr CszfH3T0rdyPZFwfclQbM03hH9O25yUDeY7oCmK84RAjSqSOz3ogXTHbdBsyaz/cQSQ== 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=GchyjEGV; arc=none smtp.remote-ip=74.125.231.72 Received: from mail-oa2-f8.google.com (mail-oa2-f8.google.com [74.125.231.72]) (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 8BD45606A31A for ; Tue, 1 Dec 2026 04:43:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8BD45606A31A 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=GchyjEGV Received: by mail-oa2-f8.google.com with SMTP id 586e51a60fabf-4ad654ea104so2721864fac.1 for ; Mon, 30 Nov 2026 20:43:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100212; x=1796705012; 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=XQc99gcAwfb9j+Sszj7rv3OUGFplb1Oau/oZYprUics=; b=GchyjEGV3Oaty/5JGv7fp3IgIVR1ZeMoZTEIcoBQjL2zxTbnEpFlsmXWflZNYwZ/s1 Bu4E3fkJQgXSf/SmoJ+gSBf2Iw8Ei6JpoCkMMqPUnk/4X8BVvZY6NKIXDXpW1uWB7kZ8 KiltWVioZC60I5e1+OfPtpu/IRwHnufLdXU2HMUqlNX/zY9YYLP88FtGTISOeEvAkrSA 9tDZTWU1a8i0fLpbFy1CeDUJFwA/L7kdVwydny0Bx5CRsx2rzK9lkiRHxDp3ZeKvh4JT 3NXMaD4C1PbWv1bIqEviktaWwpMA0sRP13skFnJoNcP5kPcHDUNSqMG040yzkeKJVGO2 dW8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100212; x=1796705012; 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=XQc99gcAwfb9j+Sszj7rv3OUGFplb1Oau/oZYprUics=; b=sWO4fxnNQiwBDLs6i59gC9ETtE55uKbENRJVBNCcX621qUdGAQkMj1ysj+fydzwmCS afAwOAuTa0l2vsyR+ed/Ua1xmg39JQZesTbyJcZoTRpcvFh1IFn0oh874NKwdWuZPMzh K0v23eaUaBx2ON5K/7EItwW6YkjVuFgKtYOxbqYGqebj4oVKrhd9CS3pDY6kpq4XbLeb luWYtwnifgunX+7KlLohKWABN8+c7dtYQ38y4cLeRSQ5rHf/xZNNfSWEb6R1/Jmk5plr Zcx5oPK2EsmP4fIXVJ6poxoeUPHKUoGa5Xr/EoCTFLmCxb6gFBcGOIHhi4k8MHo1IfT/ Uokg== X-Gm-Message-State: APuU4Z9Su8xpXYeifLGmYszcEgZGvUZmMc00czwRw5Y3tg0cKlkvSUE/ lXQQnPZi8YuxyDYgmApU9rkE1IFNDcIdpi/lw2lejwBzaEHDg5SeDD/ftJbhGKXB31U= X-Gm-Gg: AZFdTZeMHBC7TPIcCcZFBk+trCcD4EmsWjq+cOsH7Y991eJZb43YL8SCNiJLPLU6i8a OMAhY+pazX1kL1zbKe0Ubv9f18NJ7Um+hrIjLuiU/S+5pT9eaT/j+kgDQ6G4F2XmuA1jYNmt4k2 0qyTPWNHpXOieKXJJ2FEslpvYXDMqlEQ9ICCRV7e0bYQIEnakBFi0fWffQ9jgKXunbM7Du8f0Ma droDFQ5e7ZGhkYiWIKgg7uL39hDOMd1q0ABul/JHJRy4MlhC2QXc7v+DLVxcDw6qrI00OK5tJZf NkLUskyUwajzEWV7AgL0goaHTjAGeV2rwr/+xq4eucm1YC6ZCZH+9njnFZAdZoWzbR+eYrdNOe5 nC5tTEprnqjkspXSKJA1KT4muKz3FjRx5ExehQjMWVuFniW1g/u4/QKdT0sWbxwoo3puMMl5J+y kWQB70NHFYU3CHVNhZ2KKwJtyGHflElEZC+46PrEMmCvhhtA9qjFRZTrElpOquSN3FdzZEHb4lM 4mJG1BaBuY1qQyWyzF9no0+0C7iW54tNAJU61NSUFehSg7y4mJ3TyftL4Z5yQUPkeGkdNUfrYkK 8uoUDN5jB2DtHh1NN3vWOynDFdeJAJfpyJkYeGz+hdy2MBugTSFaxKSRMtwYd0vP987EUMpFpSt AE40wPFYSYAA8slhxGCev/5WLOvxqwloDsEoI X-Received: by 2002:a05:6871:8912:b0:4b4:4d98:2514 with SMTP id 586e51a60fabf-4bc8259c9c6mr36281082fac.11.1796100212336; Mon, 30 Nov 2026 20:43:32 -0800 (PST) Received: from localhost ([2a03:2880:10ff:42::]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4bc5fc97bb2sm30451207fac.9.2026.11.30.20.43.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:43:30 -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 67/73] bpf: Run the cleanup pads of a program when the kernel cancels it Date: Tue, 1 Dec 2026 05:39:34 +0100 Message-ID: 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=15441; h=from:subject:message-id; bh=k8qLIMmgsFkTiE0W4EceEvlYuxqP5u2Vf3hs66gBvPg=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb4nUw191bb9u2Us8UDtw7hT3Kg63qbpx6yOip0R+nLK5 Xi6xo5SFQYyLQVZMkaXk/z4m4xOVvwNtl3HDzGFlAhnCwMUpABPpt2dk2MXltnqeZHP9NoZt7RmHdu 5dnGOh4bt+Fsf2MtkopweToxgZelg9eD5vVOmZs6LurR2n6rc28UcrW/wt5i6dFjor5dQvJgA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently everything that a cancellation needs to run a cleanup pad is in place, but nothing calls bpf_find_cleanup_pads(), so no unwind_goto is marked as leading to a cleanup pad. Call it from bpf_check(), before the main pass. For a program this means: - When the kernel cancels the program in a subprog or in a callback, the code at the target of the unwind_goto after the call runs if it is a cleanup pad. R0 is 0 at the target. - The unwind_exit that this code ends in leaves with the value of the cancellation, whatever is in R0, and no unwind_goto is taken for it. The cancellation continues in the same way with each caller. - A cancellation still cannot be caught. A landing pad of the program that can continue running is not entered, and a cancelled frame never returns to its caller. - A cleanup pad is verified a second time, for the cancelled frame. An error on that path rejects the program, also one that loaded before. A cancellation point inside a cleanup pad behaves as follows: - If the pad runs for the program's own unwinding, a cancellation starts there as anywhere else. - If the pad already runs for a cancellation, the frame leaves at that point through a generated pad. The rest of that cleanup does not run, and the program still returns the value of the first cancellation. A call that has a cleanup pad of its own is different: the frame continues in that pad. - After a time-out the time stays up, so a loop in cleanup code ends at its first may_goto. After a fault in arena, bpf_throw() or a recursion that is too deep, a cleanup pad is not cut short this way. A program can count on a cleanup pad where a cancellation reaches it. The pad is entered, or the program is rejected and the log says why: - The stack of the function has no room for the hidden words. - The verifier cannot release what the frame still holds after the cleanup pad, or at a site that the frame reaches in the pad or in a function that the pad calls. A cancelled frame gets no hidden slots of its own. - The cleanup pad changes what a caller holds in a way that the generated pad of the caller cannot follow. It unlocks a lock or leaves an RCU section of the caller on some paths only, writes to its irq flags, or releases what a function on the stack needs a hidden slot for. The destructors of a frame do not run on a cancellation in these cases, and in no other: - The program's landing pad can return or never reaches unwind_exit: a catch clause, and the landing pad of a noexcept function. Destructors that such a landing pad runs before that do not run either. - The function cannot be cancelled. - The frame is the one in which the cancellation starts: the frame of the loop, of the access to arena, of the recursion check or of bpf_throw(). Inlining decides which frame that is. - The call of a frame above it has no unwind_goto, because the callee is noexcept or nounwind or was inlined. A function in which a cancellation enters a cleanup pad has hidden words in its frame, and a function that had no stack gets one. Only a mixed function also pays a store per call. verifier.rst now describes cleanup pads, the "cancelled" word and the save words, and the uapi comment of BPF_UNWIND_GOTO says when the code at its target runs for a cancellation. No existing test changes. Signed-off-by: Kumar Kartikeya Dwivedi --- Documentation/bpf/verifier.rst | 109 ++++++++++++++++++++++++++++++++- include/uapi/linux/bpf.h | 17 +++++ kernel/bpf/verifier.c | 4 ++ tools/include/uapi/linux/bpf.h | 17 +++++ 4 files changed, 144 insertions(+), 3 deletions(-) diff --git a/Documentation/bpf/verifier.rst b/Documentation/bpf/verifier.rst index 8555d023dbaf..2acb05e996fd 100644 --- a/Documentation/bpf/verifier.rst +++ b/Documentation/bpf/verifier.rst @@ -1032,7 +1032,8 @@ The landing pad of the call releases nothing, and nothing is added at run time. A call hands over a pointer in one of ``R1`` - ``R5`` that the callee reads and that cannot be NULL. It does not hand over an iterator, a dynptr, a reference that was made from another one, or one that another one was made from. It hands -nothing over to a recursive function or to a callback. +nothing over to a recursive function or to a callback, nor while a frame on the +stack is cancelled already, see `Cleanup pads`_. The program is still rejected if the callee has no location for the pointer at one of its sites, if a landing pad of the callee would release under a lock of @@ -1102,8 +1103,8 @@ program has an ``unwind_goto`` after a call, the ``cancel_goto`` follows it:: pad: r0 = 0 exit -A cancellation enters no landing pad of the program, so the program cannot -catch it. +A cancellation enters a landing pad of the program only if that pad cannot +catch it, see `Cleanup pads`_. Where a callee leaves with ``unwind_exit`` and the caller has no ``unwind_goto`` after the call, the verifier adds one, with a landing pad that @@ -1131,6 +1132,106 @@ A global function is different because its callers are checked without it and only see a number as its return value. A compiler that wants an object to get through a global function gives the call there a landing pad of its own. +Cleanup pads +------------ + +A *cleanup pad* is a landing pad of the program that can only clean up and +continue unwinding. It runs a destructor of C++ or drop glue of Rust. Two rules +decide it, see ``bpf_find_cleanup_pads()``. From the first insn of the pad, + +- no path in the function reaches an insn that returns to the caller: ``exit``, + a tail call, ``LD_ABS`` or ``LD_IND``, +- some path reaches ``unwind_exit``. + +A cancellation runs the cleanup pads of the frames that it leaves. The same +code then serves the program's own unwinding and a cancellation, so the frame +remembers which of the two is under way in a hidden word of its stack, the +"cancelled" word:: + + what the program has what the verifier makes of it + + call foo call foo + unwind_goto pad unwind_goto stub + ... cancel_goto entry + pad: call dtor ... + unwind_exit pad: call dtor + gotol rest + stub: cancelled = 0 <- own unwinding + gotol pad + entry: cancelled = 1 <- cancellation + r0 = 0 + gotol pad + rest: if cancelled == 0 goto +N + release what the frame still holds + cancel_exit + unwind_exit + +The entry puts 0 into ``R0``, where the program's own unwinding has the object +that it threw. The code at ``rest``, the remainder of the pad, replaces its +``unwind_exit``. A frame that the program unwinds still leaves with +``unwind_exit``. A cancelled frame leaves with ``cancel_exit`` and the value of +the cancellation, whatever is in ``R0``. So no landing pad of the program in a +caller can catch the cancellation, and it continues in the same way with each +caller. + +Nothing on the normal path writes the "cancelled" word. A function in which a +cancellation enters a cleanup pad pays for its hidden words in stack, and a +function that had no stack gets one. A function also pays one store per call, +which clears the word at its entry, if its normal path falls into a cleanup pad +or jumps to it, or if a cleanup pad jumps back before its call. + +The verifier checks a cleanup pad a second time, for the cancelled frame: with +0 in ``R0``, and with what the frame holds when its callee is cancelled, see +``enter_cleanup_pad()``. An error on that path rejects the program. + +The remainder releases what the frame still holds after the cleanup pad. Only +``R0`` is live at the ``unwind_exit`` of the pad. So the entry keeps the +pointers that the frame holds in ``R6`` - ``R9`` or in stack slots in hidden +*save words*, at most 8, see ``visit_saves()``. Only a pointer is saved. The +cleanup pad itself has to release irq flags, an iterator or a dynptr in the +stack of a caller, and a lock whose address only a register holds. If the +states at a call do not agree on what the frame holds, the entry of that call +saves nothing, and the remainder releases only what has a location at the +``unwind_exit``. + +A cleanup pad can have sites of its own. If the pad runs for the program's own +unwinding, a cancellation starts there as anywhere else. If the pad already +runs for a cancellation, the frame leaves at that site through a landing pad. +The rest of the cleanup pad does not run, and the program still returns the +value of the first cancellation. A call that has a cleanup pad of its own is +different: the frame continues in that pad. After a time-out the time stays +up, so a loop in a cleanup pad ends at its first ``may_goto``. After a fault in +arena, ``bpf_throw()`` or a recursion that is too deep, a cleanup pad is not +cut short this way. + +A program can count on a cleanup pad where a cancellation reaches it: the pad +is entered, or the program is rejected. The log then has a line with "the +kernel would enter", which names the cleanup pad where the verifier knows which +one it is. The program is rejected in these cases: + +- The stack of the function has no room for the hidden words. +- The verifier cannot release what the frame still holds after the cleanup pad, + or at a site that the frame reaches in the pad or in a function that the pad + calls. A cancelled frame gets no hidden slots of its own. +- The cleanup pad changes what a caller holds in a way that the landing pad of + the caller cannot follow. It unlocks a lock or leaves an RCU section of the + caller on some paths only, writes to its irq flags, or releases what a + function on the stack needs a hidden slot for. + +The destructors of a frame do not run on a cancellation in these cases, and in +no other: + +- The program's landing pad can return, or never reaches ``unwind_exit``: a + catch clause, and the landing pad of a ``noexcept`` function, which ends in + ``std::terminate()``. Destructors that such a landing pad runs before that + do not run either. +- The function cannot be cancelled. +- The frame is the one in which the cancellation starts: the frame of the loop, + of the access to arena, of the recursion check or of ``bpf_throw()``. + Inlining decides which frame that is. +- The call of a frame above it has no ``unwind_goto``, because the callee is + ``noexcept`` or ``nounwind`` or was inlined. + After an error ============== @@ -1702,6 +1803,8 @@ budget a limit on the cost of one step cancel the kernel ends the program before it returns by itself, and each frame releases what it holds checked the bytes of a packet that are known to be before its end +cleanup pad a landing pad of the program that can only clean up and + continue unwinding control_deps xregs whose value flows to a jump that can skip an unproven insn correlated two branches: one tests a result that the other one stored diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 95b41ae6584d..bd793db5489d 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -66,6 +66,23 @@ enum bpf_cond_pseudo_jmp { * Follows a call of a bpf subprog, callx, or a call of a helper or kfunc * that calls a callback. Taken when the subprog or the callback left with * BPF_EXIT that has src_reg == BPF_UNWIND_EXIT. + * When the kernel cancels the program in the subprog or the callback, + * for a loop that is out of time, a recursion that is too deep, a fault + * in arena or bpf_throw(), the code at the target runs too if it is a + * cleanup pad: no path from the target gets to BPF_EXIT without + * BPF_UNWIND_EXIT, to a tail call or to BPF_LD_ABS or BPF_LD_IND, and + * some path gets to BPF_EXIT with BPF_UNWIND_EXIT. R0 is 0 at the + * target then. That BPF_EXIT leaves with the value of the cancellation + * whatever is in R0, and no BPF_UNWIND_GOTO is taken for it: the + * cancellation continues in the same way with each caller. + * A program can count on a cleanup pad where a cancellation reaches it: + * the pad is entered, or the program is rejected and the log says why. + * A cancellation does not reach it in a function that cannot be + * cancelled, in the function in which the cancellation starts, and at a + * call that has no BPF_UNWIND_GOTO. And when the time of the program is + * up, a loop in a cleanup pad ends at its first may_goto: what follows + * the loop does not run. + * See Documentation/bpf/verifier.rst. */ BPF_UNWIND_GOTO = 1, }; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index ada65af7e4a3..bd76774ca60f 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -27489,6 +27489,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, if (ret < 0) goto skip_full_check; + ret = bpf_find_cleanup_pads(env); + if (ret < 0) + goto skip_full_check; + ret = bpf_flow_place_guards(env); if (ret < 0) goto skip_full_check; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 95b41ae6584d..bd793db5489d 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -66,6 +66,23 @@ enum bpf_cond_pseudo_jmp { * Follows a call of a bpf subprog, callx, or a call of a helper or kfunc * that calls a callback. Taken when the subprog or the callback left with * BPF_EXIT that has src_reg == BPF_UNWIND_EXIT. + * When the kernel cancels the program in the subprog or the callback, + * for a loop that is out of time, a recursion that is too deep, a fault + * in arena or bpf_throw(), the code at the target runs too if it is a + * cleanup pad: no path from the target gets to BPF_EXIT without + * BPF_UNWIND_EXIT, to a tail call or to BPF_LD_ABS or BPF_LD_IND, and + * some path gets to BPF_EXIT with BPF_UNWIND_EXIT. R0 is 0 at the + * target then. That BPF_EXIT leaves with the value of the cancellation + * whatever is in R0, and no BPF_UNWIND_GOTO is taken for it: the + * cancellation continues in the same way with each caller. + * A program can count on a cleanup pad where a cancellation reaches it: + * the pad is entered, or the program is rejected and the log says why. + * A cancellation does not reach it in a function that cannot be + * cancelled, in the function in which the cancellation starts, and at a + * call that has no BPF_UNWIND_GOTO. And when the time of the program is + * up, a loop in a cleanup pad ends at its first may_goto: what follows + * the loop does not run. + * See Documentation/bpf/verifier.rst. */ BPF_UNWIND_GOTO = 1, }; -- 2.53.1-Meta