Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.139 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100090; b=Z1XYB0QTtAvM+jtFs6k/Rfd8vACcT/wpgJEWj/kc0WtezB7oxDi782rrnsVlugmo5KNd GSQIaQR7GqL+i0WfTYGNCNASIb/IMxsL3LxXWAoMrFQ4rQ6VBhvUbBztRpr0cyC7QxCCW ZUj+CnfgpO5WosOl+jZ0+/PJ8d2Lbpbq+QGUYlM78SqLvCKYEbscju4hcRc6aS5Lyag3I Nc/f0hQ+AZJWPmqTP5wZjEKy2X9a9I3L86Mt7+LJUnRPGn47ALJLfkVV6F2frN1lHrnFf WJBB0eu8Ua8XZ1XYIKFeIjjRcveW3r/EmH2EPOJA1wxVeMA0Eca1511BvvuACEiEgAQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100090; 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=cONwqZONqsPVVAPHlPsA+j07u0xYv1hm1MRD6ohAxFw=; b=mGagvAARDq/3HLuDX8ZMeg/pXAqWn8v5rpdg8ZRBkgeqHdQnds9loBWYv2sxDtBZg+8t NQPFMQo4bR3N0jXFAkdMFXODenuMrqSdm3XwdZkIxLvTvQLqaarce4jwrfQ2U6s6CQAhj T542vnFOV8NmyDmm7UGPDldQuwOujiXzkXtfm1lXxuJ4fvtaX9f3g94sqiC7Z06hGm1HR sqaCdfjWzGtAoWQBnu2mPJNP+68Dnuv/dACgH3gUs5q8iJATEcZTVEUlhjkRxmJarGV/o l6drbOKe0grXRyAZmgdN3QupDP5p6onPY80Ke1BztcQ9wQC9ov+yJzasXSFRAb56fww== 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=us4qhss+; arc=none smtp.remote-ip=74.125.231.139 Received: from mail-oo2-f11.google.com (mail-oo2-f11.google.com [74.125.231.139]) (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 5AE0A606A31A for ; Tue, 1 Dec 2026 04:41:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5AE0A606A31A 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=us4qhss+ Received: by mail-oo2-f11.google.com with SMTP id 006d021491bc7-71b9fb2b4beso1304995eaf.1 for ; Mon, 30 Nov 2026 20:41:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100088; x=1796704888; 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=cONwqZONqsPVVAPHlPsA+j07u0xYv1hm1MRD6ohAxFw=; b=us4qhss+Bv9asCy43GcI/G7XdTDcnJrXXKzd5rOkAlD4u0x0R10WYt7GnHEArHXLYG Bkoeu1fq3xKtf2MEeA6KDxTSjDO6CZbjQOpzoUNpBJQ1mwWkJzT8myLiweU9LDjcyZfL Pmy/tqbd1iDF325buyQ6rDJZsldzIGomFQPivka7SnHwacw0Dj03MzfNdNk0GO1gG2Pl 4Vk/niYbM1WTDEP7wStMLuoc2vU9T8pWzHy+QON/gexz1bWsH8/8fsaG1AzlyWBYi4B7 PrGQEoPF/r8FJ+ji7ZzQV/0vTGl6vBfsXk+EVakWAkeNIyDkC17qRvRBOlPnpxt0458D 42Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100088; x=1796704888; 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=cONwqZONqsPVVAPHlPsA+j07u0xYv1hm1MRD6ohAxFw=; b=KGCm2RY06cSh17JyUF3rKTOhCJD05FHNlCDFhBEFz3CSWDPfeNImU3lgURbclk/WAv e+vocDGfpWu9QyHrTzduYbSfV0IaofLeEpduJ1oi0oq0AS1BAglCjJ/U2Z+zNTfm/cQm +CONYEPtrEBK1+iGezVldD5pCjO68Ace11Yq6e0Fio6iv1o6cID9ZSw7UJ75wImCSHNQ Sg5zXUbT7eEMXJrqAARDAHoh/Z6J5LNw69Kult5Xb4ipZSfKapNZQCQjQoyuWc7qfoi3 iwa0ZWlz/3m5234svweUimHrRTDcaZBxr+DjUXaKcHz9gAFZ1UgjwMWYgyI4z/1acXc+ iaJg== X-Gm-Message-State: APuU4Z9lFK7Qr+3tOQ5TxZ0diVGLA02DGW+HnecoET0qrlf53+/nOzLJ FZaOnvuX4JV4Rfe8z1d9Ljp5gKYSyxQ2Ne0x7EzLbTYrNMpBbVCKAv/GGDQ4CPA6cfw= X-Gm-Gg: AZFdTZck7bzvXC5X3e3wyMacgnVXBywTBYaU9d7DldUTiFVUt3XYgFlZggKhtiwixFp sFF3SIzosIqc5l2uGFAxYAFP4VbtxeqhVRfKi4RLNsMR8aNfd2ICrHJSS+HxzfSzDy4ftXkXOy6 jbFjNEpT1H6lkPFQGgkYdYcil+wJNKGycA/54ejr+2zSX6F6thXxHwkOhc0gUZqZ0UbFyCW6UUo EarkRrveK7AqOyAv20Tcwh8Z6VeU79PNp1QVA7MB4vtpQcmIQANHi3LWI1l5m1hj/9EOIFXIhpk rU5owZFpR6OUZaLY6N6kwMW4ugZB/H5sK/Een5gflKdcERUQcv9GWdkKht3t5vF++XPn0buSrqG O+BZ0CEwmIt1gJ17ZPyQvVAKOKEcuxhm/T14brWlM9WmnvrsEcQoYjGQm1VH6Jj3/Pyu8PscXCK nzizn98v+PP0etLRHfr95A+40DgS5sVFfuRMoxVJgsZ9biEyHMJnOaTNnfSgDz+YPWy8afDdzpl 5Uk8tLq7NnC5Ejb8220YNHxgTG+8L7uFK+WLdhzaRy1r/nkyYFXHd8QgQisIXc7n/1oJlrPvInG yvnI1cccuRrZH0+dpfTO8/WmUkT4KGOs8p0i9AXBrw7CR++totAcBme3qIEi9f565ZmYLlFsShJ jAutQM1aGUTL01xQWIOXw1pd8qzJDlIV6FQxoUw== X-Received: by 2002:a05:6820:612:b0:71b:7b70:671f with SMTP id 006d021491bc7-71b7b706966mr17352588eaf.71.1796100087545; Mon, 30 Nov 2026 20:41:27 -0800 (PST) Received: from localhost ([2a03:2880:10ff:45::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b3806fc45sm23829175eaf.8.2026.11.30.20.41.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:41:26 -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 28/73] bpf: Let hidden slots follow what a subprog does for its caller Date: Tue, 1 Dec 2026 05:38:55 +0100 Message-ID: <34d26948820d3bdbeea1541cc08b691feee86ad4.1796099737.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=57555; h=from:subject:message-id; bh=nbmiumgYNsnsU5hb2pY5wMWBujHOGla7dw6ksPoOjAk=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb962Z+ocur0foz9OONRd/WDppuNHrzRrvPde9m33Hcu4 4HWsHaUsDGJcDLJiiiwl//cxGZ+o/B1ou4wbZg4rE8gQBi5OAZhI/DKG/9k6Bi+ez8qJc70hn/Z450 zrm7vtHHviU9trxNX73271C2FkmB3XL37IJp4lKHsZm8UHq1V9q3t1utbfbg48s9rrgOcpBgA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently the stores that keep hidden slots up to date stand next to the insn that acquires or releases. A function gets no slots for a group of resources once a subprog that it calls returns with something other than one pointer in R0, leaves a section of the function or releases a lock of it. So a lock that is taken and dropped through two functions works only where the program counter of the caller tells what is held: /* returns 0 with irq flags saved and preemption disabled, or -EBUSY */ static int lock(struct lock *l, unsigned long *flags); if (flag && lock(l, &flags)) return 0; work(l); /* can be cancelled */ if (flag) unlock(l, &flags); At the call of work() the frame is in the section and has the flags saved on some paths only, and the program is rejected. This is the lock of bpfjailer where the compiler does not inline it. Let the call stand for what the subprog does, as the call of a kfunc does. The stores are in the caller, after the call, and only a normal return reaches them: - The subprog returns in one section more: the slot takes 1. - It returns with irq flags that it saved where an argument of the call points: the slot takes that address. The argument register is dead after the call, so the address waits in one more hidden word of the function, the pending word, which a store before the call fills. - It returns with these on some returns only: the stores are under a test of R0. The lower half of R0 has to be 0 on all returns of one kind and not 0 on all others. - It has left a section that the frame entered, or released the lock or the irq flags that the frame held: the slot is cleared. The call of lock() becomes this, where '+' marks what the verifier adds: + *(u64 *)(r10 - 32) = r2 r2 is &flags: the pending word call lock + if w0 != 0 goto +1 + *(u64 *)(r10 - 16) = 1 the slot of the section + if w0 != 0 goto +2 + r1 = *(u64 *)(r10 - 32) + *(u64 *)(r10 - 24) = r1 the slot of the irq flags lock() itself can need a slot for the same flags. Its landing pad restores them through their address, which lock() gets in an argument, and the compiler keeps the argument no longer than the function reads it. If lock() can be cancelled after the save, no register or stack slot has the address there. Let such flags ask for hidden slots for the locks of the function, as a pointer that no location holds asks for the slots for references: the slot of the call that saved the flags has the address. The flags are still in the stack of the caller when the pad runs, because states are joined only if the same stack slots hold irq flags. Two cases need care: - A subprog that is cancelled does not get to the stores after its call. Its own landing pad has undone what it acquired and the sections that it left, but not the release of a lock or irq flags of the caller: the slot still has the address. So a frame that has released such a thing for a caller is marked (took_locks), as is every frame between the two. Where a marked frame is cancelled, or returns to a helper or to an unwind_goto, the caller gets no slots for its locks. - Which argument had the address of the irq flags is asked of the state that returns from the subprog. The subprog has stopped reading its arguments by then, and a state that had something else in that register could have been pruned on the way. So what R1-R5 had at the call, as far as they point to the stack, is part of the join key of the callee. These cases still rule out slots, and the program is rejected where a landing pad needs them: - a subprog that returns with a lock other than irq flags, with two pointers or with an iterator, - a subprog whose R0 does not tell what it returns with, or that releases for its caller on some returns only, - a subprog that releases a pointer of its caller, - a function that leaves a section of its caller and needs slots for its own sections. Signed-off-by: Kumar Kartikeya Dwivedi --- include/linux/bpf_verifier.h | 78 +++- kernel/bpf/fixups.c | 6 +- kernel/bpf/pads.c | 676 ++++++++++++++++++++++++++++++----- kernel/bpf/verifier.c | 11 +- 4 files changed, 659 insertions(+), 112 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index d101bda70824..b4b19cc605df 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -501,10 +501,10 @@ enum bpf_pad_conflict { #define BPF_PAD_MAX_CANDS 6 /* * The most insns that keep hidden slots up to date at one call: nine for each of at most - * BPF_PAD_MAX_ENTS slots, five that compare and clear it and four that find a free one. And the - * call. + * BPF_PAD_MAX_ENTS slots, five that compare and clear it and four that find a free one. Three per + * group after the call of a subprog, a test of R0 and two for the pending word. And the call. */ -#define BPF_PAD_MAX_SLOT_INSNS (BPF_PAD_MAX_ENTS * 9 + 1) +#define BPF_PAD_MAX_SLOT_INSNS (BPF_PAD_MAX_ENTS * 9 + 3 * BPF_PAD_NR_GROUPS + 1) /* The kinds of location */ enum bpf_place_loc { @@ -573,6 +573,7 @@ struct bpf_pad_plan { bool no_place_atomic; /* enum bpf_pad_conflict: no location has a pointer. No error if a hidden slot has it. */ u8 no_place; + u8 no_irq_place; /* the same for irq flags in the stack of a caller */ u8 nr_ents; bool ordered; /* see order_ents() */ struct bpf_pad_ent *ents; /* oldest first. The pad releases the last one first. */ @@ -584,6 +585,7 @@ struct bpf_pad_plan { u32 nr_callees; /* the pad starts that many insns after the last insn of the subprog, once it is emitted */ u32 pad; + u32 bytes; /* its part of env->mem, see charge_plan() */ }; /* The groups of classes. A function has hidden slots for all of a group or for none of it. */ @@ -595,12 +597,44 @@ enum bpf_pad_group { BPF_PAD_NR_GROUPS, }; +/* What the lower half of R0 is on a return of a subprog, see bpf_pad_note_return() */ +enum { + BPF_RET_ZERO = BIT(0), + BPF_RET_NONZERO = BIT(1), + BPF_RET_UNKNOWN = BIT(2), +}; + +/* When a store after the call of a subprog runs, see bpf_pad_note_return() */ +enum bpf_pad_cond { + BPF_PAD_ALWAYS, + BPF_PAD_IF_ZERO, /* the lower half of R0 is 0 */ + BPF_PAD_IF_NONZERO, +}; + /* * What the main pass records at an insn for the hidden slots of its function: what the insn * acquires or releases, and which register has the pointer. The stores are emitted from it, so it * has to hold for all states that reach the insn, or the function gets no slots for the group. + * The call of a subprog acquires what the subprog returns with, see bpf_pad_note_return(). */ struct bpf_pad_note { + /* the call of a subprog has one note for each group that the subprog returns with */ + struct bpf_pad_note *next; + /* + * The insn calls a subprog that returns with the resource, and the slot is filled after + * the call. For irq flags, 'src' is the argument that had their address at the call. + */ + bool by_callee; + u8 cond; /* enum bpf_pad_cond for that store, see subprog_take_slots() */ + /* + * In the first note of the call of a subprog, by group: BPF_RET_* of the returns with and + * without something of the group, and whether the subprog had released what the frame + * held of the group on some return (takes) and had not on some return (keeps). + */ + u8 hold_r0[BPF_PAD_NR_GROUPS]; + u8 nohold_r0[BPF_PAD_NR_GROUPS]; + u8 takes; + u8 keeps; /* The insn acquires a resource, or it calls a subprog that returns with one */ bool acquires; u8 src; /* the register that is stored in the slot */ @@ -654,6 +688,13 @@ static inline bool bpf_trip_is_saved(const struct loop_stack_entry *entry) /* state of the program: * type of all registers and stack info */ +/* What an argument register had when a frame was called. All 0 if it is no such pointer. */ +struct bpf_pad_arg { + s16 off; + u8 frame; + bool stack; /* a pointer to the stack of 'frame', at the constant offset 'off' */ +}; + struct bpf_func_state { struct bpf_reg_state regs[MAX_BPF_REG]; /* equal in states that have a join, see bpf_same_join_key() */ @@ -691,6 +732,18 @@ struct bpf_func_state { bool in_rcu_callback_fn; /* parent_id of the args that are gone when this frame returns */ u32 frame_id; + /* + * Bit f: the frame, or a subprog that returned to it, released a lock or irq flags of + * frame f. If the frame returns, a store after its call clears the hidden slot. If not, + * the function of frame f gets no hidden slots for locks, see took_lock_of_caller(). + */ + u32 took_locks; + /* + * What R1-R5 had at the call of the frame, see bpf_pad_note_args(). irq_arg() finds here + * which argument had the address of irq flags that the frame returns with. The registers + * are dead by then and would not be compared, so this is in the join key. + */ + struct bpf_pad_arg call_args[MAX_BPF_FUNC_REG_ARGS]; ); bool no_stack_arg_load; /* @@ -1366,6 +1419,11 @@ struct bpf_subprog_info { u16 stack_extra; u16 arena_stack_depth; /* size of the frame in the stack in arena */ u32 cycle; /* the first subprog of its cycle of the call graph, 0 if it is on none */ + /* + * The pending word: the address of irq flags waits there over the call of a subprog that + * saves them, see bpf_pad_note_return(). An offset from R10, 0: none. + */ + s16 pend_off; u32 insns_total; u32 insns_self; /* offsets in range [stack_depth .. fastcall_stack_off) @@ -1435,7 +1493,7 @@ struct bpf_subprog_info { u8 no_slots: 4; /* the groups that it has hidden slots for. Each bpf_pads_settle() decides again. */ u8 slots: 4; - /* its hidden slots take that many words of 8 bytes below its stack */ + /* its hidden slots and words take that many words of 8 bytes below its stack */ u8 hidden_words; u8 code_stack_args; u16 stack_arg_cnt; /* incoming + max outgoing */ @@ -1452,7 +1510,7 @@ static inline bool bpf_subprog_unwinds(const struct bpf_subprog_info *sub) return sub->can_unwind || (sub->recursive && sub->bail_unwinds); } -/* The stack depth of @sub with its hidden slots */ +/* The stack depth of @sub with its hidden slots and words */ static inline u32 bpf_stack_depth_with_slots(const struct bpf_subprog_info *sub) { return round_up(sub->stack_depth, BPF_REG_SIZE) + sub->hidden_words * BPF_REG_SIZE; @@ -2129,13 +2187,15 @@ int bpf_pad_note_callee(struct bpf_verifier_env *env, int call_idx, int callee); void bpf_pad_no_slots(struct bpf_verifier_env *env, enum bpf_pad_group group); void bpf_pad_note_acquire(struct bpf_verifier_env *env, const struct bpf_reg_state *reg); void bpf_pad_note_release(struct bpf_verifier_env *env, int idx); +void bpf_pad_note_args(struct bpf_func_state *callee, const struct bpf_func_state *caller); +size_t bpf_pad_note_free(struct bpf_pad_note *note); void bpf_pad_note_section(struct bpf_verifier_env *env, enum bpf_pad_class class, bool enter); void bpf_pad_note_return(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, const struct bpf_func_state *caller, int call_idx, bool plain); -void bpf_pad_plans_free(struct bpf_insn_aux_data *aux); +size_t bpf_pad_plans_free(struct bpf_insn_aux_data *aux); void bpf_pads_new_round(struct bpf_verifier_env *env); void bpf_pads_move(struct bpf_verifier_env *env, struct bpf_pads_moved *moved, bool save); -void bpf_pads_free_moved(struct bpf_pads_moved *moved, u32 cnt); +size_t bpf_pads_free_moved(struct bpf_pads_moved *moved, u32 cnt); struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, int insn_idx, enum bpf_site_kind kind); @@ -2152,8 +2212,8 @@ int bpf_pad_slot_notes(struct bpf_verifier_env *env, int s, const struct bpf_pad int bpf_pad_entry_insns(const struct bpf_pad_note **notes, int nr, struct bpf_insn *buf); int bpf_pad_insns_before(const struct bpf_subprog_info *sub, const struct bpf_pad_note **notes, int nr, const struct bpf_pad_note *note, struct bpf_insn *buf); -int bpf_pad_insns_after(const struct bpf_subprog_info *sub, const struct bpf_pad_note *note, - struct bpf_insn *buf); +int bpf_pad_insns_after(const struct bpf_subprog_info *sub, const struct bpf_pad_note **notes, + int nr, const struct bpf_pad_note *note, struct bpf_insn *buf); void bpf_pads_log(struct bpf_verifier_env *env); int bpf_copy_verifier_state(struct bpf_verifier_env *env, struct bpf_verifier_state *dst_state, diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index 46ff571ef76e..b3a7f739bde1 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -808,7 +808,7 @@ static int insert_slot_stores(struct bpf_verifier_env *env, struct bpf_insn *buf /* the stores after a call follow the program's unwind_goto */ call = i - 1 - (i > sub->start && bpf_is_unwind_goto(insn - 1)); if (call >= (int)sub->start && !bpf_is_unwind_goto(insn)) - n += bpf_pad_insns_after(sub, aux[call].pad_note, &buf[n]); + n += bpf_pad_insns_after(sub, notes, nr, aux[call].pad_note, &buf[n]); err = n ? bpf_patch_list_append(env, i - 1, buf, n) : 0; if (err) return err; @@ -1061,8 +1061,8 @@ void bpf_clear_insn_aux_data(struct bpf_verifier_env *env, int start, int len) free_loop(aux_data[i].callback_loop); free_loop(aux_data[i].recursion_loop); aux_data[i].loop = aux_data[i].callback_loop = aux_data[i].recursion_loop = NULL; - bpf_pad_plans_free(&aux_data[i]); - kfree(aux_data[i].pad_note); + env->mem -= bpf_pad_plans_free(&aux_data[i]); + env->mem -= bpf_pad_note_free(aux_data[i].pad_note); aux_data[i].pad_note = NULL; } } diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index e8632ba11e0b..01c5c23f8c5c 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -101,6 +101,10 @@ * * A frame can be in fewer sections than it was called in. Its pad enters the sections that the * frame left, so that the caller is in the sections of its call wherever the callee is cancelled. + * + * A subprog can acquire or release for its caller, as lock() and unlock() functions do. Where the + * caller has hidden slots, the call stands for what the subprog does, like the call of a kfunc. The + * stores are in the caller, after the call, see bpf_pad_note_return(). */ #include #include @@ -340,6 +344,19 @@ struct bpf_pad_plan *bpf_pad_plan(const struct bpf_insn_aux_data *aux, enum bpf_ return NULL; } +/* Charge the memory of @plan to env->mem. Call it again when @plan has grown. */ +static void charge_plan(struct bpf_verifier_env *env, struct bpf_pad_plan *plan) +{ + u32 bytes = sizeof(*plan); + + if (plan->ents) + bytes += BPF_PAD_MAX_ENTS * sizeof(*plan->ents); + if (plan->nr_callees) + bytes += roundup_pow_of_two(plan->nr_callees) * sizeof(*plan->callee); + env->mem += bytes - (s64)plan->bytes; + plan->bytes = bytes; +} + /* @callee came back to the call of @plan: it takes its place in the list */ static int plan_add_callee(struct bpf_verifier_env *env, struct bpf_pad_plan *plan, u32 callee) { @@ -366,6 +383,7 @@ static int plan_add_callee(struct bpf_verifier_env *env, struct bpf_pad_plan *pl memmove(&plan->callee[lo + 1], &plan->callee[lo], (n - lo) * sizeof(*list)); plan->callee[lo] = callee; plan->nr_callees++; + charge_plan(env, plan); return 0; } @@ -386,6 +404,7 @@ struct bpf_pad_plan *bpf_pad_plan_get(struct bpf_verifier_env *env, int insn_idx bitmap_fill(plan->nulls.slots, MAX_BPF_STACK_SLOTS); plan->next = aux->pad_plans; aux->pad_plans = plan; + charge_plan(env, plan); return plan; } @@ -399,22 +418,43 @@ int bpf_pad_note_callee(struct bpf_verifier_env *env, int call_idx, int callee) return plan_add_callee(env, plan, callee); } -static void free_plans(struct bpf_pad_plan *plan) +/* Free a list of records. Returns the bytes that they had charged to env->mem. */ +static size_t free_plans(struct bpf_pad_plan *plan) { struct bpf_pad_plan *next; + size_t bytes = 0; for (; plan; plan = next) { next = plan->next; + bytes += plan->bytes; kfree(plan->ents); kfree(plan->callee); kfree(plan); } + return bytes; } -void bpf_pad_plans_free(struct bpf_insn_aux_data *aux) +/* Free the records of an insn. Returns the bytes that they had charged to env->mem. */ +size_t bpf_pad_plans_free(struct bpf_insn_aux_data *aux) { - free_plans(aux->pad_plans); + size_t bytes = free_plans(aux->pad_plans); + aux->pad_plans = NULL; + return bytes; +} + +/* Free the notes of an insn. Returns the bytes that they had charged to env->mem. */ +size_t bpf_pad_note_free(struct bpf_pad_note *note) +{ + struct bpf_pad_note *next; + size_t bytes = 0; + + for (; note; note = next) { + next = note->next; + bytes += sizeof(*note); + kfree(note); + } + return bytes; } /* A pass or a path-sensitive run begins */ @@ -425,8 +465,8 @@ void bpf_pads_new_round(struct bpf_verifier_env *env) env->pads_late = false; for (i = 0; i < env->prog->len; i++, aux++) { - bpf_pad_plans_free(aux); - kfree(aux->pad_note); + env->mem -= bpf_pad_plans_free(aux); + env->mem -= bpf_pad_note_free(aux->pad_note); aux->pad_note = NULL; } } @@ -443,15 +483,15 @@ void bpf_pads_move(struct bpf_verifier_env *env, struct bpf_pads_moved *moved, b for (i = 0; i < env->prog->len; i++, aux++, moved++) { if (save) { - free_plans(moved->plans); - kfree(moved->note); + env->mem -= free_plans(moved->plans); + env->mem -= bpf_pad_note_free(moved->note); moved->plans = aux->pad_plans; moved->note = aux->pad_note; aux->pad_plans = NULL; aux->pad_note = NULL; } else { - free_plans(aux->pad_plans); - kfree(aux->pad_note); + env->mem -= free_plans(aux->pad_plans); + env->mem -= bpf_pad_note_free(aux->pad_note); aux->pad_plans = moved->plans; aux->pad_note = moved->note; moved->plans = NULL; @@ -460,15 +500,17 @@ void bpf_pads_move(struct bpf_verifier_env *env, struct bpf_pads_moved *moved, b } } -/* The snapshot that has @moved of @cnt insns is freed */ -void bpf_pads_free_moved(struct bpf_pads_moved *moved, u32 cnt) +/* Free what a snapshot keeps in @moved for @cnt insns. Returns the bytes charged to env->mem. */ +size_t bpf_pads_free_moved(struct bpf_pads_moved *moved, u32 cnt) { + size_t bytes = 0; u32 i; for (i = 0; i < cnt; i++) { - free_plans(moved[i].plans); - kfree(moved[i].note); + bytes += free_plans(moved[i].plans); + bytes += bpf_pad_note_free(moved[i].note); } + return bytes; } /* @@ -970,6 +1012,27 @@ static int ref_ent(struct bpf_verifier_env *env, struct bpf_pad_ent *ent, return err; } +/* + * Is @ref irq flags in the stack of a caller of the current frame of @st? The function got their + * address in an argument and saved the flags there, like a function that takes a lock for its + * caller. The compiler keeps the argument no longer than the function reads it, so a later site has + * no location for the address, and a hidden slot has to provide it, see bpf_pad_note_acquire(). The + * flags are still there when the pad runs: states are merged only if the same stack slots hold irq + * flags. + */ +static bool irq_in_caller(const struct bpf_verifier_state *st, const struct bpf_reference_state *ref) +{ + int f, i; + + if (ref->type != REF_TYPE_IRQ) + return false; + for (f = st->curframe - 1; f >= 0; f--) + for (i = 0; i < bpf_stack_nr_slots(st->frame[f]); i++) + if (slot_is_obj(st->frame[f], i, ref)) + return true; + return false; +} + /* Returns the release of @plan that refs[@ref] of the current state was matched with, or NULL */ static struct bpf_pad_ent *held_ent(const struct bpf_pad_plan *plan, int ref) { @@ -1169,6 +1232,11 @@ static int visit_refs(struct bpf_verifier_env *env, struct bpf_pad_plan *plan, plan->no_place_atomic |= !pad_can_sleep(st, false); continue; } + /* the same for irq flags that a caller has, see irq_in_caller() */ + if (err == BPF_CONFLICT_NO_PLACE && irq_in_caller(st, ref)) { + plan->no_irq_place = err; + continue; + } if (err) return err; ent = match_ent(plan, &new, !pass); @@ -1216,6 +1284,29 @@ static int visit_refs(struct bpf_verifier_env *env, struct bpf_pad_plan *plan, return 0; } +/* @sub gets no hidden slots for @groups, for what @st did */ +static void sub_no_slots(struct bpf_subprog_info *sub, const struct bpf_verifier_state *st, + u8 groups) +{ + sub->no_slots |= groups; +} + +/* + * A frame of @st has released locks of the frames in @took and leaves without a return to the + * stores after its call, see took_lock_of_caller(). The hidden slots of those frames would keep the + * addresses, so their functions get no hidden slots for locks. + */ +static void took_locks_no_slots(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, + u32 took) +{ + int f; + + for (f = 0; took; f++, took >>= 1) + if (took & 1) + sub_no_slots(&env->subprog_info[st->frame[f]->subprogno], st, + BIT(BPF_PAD_GROUP_LOCKS)); +} + /* * The current frame of @st is about to be cancelled at the site of @kind at @insn_idx. Add what it * holds to the record of the site, also if it holds nothing. Returns the record, whose 'conflict' @@ -1240,9 +1331,12 @@ struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, plan = bpf_pad_plan_get(env, insn_idx, kind); if (!plan) return ERR_PTR(-ENOMEM); + /* the frame is cancelled before it returns to the stores after its call */ + took_locks_no_slots(env, st, st->frame[st->curframe]->took_locks); if (plan->conflict) return plan; err = visit_refs(env, plan, st, insn_idx); + charge_plan(env, plan); if (err < 0) return ERR_PTR(err); plan->conflict = err; @@ -1272,6 +1366,12 @@ struct bpf_pad_plan *bpf_pad_visit(struct bpf_verifier_env *env, return plan; } +static bool same_release(const struct bpf_pad_ent *a, const struct bpf_pad_ent *b) +{ + return a->class == b->class && a->rcu == b->rcu && a->obj_type == b->obj_type && + !memcmp(&a->release, &b->release, sizeof(a->release)); +} + static int class_group(int class) { if (class < BPF_PAD_NR_SECTIONS) @@ -1294,11 +1394,49 @@ static struct bpf_pad_note *pad_note_get(struct bpf_verifier_env *env, int insn_ { struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; - if (!aux->pad_note) + if (!aux->pad_note) { aux->pad_note = kzalloc_obj(*aux->pad_note, BPF_VERIFIER_GFP); + if (aux->pad_note) + env->mem += sizeof(*aux->pad_note); + } return aux->pad_note; } +/* + * Returns the note of the insn at @insn_idx for what it acquires of @group, or NULL if there is no + * memory. An insn has one note. With @chain, for the call of a subprog, it has one per group: the + * subprog can return in a section and with irq flags, and each has slots of its own. + */ +static struct bpf_pad_note *acquire_note_get(struct bpf_verifier_env *env, int insn_idx, int group, + bool chain) +{ + struct bpf_pad_note *head = pad_note_get(env, insn_idx), *note; + + if (!head || !chain) + return head; + for (note = head; note; note = note->next) + if (!note->acquires || class_group(note->ent.class) == group) + return note; + note = kzalloc_obj(*note, BPF_VERIFIER_GFP); + if (!note) + return NULL; + env->mem += sizeof(*note); + note->next = head->next; + head->next = note; + return note; +} + +/* Does the insn of @aux acquire something of @group, itself or through its callee? */ +static bool insn_acquires(const struct bpf_insn_aux_data *aux, int group) +{ + const struct bpf_pad_note *note; + + for (note = aux->pad_note; note; note = note->next) + if (note->acquires && class_group(note->ent.class) == group) + return true; + return false; +} + /* * The current insn changes a resource of @group that a caller of its frame holds, and no store in * the caller can follow that. Another frame may hold the same object as well. So no function on the @@ -1313,6 +1451,43 @@ void bpf_pad_no_slots(struct bpf_verifier_env *env, enum bpf_pad_group group) env->subprog_info[st->frame[f]->subprogno].no_slots |= BIT(group); } +/* + * The current insn leaves a section of @class that a caller of its frame entered. From here on the + * function is in one section less than it was called in, which no hidden slot can say, so it gets + * none for the class. Its callers keep theirs: if the frame is cancelled, its pad enters the + * section again, and if it returns, the call stands for the release, see bpf_pad_note_return(). + */ +static void left_of_caller(struct bpf_verifier_env *env, enum bpf_pad_class class) +{ + const struct bpf_verifier_state *st = env->cur_state; + + sub_no_slots(&env->subprog_info[st->frame[st->curframe]->subprogno], st, BIT(class)); +} + +/* + * The current insn releases refs[@idx], a lock or irq flags that a caller of its frame acquired. A + * hidden slot of that caller may hold the address, and the store that clears it is in the caller, + * after the call. So every frame between the two is marked ('took_locks'), and + * bpf_pad_note_return() looks at the mark of the frame that returns to the caller. If a marked + * frame is cancelled instead, the slot is not cleared, see bpf_pad_visit(). + */ +static void took_lock_of_caller(struct bpf_verifier_env *env, int idx) +{ + struct bpf_verifier_state *st = env->cur_state; + int f, j; + + for (f = st->curframe - 1; f > 0 && st->frame[f]->entry_refs > idx; f--) + ; + /* + * The function is verified separately, and no frame of this state holds the lock. The + * frames are marked when the call is verified again in the frames of its caller. + */ + if (f < 0 || st->frame[f]->entry_refs > idx) + return; + for (j = f + 1; j <= st->curframe; j++) + st->frame[j]->took_locks |= BIT(f); +} + /* * Fill @ent with the release of @ref, one of refs[] of @st, through a hidden slot. The slot gets * what is in @reg: the pointer that the release takes, plus a constant. @by_type: no insn of the @@ -1356,10 +1531,10 @@ static bool slot_ent(struct bpf_verifier_env *env, struct bpf_pad_ent *ent, * frame can hold two of them at once: slots do not record the order of release. */ static void note_acquire(struct bpf_verifier_env *env, struct bpf_subprog_info *sub, int insn_idx, - const struct bpf_pad_ent *ent, int src, int held) + const struct bpf_pad_ent *ent, int src, int held, bool chain, bool by_callee) { - struct bpf_pad_note *note = pad_note_get(env, insn_idx); int group = class_group(ent->class); + struct bpf_pad_note *note = acquire_note_get(env, insn_idx, group, chain); if (!note) { sub->no_slots |= BIT(group); @@ -1367,11 +1542,13 @@ static void note_acquire(struct bpf_verifier_env *env, struct bpf_subprog_info * } if (!note->acquires) { note->acquires = true; + note->by_callee = by_callee; note->src = src; note->ent = *ent; note->ent.origin = insn_idx; } - if (note->src != src || note->ent.class != ent->class || note->ent.rcu != ent->rcu || + if (note->src != src || note->by_callee != by_callee || + note->ent.class != ent->class || note->ent.rcu != ent->rcu || memcmp(¬e->ent.release, &ent->release, sizeof(ent->release)) || note->ent.obj_type != ent->obj_type || note->ent.cand[0][0].delta != ent->cand[0][0].delta || @@ -1406,7 +1583,8 @@ void bpf_pad_note_acquire(struct bpf_verifier_env *env, const struct bpf_reg_sta sub->no_slots |= BIT(group); else note_acquire(env, sub, env->insn_idx, &ent, reg - frame->regs, - count_refs(st, frame->entry_refs, st->acquired_refs - 1, group)); + count_refs(st, frame->entry_refs, st->acquired_refs - 1, group), + false, false); } /* If @ref is an object in the stack: its first slot, with its address in *@addr. Else NULL. */ @@ -1467,7 +1645,10 @@ void bpf_pad_note_release(struct bpf_verifier_env *env, int idx) if (env->pad_releases) return; if (idx < frame->entry_refs) { - bpf_pad_no_slots(env, group); + if (group == BPF_PAD_GROUP_LOCKS) + took_lock_of_caller(env, idx); + else + bpf_pad_no_slots(env, group); return; } obj = find_obj(st, ref, &addr); @@ -1498,49 +1679,241 @@ void bpf_pad_note_section(struct bpf_verifier_env *env, enum bpf_pad_class class }; if (enter) - note_acquire(env, sub, env->insn_idx, &ent, 0, max(frame_sections(st, class), 0)); + note_acquire(env, sub, env->insn_idx, &ent, 0, max(frame_sections(st, class), 0), + false, false); else if (frame_sections(st, class) > 0) note_release(env, sub, class, 0, 0); else - bpf_pad_no_slots(env, class_group(class)); + left_of_caller(env, class); +} + +/* + * Record in @callee what R1-R5 of @caller hold at the call, as far as bpf_pad_note_return() needs + * it: a pointer to the stack at a constant offset can be the address of irq flags that the callee + * saves for its caller. The verifier compares this before it merges two states of the callee: the + * callee stops reading an argument long before it returns, and a state that had something else in + * the register must not stand for one that had the address. @caller is NULL for a function that is + * verified separately. + */ +void bpf_pad_note_args(struct bpf_func_state *callee, const struct bpf_func_state *caller) +{ + const struct bpf_reg_state *reg; + struct bpf_pad_arg *arg; + int i; + + memset(callee->call_args, 0, sizeof(callee->call_args)); + for (i = 0; caller && i < MAX_BPF_FUNC_REG_ARGS; i++) { + reg = &caller->regs[BPF_REG_1 + i]; + arg = &callee->call_args[i]; + if (reg->type != PTR_TO_STACK || !tnum_is_const(reg->var_off) || + (s64)reg->var_off.value != (s16)reg->var_off.value) + continue; + arg->stack = true; + arg->frame = reg->frameno; + arg->off = reg->var_off.value; + } +} + +/* What is known about the lower half of R0 at a return: BPF_RET_ZERO, NONZERO or UNKNOWN */ +static u8 ret_class(const struct bpf_reg_state *r0) +{ + if (r0->type != SCALAR_VALUE) + return BPF_RET_UNKNOWN; + if (tnum_subreg_is_const(r0->var_off) && !tnum_subreg(r0->var_off).value) + return BPF_RET_ZERO; + if (reg_u32_min(r0) > 0 || reg_s32_max(r0) < 0 || reg_s32_min(r0) > 0) + return BPF_RET_NONZERO; + return BPF_RET_UNKNOWN; +} + +/* The release of a section of @class through a hidden slot, without the offset of the slot */ +static void section_ent(struct bpf_pad_ent *ent, enum bpf_pad_class class) +{ + *ent = (struct bpf_pad_ent){ + .ref = -1, + .class = class, + .release = release_call(class), + .nr_cands[0] = 1, + .cand[0][0] = { .loc = BPF_PLACE_SPILL, .null = true }, + }; +} + +/* + * The frame @callee of @st returns with the irq flags @ref, which it saved for its caller. Returns + * the argument register that had their address at the call, with what it had beyond the address in + * *@delta, or -1. An argument that had the address itself is preferred to one that pointed + * elsewhere into the same frame, like the address of a guard that contains the flags. Every state + * that this one stands for had the same in that register, see bpf_pad_note_args(). + */ +static int irq_arg(const struct bpf_verifier_state *st, const struct bpf_func_state *callee, + const struct bpf_reference_state *ref, int *delta) +{ + const struct bpf_stack_state *obj; + const struct bpf_pad_arg *arg; + struct bpf_reg_state addr; + int k, pass; + + if (ref->type != REF_TYPE_IRQ) + return -1; + obj = find_obj(st, ref, &addr); + if (!obj || obj->spilled_ptr.irq.kfunc_class != IRQ_NATIVE_KFUNC || + addr.frameno >= callee->frameno) + return -1; + for (pass = 0; pass < 2; pass++) { + for (k = 0; k < MAX_BPF_FUNC_REG_ARGS; k++) { + arg = &callee->call_args[k]; + if (!arg->stack || arg->frame != addr.frameno) + continue; + *delta = arg->off - (int)addr.var_off.value; + if (*delta != (s16)*delta || (!pass && *delta)) + continue; + return BPF_REG_1 + k; + } + } + return -1; } /* - * The current frame of @st returns to the call at @call_idx in @caller, with what it acquired and - * did not release. @plain: with exit, and it is no callback. - * One pointer that is in R0 is what a kfunc returns: the call acquires it. What releases it follows - * from its type: states with the same pointer from different insns are the same to the main pass. - * The store after the call takes R0 as it is, so it has to be 0 when nothing comes with it. - * Nothing else has a store: it goes past the store if the frame unwinds. + * The current frame of @st returns to the call at @call_idx in @caller with what it acquired and + * did not release. @plain: it returns with exit and is no callback. For the hidden slots of the + * caller the call stands for what the subprog does, like the call of a kfunc. The stores are in the + * caller, after the call, where only a normal return gets. Here lock(l, &flags) returns 0 with irq + * flags saved and preemption disabled, or -EBUSY: + * + * *(u64 *)(r10 - 32) = r2 r2 is &flags: the pending word + * call lock + * if w0 != 0 goto +1 + * *(u64 *)(r10 - 16) = 1 the slot of the section + * if w0 != 0 goto +2 + * r1 = *(u64 *)(r10 - 32) + * *(u64 *)(r10 - 24) = r1 the slot of the irq flags + * + * A slot is filled if the subprog returns with one pointer in R0, in one section more, or with irq + * flags that it saved where an argument of the call points, see irq_arg(). A slot is cleared if the + * subprog has left a section of the frame, or has released the lock or the irq flags of the frame, + * or a pointer of the frame that an argument register of the call had, see took_ref_of_caller(). An + * argument register is dead after the call, so what it had waits over the call in a hidden word of + * the function, the PENDING WORD. + * + * What a call can stand for: + * - One pointer that is in R0 is what a kfunc returns. What releases it follows from its type: + * states with the same pointer from different insns are the same to the main pass. The store + * takes R0 as it is, so it has to be 0 when nothing comes with it. + * - One section more of a class. + * - Irq flags that the subprog saved where an argument of the call points, see irq_arg(). The + * argument is gone behind the call: it waits in a hidden word of the function, the PENDING + * WORD, which the store behind the call reads. + * - One section less of a class, of those that the frame itself is in. The first slot of the + * class that is not 0 is cleared. + * - The lock or the irq flags that the frame holds are released: a frame with slots for them + * holds one at a time, see note_acquire(), so all of those slots are cleared. + * A subprog that returns with something on some returns only is like bpf_res_spin_lock(): R0 has + * to tell, 0 or not 0 in its lower half on all returns of the one kind and the opposite on all + * others. Each return is noted, and subprog_take_slots() decides when all have been here. One + * that has released on some returns only cannot be told: no slots for the group. + * Anything else that comes back has no store either, and the function gets no slots for its + * group: two pointers, an iterator, a lock, a section that the frame is not in, more than one + * section. */ void bpf_pad_note_return(struct bpf_verifier_env *env, const struct bpf_verifier_state *st, const struct bpf_func_state *caller, int call_idx, bool plain) { const struct bpf_func_state *callee = st->frame[st->curframe]; - const struct bpf_reference_state *ref = &st->refs[callee->entry_refs]; + const struct bpf_reference_state *ref = &st->refs[callee->entry_refs], *lock = NULL, *ptr = NULL; struct bpf_subprog_info *sub = &env->subprog_info[caller->subprogno]; struct bpf_insn_aux_data *aux = &env->insn_aux_data[call_idx]; const struct bpf_reg_state *r0 = &callee->regs[BPF_REG_0]; - int i, nr = st->acquired_refs - callee->entry_refs, in_r0 = 0; + int i, d, before, delta, arg, nr = st->acquired_refs - callee->entry_refs, in_r0 = 0; + int nr_locks = 0, nr_refs = 0; + u8 brings = 0, takes = 0, keeps = 0, cls; + struct bpf_pad_note *head; struct bpf_pad_place place; struct bpf_pad_ent ent; - for (i = 0; i < BPF_PAD_NR_SECTIONS; i++) - if (frame_sections(st, i)) - sub->no_slots |= BIT(i); - if (plain && nr == 1 && ref->type == REF_TYPE_PTR && !ref->parent_id) - add_ref_cand(&place, &in_r0, r0, ref, BPF_PLACE_REG, BPF_REG_0); - if (in_r0 && slot_ent(env, &ent, st, ref, r0, true) && + head = plain ? pad_note_get(env, call_idx) : NULL; + /* + * No store follows this return: the frame is a callback, which returns to its helper, it + * left with unwind_exit, or there is no memory for the note. The caller gets no slots for + * what the frame returns with, nor for a lock that the frame has released for it. + */ + if (!head) { + for (i = 0; i < BPF_PAD_NR_SECTIONS; i++) + if (frame_sections(st, i)) + sub_no_slots(sub, st, BIT(i)); + for (i = 0; i < nr; i++) + sub_no_slots(sub, st, BIT(class_group(ref_class(&ref[i])))); + took_locks_no_slots(env, st, callee->took_locks & BIT(caller->frameno)); + goto out; + } + for (i = 0; i < nr; i++) { + if (class_group(ref_class(&ref[i])) == BPF_PAD_GROUP_LOCKS) { + lock = &ref[i]; + nr_locks++; + } else { + ptr = &ref[i]; + nr_refs++; + } + } + + for (i = 0; i < BPF_PAD_NR_SECTIONS; i++) { + /* the change of the number of sections, and how many the caller had entered */ + d = frame_sections(st, i); + before = i == BPF_PAD_RCU ? callee->call_rcu_locks - caller->call_rcu_locks : + callee->call_preempt_locks - caller->call_preempt_locks; + if (d == -1 && before > 0) { + takes |= BIT(i); + continue; + } + keeps |= BIT(i); + if (d == 1) { + brings |= BIT(i); + section_ent(&ent, i); + note_acquire(env, sub, call_idx, &ent, 0, max(before, 0), true, true); + } else if (d) { + sub_no_slots(sub, st, BIT(i)); + } + } + + arg = nr_locks == 1 ? irq_arg(st, callee, lock, &delta) : -1; + if (arg >= 0) { + brings |= BIT(BPF_PAD_GROUP_LOCKS); + section_ent(&ent, BPF_PAD_IRQ); + ent.cand[0][0].delta = delta; + note_acquire(env, sub, call_idx, &ent, arg, + count_refs(st, caller->entry_refs, callee->entry_refs, + BPF_PAD_GROUP_LOCKS), true, true); + } else if (nr_locks) { + sub_no_slots(sub, st, BIT(BPF_PAD_GROUP_LOCKS)); + } + if (callee->took_locks & BIT(caller->frameno)) + takes |= BIT(BPF_PAD_GROUP_LOCKS); + else + keeps |= BIT(BPF_PAD_GROUP_LOCKS); + + if (nr_refs == 1 && ptr->type == REF_TYPE_PTR && !ptr->parent_id) + add_ref_cand(&place, &in_r0, r0, ptr, BPF_PLACE_REG, BPF_REG_0); + if (in_r0 && slot_ent(env, &ent, st, ptr, r0, true) && can_release_under_lock(env, st, caller, &ent)) { note_acquire(env, sub, call_idx, &ent, BPF_REG_0, count_refs(st, caller->entry_refs, callee->entry_refs, - BPF_PAD_GROUP_REFS)); + BPF_PAD_GROUP_REFS), true, false); } else { - for (i = 0; i < nr; i++) - sub->no_slots |= BIT(class_group(ref_class(&ref[i]))); - aux->call_brings_nothing |= plain && !reg_is_null(r0); + if (nr_refs) + sub->no_slots |= BIT(BPF_PAD_GROUP_REFS); + aux->call_brings_nothing |= !reg_is_null(r0); } - if (aux->call_brings_nothing && aux->pad_note && aux->pad_note->acquires) + + cls = ret_class(r0); + for (i = 0; i < BPF_PAD_NR_GROUPS; i++) { + if (brings & BIT(i)) + head->hold_r0[i] |= cls; + else + head->nohold_r0[i] |= cls; + } + head->takes |= takes; + head->keeps |= keeps; +out: + if (aux->call_brings_nothing && insn_acquires(aux, BPF_PAD_GROUP_REFS)) sub->no_slots |= BIT(BPF_PAD_GROUP_REFS); } @@ -1730,6 +2103,7 @@ static int settle_plan(struct bpf_verifier_env *env, struct bpf_pad_plan *plan) if (!err && plan->sections_differ) err = BPF_CONFLICT_SECTIONS_DIFFER; err = err ?: plan->no_place; + err = err ?: plan->no_irq_place; for (i = 0; !err && i < plan->nr_ents; i++) err = settle_ent(env, plan, &plan->ents[i]); /* @@ -1760,11 +2134,10 @@ static bool acquires_stack_obj(struct bpf_verifier_env *env, const struct bpf_su const struct bpf_pad_note *note; int i; - for (i = sub->start; i < sub[1].start; i++) { - note = env->insn_aux_data[i].pad_note; - if (note && note->acquires && note->ent.class > BPF_PAD_REF) - return true; - } + for (i = sub->start; i < sub[1].start; i++) + for (note = env->insn_aux_data[i].pad_note; note; note = note->next) + if (note->acquires && note->ent.class > BPF_PAD_REF) + return true; return false; } @@ -1783,6 +2156,8 @@ static u8 plan_needs_slots(struct bpf_verifier_env *env, const struct bpf_pad_pl return 0; if (plan->no_place && all_ptrs) groups |= BIT(BPF_PAD_GROUP_REFS); + if (plan->no_irq_place) + groups |= BIT(BPF_PAD_GROUP_LOCKS); for (i = 0; i < plan->nr_ents; i++) { err = settle_ent(env, plan, &plan->ents[i]); if (err == BPF_CONFLICT_NOT_ALWAYS_HELD || err == BPF_CONFLICT_CALLER_SAVED) @@ -1826,8 +2201,10 @@ static int plan_take_slots(struct bpf_pad_plan *plan, int group, struct bpf_pad_ } if (all_ptrs) plan->no_place = 0; - if (group == BPF_PAD_GROUP_LOCKS) + if (group == BPF_PAD_GROUP_LOCKS) { + plan->no_irq_place = 0; pos = plan->nr_ents; + } while (group == BPF_PAD_GROUP_REFS && pos < plan->nr_ents && plan->ents[pos].class < BPF_PAD_NR_SECTIONS) pos++; @@ -1847,6 +2224,22 @@ static int plan_take_slots(struct bpf_pad_plan *plan, int group, struct bpf_pad_ return 0; } +/* + * Can the calls of @a and @b share a hidden slot? A frame with slots for locks holds one lock or + * one set of irq flags at a time, see note_fill(), so two calls that acquire the same kind never + * need a slot at the same time. The same holds for the sections of a class if no call enters one + * while the frame is in one. A function that takes a lock at sixteen calls has one slot for it. + */ +static bool notes_share_slot(const struct bpf_pad_note *a, const struct bpf_pad_note *b) +{ + int group = class_group(a->ent.class); + + if (group == BPF_PAD_GROUP_REFS || a->held || b->held) + return false; + return same_release(&a->ent, &b->ent) && + a->ent.cand[0][0].delta == b->ent.cand[0][0].delta; +} + /* * Give subprog @s hidden slots for all that it acquires of @group. A call outside of a loop gets * one slot. A call in a loop can run again while the frame holds what it acquired before, so it @@ -1857,26 +2250,57 @@ static int subprog_take_slots(struct bpf_verifier_env *env, int s, int group) { struct bpf_subprog_info *sub = &env->subprog_info[s]; struct bpf_insn_aux_data *aux = env->insn_aux_data; - struct bpf_pad_note *note, *notes[BPF_PAD_MAX_ENTS]; + struct bpf_pad_note *head, *note, *notes[BPF_PAD_MAX_ENTS]; bool all_ptrs = group == BPF_PAD_GROUP_REFS && !acquires_stack_obj(env, sub); - int i, j, err, left, nr = 0, slots = 0, words = 0; + int i, j, k, err, left, nr = 0, slots = 0, words = 0, pend_off = 0; struct bpf_pad_plan *plan; + u8 hold, nohold; for (i = sub->start; i < sub[1].start; i++) { - note = aux[i].pad_note; - if (!note || !note->acquires || class_group(note->ent.class) != group) - continue; - err = nr < BPF_PAD_MAX_ENTS ? get_release(env, ¬e->ent.release) : 1; - if (err) - return min(err, 0); - notes[nr++] = note; - note->nr_slots = bpf_loop_at_index(env, i) < 0 ? 1 : 1 + note->held; - note->off = -(int)bpf_stack_depth_with_slots(sub) - (words + 1) * BPF_REG_SIZE; - slots += note->nr_slots; - words += note->nr_slots * slot_words(note); + head = aux[i].pad_note; + /* a lock or section was released on some returns only: no store fits all returns */ + if (head && (head->takes & head->keeps & BIT(group))) + return 0; + for (note = head; note; note = note->next) { + if (!note->acquires || class_group(note->ent.class) != group) + continue; + err = get_release(env, ¬e->ent.release); + if (err) + return min(err, 0); + /* when does the subprog return with it? See bpf_pad_note_return(). */ + hold = head->hold_r0[group]; + nohold = head->nohold_r0[group]; + if (!note->by_callee || !nohold) + note->cond = BPF_PAD_ALWAYS; + else if (hold == BPF_RET_ZERO && nohold == BPF_RET_NONZERO) + note->cond = BPF_PAD_IF_ZERO; + else if (hold == BPF_RET_NONZERO && nohold == BPF_RET_ZERO) + note->cond = BPF_PAD_IF_NONZERO; + else + return 0; + /* the address of irq flags waits in the pending word over the call */ + if (note->by_callee && note->ent.class == BPF_PAD_IRQ) + pend_off = -1; + note->nr_slots = bpf_loop_at_index(env, i) < 0 ? 1 : 1 + note->held; + for (k = 0; k < nr && !notes_share_slot(notes[k], note); k++) + ; + if (k < nr) { + note->off = notes[k]->off; + continue; + } + if (nr == BPF_PAD_MAX_ENTS) + return 0; + notes[nr++] = note; + note->off = -(int)bpf_stack_depth_with_slots(sub) - + (words + 1) * BPF_REG_SIZE; + slots += note->nr_slots; + words += note->nr_slots * slot_words(note); + } } if (!nr) return 0; + if (pend_off) + pend_off = -(int)bpf_stack_depth_with_slots(sub) - ++words * BPF_REG_SIZE; for (i = sub->start; i < sub[1].start; i++) { for (plan = aux[i].pad_plans; plan; plan = plan->next) { for (j = 0, left = 0; j < plan->nr_ents; j++) @@ -1892,10 +2316,13 @@ static int subprog_take_slots(struct bpf_verifier_env *env, int s, int group) sub->hidden_words -= words; return err; } + if (pend_off) + sub->pend_off = pend_off; for (i = sub->start; i < sub[1].start; i++) { for (plan = aux[i].pad_plans; plan; plan = plan->next) { err = plan_has_slots(plan) ? plan_take_slots(plan, group, notes, nr, all_ptrs) : 0; + charge_plan(env, plan); if (err) return err; } @@ -1922,6 +2349,7 @@ static int settle_slots(struct bpf_verifier_env *env) for (s = 0; s < env->subprog_cnt; s++) { env->subprog_info[s].slots = 0; env->subprog_info[s].hidden_words = 0; + env->subprog_info[s].pend_off = 0; } for (s = 0; s < env->subprog_cnt; s++) { sub = &env->subprog_info[s]; @@ -1950,28 +2378,33 @@ static bool note_has_slots(const struct bpf_subprog_info *sub, const struct bpf_ } /* - * The calls of subprog @s that have hidden slots go to @notes, which has room for BPF_PAD_MAX_ENTS. - * A slot is an entry of each plan of the subprog, so all groups together have no more slots than - * that, see plan_has_room(), and the insns of the stores are sized for as many, see - * BPF_PAD_MAX_SLOT_INSNS. Nothing else keeps the calls that few: check it where they are written. - * Returns how many there are. + * Collect in @notes, which has room for BPF_PAD_MAX_ENTS, the calls of subprog @s that have hidden + * slots, one call for each slot. A slot is a release of every pad of the subprog, so there are no + * more than that, and BPF_PAD_MAX_SLOT_INSNS is sized for as many. Returns how many there are, or + * -EFAULT if there are more. */ int bpf_pad_slot_notes(struct bpf_verifier_env *env, int s, const struct bpf_pad_note **notes) { const struct bpf_subprog_info *sub = &env->subprog_info[s]; const struct bpf_pad_note *note; - int i, nr = 0, slots = 0; + int i, k, nr = 0, slots = 0; for (i = sub->start; i < sub[1].start; i++) { - note = env->insn_aux_data[i].pad_note; - if (!note_has_slots(sub, note)) - continue; - slots += note->nr_slots; - if (verifier_bug_if(slots > BPF_PAD_MAX_ENTS, env, - "func#%d has %d hidden slots, a landing pad has %d entries", - s, slots, BPF_PAD_MAX_ENTS)) - return -EFAULT; - notes[nr++] = note; + for (note = env->insn_aux_data[i].pad_note; note; note = note->next) { + if (!note_has_slots(sub, note)) + continue; + /* only the first of the calls that share a slot, see notes_share_slot() */ + for (k = 0; k < nr && notes[k]->off != note->off; k++) + ; + if (k < nr) + continue; + slots += note->nr_slots; + if (verifier_bug_if(slots > BPF_PAD_MAX_ENTS, env, + "func#%d has %d hidden slots, a landing pad has %d entries", + s, slots, BPF_PAD_MAX_ENTS)) + return -EFAULT; + notes[nr++] = note; + } } return nr; } @@ -1992,12 +2425,12 @@ int bpf_pad_entry_insns(const struct bpf_pad_note **notes, int nr, struct bpf_in } /* - * The call of @note acquires: a slot takes what the release takes, unconditionally. 0 in R0 means - * that nothing was acquired, and 0 in a slot means the same. A section has no handle: 1. - * If the call has several slots the first one that is 0 is filled. The frame holds no more than - * 'held' of the group, so there is one. + * Emit the stores that fill a hidden slot of the call of @note with R@src, the pointer that the + * release takes. The slot of a section takes 1. The store has no condition: 0 in R0 means that + * nothing was acquired, and 0 in a slot means the same. If the call has several slots, the first + * one that is 0 is filled. One is free, because the frame holds no more than 'held' of the group. */ -static int set_insns(const struct bpf_pad_note *note, struct bpf_insn *buf) +static int set_insns(const struct bpf_pad_note *note, int src, struct bpf_insn *buf) { int k, n = 0, nr = note->nr_slots, off = note->off; @@ -2009,13 +2442,13 @@ static int set_insns(const struct bpf_pad_note *note, struct bpf_insn *buf) if (note->ent.class < BPF_PAD_NR_SECTIONS) buf[n++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, off, 1); else - buf[n++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, note->src, off); + buf[n++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, src, off); if (k < nr - 1) buf[n++] = BPF_JMP_A((nr - 1 - k) * 4 - 1); } /* the irq flags, the next argument: the pad reads the word if the slot is not 0 */ if (slot_words(note) == 2) - buf[n++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, note->src + 1, off); + buf[n++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, src + 1, off); return n; } @@ -2061,7 +2494,10 @@ static int clear_insns(const struct bpf_pad_note **notes, int nr, const struct b return n; } -/* The insns that go before the call of @note in @sub. @notes: the @nr calls with slots. */ +/* + * Emit the insns that go before the insn of @note, the first note of an insn in @sub. @notes are + * the @nr calls that have slots. Returns the number of insns. + */ int bpf_pad_insns_before(const struct bpf_subprog_info *sub, const struct bpf_pad_note **notes, int nr, const struct bpf_pad_note *note, struct bpf_insn *buf) { @@ -2069,26 +2505,74 @@ int bpf_pad_insns_before(const struct bpf_subprog_info *sub, const struct bpf_pa if (note && note->releases && (sub->slots & BIT(note->group))) n = clear_insns(notes, nr, note, buf); - /* an address that the call takes. The resource is held when the call returns. */ - if (note_has_slots(sub, note) && note->ent.class != BPF_PAD_REF) - n += set_insns(note, &buf[n]); + for (; note; note = note->next) { + if (!note_has_slots(sub, note)) + continue; + /* + * The callee saves irq flags where the argument points. The address waits in the + * pending word until the callee has returned with them, see bpf_pad_insns_after(). + */ + if (note->by_callee && note->ent.class == BPF_PAD_IRQ) + buf[n++] = BPF_STX_MEM(BPF_DW, BPF_REG_FP, note->src, sub->pend_off); + /* the call takes the address, and the resource is held when the call returns */ + else if (!note->by_callee && note->ent.class != BPF_PAD_REF) + n += set_insns(note, note->src, &buf[n]); + } return n; } -/* The insns that go after the call of @note, which is in @sub */ -int bpf_pad_insns_after(const struct bpf_subprog_info *sub, const struct bpf_pad_note *note, - struct bpf_insn *buf) +/* + * The insns that go after the insn of @note, the first note of the insn, which is in @sub. @notes: + * the @nr calls with slots. + * Behind a call of a subprog, see bpf_pad_note_return(): the slots of what the subprog has + * released are cleared, then the slots of what it returned with are filled, if R0 says that it + * returned with it. R1-R5 are dead there: R1 takes the address from the pending word. + */ +int bpf_pad_insns_after(const struct bpf_subprog_info *sub, const struct bpf_pad_note **notes, + int nr, const struct bpf_pad_note *note, struct bpf_insn *buf) { - int n = 0; + struct bpf_pad_note rel = {}; + int g, i, k, n = 0, first; + u8 takes; - if (!note_has_slots(sub, note)) + if (!note) return 0; - if (note->ent.class == BPF_PAD_REF) - return set_insns(note, buf); - /* the lock was not taken. The upper half of an int is not defined. */ - if (note->ent.class == BPF_PAD_RES_LOCK || note->ent.class == BPF_PAD_RES_LOCK_IRQ) { - buf[n++] = BPF_JMP32_IMM(BPF_JEQ, BPF_REG_0, 0, 1); - buf[n++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, note->off, 0); + takes = note->takes & sub->slots; + for (g = 0; g < BPF_PAD_NR_SECTIONS; g++) { + if (!(takes & BIT(g))) + continue; + rel.group = g; + n += clear_insns(notes, nr, &rel, &buf[n]); + } + /* the frame holds one lock or set of irq flags at a time: whichever slot had it */ + for (i = 0; (takes & BIT(BPF_PAD_GROUP_LOCKS)) && i < nr; i++) { + if (class_group(notes[i]->ent.class) != BPF_PAD_GROUP_LOCKS) + continue; + for (k = 0; k < notes[i]->nr_slots * slot_words(notes[i]); k++) + buf[n++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, + notes[i]->off - k * BPF_REG_SIZE, 0); + } + for (; note; note = note->next) { + if (!note_has_slots(sub, note)) + continue; + if (note->by_callee) { + first = n; + if (note->cond != BPF_PAD_ALWAYS) + buf[n++] = BPF_JMP32_IMM(note->cond == BPF_PAD_IF_ZERO ? + BPF_JNE : BPF_JEQ, BPF_REG_0, 0, 0); + if (note->ent.class == BPF_PAD_IRQ) + buf[n++] = BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_FP, sub->pend_off); + n += set_insns(note, BPF_REG_1, &buf[n]); + if (note->cond != BPF_PAD_ALWAYS) + buf[first].off = n - first - 1; + } else if (note->ent.class == BPF_PAD_REF) { + n += set_insns(note, note->src, &buf[n]); + } else if (note->ent.class == BPF_PAD_RES_LOCK || + note->ent.class == BPF_PAD_RES_LOCK_IRQ) { + /* the lock was not taken if the lower half of R0 is not 0 */ + buf[n++] = BPF_JMP32_IMM(BPF_JEQ, BPF_REG_0, 0, 1); + buf[n++] = BPF_ST_MEM(BPF_DW, BPF_REG_FP, note->off, 0); + } } return n; } diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 7681782ef66c..34238772cc7a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11454,6 +11454,7 @@ static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int calls callee->entry_preempt_locks = state->active_preempt_locks; callee->call_rcu_locks = state->active_rcu_locks; callee->call_preempt_locks = state->active_preempt_locks; + bpf_pad_note_args(callee, caller); err = set_callee_state_cb(env, caller, callee, callsite); if (err) goto err_out; @@ -11901,6 +11902,7 @@ static int call_func_on_its_own(struct bpf_verifier_env *env, int subprog, int i callee->entry_preempt_locks = entry.active_preempt_locks; callee->call_rcu_locks = entry.active_rcu_locks; callee->call_preempt_locks = entry.active_preempt_locks; + bpf_pad_note_args(callee, NULL); account_current_path(env); env->subprog_info[subprog].on_its_own = true; @@ -21767,14 +21769,15 @@ static bool frame_is_top(const struct bpf_verifier_state *st) } /* - * The current frame of @st holds resources that it acquired since its entry, or it is in other - * sections than it was called in: more of them, or fewer, where it has left one of a caller + * Does the current frame of @st hold what it acquired since its entry, or is it in other sections + * than it was called in? A frame that released a lock of a caller (took_locks) counts too: the + * hidden slots of the caller have to learn that, see bpf_pad_visit(). */ static bool frame_holds_resources(const struct bpf_verifier_state *st) { const struct bpf_func_state *frame = st->frame[st->curframe]; - return st->acquired_refs != frame->entry_refs || + return st->acquired_refs != frame->entry_refs || frame->took_locks || st->active_rcu_locks != frame->call_rcu_locks || st->active_preempt_locks != frame->call_preempt_locks; } @@ -24116,7 +24119,7 @@ static void free_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna { if (!snapshot) return; - bpf_pads_free_moved(snapshot->pads, env->prog->len); + env->mem -= bpf_pads_free_moved(snapshot->pads, env->prog->len); kvfree(snapshot); } -- 2.53.1-Meta