• Dave Marchevsky's avatar
    bpf: Add 'owner' field to bpf_{list,rb}_node · c3c510ce
    Dave Marchevsky authored
    As described by Kumar in [0], in shared ownership scenarios it is
    necessary to do runtime tracking of {rb,list} node ownership - and
    synchronize updates using this ownership information - in order to
    prevent races. This patch adds an 'owner' field to struct bpf_list_node
    and bpf_rb_node to implement such runtime tracking.
    
    The owner field is a void * that describes the ownership state of a
    node. It can have the following values:
    
      NULL           - the node is not owned by any data structure
      BPF_PTR_POISON - the node is in the process of being added to a data
                       structure
      ptr_to_root    - the pointee is a data structure 'root'
                       (bpf_rb_root / bpf_list_head) which owns this node
    
    The field is initially NULL (set by bpf_obj_init_field default behavior)
    and transitions states in the following sequence:
    
      Insertion: NULL -> BPF_PTR_POISON -> ptr_to_root
      Removal:   ptr_to_root -> NULL
    
    Before a node has been successfully inserted, it is not protected by any
    root's lock, and therefore two programs can attempt to add the same node
    to different roots simultaneously. For this reason the intermediate
    BPF_PTR_POISON state is necessary. For removal, the node is protected
    by some root's lock so this intermediate hop isn't necessary.
    
    Note that bpf_list_pop_{front,back} helpers don't need to check owner
    before removing as the node-to-be-removed is not passed in as input and
    is instead taken directly from the list. Do the check anyways and
    WARN_ON_ONCE in this unexpected scenario.
    
    Selftest changes in this patch are entirely mechanical: some BTF
    tests have hardcoded struct sizes for structs that contain
    bpf_{list,rb}_node fields, those were adjusted to account for the new
    sizes. Selftest additions to validate the owner field are added in a
    further patch in the series.
    
      [0]: https://lore.kernel.org/bpf/d7hyspcow5wtjcmw4fugdgyp3fwhljwuscp3xyut5qnwivyeru@ysdq543otzv2Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
    Suggested-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/r/20230718083813.3416104-4-davemarchevsky@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    c3c510ce
helpers.c 66.1 KB