@@ -226,15 +226,187 @@ and they work on an entire file, not just a single patch.
226226So, let's pick a file in the kernel and see what checkpatch.pl tells us
227227about it:
228228
229- $ ./scripts/checkpatch.pl --file --terse drivers/staging/
230-
231- This is because of one thing that all developers have in
232- common, they have a brain.
233-
234-
235- The Linux kernel is no exception and describes al
236-
237-
229+ $ ./scripts/checkpatch.pl --file --terse drivers/staging/comedi/drivers/ni_labpc.c
230+ drivers/staging/comedi/drivers/ni_labpc.c:4: WARNING: line over 80 characters
231+ ...
232+ drivers/staging/comedi/drivers/ni_labpc.c:486: WARNING: braces {} are not necessary for single statement blocks
233+ drivers/staging/comedi/drivers/ni_labpc.c:489: WARNING: braces {} are not necessary for single statement blocks
234+ ...
235+ drivers/staging/comedi/drivers/ni_labpc.c:587: WARNING: suspect code indent for conditional statements (8, 0)
236+ ...
237+ drivers/staging/comedi/drivers/ni_labpc.c:743: WARNING: printk() should include KERN_ facility level
238+ drivers/staging/comedi/drivers/ni_labpc.c:750: WARNING: kfree(NULL) is safe this check is probably not required
239+ ...
240+ drivers/staging/comedi/drivers/ni_labpc.c:2028: WARNING: EXPORT_SYMBOL(foo); should immediately follow its function/variable
241+ total: 0 errors, 76 warnings, 2028 lines checked
242+
243+
244+ I've removed a lot of the warnings from the above output, as there was a
245+ total of 76 of them, and they are all varients of the above ones.
246+
247+ As can be seen, the checkpatch.pl tool points out where the code has
248+ gone beyond the 80 character limit, and where braces were used that they
249+ were not needed, as well as a few other things that should be cleaned up
250+ in the file.
251+
252+ Now that we know what needs to be done, fire up your favorite editor,
253+ and let us fix something. How about the brace warning, that should be
254+ simple to resolve.
255+
256+ Looking at the original code, lines 486-490 look like the following:
257+
258+ if (irq) {
259+ printk(", irq %u", irq);
260+ }
261+ if (dma_chan) {
262+ printk(", dma %u", dma_chan);
263+ }
264+
265+ A simple removal of those extra braces results in:
266+ if (irq)
267+ printk(", irq %u", irq);
268+ if (dma_chan)
269+ printk(", dma %u", dma_chan);
270+
271+ Save the file, and run the checkpatch tool again to verify that the
272+ warning is gone:
273+ $ ./scripts/checkpatch.pl --file --terse drivers/staging/comedi/drivers/ni_labpc.c | grep 486
274+ $
275+
276+ And of course build the file to verify that you did not break anything:
277+ $ make drivers/staging/comedi/drivers/ni_labpc.o
278+ CHK include/linux/version.h
279+ CHK include/generated/utsrelease.h
280+ CALL scripts/checksyscalls.sh
281+ CC [M] drivers/staging/comedi/drivers/ni_labpc.o
282+
283+ Yes, it still builds, so all is good.
284+
285+ Great, you have now made your first kernel code fix!
286+
287+ But, how do you take this change, and get it to the kernel developers in
288+ the format that they can apply it?
289+
290+ -- More git fun
291+
292+ As you edited this file within a git repository, your change to it is
293+ caught by git. This can be seen by running the 'git status' command:
294+ $ git status
295+ # On branch tutorial
296+ # Changed but not updated:
297+ # (use "git add <file>..." to update what will be committed)
298+ # (use "git checkout -- <file>..." to discard changes in working directory)
299+ #
300+ # modified: drivers/staging/comedi/drivers/ni_labpc.c
301+ #
302+ no changes added to commit (use "git add" and/or "git commit -a")
303+
304+ This output shows that we are on the branch called 'tutorial', and that
305+ we have one file modified at the moment, the ni_labpc.c file.
306+
307+ If we ask for git to show what we changed, we will see the actual lines:
308+
309+ $ git diff
310+ diff --git a/drivers/staging/comedi/drivers/ni_labpc.c b/drivers/staging/comedi/drivers/ni_labpc.c
311+ index dc3f398..a01e35d 100644
312+ --- a/drivers/staging/comedi/drivers/ni_labpc.c
313+ +++ b/drivers/staging/comedi/drivers/ni_labpc.c
314+ @@ -483,12 +483,10 @@ int labpc_common_attach(struct comedi_device *dev, unsigned long iobase,
315+
316+ printk("comedi%d: ni_labpc: %s, io 0x%lx", dev->minor, thisboard->name,
317+ iobase);
318+ - if (irq) {
319+ + if (irq)
320+ printk(", irq %u", irq);
321+ - }
322+ - if (dma_chan) {
323+ + if (dma_chan)
324+ printk(", dma %u", dma_chan);
325+ - }
326+ printk("\n");
327+
328+ if (iobase == 0) {
329+
330+ This output is in the format that the tool 'patch' can use to apply a
331+ change to a body of code. The leading '-' and '+' on some lines show
332+ what lines are removed, and what lines are added. Reading these diff
333+ outputs soon becomes natural, and is the format in which you need to
334+ send to the kernel maintainer to get the change accepted.
335+
336+ --- Description, description, description
337+
338+ The raw diff output shows what code is changed, but for every kernel
339+ patch, more information needs to be provided in order for it to be
340+ accepted. This "metadata" is as important as the code changes, as it is
341+ used to show who made the change, why the change was made, and who
342+ reviewed the change.
343+
344+ Here is a sample change that was accepted into the Linux kernel tree a
345+ while ago:
346+
347+ USB: otg: Fix bug on remove path without transceiver
348+
349+ In the case where a gadget driver is removed while no
350+ transceiver was found at probe time, a bug in
351+ otg_put_transceiver() will trigger.
352+
353+ Signed-off-by: Robert Jarzmik <robert.jarzmik@free.fr>
354+ Acked-by: David Brownell <dbrownell@users.sourceforge.net>
355+ Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
356+
357+ --- a/drivers/usb/otg/otg.c
358+ +++ b/drivers/usb/otg/otg.c
359+ @@ -43,7 +43,8 @@ EXPORT_SYMBOL(otg_get_transceiver);
360+ void otg_put_transceiver(struct otg_transceiver *x)
361+ {
362+ - put_device(x->dev);
363+ + if (x)
364+ + put_device(x->dev);
365+ }
366+
367+
368+ The first line of the change, is a one line summary of what part of the
369+ kernel the change is for, and very briefly, what it does:
370+ USB: otg: Fix bug on remove path without tranceiver
371+
372+ Then comes a more descriptive paragraph that explains why the change is
373+ needed:
374+ In the case where a gadget driver is removed while no
375+ transceiver was found at probe time, a bug in
376+ otg_put_transceiver() will trigger.
377+
378+ After that, comes a few lines that show who made and reviewed the patch:
379+ Signed-off-by: Robert Jarzmik <robert.jarzmik@free.fr>
380+ Acked-by: David Brownell <dbrownell@users.sourceforge.net>
381+ Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
382+
383+ The term "Signed-off-by:" referrs to the ability for the developer to
384+ properly claim that they are allowed to make this change, and offer it
385+ up under the acceptable license to be able for it to be added to the
386+ Linux kernel source tree. This agreement is called the "Developer's
387+ Certificate of Origin" and can be found in full in the file,
388+ Documentation/SubmittingPatches in the Linux kernel source tree.
389+
390+ A brief summary of what the Developer's Certificate of Origin consists
391+ of, is the following:
392+
393+ (a) I created this change; or
394+ (b) Based this on a previous work with a
395+ compatible license; or
396+ (c) Provided to me by (a), (b), or (c) and not
397+ modified
398+ (d) This contribution is public.
399+
400+ It is a very simple to understand agreement, and ensures that everyone
401+ involved knows that the change is legally acceptable.
402+
403+ Every developer who the patch goes through, adds their "Signed-off-by:"
404+ to it as the patch flows through the developer and maintainer chain
405+ before it is accepted into the Linux kernel source tree. This ensures
406+ that every line of code in the Linux kernel, can be tracked back to the
407+ developer who created it, and the developers who reviewed it.
408+
409+ Af
238410
239411
240412
0 commit comments