10e4f07a6SMauro Carvalho Chehab.. _development_advancedtopics: 20e4f07a6SMauro Carvalho Chehab 30e4f07a6SMauro Carvalho ChehabAdvanced topics 40e4f07a6SMauro Carvalho Chehab=============== 50e4f07a6SMauro Carvalho Chehab 60e4f07a6SMauro Carvalho ChehabAt this point, hopefully, you have a handle on how the development process 70e4f07a6SMauro Carvalho Chehabworks. There is still more to learn, however! This section will cover a 80e4f07a6SMauro Carvalho Chehabnumber of topics which can be helpful for developers wanting to become a 90e4f07a6SMauro Carvalho Chehabregular part of the Linux kernel development process. 100e4f07a6SMauro Carvalho Chehab 110e4f07a6SMauro Carvalho ChehabManaging patches with git 120e4f07a6SMauro Carvalho Chehab------------------------- 130e4f07a6SMauro Carvalho Chehab 140e4f07a6SMauro Carvalho ChehabThe use of distributed version control for the kernel began in early 2002, 150e4f07a6SMauro Carvalho Chehabwhen Linus first started playing with the proprietary BitKeeper 160e4f07a6SMauro Carvalho Chehabapplication. While BitKeeper was controversial, the approach to software 170e4f07a6SMauro Carvalho Chehabversion management it embodied most certainly was not. Distributed version 180e4f07a6SMauro Carvalho Chehabcontrol enabled an immediate acceleration of the kernel development 190e4f07a6SMauro Carvalho Chehabproject. In current times, there are several free alternatives to 200e4f07a6SMauro Carvalho ChehabBitKeeper. For better or for worse, the kernel project has settled on git 210e4f07a6SMauro Carvalho Chehabas its tool of choice. 220e4f07a6SMauro Carvalho Chehab 230e4f07a6SMauro Carvalho ChehabManaging patches with git can make life much easier for the developer, 240e4f07a6SMauro Carvalho Chehabespecially as the volume of those patches grows. Git also has its rough 250e4f07a6SMauro Carvalho Chehabedges and poses certain hazards; it is a young and powerful tool which is 260e4f07a6SMauro Carvalho Chehabstill being civilized by its developers. This document will not attempt to 270e4f07a6SMauro Carvalho Chehabteach the reader how to use git; that would be sufficient material for a 280e4f07a6SMauro Carvalho Chehablong document in its own right. Instead, the focus here will be on how git 290e4f07a6SMauro Carvalho Chehabfits into the kernel development process in particular. Developers who 300e4f07a6SMauro Carvalho Chehabwish to come up to speed with git will find more information at: 310e4f07a6SMauro Carvalho Chehab 32*93431e06SAlexander A. Klimov https://git-scm.com/ 330e4f07a6SMauro Carvalho Chehab 34*93431e06SAlexander A. Klimov https://www.kernel.org/pub/software/scm/git/docs/user-manual.html 350e4f07a6SMauro Carvalho Chehab 360e4f07a6SMauro Carvalho Chehaband on various tutorials found on the web. 370e4f07a6SMauro Carvalho Chehab 380e4f07a6SMauro Carvalho ChehabThe first order of business is to read the above sites and get a solid 390e4f07a6SMauro Carvalho Chehabunderstanding of how git works before trying to use it to make patches 400e4f07a6SMauro Carvalho Chehabavailable to others. A git-using developer should be able to obtain a copy 410e4f07a6SMauro Carvalho Chehabof the mainline repository, explore the revision history, commit changes to 420e4f07a6SMauro Carvalho Chehabthe tree, use branches, etc. An understanding of git's tools for the 430e4f07a6SMauro Carvalho Chehabrewriting of history (such as rebase) is also useful. Git comes with its 440e4f07a6SMauro Carvalho Chehabown terminology and concepts; a new user of git should know about refs, 450e4f07a6SMauro Carvalho Chehabremote branches, the index, fast-forward merges, pushes and pulls, detached 460e4f07a6SMauro Carvalho Chehabheads, etc. It can all be a little intimidating at the outset, but the 470e4f07a6SMauro Carvalho Chehabconcepts are not that hard to grasp with a bit of study. 480e4f07a6SMauro Carvalho Chehab 490e4f07a6SMauro Carvalho ChehabUsing git to generate patches for submission by email can be a good 500e4f07a6SMauro Carvalho Chehabexercise while coming up to speed. 510e4f07a6SMauro Carvalho Chehab 520e4f07a6SMauro Carvalho ChehabWhen you are ready to start putting up git trees for others to look at, you 530e4f07a6SMauro Carvalho Chehabwill, of course, need a server that can be pulled from. Setting up such a 540e4f07a6SMauro Carvalho Chehabserver with git-daemon is relatively straightforward if you have a system 550e4f07a6SMauro Carvalho Chehabwhich is accessible to the Internet. Otherwise, free, public hosting sites 560e4f07a6SMauro Carvalho Chehab(Github, for example) are starting to appear on the net. Established 570e4f07a6SMauro Carvalho Chehabdevelopers can get an account on kernel.org, but those are not easy to come 58*93431e06SAlexander A. Klimovby; see https://kernel.org/faq/ for more information. 590e4f07a6SMauro Carvalho Chehab 600e4f07a6SMauro Carvalho ChehabThe normal git workflow involves the use of a lot of branches. Each line 610e4f07a6SMauro Carvalho Chehabof development can be separated into a separate "topic branch" and 620e4f07a6SMauro Carvalho Chehabmaintained independently. Branches in git are cheap, there is no reason to 630e4f07a6SMauro Carvalho Chehabnot make free use of them. And, in any case, you should not do your 640e4f07a6SMauro Carvalho Chehabdevelopment in any branch which you intend to ask others to pull from. 650e4f07a6SMauro Carvalho ChehabPublicly-available branches should be created with care; merge in patches 660e4f07a6SMauro Carvalho Chehabfrom development branches when they are in complete form and ready to go - 670e4f07a6SMauro Carvalho Chehabnot before. 680e4f07a6SMauro Carvalho Chehab 690e4f07a6SMauro Carvalho ChehabGit provides some powerful tools which can allow you to rewrite your 700e4f07a6SMauro Carvalho Chehabdevelopment history. An inconvenient patch (one which breaks bisection, 710e4f07a6SMauro Carvalho Chehabsay, or which has some other sort of obvious bug) can be fixed in place or 720e4f07a6SMauro Carvalho Chehabmade to disappear from the history entirely. A patch series can be 730e4f07a6SMauro Carvalho Chehabrewritten as if it had been written on top of today's mainline, even though 740e4f07a6SMauro Carvalho Chehabyou have been working on it for months. Changes can be transparently 750e4f07a6SMauro Carvalho Chehabshifted from one branch to another. And so on. Judicious use of git's 760e4f07a6SMauro Carvalho Chehabability to revise history can help in the creation of clean patch sets with 770e4f07a6SMauro Carvalho Chehabfewer problems. 780e4f07a6SMauro Carvalho Chehab 790e4f07a6SMauro Carvalho ChehabExcessive use of this capability can lead to other problems, though, beyond 800e4f07a6SMauro Carvalho Chehaba simple obsession for the creation of the perfect project history. 810e4f07a6SMauro Carvalho ChehabRewriting history will rewrite the changes contained in that history, 820e4f07a6SMauro Carvalho Chehabturning a tested (hopefully) kernel tree into an untested one. But, beyond 830e4f07a6SMauro Carvalho Chehabthat, developers cannot easily collaborate if they do not have a shared 840e4f07a6SMauro Carvalho Chehabview of the project history; if you rewrite history which other developers 850e4f07a6SMauro Carvalho Chehabhave pulled into their repositories, you will make life much more difficult 860e4f07a6SMauro Carvalho Chehabfor those developers. So a simple rule of thumb applies here: history 870e4f07a6SMauro Carvalho Chehabwhich has been exported to others should generally be seen as immutable 880e4f07a6SMauro Carvalho Chehabthereafter. 890e4f07a6SMauro Carvalho Chehab 900e4f07a6SMauro Carvalho ChehabSo, once you push a set of changes to your publicly-available server, those 910e4f07a6SMauro Carvalho Chehabchanges should not be rewritten. Git will attempt to enforce this rule if 920e4f07a6SMauro Carvalho Chehabyou try to push changes which do not result in a fast-forward merge 930e4f07a6SMauro Carvalho Chehab(i.e. changes which do not share the same history). It is possible to 940e4f07a6SMauro Carvalho Chehaboverride this check, and there may be times when it is necessary to rewrite 950e4f07a6SMauro Carvalho Chehaban exported tree. Moving changesets between trees to avoid conflicts in 960e4f07a6SMauro Carvalho Chehablinux-next is one example. But such actions should be rare. This is one 970e4f07a6SMauro Carvalho Chehabof the reasons why development should be done in private branches (which 980e4f07a6SMauro Carvalho Chehabcan be rewritten if necessary) and only moved into public branches when 990e4f07a6SMauro Carvalho Chehabit's in a reasonably advanced state. 1000e4f07a6SMauro Carvalho Chehab 1010e4f07a6SMauro Carvalho ChehabAs the mainline (or other tree upon which a set of changes is based) 1020e4f07a6SMauro Carvalho Chehabadvances, it is tempting to merge with that tree to stay on the leading 1030e4f07a6SMauro Carvalho Chehabedge. For a private branch, rebasing can be an easy way to keep up with 1040e4f07a6SMauro Carvalho Chehabanother tree, but rebasing is not an option once a tree is exported to the 1050e4f07a6SMauro Carvalho Chehabworld. Once that happens, a full merge must be done. Merging occasionally 1060e4f07a6SMauro Carvalho Chehabmakes good sense, but overly frequent merges can clutter the history 1070e4f07a6SMauro Carvalho Chehabneedlessly. Suggested technique in this case is to merge infrequently, and 1080e4f07a6SMauro Carvalho Chehabgenerally only at specific release points (such as a mainline -rc 1090e4f07a6SMauro Carvalho Chehabrelease). If you are nervous about specific changes, you can always 1100e4f07a6SMauro Carvalho Chehabperform test merges in a private branch. The git "rerere" tool can be 1110e4f07a6SMauro Carvalho Chehabuseful in such situations; it remembers how merge conflicts were resolved 1120e4f07a6SMauro Carvalho Chehabso that you don't have to do the same work twice. 1130e4f07a6SMauro Carvalho Chehab 1140e4f07a6SMauro Carvalho ChehabOne of the biggest recurring complaints about tools like git is this: the 1150e4f07a6SMauro Carvalho Chehabmass movement of patches from one repository to another makes it easy to 1160e4f07a6SMauro Carvalho Chehabslip in ill-advised changes which go into the mainline below the review 1170e4f07a6SMauro Carvalho Chehabradar. Kernel developers tend to get unhappy when they see that kind of 1180e4f07a6SMauro Carvalho Chehabthing happening; putting up a git tree with unreviewed or off-topic patches 1190e4f07a6SMauro Carvalho Chehabcan affect your ability to get trees pulled in the future. Quoting Linus: 1200e4f07a6SMauro Carvalho Chehab 1210e4f07a6SMauro Carvalho Chehab:: 1220e4f07a6SMauro Carvalho Chehab 1230e4f07a6SMauro Carvalho Chehab You can send me patches, but for me to pull a git patch from you, I 1240e4f07a6SMauro Carvalho Chehab need to know that you know what you're doing, and I need to be able 1250e4f07a6SMauro Carvalho Chehab to trust things *without* then having to go and check every 1260e4f07a6SMauro Carvalho Chehab individual change by hand. 1270e4f07a6SMauro Carvalho Chehab 128*93431e06SAlexander A. Klimov(https://lwn.net/Articles/224135/). 1290e4f07a6SMauro Carvalho Chehab 1300e4f07a6SMauro Carvalho ChehabTo avoid this kind of situation, ensure that all patches within a given 1310e4f07a6SMauro Carvalho Chehabbranch stick closely to the associated topic; a "driver fixes" branch 1320e4f07a6SMauro Carvalho Chehabshould not be making changes to the core memory management code. And, most 1330e4f07a6SMauro Carvalho Chehabimportantly, do not use a git tree to bypass the review process. Post an 1340e4f07a6SMauro Carvalho Chehaboccasional summary of the tree to the relevant list, and, when the time is 1350e4f07a6SMauro Carvalho Chehabright, request that the tree be included in linux-next. 1360e4f07a6SMauro Carvalho Chehab 1370e4f07a6SMauro Carvalho ChehabIf and when others start to send patches for inclusion into your tree, 1380e4f07a6SMauro Carvalho Chehabdon't forget to review them. Also ensure that you maintain the correct 1390e4f07a6SMauro Carvalho Chehabauthorship information; the git "am" tool does its best in this regard, but 1400e4f07a6SMauro Carvalho Chehabyou may have to add a "From:" line to the patch if it has been relayed to 1410e4f07a6SMauro Carvalho Chehabyou via a third party. 1420e4f07a6SMauro Carvalho Chehab 1430e4f07a6SMauro Carvalho ChehabWhen requesting a pull, be sure to give all the relevant information: where 1440e4f07a6SMauro Carvalho Chehabyour tree is, what branch to pull, and what changes will result from the 1450e4f07a6SMauro Carvalho Chehabpull. The git request-pull command can be helpful in this regard; it will 1460e4f07a6SMauro Carvalho Chehabformat the request as other developers expect, and will also check to be 1470e4f07a6SMauro Carvalho Chehabsure that you have remembered to push those changes to the public server. 1480e4f07a6SMauro Carvalho Chehab 1490e4f07a6SMauro Carvalho Chehab 1500e4f07a6SMauro Carvalho ChehabReviewing patches 1510e4f07a6SMauro Carvalho Chehab----------------- 1520e4f07a6SMauro Carvalho Chehab 1530e4f07a6SMauro Carvalho ChehabSome readers will certainly object to putting this section with "advanced 1540e4f07a6SMauro Carvalho Chehabtopics" on the grounds that even beginning kernel developers should be 1550e4f07a6SMauro Carvalho Chehabreviewing patches. It is certainly true that there is no better way to 1560e4f07a6SMauro Carvalho Chehablearn how to program in the kernel environment than by looking at code 1570e4f07a6SMauro Carvalho Chehabposted by others. In addition, reviewers are forever in short supply; by 1580e4f07a6SMauro Carvalho Chehablooking at code you can make a significant contribution to the process as a 1590e4f07a6SMauro Carvalho Chehabwhole. 1600e4f07a6SMauro Carvalho Chehab 1610e4f07a6SMauro Carvalho ChehabReviewing code can be an intimidating prospect, especially for a new kernel 1620e4f07a6SMauro Carvalho Chehabdeveloper who may well feel nervous about questioning code - in public - 1630e4f07a6SMauro Carvalho Chehabwhich has been posted by those with more experience. Even code written by 1640e4f07a6SMauro Carvalho Chehabthe most experienced developers can be improved, though. Perhaps the best 1650e4f07a6SMauro Carvalho Chehabpiece of advice for reviewers (all reviewers) is this: phrase review 1660e4f07a6SMauro Carvalho Chehabcomments as questions rather than criticisms. Asking "how does the lock 1670e4f07a6SMauro Carvalho Chehabget released in this path?" will always work better than stating "the 1680e4f07a6SMauro Carvalho Chehablocking here is wrong." 1690e4f07a6SMauro Carvalho Chehab 1700e4f07a6SMauro Carvalho ChehabDifferent developers will review code from different points of view. Some 1710e4f07a6SMauro Carvalho Chehabare mostly concerned with coding style and whether code lines have trailing 1720e4f07a6SMauro Carvalho Chehabwhite space. Others will focus primarily on whether the change implemented 1730e4f07a6SMauro Carvalho Chehabby the patch as a whole is a good thing for the kernel or not. Yet others 1740e4f07a6SMauro Carvalho Chehabwill check for problematic locking, excessive stack usage, possible 1750e4f07a6SMauro Carvalho Chehabsecurity issues, duplication of code found elsewhere, adequate 1760e4f07a6SMauro Carvalho Chehabdocumentation, adverse effects on performance, user-space ABI changes, etc. 1770e4f07a6SMauro Carvalho ChehabAll types of review, if they lead to better code going into the kernel, are 1780e4f07a6SMauro Carvalho Chehabwelcome and worthwhile. 179