xref: /OK3568_Linux_fs/kernel/Documentation/bpf/bpf_devel_QA.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=================================
2*4882a593SmuzhiyunHOWTO interact with BPF subsystem
3*4882a593Smuzhiyun=================================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThis document provides information for the BPF subsystem about various
6*4882a593Smuzhiyunworkflows related to reporting bugs, submitting patches, and queueing
7*4882a593Smuzhiyunpatches for stable kernels.
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunFor general information about submitting patches, please refer to
10*4882a593Smuzhiyun`Documentation/process/`_. This document only describes additional specifics
11*4882a593Smuzhiyunrelated to BPF.
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun.. contents::
14*4882a593Smuzhiyun    :local:
15*4882a593Smuzhiyun    :depth: 2
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunReporting bugs
18*4882a593Smuzhiyun==============
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunQ: How do I report bugs for BPF kernel code?
21*4882a593Smuzhiyun--------------------------------------------
22*4882a593SmuzhiyunA: Since all BPF kernel development as well as bpftool and iproute2 BPF
23*4882a593Smuzhiyunloader development happens through the bpf kernel mailing list,
24*4882a593Smuzhiyunplease report any found issues around BPF to the following mailing
25*4882a593Smuzhiyunlist:
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun bpf@vger.kernel.org
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunThis may also include issues related to XDP, BPF tracing, etc.
30*4882a593Smuzhiyun
31*4882a593SmuzhiyunGiven netdev has a high volume of traffic, please also add the BPF
32*4882a593Smuzhiyunmaintainers to Cc (from kernel MAINTAINERS_ file):
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun* Alexei Starovoitov <ast@kernel.org>
35*4882a593Smuzhiyun* Daniel Borkmann <daniel@iogearbox.net>
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunIn case a buggy commit has already been identified, make sure to keep
38*4882a593Smuzhiyunthe actual commit authors in Cc as well for the report. They can
39*4882a593Smuzhiyuntypically be identified through the kernel's git tree.
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun**Please do NOT report BPF issues to bugzilla.kernel.org since it
42*4882a593Smuzhiyunis a guarantee that the reported issue will be overlooked.**
43*4882a593Smuzhiyun
44*4882a593SmuzhiyunSubmitting patches
45*4882a593Smuzhiyun==================
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunQ: To which mailing list do I need to submit my BPF patches?
48*4882a593Smuzhiyun------------------------------------------------------------
49*4882a593SmuzhiyunA: Please submit your BPF patches to the bpf kernel mailing list:
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun bpf@vger.kernel.org
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunIn case your patch has changes in various different subsystems (e.g.
54*4882a593Smuzhiyunnetworking, tracing, security, etc), make sure to Cc the related kernel mailing
55*4882a593Smuzhiyunlists and maintainers from there as well, so they are able to review
56*4882a593Smuzhiyunthe changes and provide their Acked-by's to the patches.
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunQ: Where can I find patches currently under discussion for BPF subsystem?
59*4882a593Smuzhiyun-------------------------------------------------------------------------
60*4882a593SmuzhiyunA: All patches that are Cc'ed to netdev are queued for review under netdev
61*4882a593Smuzhiyunpatchwork project:
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun  https://patchwork.kernel.org/project/netdevbpf/list/
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunThose patches which target BPF, are assigned to a 'bpf' delegate for
66*4882a593Smuzhiyunfurther processing from BPF maintainers. The current queue with
67*4882a593Smuzhiyunpatches under review can be found at:
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun  https://patchwork.kernel.org/project/netdevbpf/list/?delegate=121173
70*4882a593Smuzhiyun
71*4882a593SmuzhiyunOnce the patches have been reviewed by the BPF community as a whole
72*4882a593Smuzhiyunand approved by the BPF maintainers, their status in patchwork will be
73*4882a593Smuzhiyunchanged to 'Accepted' and the submitter will be notified by mail. This
74*4882a593Smuzhiyunmeans that the patches look good from a BPF perspective and have been
75*4882a593Smuzhiyunapplied to one of the two BPF kernel trees.
76*4882a593Smuzhiyun
77*4882a593SmuzhiyunIn case feedback from the community requires a respin of the patches,
78*4882a593Smuzhiyuntheir status in patchwork will be set to 'Changes Requested', and purged
79*4882a593Smuzhiyunfrom the current review queue. Likewise for cases where patches would
80*4882a593Smuzhiyunget rejected or are not applicable to the BPF trees (but assigned to
81*4882a593Smuzhiyunthe 'bpf' delegate).
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunQ: How do the changes make their way into Linux?
84*4882a593Smuzhiyun------------------------------------------------
85*4882a593SmuzhiyunA: There are two BPF kernel trees (git repositories). Once patches have
86*4882a593Smuzhiyunbeen accepted by the BPF maintainers, they will be applied to one
87*4882a593Smuzhiyunof the two BPF trees:
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git/
90*4882a593Smuzhiyun * https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/
91*4882a593Smuzhiyun
92*4882a593SmuzhiyunThe bpf tree itself is for fixes only, whereas bpf-next for features,
93*4882a593Smuzhiyuncleanups or other kind of improvements ("next-like" content). This is
94*4882a593Smuzhiyunanalogous to net and net-next trees for networking. Both bpf and
95*4882a593Smuzhiyunbpf-next will only have a master branch in order to simplify against
96*4882a593Smuzhiyunwhich branch patches should get rebased to.
97*4882a593Smuzhiyun
98*4882a593SmuzhiyunAccumulated BPF patches in the bpf tree will regularly get pulled
99*4882a593Smuzhiyuninto the net kernel tree. Likewise, accumulated BPF patches accepted
100*4882a593Smuzhiyuninto the bpf-next tree will make their way into net-next tree. net and
101*4882a593Smuzhiyunnet-next are both run by David S. Miller. From there, they will go
102*4882a593Smuzhiyuninto the kernel mainline tree run by Linus Torvalds. To read up on the
103*4882a593Smuzhiyunprocess of net and net-next being merged into the mainline tree, see
104*4882a593Smuzhiyunthe :ref:`netdev-FAQ`
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun
108*4882a593SmuzhiyunOccasionally, to prevent merge conflicts, we might send pull requests
109*4882a593Smuzhiyunto other trees (e.g. tracing) with a small subset of the patches, but
110*4882a593Smuzhiyunnet and net-next are always the main trees targeted for integration.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunThe pull requests will contain a high-level summary of the accumulated
113*4882a593Smuzhiyunpatches and can be searched on netdev kernel mailing list through the
114*4882a593Smuzhiyunfollowing subject lines (``yyyy-mm-dd`` is the date of the pull
115*4882a593Smuzhiyunrequest)::
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun  pull-request: bpf yyyy-mm-dd
118*4882a593Smuzhiyun  pull-request: bpf-next yyyy-mm-dd
119*4882a593Smuzhiyun
120*4882a593SmuzhiyunQ: How do I indicate which tree (bpf vs. bpf-next) my patch should be applied to?
121*4882a593Smuzhiyun---------------------------------------------------------------------------------
122*4882a593Smuzhiyun
123*4882a593SmuzhiyunA: The process is the very same as described in the :ref:`netdev-FAQ`,
124*4882a593Smuzhiyunso please read up on it. The subject line must indicate whether the
125*4882a593Smuzhiyunpatch is a fix or rather "next-like" content in order to let the
126*4882a593Smuzhiyunmaintainers know whether it is targeted at bpf or bpf-next.
127*4882a593Smuzhiyun
128*4882a593SmuzhiyunFor fixes eventually landing in bpf -> net tree, the subject must
129*4882a593Smuzhiyunlook like::
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun  git format-patch --subject-prefix='PATCH bpf' start..finish
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunFor features/improvements/etc that should eventually land in
134*4882a593Smuzhiyunbpf-next -> net-next, the subject must look like::
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun  git format-patch --subject-prefix='PATCH bpf-next' start..finish
137*4882a593Smuzhiyun
138*4882a593SmuzhiyunIf unsure whether the patch or patch series should go into bpf
139*4882a593Smuzhiyunor net directly, or bpf-next or net-next directly, it is not a
140*4882a593Smuzhiyunproblem either if the subject line says net or net-next as target.
141*4882a593SmuzhiyunIt is eventually up to the maintainers to do the delegation of
142*4882a593Smuzhiyunthe patches.
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunIf it is clear that patches should go into bpf or bpf-next tree,
145*4882a593Smuzhiyunplease make sure to rebase the patches against those trees in
146*4882a593Smuzhiyunorder to reduce potential conflicts.
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunIn case the patch or patch series has to be reworked and sent out
149*4882a593Smuzhiyunagain in a second or later revision, it is also required to add a
150*4882a593Smuzhiyunversion number (``v2``, ``v3``, ...) into the subject prefix::
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun  git format-patch --subject-prefix='PATCH bpf-next v2' start..finish
153*4882a593Smuzhiyun
154*4882a593SmuzhiyunWhen changes have been requested to the patch series, always send the
155*4882a593Smuzhiyunwhole patch series again with the feedback incorporated (never send
156*4882a593Smuzhiyunindividual diffs on top of the old series).
157*4882a593Smuzhiyun
158*4882a593SmuzhiyunQ: What does it mean when a patch gets applied to bpf or bpf-next tree?
159*4882a593Smuzhiyun-----------------------------------------------------------------------
160*4882a593SmuzhiyunA: It means that the patch looks good for mainline inclusion from
161*4882a593Smuzhiyuna BPF point of view.
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunBe aware that this is not a final verdict that the patch will
164*4882a593Smuzhiyunautomatically get accepted into net or net-next trees eventually:
165*4882a593Smuzhiyun
166*4882a593SmuzhiyunOn the bpf kernel mailing list reviews can come in at any point
167*4882a593Smuzhiyunin time. If discussions around a patch conclude that they cannot
168*4882a593Smuzhiyunget included as-is, we will either apply a follow-up fix or drop
169*4882a593Smuzhiyunthem from the trees entirely. Therefore, we also reserve to rebase
170*4882a593Smuzhiyunthe trees when deemed necessary. After all, the purpose of the tree
171*4882a593Smuzhiyunis to:
172*4882a593Smuzhiyun
173*4882a593Smuzhiyuni) accumulate and stage BPF patches for integration into trees
174*4882a593Smuzhiyun   like net and net-next, and
175*4882a593Smuzhiyun
176*4882a593Smuzhiyunii) run extensive BPF test suite and
177*4882a593Smuzhiyun    workloads on the patches before they make their way any further.
178*4882a593Smuzhiyun
179*4882a593SmuzhiyunOnce the BPF pull request was accepted by David S. Miller, then
180*4882a593Smuzhiyunthe patches end up in net or net-next tree, respectively, and
181*4882a593Smuzhiyunmake their way from there further into mainline. Again, see the
182*4882a593Smuzhiyun:ref:`netdev-FAQ` for additional information e.g. on how often they are
183*4882a593Smuzhiyunmerged to mainline.
184*4882a593Smuzhiyun
185*4882a593SmuzhiyunQ: How long do I need to wait for feedback on my BPF patches?
186*4882a593Smuzhiyun-------------------------------------------------------------
187*4882a593SmuzhiyunA: We try to keep the latency low. The usual time to feedback will
188*4882a593Smuzhiyunbe around 2 or 3 business days. It may vary depending on the
189*4882a593Smuzhiyuncomplexity of changes and current patch load.
190*4882a593Smuzhiyun
191*4882a593SmuzhiyunQ: How often do you send pull requests to major kernel trees like net or net-next?
192*4882a593Smuzhiyun----------------------------------------------------------------------------------
193*4882a593Smuzhiyun
194*4882a593SmuzhiyunA: Pull requests will be sent out rather often in order to not
195*4882a593Smuzhiyunaccumulate too many patches in bpf or bpf-next.
196*4882a593Smuzhiyun
197*4882a593SmuzhiyunAs a rule of thumb, expect pull requests for each tree regularly
198*4882a593Smuzhiyunat the end of the week. In some cases pull requests could additionally
199*4882a593Smuzhiyuncome also in the middle of the week depending on the current patch
200*4882a593Smuzhiyunload or urgency.
201*4882a593Smuzhiyun
202*4882a593SmuzhiyunQ: Are patches applied to bpf-next when the merge window is open?
203*4882a593Smuzhiyun-----------------------------------------------------------------
204*4882a593SmuzhiyunA: For the time when the merge window is open, bpf-next will not be
205*4882a593Smuzhiyunprocessed. This is roughly analogous to net-next patch processing,
206*4882a593Smuzhiyunso feel free to read up on the :ref:`netdev-FAQ` about further details.
207*4882a593Smuzhiyun
208*4882a593SmuzhiyunDuring those two weeks of merge window, we might ask you to resend
209*4882a593Smuzhiyunyour patch series once bpf-next is open again. Once Linus released
210*4882a593Smuzhiyuna ``v*-rc1`` after the merge window, we continue processing of bpf-next.
211*4882a593Smuzhiyun
212*4882a593SmuzhiyunFor non-subscribers to kernel mailing lists, there is also a status
213*4882a593Smuzhiyunpage run by David S. Miller on net-next that provides guidance:
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun  http://vger.kernel.org/~davem/net-next.html
216*4882a593Smuzhiyun
217*4882a593SmuzhiyunQ: Verifier changes and test cases
218*4882a593Smuzhiyun----------------------------------
219*4882a593SmuzhiyunQ: I made a BPF verifier change, do I need to add test cases for
220*4882a593SmuzhiyunBPF kernel selftests_?
221*4882a593Smuzhiyun
222*4882a593SmuzhiyunA: If the patch has changes to the behavior of the verifier, then yes,
223*4882a593Smuzhiyunit is absolutely necessary to add test cases to the BPF kernel
224*4882a593Smuzhiyunselftests_ suite. If they are not present and we think they are
225*4882a593Smuzhiyunneeded, then we might ask for them before accepting any changes.
226*4882a593Smuzhiyun
227*4882a593SmuzhiyunIn particular, test_verifier.c is tracking a high number of BPF test
228*4882a593Smuzhiyuncases, including a lot of corner cases that LLVM BPF back end may
229*4882a593Smuzhiyungenerate out of the restricted C code. Thus, adding test cases is
230*4882a593Smuzhiyunabsolutely crucial to make sure future changes do not accidentally
231*4882a593Smuzhiyunaffect prior use-cases. Thus, treat those test cases as: verifier
232*4882a593Smuzhiyunbehavior that is not tracked in test_verifier.c could potentially
233*4882a593Smuzhiyunbe subject to change.
234*4882a593Smuzhiyun
235*4882a593SmuzhiyunQ: samples/bpf preference vs selftests?
236*4882a593Smuzhiyun---------------------------------------
237*4882a593SmuzhiyunQ: When should I add code to `samples/bpf/`_ and when to BPF kernel
238*4882a593Smuzhiyunselftests_ ?
239*4882a593Smuzhiyun
240*4882a593SmuzhiyunA: In general, we prefer additions to BPF kernel selftests_ rather than
241*4882a593Smuzhiyun`samples/bpf/`_. The rationale is very simple: kernel selftests are
242*4882a593Smuzhiyunregularly run by various bots to test for kernel regressions.
243*4882a593Smuzhiyun
244*4882a593SmuzhiyunThe more test cases we add to BPF selftests, the better the coverage
245*4882a593Smuzhiyunand the less likely it is that those could accidentally break. It is
246*4882a593Smuzhiyunnot that BPF kernel selftests cannot demo how a specific feature can
247*4882a593Smuzhiyunbe used.
248*4882a593Smuzhiyun
249*4882a593SmuzhiyunThat said, `samples/bpf/`_ may be a good place for people to get started,
250*4882a593Smuzhiyunso it might be advisable that simple demos of features could go into
251*4882a593Smuzhiyun`samples/bpf/`_, but advanced functional and corner-case testing rather
252*4882a593Smuzhiyuninto kernel selftests.
253*4882a593Smuzhiyun
254*4882a593SmuzhiyunIf your sample looks like a test case, then go for BPF kernel selftests
255*4882a593Smuzhiyuninstead!
256*4882a593Smuzhiyun
257*4882a593SmuzhiyunQ: When should I add code to the bpftool?
258*4882a593Smuzhiyun-----------------------------------------
259*4882a593SmuzhiyunA: The main purpose of bpftool (under tools/bpf/bpftool/) is to provide
260*4882a593Smuzhiyuna central user space tool for debugging and introspection of BPF programs
261*4882a593Smuzhiyunand maps that are active in the kernel. If UAPI changes related to BPF
262*4882a593Smuzhiyunenable for dumping additional information of programs or maps, then
263*4882a593Smuzhiyunbpftool should be extended as well to support dumping them.
264*4882a593Smuzhiyun
265*4882a593SmuzhiyunQ: When should I add code to iproute2's BPF loader?
266*4882a593Smuzhiyun---------------------------------------------------
267*4882a593SmuzhiyunA: For UAPI changes related to the XDP or tc layer (e.g. ``cls_bpf``),
268*4882a593Smuzhiyunthe convention is that those control-path related changes are added to
269*4882a593Smuzhiyuniproute2's BPF loader as well from user space side. This is not only
270*4882a593Smuzhiyunuseful to have UAPI changes properly designed to be usable, but also
271*4882a593Smuzhiyunto make those changes available to a wider user base of major
272*4882a593Smuzhiyundownstream distributions.
273*4882a593Smuzhiyun
274*4882a593SmuzhiyunQ: Do you accept patches as well for iproute2's BPF loader?
275*4882a593Smuzhiyun-----------------------------------------------------------
276*4882a593SmuzhiyunA: Patches for the iproute2's BPF loader have to be sent to:
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun  netdev@vger.kernel.org
279*4882a593Smuzhiyun
280*4882a593SmuzhiyunWhile those patches are not processed by the BPF kernel maintainers,
281*4882a593Smuzhiyunplease keep them in Cc as well, so they can be reviewed.
282*4882a593Smuzhiyun
283*4882a593SmuzhiyunThe official git repository for iproute2 is run by Stephen Hemminger
284*4882a593Smuzhiyunand can be found at:
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun  https://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git/
287*4882a593Smuzhiyun
288*4882a593SmuzhiyunThe patches need to have a subject prefix of '``[PATCH iproute2
289*4882a593Smuzhiyunmaster]``' or '``[PATCH iproute2 net-next]``'. '``master``' or
290*4882a593Smuzhiyun'``net-next``' describes the target branch where the patch should be
291*4882a593Smuzhiyunapplied to. Meaning, if kernel changes went into the net-next kernel
292*4882a593Smuzhiyuntree, then the related iproute2 changes need to go into the iproute2
293*4882a593Smuzhiyunnet-next branch, otherwise they can be targeted at master branch. The
294*4882a593Smuzhiyuniproute2 net-next branch will get merged into the master branch after
295*4882a593Smuzhiyunthe current iproute2 version from master has been released.
296*4882a593Smuzhiyun
297*4882a593SmuzhiyunLike BPF, the patches end up in patchwork under the netdev project and
298*4882a593Smuzhiyunare delegated to 'shemminger' for further processing:
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun  http://patchwork.ozlabs.org/project/netdev/list/?delegate=389
301*4882a593Smuzhiyun
302*4882a593SmuzhiyunQ: What is the minimum requirement before I submit my BPF patches?
303*4882a593Smuzhiyun------------------------------------------------------------------
304*4882a593SmuzhiyunA: When submitting patches, always take the time and properly test your
305*4882a593Smuzhiyunpatches *prior* to submission. Never rush them! If maintainers find
306*4882a593Smuzhiyunthat your patches have not been properly tested, it is a good way to
307*4882a593Smuzhiyunget them grumpy. Testing patch submissions is a hard requirement!
308*4882a593Smuzhiyun
309*4882a593SmuzhiyunNote, fixes that go to bpf tree *must* have a ``Fixes:`` tag included.
310*4882a593SmuzhiyunThe same applies to fixes that target bpf-next, where the affected
311*4882a593Smuzhiyuncommit is in net-next (or in some cases bpf-next). The ``Fixes:`` tag is
312*4882a593Smuzhiyuncrucial in order to identify follow-up commits and tremendously helps
313*4882a593Smuzhiyunfor people having to do backporting, so it is a must have!
314*4882a593Smuzhiyun
315*4882a593SmuzhiyunWe also don't accept patches with an empty commit message. Take your
316*4882a593Smuzhiyuntime and properly write up a high quality commit message, it is
317*4882a593Smuzhiyunessential!
318*4882a593Smuzhiyun
319*4882a593SmuzhiyunThink about it this way: other developers looking at your code a month
320*4882a593Smuzhiyunfrom now need to understand *why* a certain change has been done that
321*4882a593Smuzhiyunway, and whether there have been flaws in the analysis or assumptions
322*4882a593Smuzhiyunthat the original author did. Thus providing a proper rationale and
323*4882a593Smuzhiyundescribing the use-case for the changes is a must.
324*4882a593Smuzhiyun
325*4882a593SmuzhiyunPatch submissions with >1 patch must have a cover letter which includes
326*4882a593Smuzhiyuna high level description of the series. This high level summary will
327*4882a593Smuzhiyunthen be placed into the merge commit by the BPF maintainers such that
328*4882a593Smuzhiyunit is also accessible from the git log for future reference.
329*4882a593Smuzhiyun
330*4882a593SmuzhiyunQ: Features changing BPF JIT and/or LLVM
331*4882a593Smuzhiyun----------------------------------------
332*4882a593SmuzhiyunQ: What do I need to consider when adding a new instruction or feature
333*4882a593Smuzhiyunthat would require BPF JIT and/or LLVM integration as well?
334*4882a593Smuzhiyun
335*4882a593SmuzhiyunA: We try hard to keep all BPF JITs up to date such that the same user
336*4882a593Smuzhiyunexperience can be guaranteed when running BPF programs on different
337*4882a593Smuzhiyunarchitectures without having the program punt to the less efficient
338*4882a593Smuzhiyuninterpreter in case the in-kernel BPF JIT is enabled.
339*4882a593Smuzhiyun
340*4882a593SmuzhiyunIf you are unable to implement or test the required JIT changes for
341*4882a593Smuzhiyuncertain architectures, please work together with the related BPF JIT
342*4882a593Smuzhiyundevelopers in order to get the feature implemented in a timely manner.
343*4882a593SmuzhiyunPlease refer to the git log (``arch/*/net/``) to locate the necessary
344*4882a593Smuzhiyunpeople for helping out.
345*4882a593Smuzhiyun
346*4882a593SmuzhiyunAlso always make sure to add BPF test cases (e.g. test_bpf.c and
347*4882a593Smuzhiyuntest_verifier.c) for new instructions, so that they can receive
348*4882a593Smuzhiyunbroad test coverage and help run-time testing the various BPF JITs.
349*4882a593Smuzhiyun
350*4882a593SmuzhiyunIn case of new BPF instructions, once the changes have been accepted
351*4882a593Smuzhiyuninto the Linux kernel, please implement support into LLVM's BPF back
352*4882a593Smuzhiyunend. See LLVM_ section below for further information.
353*4882a593Smuzhiyun
354*4882a593SmuzhiyunStable submission
355*4882a593Smuzhiyun=================
356*4882a593Smuzhiyun
357*4882a593SmuzhiyunQ: I need a specific BPF commit in stable kernels. What should I do?
358*4882a593Smuzhiyun--------------------------------------------------------------------
359*4882a593SmuzhiyunA: In case you need a specific fix in stable kernels, first check whether
360*4882a593Smuzhiyunthe commit has already been applied in the related ``linux-*.y`` branches:
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun  https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/
363*4882a593Smuzhiyun
364*4882a593SmuzhiyunIf not the case, then drop an email to the BPF maintainers with the
365*4882a593Smuzhiyunnetdev kernel mailing list in Cc and ask for the fix to be queued up:
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun  netdev@vger.kernel.org
368*4882a593Smuzhiyun
369*4882a593SmuzhiyunThe process in general is the same as on netdev itself, see also the
370*4882a593Smuzhiyun:ref:`netdev-FAQ`.
371*4882a593Smuzhiyun
372*4882a593SmuzhiyunQ: Do you also backport to kernels not currently maintained as stable?
373*4882a593Smuzhiyun----------------------------------------------------------------------
374*4882a593SmuzhiyunA: No. If you need a specific BPF commit in kernels that are currently not
375*4882a593Smuzhiyunmaintained by the stable maintainers, then you are on your own.
376*4882a593Smuzhiyun
377*4882a593SmuzhiyunThe current stable and longterm stable kernels are all listed here:
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun  https://www.kernel.org/
380*4882a593Smuzhiyun
381*4882a593SmuzhiyunQ: The BPF patch I am about to submit needs to go to stable as well
382*4882a593Smuzhiyun-------------------------------------------------------------------
383*4882a593SmuzhiyunWhat should I do?
384*4882a593Smuzhiyun
385*4882a593SmuzhiyunA: The same rules apply as with netdev patch submissions in general, see
386*4882a593Smuzhiyunthe :ref:`netdev-FAQ`.
387*4882a593Smuzhiyun
388*4882a593SmuzhiyunNever add "``Cc: stable@vger.kernel.org``" to the patch description, but
389*4882a593Smuzhiyunask the BPF maintainers to queue the patches instead. This can be done
390*4882a593Smuzhiyunwith a note, for example, under the ``---`` part of the patch which does
391*4882a593Smuzhiyunnot go into the git log. Alternatively, this can be done as a simple
392*4882a593Smuzhiyunrequest by mail instead.
393*4882a593Smuzhiyun
394*4882a593SmuzhiyunQ: Queue stable patches
395*4882a593Smuzhiyun-----------------------
396*4882a593SmuzhiyunQ: Where do I find currently queued BPF patches that will be submitted
397*4882a593Smuzhiyunto stable?
398*4882a593Smuzhiyun
399*4882a593SmuzhiyunA: Once patches that fix critical bugs got applied into the bpf tree, they
400*4882a593Smuzhiyunare queued up for stable submission under:
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun  http://patchwork.ozlabs.org/bundle/bpf/stable/?state=*
403*4882a593Smuzhiyun
404*4882a593SmuzhiyunThey will be on hold there at minimum until the related commit made its
405*4882a593Smuzhiyunway into the mainline kernel tree.
406*4882a593Smuzhiyun
407*4882a593SmuzhiyunAfter having been under broader exposure, the queued patches will be
408*4882a593Smuzhiyunsubmitted by the BPF maintainers to the stable maintainers.
409*4882a593Smuzhiyun
410*4882a593SmuzhiyunTesting patches
411*4882a593Smuzhiyun===============
412*4882a593Smuzhiyun
413*4882a593SmuzhiyunQ: How to run BPF selftests
414*4882a593Smuzhiyun---------------------------
415*4882a593SmuzhiyunA: After you have booted into the newly compiled kernel, navigate to
416*4882a593Smuzhiyunthe BPF selftests_ suite in order to test BPF functionality (current
417*4882a593Smuzhiyunworking directory points to the root of the cloned git tree)::
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun  $ cd tools/testing/selftests/bpf/
420*4882a593Smuzhiyun  $ make
421*4882a593Smuzhiyun
422*4882a593SmuzhiyunTo run the verifier tests::
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun  $ sudo ./test_verifier
425*4882a593Smuzhiyun
426*4882a593SmuzhiyunThe verifier tests print out all the current checks being
427*4882a593Smuzhiyunperformed. The summary at the end of running all tests will dump
428*4882a593Smuzhiyuninformation of test successes and failures::
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun  Summary: 418 PASSED, 0 FAILED
431*4882a593Smuzhiyun
432*4882a593SmuzhiyunIn order to run through all BPF selftests, the following command is
433*4882a593Smuzhiyunneeded::
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun  $ sudo make run_tests
436*4882a593Smuzhiyun
437*4882a593SmuzhiyunSee the kernels selftest `Documentation/dev-tools/kselftest.rst`_
438*4882a593Smuzhiyundocument for further documentation.
439*4882a593Smuzhiyun
440*4882a593SmuzhiyunTo maximize the number of tests passing, the .config of the kernel
441*4882a593Smuzhiyununder test should match the config file fragment in
442*4882a593Smuzhiyuntools/testing/selftests/bpf as closely as possible.
443*4882a593Smuzhiyun
444*4882a593SmuzhiyunFinally to ensure support for latest BPF Type Format features -
445*4882a593Smuzhiyundiscussed in `Documentation/bpf/btf.rst`_ - pahole version 1.16
446*4882a593Smuzhiyunis required for kernels built with CONFIG_DEBUG_INFO_BTF=y.
447*4882a593Smuzhiyunpahole is delivered in the dwarves package or can be built
448*4882a593Smuzhiyunfrom source at
449*4882a593Smuzhiyun
450*4882a593Smuzhiyunhttps://github.com/acmel/dwarves
451*4882a593Smuzhiyun
452*4882a593SmuzhiyunSome distros have pahole version 1.16 packaged already, e.g.
453*4882a593SmuzhiyunFedora, Gentoo.
454*4882a593Smuzhiyun
455*4882a593SmuzhiyunQ: Which BPF kernel selftests version should I run my kernel against?
456*4882a593Smuzhiyun---------------------------------------------------------------------
457*4882a593SmuzhiyunA: If you run a kernel ``xyz``, then always run the BPF kernel selftests
458*4882a593Smuzhiyunfrom that kernel ``xyz`` as well. Do not expect that the BPF selftest
459*4882a593Smuzhiyunfrom the latest mainline tree will pass all the time.
460*4882a593Smuzhiyun
461*4882a593SmuzhiyunIn particular, test_bpf.c and test_verifier.c have a large number of
462*4882a593Smuzhiyuntest cases and are constantly updated with new BPF test sequences, or
463*4882a593Smuzhiyunexisting ones are adapted to verifier changes e.g. due to verifier
464*4882a593Smuzhiyunbecoming smarter and being able to better track certain things.
465*4882a593Smuzhiyun
466*4882a593SmuzhiyunLLVM
467*4882a593Smuzhiyun====
468*4882a593Smuzhiyun
469*4882a593SmuzhiyunQ: Where do I find LLVM with BPF support?
470*4882a593Smuzhiyun-----------------------------------------
471*4882a593SmuzhiyunA: The BPF back end for LLVM is upstream in LLVM since version 3.7.1.
472*4882a593Smuzhiyun
473*4882a593SmuzhiyunAll major distributions these days ship LLVM with BPF back end enabled,
474*4882a593Smuzhiyunso for the majority of use-cases it is not required to compile LLVM by
475*4882a593Smuzhiyunhand anymore, just install the distribution provided package.
476*4882a593Smuzhiyun
477*4882a593SmuzhiyunLLVM's static compiler lists the supported targets through
478*4882a593Smuzhiyun``llc --version``, make sure BPF targets are listed. Example::
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun     $ llc --version
481*4882a593Smuzhiyun     LLVM (http://llvm.org/):
482*4882a593Smuzhiyun       LLVM version 10.0.0
483*4882a593Smuzhiyun       Optimized build.
484*4882a593Smuzhiyun       Default target: x86_64-unknown-linux-gnu
485*4882a593Smuzhiyun       Host CPU: skylake
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun       Registered Targets:
488*4882a593Smuzhiyun         aarch64    - AArch64 (little endian)
489*4882a593Smuzhiyun         bpf        - BPF (host endian)
490*4882a593Smuzhiyun         bpfeb      - BPF (big endian)
491*4882a593Smuzhiyun         bpfel      - BPF (little endian)
492*4882a593Smuzhiyun         x86        - 32-bit X86: Pentium-Pro and above
493*4882a593Smuzhiyun         x86-64     - 64-bit X86: EM64T and AMD64
494*4882a593Smuzhiyun
495*4882a593SmuzhiyunFor developers in order to utilize the latest features added to LLVM's
496*4882a593SmuzhiyunBPF back end, it is advisable to run the latest LLVM releases. Support
497*4882a593Smuzhiyunfor new BPF kernel features such as additions to the BPF instruction
498*4882a593Smuzhiyunset are often developed together.
499*4882a593Smuzhiyun
500*4882a593SmuzhiyunAll LLVM releases can be found at: http://releases.llvm.org/
501*4882a593Smuzhiyun
502*4882a593SmuzhiyunQ: Got it, so how do I build LLVM manually anyway?
503*4882a593Smuzhiyun--------------------------------------------------
504*4882a593SmuzhiyunA: You need cmake and gcc-c++ as build requisites for LLVM. Once you have
505*4882a593Smuzhiyunthat set up, proceed with building the latest LLVM and clang version
506*4882a593Smuzhiyunfrom the git repositories::
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun     $ git clone https://github.com/llvm/llvm-project.git
509*4882a593Smuzhiyun     $ mkdir -p llvm-project/llvm/build/install
510*4882a593Smuzhiyun     $ cd llvm-project/llvm/build
511*4882a593Smuzhiyun     $ cmake .. -G "Ninja" -DLLVM_TARGETS_TO_BUILD="BPF;X86" \
512*4882a593Smuzhiyun                -DLLVM_ENABLE_PROJECTS="clang"    \
513*4882a593Smuzhiyun                -DBUILD_SHARED_LIBS=OFF           \
514*4882a593Smuzhiyun                -DCMAKE_BUILD_TYPE=Release        \
515*4882a593Smuzhiyun                -DLLVM_BUILD_RUNTIME=OFF
516*4882a593Smuzhiyun     $ ninja
517*4882a593Smuzhiyun
518*4882a593SmuzhiyunThe built binaries can then be found in the build/bin/ directory, where
519*4882a593Smuzhiyunyou can point the PATH variable to.
520*4882a593Smuzhiyun
521*4882a593SmuzhiyunSet ``-DLLVM_TARGETS_TO_BUILD`` equal to the target you wish to build, you
522*4882a593Smuzhiyunwill find a full list of targets within the llvm-project/llvm/lib/Target
523*4882a593Smuzhiyundirectory.
524*4882a593Smuzhiyun
525*4882a593SmuzhiyunQ: Reporting LLVM BPF issues
526*4882a593Smuzhiyun----------------------------
527*4882a593SmuzhiyunQ: Should I notify BPF kernel maintainers about issues in LLVM's BPF code
528*4882a593Smuzhiyungeneration back end or about LLVM generated code that the verifier
529*4882a593Smuzhiyunrefuses to accept?
530*4882a593Smuzhiyun
531*4882a593SmuzhiyunA: Yes, please do!
532*4882a593Smuzhiyun
533*4882a593SmuzhiyunLLVM's BPF back end is a key piece of the whole BPF
534*4882a593Smuzhiyuninfrastructure and it ties deeply into verification of programs from the
535*4882a593Smuzhiyunkernel side. Therefore, any issues on either side need to be investigated
536*4882a593Smuzhiyunand fixed whenever necessary.
537*4882a593Smuzhiyun
538*4882a593SmuzhiyunTherefore, please make sure to bring them up at netdev kernel mailing
539*4882a593Smuzhiyunlist and Cc BPF maintainers for LLVM and kernel bits:
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun* Yonghong Song <yhs@fb.com>
542*4882a593Smuzhiyun* Alexei Starovoitov <ast@kernel.org>
543*4882a593Smuzhiyun* Daniel Borkmann <daniel@iogearbox.net>
544*4882a593Smuzhiyun
545*4882a593SmuzhiyunLLVM also has an issue tracker where BPF related bugs can be found:
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun  https://bugs.llvm.org/buglist.cgi?quicksearch=bpf
548*4882a593Smuzhiyun
549*4882a593SmuzhiyunHowever, it is better to reach out through mailing lists with having
550*4882a593Smuzhiyunmaintainers in Cc.
551*4882a593Smuzhiyun
552*4882a593SmuzhiyunQ: New BPF instruction for kernel and LLVM
553*4882a593Smuzhiyun------------------------------------------
554*4882a593SmuzhiyunQ: I have added a new BPF instruction to the kernel, how can I integrate
555*4882a593Smuzhiyunit into LLVM?
556*4882a593Smuzhiyun
557*4882a593SmuzhiyunA: LLVM has a ``-mcpu`` selector for the BPF back end in order to allow
558*4882a593Smuzhiyunthe selection of BPF instruction set extensions. By default the
559*4882a593Smuzhiyun``generic`` processor target is used, which is the base instruction set
560*4882a593Smuzhiyun(v1) of BPF.
561*4882a593Smuzhiyun
562*4882a593SmuzhiyunLLVM has an option to select ``-mcpu=probe`` where it will probe the host
563*4882a593Smuzhiyunkernel for supported BPF instruction set extensions and selects the
564*4882a593Smuzhiyunoptimal set automatically.
565*4882a593Smuzhiyun
566*4882a593SmuzhiyunFor cross-compilation, a specific version can be select manually as well ::
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun     $ llc -march bpf -mcpu=help
569*4882a593Smuzhiyun     Available CPUs for this target:
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun       generic - Select the generic processor.
572*4882a593Smuzhiyun       probe   - Select the probe processor.
573*4882a593Smuzhiyun       v1      - Select the v1 processor.
574*4882a593Smuzhiyun       v2      - Select the v2 processor.
575*4882a593Smuzhiyun     [...]
576*4882a593Smuzhiyun
577*4882a593SmuzhiyunNewly added BPF instructions to the Linux kernel need to follow the same
578*4882a593Smuzhiyunscheme, bump the instruction set version and implement probing for the
579*4882a593Smuzhiyunextensions such that ``-mcpu=probe`` users can benefit from the
580*4882a593Smuzhiyunoptimization transparently when upgrading their kernels.
581*4882a593Smuzhiyun
582*4882a593SmuzhiyunIf you are unable to implement support for the newly added BPF instruction
583*4882a593Smuzhiyunplease reach out to BPF developers for help.
584*4882a593Smuzhiyun
585*4882a593SmuzhiyunBy the way, the BPF kernel selftests run with ``-mcpu=probe`` for better
586*4882a593Smuzhiyuntest coverage.
587*4882a593Smuzhiyun
588*4882a593SmuzhiyunQ: clang flag for target bpf?
589*4882a593Smuzhiyun-----------------------------
590*4882a593SmuzhiyunQ: In some cases clang flag ``-target bpf`` is used but in other cases the
591*4882a593Smuzhiyundefault clang target, which matches the underlying architecture, is used.
592*4882a593SmuzhiyunWhat is the difference and when I should use which?
593*4882a593Smuzhiyun
594*4882a593SmuzhiyunA: Although LLVM IR generation and optimization try to stay architecture
595*4882a593Smuzhiyunindependent, ``-target <arch>`` still has some impact on generated code:
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun- BPF program may recursively include header file(s) with file scope
598*4882a593Smuzhiyun  inline assembly codes. The default target can handle this well,
599*4882a593Smuzhiyun  while ``bpf`` target may fail if bpf backend assembler does not
600*4882a593Smuzhiyun  understand these assembly codes, which is true in most cases.
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun- When compiled without ``-g``, additional elf sections, e.g.,
603*4882a593Smuzhiyun  .eh_frame and .rela.eh_frame, may be present in the object file
604*4882a593Smuzhiyun  with default target, but not with ``bpf`` target.
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun- The default target may turn a C switch statement into a switch table
607*4882a593Smuzhiyun  lookup and jump operation. Since the switch table is placed
608*4882a593Smuzhiyun  in the global readonly section, the bpf program will fail to load.
609*4882a593Smuzhiyun  The bpf target does not support switch table optimization.
610*4882a593Smuzhiyun  The clang option ``-fno-jump-tables`` can be used to disable
611*4882a593Smuzhiyun  switch table generation.
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun- For clang ``-target bpf``, it is guaranteed that pointer or long /
614*4882a593Smuzhiyun  unsigned long types will always have a width of 64 bit, no matter
615*4882a593Smuzhiyun  whether underlying clang binary or default target (or kernel) is
616*4882a593Smuzhiyun  32 bit. However, when native clang target is used, then it will
617*4882a593Smuzhiyun  compile these types based on the underlying architecture's conventions,
618*4882a593Smuzhiyun  meaning in case of 32 bit architecture, pointer or long / unsigned
619*4882a593Smuzhiyun  long types e.g. in BPF context structure will have width of 32 bit
620*4882a593Smuzhiyun  while the BPF LLVM back end still operates in 64 bit. The native
621*4882a593Smuzhiyun  target is mostly needed in tracing for the case of walking ``pt_regs``
622*4882a593Smuzhiyun  or other kernel structures where CPU's register width matters.
623*4882a593Smuzhiyun  Otherwise, ``clang -target bpf`` is generally recommended.
624*4882a593Smuzhiyun
625*4882a593SmuzhiyunYou should use default target when:
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun- Your program includes a header file, e.g., ptrace.h, which eventually
628*4882a593Smuzhiyun  pulls in some header files containing file scope host assembly codes.
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun- You can add ``-fno-jump-tables`` to work around the switch table issue.
631*4882a593Smuzhiyun
632*4882a593SmuzhiyunOtherwise, you can use ``bpf`` target. Additionally, you *must* use bpf target
633*4882a593Smuzhiyunwhen:
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun- Your program uses data structures with pointer or long / unsigned long
636*4882a593Smuzhiyun  types that interface with BPF helpers or context data structures. Access
637*4882a593Smuzhiyun  into these structures is verified by the BPF verifier and may result
638*4882a593Smuzhiyun  in verification failures if the native architecture is not aligned with
639*4882a593Smuzhiyun  the BPF architecture, e.g. 64-bit. An example of this is
640*4882a593Smuzhiyun  BPF_PROG_TYPE_SK_MSG require ``-target bpf``
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun.. Links
644*4882a593Smuzhiyun.. _Documentation/process/: https://www.kernel.org/doc/html/latest/process/
645*4882a593Smuzhiyun.. _MAINTAINERS: ../../MAINTAINERS
646*4882a593Smuzhiyun.. _netdev-FAQ: ../networking/netdev-FAQ.rst
647*4882a593Smuzhiyun.. _samples/bpf/: ../../samples/bpf/
648*4882a593Smuzhiyun.. _selftests: ../../tools/testing/selftests/bpf/
649*4882a593Smuzhiyun.. _Documentation/dev-tools/kselftest.rst:
650*4882a593Smuzhiyun   https://www.kernel.org/doc/html/latest/dev-tools/kselftest.html
651*4882a593Smuzhiyun.. _Documentation/bpf/btf.rst: btf.rst
652*4882a593Smuzhiyun
653*4882a593SmuzhiyunHappy BPF hacking!
654