Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IRQ save/restore #4714

Open
wants to merge 7 commits into
base: bpf-next_base
Choose a base branch
from

Commits on Dec 2, 2024

  1. bpf: Consolidate locks and reference state in verifier state

    Currently, state for RCU read locks and preemption is in
    bpf_verifier_state, while locks and pointer reference state remains in
    bpf_func_state. There is no particular reason to keep the latter in
    bpf_func_state. Additionally, it is copied into a new frame's state and
    copied back to the caller frame's state everytime the verifier processes
    a pseudo call instruction. This is a bit wasteful, given this state is
    global for a given verification state / path.
    
    Move all resource and reference related state in bpf_verifier_state
    structure in this patch, in preparation for introducing new reference
    state types in the future.
    
    Since we switch print_verifier_state and friends to print using vstate,
    we now need to explicitly pass in the verifier state from the caller
    along with the bpf_func_state, so modify the prototype and callers to do
    so. To ensure func state matches the verifier state when we're printing
    data, take in frame number instead of bpf_func_state pointer instead and
    avoid inconsistencies induced by the caller.
    
    Acked-by: Eduard Zingerman <[email protected]>
    Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
    kkdwivedi authored and Kernel Patches Daemon committed Dec 2, 2024
    Configuration menu
    Copy the full SHA
    2cface0 View commit details
    Browse the repository at this point in the history
  2. bpf: Refactor {acquire,release}_reference_state

    In preparation for introducing support for more reference types which
    have to add and remove reference state, refactor the
    acquire_reference_state and release_reference_state functions to share
    common logic.
    
    The acquire_reference_state function simply handles growing the acquired
    refs and returning the pointer to the new uninitialized element, which
    can be filled in by the caller.
    
    The release_reference_state function simply erases a reference state
    entry in the acquired_refs array and shrinks it. The callers are
    responsible for finding the suitable element by matching on various
    fields of the reference state and requesting deletion through this
    function. It is not supposed to be called directly.
    
    Existing callers of release_reference_state were using it to find and
    remove state for a given ref_obj_id without scrubbing the associated
    registers in the verifier state. Introduce release_reference_nomark to
    provide this functionality and convert callers. We now use this new
    release_reference_nomark function within release_reference as well.
    It needs to operate on a verifier state instead of taking verifier env
    as mark_ptr_or_null_regs requires operating on verifier state of the
    two branches of a NULL condition check, therefore env->cur_state cannot
    be used directly.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
    Acked-by: Eduard Zingerman <[email protected]>
    kkdwivedi authored and Kernel Patches Daemon committed Dec 2, 2024
    Configuration menu
    Copy the full SHA
    77799d5 View commit details
    Browse the repository at this point in the history
  3. bpf: Refactor mark_{dynptr,iter}_read

    There is possibility of sharing code between mark_dynptr_read and
    mark_iter_read for updating liveness information of their stack slots.
    Consolidate common logic into mark_stack_slot_obj_read function in
    preparation for the next patch which needs the same logic for its own
    stack slots.
    
    Acked-by: Eduard Zingerman <[email protected]>
    Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
    kkdwivedi authored and Kernel Patches Daemon committed Dec 2, 2024
    Configuration menu
    Copy the full SHA
    5da2847 View commit details
    Browse the repository at this point in the history
  4. bpf: Introduce support for bpf_local_irq_{save,restore}

    Teach the verifier about IRQ-disabled sections through the introduction
    of two new kfuncs, bpf_local_irq_save, to save IRQ state and disable
    them, and bpf_local_irq_restore, to restore IRQ state and enable them
    back again.
    
    For the purposes of tracking the saved IRQ state, the verifier is taught
    about a new special object on the stack of type STACK_IRQ_FLAG. This is
    a 8 byte value which saves the IRQ flags which are to be passed back to
    the IRQ restore kfunc.
    
    Renumber the enums for REF_TYPE_* to simplify the check in
    find_lock_state, filtering out non-lock types as they grow will become
    cumbersome and is unecessary.
    
    To track a dynamic number of IRQ-disabled regions and their associated
    saved states, a new resource type RES_TYPE_IRQ is introduced, which its
    state management functions: acquire_irq_state and release_irq_state,
    taking advantage of the refactoring and clean ups made in earlier
    commits.
    
    One notable requirement of the kernel's IRQ save and restore API is that
    they cannot happen out of order. For this purpose, when releasing reference
    we keep track of the prev_id we saw with REF_TYPE_IRQ. Since reference
    states are inserted in increasing order of the index, this is used to
    remember the ordering of acquisitions of IRQ saved states, so that we
    maintain a logical stack in acquisition order of resource identities,
    and can enforce LIFO ordering when restoring IRQ state. The top of the
    stack is maintained using bpf_verifier_state's active_irq_id.
    
    To maintain the stack property when releasing reference states, we need
    to modify release_reference_state to instead shift the remaining array
    left using memmove instead of swapping deleted element with last that
    might break the ordering. A selftest to test this subtle behavior is
    added in late patches.
    
    The logic to detect initialized and unitialized irq flag slots, marking
    and unmarking is similar to how it's done for iterators. No additional
    checks are needed in refsafe for REF_TYPE_IRQ, apart from the usual
    check_id satisfiability check on the ref[i].id. We have to perform the
    same check_ids check on state->active_irq_id as well.
    
    The kfuncs themselves are plain wrappers over local_irq_save and
    local_irq_restore macros.
    
    Acked-by: Eduard Zingerman <[email protected]>
    Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
    kkdwivedi authored and Kernel Patches Daemon committed Dec 2, 2024
    Configuration menu
    Copy the full SHA
    e0fa882 View commit details
    Browse the repository at this point in the history
  5. bpf: Improve verifier log for resource leak on exit

    The verifier log when leaking resources on BPF_EXIT may be a bit
    confusing, as it's a problem only when finally existing from the main
    prog, not from any of the subprogs. Hence, update the verifier error
    string and the corresponding selftests matching on it.
    
    Acked-by: Eduard Zingerman <[email protected]>
    Suggested-by: Eduard Zingerman <[email protected]>
    Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
    kkdwivedi authored and Kernel Patches Daemon committed Dec 2, 2024
    Configuration menu
    Copy the full SHA
    98c4fb4 View commit details
    Browse the repository at this point in the history
  6. selftests/bpf: Expand coverage of preempt tests to sleepable kfunc

    For preemption-related kfuncs, we don't test their interaction with
    sleepable kfuncs (we do test helpers) even though the verifier has
    code to protect against such a pattern. Expand coverage of the selftest
    to include this case.
    
    Acked-by: Eduard Zingerman <[email protected]>
    Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
    kkdwivedi authored and Kernel Patches Daemon committed Dec 2, 2024
    Configuration menu
    Copy the full SHA
    6996b20 View commit details
    Browse the repository at this point in the history
  7. selftests/bpf: Add IRQ save/restore tests

    Include tests that check for rejection in erroneous cases, like
    unbalanced IRQ-disabled counts, within and across subprogs, invalid IRQ
    flag state or input to kfuncs, behavior upon overwriting IRQ saved state
    on stack, interaction with sleepable kfuncs/helpers, global functions,
    and out of order restore. Include some success scenarios as well to
    demonstrate usage.
    
    #128/1   irq/irq_save_bad_arg:OK
    #128/2   irq/irq_restore_bad_arg:OK
    #128/3   irq/irq_restore_missing_2:OK
    #128/4   irq/irq_restore_missing_3:OK
    #128/5   irq/irq_restore_missing_3_minus_2:OK
    #128/6   irq/irq_restore_missing_1_subprog:OK
    #128/7   irq/irq_restore_missing_2_subprog:OK
    #128/8   irq/irq_restore_missing_3_subprog:OK
    #128/9   irq/irq_restore_missing_3_minus_2_subprog:OK
    #128/10  irq/irq_balance:OK
    #128/11  irq/irq_balance_n:OK
    #128/12  irq/irq_balance_subprog:OK
    #128/13  irq/irq_global_subprog:OK
    #128/14  irq/irq_restore_ooo:OK
    #128/15  irq/irq_restore_ooo_3:OK
    #128/16  irq/irq_restore_3_subprog:OK
    #128/17  irq/irq_restore_4_subprog:OK
    #128/18  irq/irq_restore_ooo_3_subprog:OK
    #128/19  irq/irq_restore_invalid:OK
    #128/20  irq/irq_save_invalid:OK
    #128/21  irq/irq_restore_iter:OK
    #128/22  irq/irq_save_iter:OK
    #128/23  irq/irq_flag_overwrite:OK
    #128/24  irq/irq_flag_overwrite_partial:OK
    #128/25  irq/irq_ooo_refs_array:OK
    #128/26  irq/irq_sleepable_helper:OK
    #128/27  irq/irq_sleepable_kfunc:OK
    #128     irq:OK
    Summary: 1/27 PASSED, 0 SKIPPED, 0 FAILED
    
    Acked-by: Eduard Zingerman <[email protected]>
    Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
    kkdwivedi authored and Kernel Patches Daemon committed Dec 2, 2024
    Configuration menu
    Copy the full SHA
    6620fb9 View commit details
    Browse the repository at this point in the history