xref: /qemu/docs/devel/submitting-a-patch.rst (revision c5955f4f)
1.. _submitting-a-patch:
2
3Submitting a Patch
4==================
5
6QEMU welcomes contributions of code (either fixing bugs or adding new
7functionality). However, we get a lot of patches, and so we have some
8guidelines about submitting patches. If you follow these, you'll help
9make our task of code review easier and your patch is likely to be
10committed faster.
11
12This page seems very long, so if you are only trying to post a quick
13one-shot fix, the bare minimum we ask is that:
14
15-  You **must** provide a Signed-off-by: line (this is a hard
16   requirement because it's how you say "I'm legally okay to contribute
17   this and happy for it to go into QEMU", modeled after the `Linux kernel
18   <http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/SubmittingPatches?id=f6f94e2ab1b33f0082ac22d71f66385a60d8157f#n297>`__
19   policy.) ``git commit -s`` or ``git format-patch -s`` will add one.
20-  All contributions to QEMU must be **sent as patches** to the
21   qemu-devel `mailing list <MailingLists>`__. Patch contributions
22   should not be posted on the bug tracker, posted on forums, or
23   externally hosted and linked to. (We have other mailing lists too,
24   but all patches must go to qemu-devel, possibly with a Cc: to another
25   list.) ``git send-email`` (`step-by-step setup
26   guide <https://git-send-email.io/>`__ and `hints and
27   tips <https://elixir.bootlin.com/linux/latest/source/Documentation/process/email-clients.rst>`__)
28   works best for delivering the patch without mangling it, but
29   attachments can be used as a last resort on a first-time submission.
30-  You must read replies to your message, and be willing to act on them.
31   Note, however, that maintainers are often willing to manually fix up
32   first-time contributions, since there is a learning curve involved in
33   making an ideal patch submission.
34
35You do not have to subscribe to post (list policy is to reply-to-all to
36preserve CCs and keep non-subscribers in the loop on the threads they
37start), although you may find it easier as a subscriber to pick up good
38ideas from other posts. If you do subscribe, be prepared for a high
39volume of email, often over one thousand messages in a week. The list is
40moderated; first-time posts from an email address (whether or not you
41subscribed) may be subject to some delay while waiting for a moderator
42to whitelist your address.
43
44The larger your contribution is, or if you plan on becoming a long-term
45contributor, then the more important the rest of this page becomes.
46Reading the table of contents below should already give you an idea of
47the basic requirements. Use the table of contents as a reference, and
48read the parts that you have doubts about.
49
50.. contents:: Table of Contents
51
52.. _writing_your_patches:
53
54Writing your Patches
55--------------------
56
57.. _use_the_qemu_coding_style:
58
59Use the QEMU coding style
60~~~~~~~~~~~~~~~~~~~~~~~~~
61
62You can run run *scripts/checkpatch.pl <patchfile>* before submitting to
63check that you are in compliance with our coding standards. Be aware
64that ``checkpatch.pl`` is not infallible, though, especially where C
65preprocessor macros are involved; use some common sense too. See also:
66
67-  :ref:`coding-style`
68-  `Automate a checkpatch run on
69   commit <https://blog.vmsplice.net/2011/03/how-to-automatically-run-checkpatchpl.html>`__
70
71.. _base_patches_against_current_git_master:
72
73Base patches against current git master
74~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75
76There's no point submitting a patch which is based on a released version
77of QEMU because development will have moved on from then and it probably
78won't even apply to master. We only apply selected bugfixes to release
79branches and then only as backports once the code has gone into master.
80
81It is also okay to base patches on top of other on-going work that is
82not yet part of the git master branch. To aid continuous integration
83tools, such as `patchew <http://patchew.org/QEMU/>`__, you should `add a
84tag <https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg01288.html>`__
85line ``Based-on: $MESSAGE_ID`` to your cover letter to make the series
86dependency obvious.
87
88.. _split_up_long_patches:
89
90Split up long patches
91~~~~~~~~~~~~~~~~~~~~~
92
93Split up longer patches into a patch series of logical code changes.
94Each change should compile and execute successfully. For instance, don't
95add a file to the makefile in patch one and then add the file itself in
96patch two. (This rule is here so that people can later use tools like
97`git bisect <http://git-scm.com/docs/git-bisect>`__ without hitting
98points in the commit history where QEMU doesn't work for reasons
99unrelated to the bug they're chasing.) Put documentation first, not
100last, so that someone reading the series can do a clean-room evaluation
101of the documentation, then validate that the code matched the
102documentation. A commit message that mentions "Also, ..." is often a
103good candidate for splitting into multiple patches. For more thoughts on
104properly splitting patches and writing good commit messages, see `this
105advice from
106OpenStack <https://wiki.openstack.org/wiki/GitCommitMessages>`__.
107
108.. _make_code_motion_patches_easy_to_review:
109
110Make code motion patches easy to review
111~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112
113If a series requires large blocks of code motion, there are tricks for
114making the refactoring easier to review. Split up the series so that
115semantic changes (or even function renames) are done in a separate patch
116from the raw code motion. Use a one-time setup of ``git config
117diff.renames true;`` ``git config diff.algorithm patience`` (refer to
118`git-config <http://git-scm.com/docs/git-config>`__). The 'diff.renames'
119property ensures file rename patches will be given in a more compact
120representation that focuses only on the differences across the file
121rename, instead of showing the entire old file as a deletion and the new
122file as an insertion. Meanwhile, the 'diff.algorithm' property ensures
123that extracting a non-contiguous subset of one file into a new file, but
124where all extracted parts occur in the same order both before and after
125the patch, will reduce churn in trying to treat unrelated ``}`` lines in
126the original file as separating hunks of changes.
127
128Ideally, a code motion patch can be reviewed by doing::
129
130    git format-patch --stdout -1 > patch;
131    diff -u <(sed -n 's/^-//p' patch) <(sed -n 's/^\+//p' patch)
132
133to focus on the few changes that weren't wholesale code motion.
134
135.. _dont_include_irrelevant_changes:
136
137Don't include irrelevant changes
138~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139
140In particular, don't include formatting, coding style or whitespace
141changes to bits of code that would otherwise not be touched by the
142patch. (It's OK to fix coding style issues in the immediate area (few
143lines) of the lines you're changing.) If you think a section of code
144really does need a reindent or other large-scale style fix, submit this
145as a separate patch which makes no semantic changes; don't put it in the
146same patch as your bug fix.
147
148For smaller patches in less frequently changed areas of QEMU, consider
149using the :ref:`trivial-patches` process.
150
151.. _write_a_meaningful_commit_message:
152
153Write a meaningful commit message
154~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155
156Commit messages should be meaningful and should stand on their own as a
157historical record of why the changes you applied were necessary or
158useful.
159
160QEMU follows the usual standard for git commit messages: the first line
161(which becomes the email subject line) is "subsystem: single line
162summary of change". Whether the "single line summary of change" starts
163with a capital is a matter of taste, but we prefer that the summary does
164not end in a dot. Look at ``git shortlog -30`` for an idea of sample
165subject lines. Then there is a blank line and a more detailed
166description of the patch, another blank and your Signed-off-by: line.
167Please do not use lines that are longer than 76 characters in your
168commit message (so that the text still shows up nicely with "git show"
169in a 80-columns terminal window).
170
171The body of the commit message is a good place to document why your
172change is important. Don't include comments like "This is a suggestion
173for fixing this bug" (they can go below the ``---`` line in the email so
174they don't go into the final commit message). Make sure the body of the
175commit message can be read in isolation even if the reader's mailer
176displays the subject line some distance apart (that is, a body that
177starts with "... so that" as a continuation of the subject line is
178harder to follow).
179
180If your patch fixes a commit that is already in the repository, please
181add an additional line with "Fixes: <at-least-12-digits-of-SHA-commit-id>
182("Fixed commit subject")" below the patch description / before your
183"Signed-off-by:" line in the commit message.
184
185If your patch fixes a bug in the gitlab bug tracker, please add a line
186with "Resolves: <URL-of-the-bug>" to the commit message, too. Gitlab can
187close bugs automatically once commits with the "Resolved:" keyword get
188merged into the master branch of the project. And if your patch addresses
189a bug in another public bug tracker, you can also use a line with
190"Buglink: <URL-of-the-bug>" for reference here, too.
191
192Example::
193
194 Fixes: 14055ce53c2d ("s390x/tcg: avoid overflows in time2tod/tod2time")
195 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/42
196 Buglink: https://bugs.launchpad.net/qemu/+bug/1804323``
197
198Some other tags that are used in commit messages include "Message-Id:"
199"Tested-by:", "Acked-by:", "Reported-by:", "Suggested-by:".  See ``git
200log`` for these keywords for example usage.
201
202.. _test_your_patches:
203
204Test your patches
205~~~~~~~~~~~~~~~~~
206
207Although QEMU has `continuous integration
208services <Testing#Continuous_Integration>`__ that attempt to test
209patches submitted to the list, it still saves everyone time if you have
210already tested that your patch compiles and works. Because QEMU is such
211a large project, it's okay to use configure arguments to limit what is
212built for faster turnaround during your development time; but it is
213still wise to also check that your patches work with a full build before
214submitting a series, especially if your changes might have an unintended
215effect on other areas of the code you don't normally experiment with.
216See `Testing <Testing>`__ for more details on what tests are available.
217Also, it is a wise idea to include a testsuite addition as part of your
218patches - either to ensure that future changes won't regress your new
219feature, or to add a test which exposes the bug that the rest of your
220series fixes. Keeping separate commits for the test and the fix allows
221reviewers to rebase the test to occur first to prove it catches the
222problem, then again to place it last in the series so that bisection
223doesn't land on a known-broken state.
224
225.. _submitting_your_patches:
226
227Submitting your Patches
228-----------------------
229
230.. _if_you_cannot_send_patch_emails:
231
232If you cannot send patch emails
233~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234
235In rare cases it may not be possible to send properly formatted patch
236emails. You can use `sourcehut <https://sourcehut.org/>`__ to send your
237patches to the QEMU mailing list by following these steps:
238
239#. Register or sign in to your account
240#. Add your SSH public key in `meta \|
241   keys <https://meta.sr.ht/keys>`__.
242#. Publish your git branch using **git push git@git.sr.ht:~USERNAME/qemu
243   HEAD**
244#. Send your patches to the QEMU mailing list using the web-based
245   ``git-send-email`` UI at https://git.sr.ht/~USERNAME/qemu/send-email
246
247`This video
248<https://spacepub.space/videos/watch/ad258d23-0ac6-488c-83fc-2bacf578de3a>`__
249shows the web-based ``git-send-email`` workflow. Documentation is
250available `here
251<https://man.sr.ht/git.sr.ht/#sending-patches-upstream>`__.
252
253.. _cc_the_relevant_maintainer:
254
255CC the relevant maintainer
256~~~~~~~~~~~~~~~~~~~~~~~~~~
257
258Send patches both to the mailing list and CC the maintainer(s) of the
259files you are modifying. look in the MAINTAINERS file to find out who
260that is. Also try using scripts/get_maintainer.pl from the repository
261for learning the most common committers for the files you touched.
262
263Example::
264
265    ~/src/qemu/scripts/get_maintainer.pl -f hw/ide/core.c
266
267In fact, you can automate this, via a one-time setup of ``git config
268sendemail.cccmd 'scripts/get_maintainer.pl --nogit-fallback'`` (Refer to
269`git-config <http://git-scm.com/docs/git-config>`__.)
270
271.. _do_not_send_as_an_attachment:
272
273Do not send as an attachment
274~~~~~~~~~~~~~~~~~~~~~~~~~~~~
275
276Send patches inline so they are easy to reply to with review comments.
277Do not put patches in attachments.
278
279.. _use_git_format_patch:
280
281Use ``git format-patch``
282~~~~~~~~~~~~~~~~~~~~~~~~
283
284Use the right diff format.
285`git format-patch <http://git-scm.com/docs/git-format-patch>`__ will
286produce patch emails in the right format (check the documentation to
287find out how to drive it). You can then edit the cover letter before
288using ``git send-email`` to mail the files to the mailing list. (We
289recommend `git send-email <http://git-scm.com/docs/git-send-email>`__
290because mail clients often mangle patches by wrapping long lines or
291messing up whitespace. Some distributions do not include send-email in a
292default install of git; you may need to download additional packages,
293such as 'git-email' on Fedora-based systems.) Patch series need a cover
294letter, with shallow threading (all patches in the series are
295in-reply-to the cover letter, but not to each other); single unrelated
296patches do not need a cover letter (but if you do send a cover letter,
297use ``--numbered`` so the cover and the patch have distinct subject lines).
298Patches are easier to find if they start a new top-level thread, rather
299than being buried in-reply-to another existing thread.
300
301.. _avoid_posting_large_binary_blob:
302
303Avoid posting large binary blob
304~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
305
306If you added binaries to the repository, consider producing the patch
307emails using ``git format-patch --no-binary`` and include a link to a
308git repository to fetch the original commit.
309
310.. _patch_emails_must_include_a_signed_off_by_line:
311
312Patch emails must include a ``Signed-off-by:`` line
313~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314
315For more information see `SubmittingPatches 1.12
316<http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/SubmittingPatches?id=f6f94e2ab1b33f0082ac22d71f66385a60d8157f#n297>`__.
317This is vital or we will not be able to apply your patch! Please use
318your real name to sign a patch (not an alias or acronym).
319
320If you wrote the patch, make sure your "From:" and "Signed-off-by:"
321lines use the same spelling. It's okay if you subscribe or contribute to
322the list via more than one address, but using multiple addresses in one
323commit just confuses things. If someone else wrote the patch, git will
324include a "From:" line in the body of the email (different from your
325envelope From:) that will give credit to the correct author; but again,
326that author's Signed-off-by: line is mandatory, with the same spelling.
327
328.. _include_a_meaningful_cover_letter:
329
330Include a meaningful cover letter
331~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
332
333This is a requirement for any series with multiple patches (as it aids
334continuous integration), but optional for an isolated patch. The cover
335letter explains the overall goal of such a series, and also provides a
336convenient 0/N email for others to reply to the series as a whole. A
337one-time setup of ``git config format.coverletter auto`` (refer to
338`git-config <http://git-scm.com/docs/git-config>`__) will generate the
339cover letter as needed.
340
341When reviewers don't know your goal at the start of their review, they
342may object to early changes that don't make sense until the end of the
343series, because they do not have enough context yet at that point of
344their review. A series where the goal is unclear also risks a higher
345number of review-fix cycles because the reviewers haven't bought into
346the idea yet. If the cover letter can explain these points to the
347reviewer, the process will be smoother patches will get merged faster.
348Make sure your cover letter includes a diffstat of changes made over the
349entire series; potential reviewers know what files they are interested
350in, and they need an easy way determine if your series touches them.
351
352.. _use_the_rfc_tag_if_needed:
353
354Use the RFC tag if needed
355~~~~~~~~~~~~~~~~~~~~~~~~~
356
357For example, "[PATCH RFC v2]". ``git format-patch --subject-prefix=RFC``
358can help.
359
360"RFC" means "Request For Comments" and is a statement that you don't
361intend for your patchset to be applied to master, but would like some
362review on it anyway. Reasons for doing this include:
363
364-  the patch depends on some pending kernel changes which haven't yet
365   been accepted, so the QEMU patch series is blocked until that
366   dependency has been dealt with, but is worth reviewing anyway
367-  the patch set is not finished yet (perhaps it doesn't cover all use
368   cases or work with all targets) but you want early review of a major
369   API change or design structure before continuing
370
371In general, since it's asking other people to do review work on a
372patchset that the submitter themselves is saying shouldn't be applied,
373it's best to:
374
375-  use it sparingly
376-  in the cover letter, be clear about why a patch is an RFC, what areas
377   of the patchset you're looking for review on, and why reviewers
378   should care
379
380.. _consider_whether_your_patch_is_applicable_for_stable:
381
382Consider whether your patch is applicable for stable
383~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
384
385If your patch fixes a severe issue or a regression, it may be applicable
386for stable. In that case, consider adding ``Cc: qemu-stable@nongnu.org``
387to your patch to notify the stable maintainers.
388
389For more details on how QEMU's stable process works, refer to the
390:ref:`stable-process` page.
391
392.. _participating_in_code_review:
393
394Participating in Code Review
395----------------------------
396
397All patches submitted to the QEMU project go through a code review
398process before they are accepted. Some areas of code that are well
399maintained may review patches quickly, lesser-loved areas of code may
400have a longer delay.
401
402.. _stay_around_to_fix_problems_raised_in_code_review:
403
404Stay around to fix problems raised in code review
405~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
406
407Not many patches get into QEMU straight away -- it is quite common that
408developers will identify bugs, or suggest a cleaner approach, or even
409just point out code style issues or commit message typos. You'll need to
410respond to these, and then send a second version of your patches with
411the issues fixed. This takes a little time and effort on your part, but
412if you don't do it then your changes will never get into QEMU. It's also
413just polite -- it is quite disheartening for a developer to spend time
414reviewing your code and suggesting improvements, only to find that
415you're not going to do anything further and it was all wasted effort.
416
417When replying to comments on your patches **reply to all and not just
418the sender** -- keeping discussion on the mailing list means everybody
419can follow it.
420
421.. _pay_attention_to_review_comments:
422
423Pay attention to review comments
424~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
425
426Someone took their time to review your work, and it pays to respect that
427effort; repeatedly submitting a series without addressing all comments
428from the previous round tends to alienate reviewers and stall your
429patch. Reviewers aren't always perfect, so it is okay if you want to
430argue that your code was correct in the first place instead of blindly
431doing everything the reviewer asked. On the other hand, if someone
432pointed out a potential issue during review, then even if your code
433turns out to be correct, it's probably a sign that you should improve
434your commit message and/or comments in the code explaining why the code
435is correct.
436
437If you fix issues that are raised during review **resend the entire
438patch series** not just the one patch that was changed. This allows
439maintainers to easily apply the fixed series without having to manually
440identify which patches are relevant. Send the new version as a complete
441fresh email or series of emails -- don't try to make it a followup to
442version 1. (This helps automatic patch email handling tools distinguish
443between v1 and v2 emails.)
444
445.. _when_resending_patches_add_a_version_tag:
446
447When resending patches add a version tag
448~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
449
450All patches beyond the first version should include a version tag -- for
451example, "[PATCH v2]". This means people can easily identify whether
452they're looking at the most recent version. (The first version of a
453patch need not say "v1", just [PATCH] is sufficient.) For patch series,
454the version applies to the whole series -- even if you only change one
455patch, you resend the entire series and mark it as "v2". Don't try to
456track versions of different patches in the series separately.  `git
457format-patch <http://git-scm.com/docs/git-format-patch>`__ and `git
458send-email <http://git-scm.com/docs/git-send-email>`__ both understand
459the ``-v2`` option to make this easier. Send each new revision as a new
460top-level thread, rather than burying it in-reply-to an earlier
461revision, as many reviewers are not looking inside deep threads for new
462patches.
463
464.. _include_version_history_in_patchset_revisions:
465
466Include version history in patchset revisions
467~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
468
469For later versions of patches, include a summary of changes from
470previous versions, but not in the commit message itself. In an email
471formatted as a git patch, the commit message is the part above the ``---``
472line, and this will go into the git changelog when the patch is
473committed. This part should be a self-contained description of what this
474version of the patch does, written to make sense to anybody who comes
475back to look at this commit in git in six months' time. The part below
476the ``---`` line and above the patch proper (git format-patch puts the
477diffstat here) is a good place to put remarks for people reading the
478patch email, and this is where the "changes since previous version"
479summary belongs. The `git-publish
480<https://github.com/stefanha/git-publish>`__ script can help with
481tracking a good summary across versions. Also, the `git-backport-diff
482<https://github.com/codyprime/git-scripts>`__ script can help focus
483reviewers on what changed between revisions.
484
485.. _tips_and_tricks:
486
487Tips and Tricks
488---------------
489
490.. _proper_use_of_reviewed_by_tags_can_aid_review:
491
492Proper use of Reviewed-by: tags can aid review
493~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
494
495When reviewing a large series, a reviewer can reply to some of the
496patches with a Reviewed-by tag, stating that they are happy with that
497patch in isolation (sometimes conditional on minor cleanup, like fixing
498whitespace, that doesn't affect code content). You should then update
499those commit messages by hand to include the Reviewed-by tag, so that in
500the next revision, reviewers can spot which patches were already clean
501from the previous round. Conversely, if you significantly modify a patch
502that was previously reviewed, remove the reviewed-by tag out of the
503commit message, as well as listing the changes from the previous
504version, to make it easier to focus a reviewer's attention to your
505changes.
506
507.. _if_your_patch_seems_to_have_been_ignored:
508
509If your patch seems to have been ignored
510~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
511
512If your patchset has received no replies you should "ping" it after a
513week or two, by sending an email as a reply-to-all to the patch mail,
514including the word "ping" and ideally also a link to the page for the
515patch on `patchew <https://patchew.org/QEMU/>`__ or
516`lore.kernel.org <https://lore.kernel.org/qemu-devel/>`__. It's worth
517double-checking for reasons why your patch might have been ignored
518(forgot to CC the maintainer? annoyed people by failing to respond to
519review comments on an earlier version?), but often for less-maintained
520areas of QEMU patches do just slip through the cracks. If your ping is
521also ignored, ping again after another week or so. As the submitter, you
522are the person with the most motivation to get your patch applied, so
523you have to be persistent.
524
525.. _is_my_patch_in:
526
527Is my patch in?
528~~~~~~~~~~~~~~~
529
530QEMU has some Continuous Integration machines that try to catch patch
531submission problems as soon as possible.  `patchew
532<http://patchew.org/QEMU/>`__ includes a web interface for tracking the
533status of various threads that have been posted to the list, and may
534send you an automated mail if it detected a problem with your patch.
535
536Once your patch has had enough review on list, the maintainer for that
537area of code will send notification to the list that they are including
538your patch in a particular staging branch. Periodically, the maintainer
539then takes care of :ref:`submitting-a-pull-request`
540for aggregating topic branches into mainline QEMU. Generally, you do not
541need to send a pull request unless you have contributed enough patches
542to become a maintainer over a particular section of code. Maintainers
543may further modify your commit, by resolving simple merge conflicts or
544fixing minor typos pointed out during review, but will always add a
545Signed-off-by line in addition to yours, indicating that it went through
546their tree. Occasionally, the maintainer's pull request may hit more
547difficult merge conflicts, where you may be requested to help rebase and
548resolve the problems. It may take a couple of weeks between when your
549patch first had a positive review to when it finally lands in qemu.git;
550release cycle freezes may extend that time even longer.
551
552.. _return_the_favor:
553
554Return the favor
555~~~~~~~~~~~~~~~~
556
557Peer review only works if everyone chips in a bit of review time. If
558everyone submitted more patches than they reviewed, we would have a
559patch backlog. A good goal is to try to review at least as many patches
560from others as what you submit. Don't worry if you don't know the code
561base as well as a maintainer; it's perfectly fine to admit when your
562review is weak because you are unfamiliar with the code.
563