graph_ds_impl.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. =========================
  2. BPF Graph Data Structures
  3. =========================
  4. This document describes implementation details of new-style "graph" data
  5. structures (linked_list, rbtree), with particular focus on the verifier's
  6. implementation of semantics specific to those data structures.
  7. Although no specific verifier code is referred to in this document, the document
  8. assumes that the reader has general knowledge of BPF verifier internals, BPF
  9. maps, and BPF program writing.
  10. Note that the intent of this document is to describe the current state of
  11. these graph data structures. **No guarantees** of stability for either
  12. semantics or APIs are made or implied here.
  13. .. contents::
  14. :local:
  15. :depth: 2
  16. Introduction
  17. ------------
  18. The BPF map API has historically been the main way to expose data structures
  19. of various types for use within BPF programs. Some data structures fit naturally
  20. with the map API (HASH, ARRAY), others less so. Consequently, programs
  21. interacting with the latter group of data structures can be hard to parse
  22. for kernel programmers without previous BPF experience.
  23. Luckily, some restrictions which necessitated the use of BPF map semantics are
  24. no longer relevant. With the introduction of kfuncs, kptrs, and the any-context
  25. BPF allocator, it is now possible to implement BPF data structures whose API
  26. and semantics more closely match those exposed to the rest of the kernel.
  27. Two such data structures - linked_list and rbtree - have many verification
  28. details in common. Because both have "root"s ("head" for linked_list) and
  29. "node"s, the verifier code and this document refer to common functionality
  30. as "graph_api", "graph_root", "graph_node", etc.
  31. Unless otherwise stated, examples and semantics below apply to both graph data
  32. structures.
  33. Unstable API
  34. ------------
  35. Data structures implemented using the BPF map API have historically used BPF
  36. helper functions - either standard map API helpers like ``bpf_map_update_elem``
  37. or map-specific helpers. The new-style graph data structures instead use kfuncs
  38. to define their manipulation helpers. Because there are no stability guarantees
  39. for kfuncs, the API and semantics for these data structures can be evolved in
  40. a way that breaks backwards compatibility if necessary.
  41. Root and node types for the new data structures are opaquely defined in the
  42. ``uapi/linux/bpf.h`` header.
  43. Locking
  44. -------
  45. The new-style data structures are intrusive and are defined similarly to their
  46. vanilla kernel counterparts:
  47. .. code-block:: c
  48. struct node_data {
  49. long key;
  50. long data;
  51. struct bpf_rb_node node;
  52. };
  53. struct bpf_spin_lock glock;
  54. struct bpf_rb_root groot __contains(node_data, node);
  55. The "root" type for both linked_list and rbtree expects to be in a map_value
  56. which also contains a ``bpf_spin_lock`` - in the above example both global
  57. variables are placed in a single-value arraymap. The verifier considers this
  58. spin_lock to be associated with the ``bpf_rb_root`` by virtue of both being in
  59. the same map_value and will enforce that the correct lock is held when
  60. verifying BPF programs that manipulate the tree. Since this lock checking
  61. happens at verification time, there is no runtime penalty.
  62. Non-owning references
  63. ---------------------
  64. **Motivation**
  65. Consider the following BPF code:
  66. .. code-block:: c
  67. struct node_data *n = bpf_obj_new(typeof(*n)); /* ACQUIRED */
  68. bpf_spin_lock(&lock);
  69. bpf_rbtree_add(&tree, n); /* PASSED */
  70. bpf_spin_unlock(&lock);
  71. From the verifier's perspective, the pointer ``n`` returned from ``bpf_obj_new``
  72. has type ``PTR_TO_BTF_ID | MEM_ALLOC``, with a ``btf_id`` of
  73. ``struct node_data`` and a nonzero ``ref_obj_id``. Because it holds ``n``, the
  74. program has ownership of the pointee's (object pointed to by ``n``) lifetime.
  75. The BPF program must pass off ownership before exiting - either via
  76. ``bpf_obj_drop``, which ``free``'s the object, or by adding it to ``tree`` with
  77. ``bpf_rbtree_add``.
  78. (``ACQUIRED`` and ``PASSED`` comments in the example denote statements where
  79. "ownership is acquired" and "ownership is passed", respectively)
  80. What should the verifier do with ``n`` after ownership is passed off? If the
  81. object was ``free``'d with ``bpf_obj_drop`` the answer is obvious: the verifier
  82. should reject programs which attempt to access ``n`` after ``bpf_obj_drop`` as
  83. the object is no longer valid. The underlying memory may have been reused for
  84. some other allocation, unmapped, etc.
  85. When ownership is passed to ``tree`` via ``bpf_rbtree_add`` the answer is less
  86. obvious. The verifier could enforce the same semantics as for ``bpf_obj_drop``,
  87. but that would result in programs with useful, common coding patterns being
  88. rejected, e.g.:
  89. .. code-block:: c
  90. int x;
  91. struct node_data *n = bpf_obj_new(typeof(*n)); /* ACQUIRED */
  92. bpf_spin_lock(&lock);
  93. bpf_rbtree_add(&tree, n); /* PASSED */
  94. x = n->data;
  95. n->data = 42;
  96. bpf_spin_unlock(&lock);
  97. Both the read from and write to ``n->data`` would be rejected. The verifier
  98. can do better, though, by taking advantage of two details:
  99. * Graph data structure APIs can only be used when the ``bpf_spin_lock``
  100. associated with the graph root is held
  101. * Both graph data structures have pointer stability
  102. * Because graph nodes are allocated with ``bpf_obj_new`` and
  103. adding / removing from the root involves fiddling with the
  104. ``bpf_{list,rb}_node`` field of the node struct, a graph node will
  105. remain at the same address after either operation.
  106. Because the associated ``bpf_spin_lock`` must be held by any program adding
  107. or removing, if we're in the critical section bounded by that lock, we know
  108. that no other program can add or remove until the end of the critical section.
  109. This combined with pointer stability means that, until the critical section
  110. ends, we can safely access the graph node through ``n`` even after it was used
  111. to pass ownership.
  112. The verifier considers such a reference a *non-owning reference*. The ref
  113. returned by ``bpf_obj_new`` is accordingly considered an *owning reference*.
  114. Both terms currently only have meaning in the context of graph nodes and API.
  115. **Details**
  116. Let's enumerate the properties of both types of references.
  117. *owning reference*
  118. * This reference controls the lifetime of the pointee
  119. * Ownership of pointee must be 'released' by passing it to some graph API
  120. kfunc, or via ``bpf_obj_drop``, which ``free``'s the pointee
  121. * If not released before program ends, verifier considers program invalid
  122. * Access to the pointee's memory will not page fault
  123. *non-owning reference*
  124. * This reference does not own the pointee
  125. * It cannot be used to add the graph node to a graph root, nor ``free``'d via
  126. ``bpf_obj_drop``
  127. * No explicit control of lifetime, but can infer valid lifetime based on
  128. non-owning ref existence (see explanation below)
  129. * Access to the pointee's memory will not page fault
  130. From verifier's perspective non-owning references can only exist
  131. between spin_lock and spin_unlock. Why? After spin_unlock another program
  132. can do arbitrary operations on the data structure like removing and ``free``-ing
  133. via bpf_obj_drop. A non-owning ref to some chunk of memory that was remove'd,
  134. ``free``'d, and reused via bpf_obj_new would point to an entirely different thing.
  135. Or the memory could go away.
  136. To prevent this logic violation all non-owning references are invalidated by the
  137. verifier after a critical section ends. This is necessary to ensure the "will
  138. not page fault" property of non-owning references. So if the verifier hasn't
  139. invalidated a non-owning ref, accessing it will not page fault.
  140. Currently ``bpf_obj_drop`` is not allowed in the critical section, so
  141. if there's a valid non-owning ref, we must be in a critical section, and can
  142. conclude that the ref's memory hasn't been dropped-and- ``free``'d or
  143. dropped-and-reused.
  144. Any reference to a node that is in an rbtree _must_ be non-owning, since
  145. the tree has control of the pointee's lifetime. Similarly, any ref to a node
  146. that isn't in rbtree _must_ be owning. This results in a nice property:
  147. graph API add / remove implementations don't need to check if a node
  148. has already been added (or already removed), as the ownership model
  149. allows the verifier to prevent such a state from being valid by simply checking
  150. types.
  151. However, pointer aliasing poses an issue for the above "nice property".
  152. Consider the following example:
  153. .. code-block:: c
  154. struct node_data *n, *m, *o, *p;
  155. n = bpf_obj_new(typeof(*n)); /* 1 */
  156. bpf_spin_lock(&lock);
  157. bpf_rbtree_add(&tree, n); /* 2 */
  158. m = bpf_rbtree_first(&tree); /* 3 */
  159. o = bpf_rbtree_remove(&tree, n); /* 4 */
  160. p = bpf_rbtree_remove(&tree, m); /* 5 */
  161. bpf_spin_unlock(&lock);
  162. bpf_obj_drop(o);
  163. bpf_obj_drop(p); /* 6 */
  164. Assume the tree is empty before this program runs. If we track verifier state
  165. changes here using numbers in above comments:
  166. 1) n is an owning reference
  167. 2) n is a non-owning reference, it's been added to the tree
  168. 3) n and m are non-owning references, they both point to the same node
  169. 4) o is an owning reference, n and m non-owning, all point to same node
  170. 5) o and p are owning, n and m non-owning, all point to the same node
  171. 6) a double-free has occurred, since o and p point to same node and o was
  172. ``free``'d in previous statement
  173. States 4 and 5 violate our "nice property", as there are non-owning refs to
  174. a node which is not in an rbtree. Statement 5 will try to remove a node which
  175. has already been removed as a result of this violation. State 6 is a dangerous
  176. double-free.
  177. At a minimum we should prevent state 6 from being possible. If we can't also
  178. prevent state 5 then we must abandon our "nice property" and check whether a
  179. node has already been removed at runtime.
  180. We prevent both by generalizing the "invalidate non-owning references" behavior
  181. of ``bpf_spin_unlock`` and doing similar invalidation after
  182. ``bpf_rbtree_remove``. The logic here being that any graph API kfunc which:
  183. * takes an arbitrary node argument
  184. * removes it from the data structure
  185. * returns an owning reference to the removed node
  186. May result in a state where some other non-owning reference points to the same
  187. node. So ``remove``-type kfuncs must be considered a non-owning reference
  188. invalidation point as well.