0001 .. title:: Kernel-doc comments
0002
0003 ===========================
0004 Writing kernel-doc comments
0005 ===========================
0006
0007 The Linux kernel source files may contain structured documentation
0008 comments in the kernel-doc format to describe the functions, types
0009 and design of the code. It is easier to keep documentation up-to-date
0010 when it is embedded in source files.
0011
0012 .. note:: The kernel-doc format is deceptively similar to javadoc,
0013 gtk-doc or Doxygen, yet distinctively different, for historical
0014 reasons. The kernel source contains tens of thousands of kernel-doc
0015 comments. Please stick to the style described here.
0016
0017 The kernel-doc structure is extracted from the comments, and proper
0018 `Sphinx C Domain`_ function and type descriptions with anchors are
0019 generated from them. The descriptions are filtered for special kernel-doc
0020 highlights and cross-references. See below for details.
0021
0022 .. _Sphinx C Domain: http://www.sphinx-doc.org/en/stable/domains.html
0023
0024 Every function that is exported to loadable modules using
0025 ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` should have a kernel-doc
0026 comment. Functions and data structures in header files which are intended
0027 to be used by modules should also have kernel-doc comments.
0028
0029 It is good practice to also provide kernel-doc formatted documentation
0030 for functions externally visible to other kernel files (not marked
0031 ``static``). We also recommend providing kernel-doc formatted
0032 documentation for private (file ``static``) routines, for consistency of
0033 kernel source code layout. This is lower priority and at the discretion
0034 of the maintainer of that kernel source file.
0035
0036 How to format kernel-doc comments
0037 ---------------------------------
0038
0039 The opening comment mark ``/**`` is used for kernel-doc comments. The
0040 ``kernel-doc`` tool will extract comments marked this way. The rest of
0041 the comment is formatted like a normal multi-line comment with a column
0042 of asterisks on the left side, closing with ``*/`` on a line by itself.
0043
0044 The function and type kernel-doc comments should be placed just before
0045 the function or type being described in order to maximise the chance
0046 that somebody changing the code will also change the documentation. The
0047 overview kernel-doc comments may be placed anywhere at the top indentation
0048 level.
0049
0050 Running the ``kernel-doc`` tool with increased verbosity and without actual
0051 output generation may be used to verify proper formatting of the
0052 documentation comments. For example::
0053
0054 scripts/kernel-doc -v -none drivers/foo/bar.c
0055
0056 The documentation format is verified by the kernel build when it is
0057 requested to perform extra gcc checks::
0058
0059 make W=n
0060
0061 Function documentation
0062 ----------------------
0063
0064 The general format of a function and function-like macro kernel-doc comment is::
0065
0066 /**
0067 * function_name() - Brief description of function.
0068 * @arg1: Describe the first argument.
0069 * @arg2: Describe the second argument.
0070 * One can provide multiple line descriptions
0071 * for arguments.
0072 *
0073 * A longer description, with more discussion of the function function_name()
0074 * that might be useful to those using or modifying it. Begins with an
0075 * empty comment line, and may include additional embedded empty
0076 * comment lines.
0077 *
0078 * The longer description may have multiple paragraphs.
0079 *
0080 * Context: Describes whether the function can sleep, what locks it takes,
0081 * releases, or expects to be held. It can extend over multiple
0082 * lines.
0083 * Return: Describe the return value of function_name.
0084 *
0085 * The return value description can also have multiple paragraphs, and should
0086 * be placed at the end of the comment block.
0087 */
0088
0089 The brief description following the function name may span multiple lines, and
0090 ends with an argument description, a blank comment line, or the end of the
0091 comment block.
0092
0093 Function parameters
0094 ~~~~~~~~~~~~~~~~~~~
0095
0096 Each function argument should be described in order, immediately following
0097 the short function description. Do not leave a blank line between the
0098 function description and the arguments, nor between the arguments.
0099
0100 Each ``@argument:`` description may span multiple lines.
0101
0102 .. note::
0103
0104 If the ``@argument`` description has multiple lines, the continuation
0105 of the description should start at the same column as the previous line::
0106
0107 * @argument: some long description
0108 * that continues on next lines
0109
0110 or::
0111
0112 * @argument:
0113 * some long description
0114 * that continues on next lines
0115
0116 If a function has a variable number of arguments, its description should
0117 be written in kernel-doc notation as::
0118
0119 * @...: description
0120
0121 Function context
0122 ~~~~~~~~~~~~~~~~
0123
0124 The context in which a function can be called should be described in a
0125 section named ``Context``. This should include whether the function
0126 sleeps or can be called from interrupt context, as well as what locks
0127 it takes, releases and expects to be held by its caller.
0128
0129 Examples::
0130
0131 * Context: Any context.
0132 * Context: Any context. Takes and releases the RCU lock.
0133 * Context: Any context. Expects <lock> to be held by caller.
0134 * Context: Process context. May sleep if @gfp flags permit.
0135 * Context: Process context. Takes and releases <mutex>.
0136 * Context: Softirq or process context. Takes and releases <lock>, BH-safe.
0137 * Context: Interrupt context.
0138
0139 Return values
0140 ~~~~~~~~~~~~~
0141
0142 The return value, if any, should be described in a dedicated section
0143 named ``Return``.
0144
0145 .. note::
0146
0147 #) The multi-line descriptive text you provide does *not* recognize
0148 line breaks, so if you try to format some text nicely, as in::
0149
0150 * Return:
0151 * 0 - OK
0152 * -EINVAL - invalid argument
0153 * -ENOMEM - out of memory
0154
0155 this will all run together and produce::
0156
0157 Return: 0 - OK -EINVAL - invalid argument -ENOMEM - out of memory
0158
0159 So, in order to produce the desired line breaks, you need to use a
0160 ReST list, e. g.::
0161
0162 * Return:
0163 * * 0 - OK to runtime suspend the device
0164 * * -EBUSY - Device should not be runtime suspended
0165
0166 #) If the descriptive text you provide has lines that begin with
0167 some phrase followed by a colon, each of those phrases will be taken
0168 as a new section heading, which probably won't produce the desired
0169 effect.
0170
0171 Structure, union, and enumeration documentation
0172 -----------------------------------------------
0173
0174 The general format of a struct, union, and enum kernel-doc comment is::
0175
0176 /**
0177 * struct struct_name - Brief description.
0178 * @member1: Description of member1.
0179 * @member2: Description of member2.
0180 * One can provide multiple line descriptions
0181 * for members.
0182 *
0183 * Description of the structure.
0184 */
0185
0186 You can replace the ``struct`` in the above example with ``union`` or
0187 ``enum`` to describe unions or enums. ``member`` is used to mean struct
0188 and union member names as well as enumerations in an enum.
0189
0190 The brief description following the structure name may span multiple
0191 lines, and ends with a member description, a blank comment line, or the
0192 end of the comment block.
0193
0194 Members
0195 ~~~~~~~
0196
0197 Members of structs, unions and enums should be documented the same way
0198 as function parameters; they immediately succeed the short description
0199 and may be multi-line.
0200
0201 Inside a struct or union description, you can use the ``private:`` and
0202 ``public:`` comment tags. Structure fields that are inside a ``private:``
0203 area are not listed in the generated output documentation.
0204
0205 The ``private:`` and ``public:`` tags must begin immediately following a
0206 ``/*`` comment marker. They may optionally include comments between the
0207 ``:`` and the ending ``*/`` marker.
0208
0209 Example::
0210
0211 /**
0212 * struct my_struct - short description
0213 * @a: first member
0214 * @b: second member
0215 * @d: fourth member
0216 *
0217 * Longer description
0218 */
0219 struct my_struct {
0220 int a;
0221 int b;
0222 /* private: internal use only */
0223 int c;
0224 /* public: the next one is public */
0225 int d;
0226 };
0227
0228 Nested structs/unions
0229 ~~~~~~~~~~~~~~~~~~~~~
0230
0231 It is possible to document nested structs and unions, like::
0232
0233 /**
0234 * struct nested_foobar - a struct with nested unions and structs
0235 * @memb1: first member of anonymous union/anonymous struct
0236 * @memb2: second member of anonymous union/anonymous struct
0237 * @memb3: third member of anonymous union/anonymous struct
0238 * @memb4: fourth member of anonymous union/anonymous struct
0239 * @bar: non-anonymous union
0240 * @bar.st1: struct st1 inside @bar
0241 * @bar.st2: struct st2 inside @bar
0242 * @bar.st1.memb1: first member of struct st1 on union bar
0243 * @bar.st1.memb2: second member of struct st1 on union bar
0244 * @bar.st2.memb1: first member of struct st2 on union bar
0245 * @bar.st2.memb2: second member of struct st2 on union bar
0246 */
0247 struct nested_foobar {
0248 /* Anonymous union/struct*/
0249 union {
0250 struct {
0251 int memb1;
0252 int memb2;
0253 };
0254 struct {
0255 void *memb3;
0256 int memb4;
0257 };
0258 };
0259 union {
0260 struct {
0261 int memb1;
0262 int memb2;
0263 } st1;
0264 struct {
0265 void *memb1;
0266 int memb2;
0267 } st2;
0268 } bar;
0269 };
0270
0271 .. note::
0272
0273 #) When documenting nested structs or unions, if the struct/union ``foo``
0274 is named, the member ``bar`` inside it should be documented as
0275 ``@foo.bar:``
0276 #) When the nested struct/union is anonymous, the member ``bar`` in it
0277 should be documented as ``@bar:``
0278
0279 In-line member documentation comments
0280 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0281
0282 The structure members may also be documented in-line within the definition.
0283 There are two styles, single-line comments where both the opening ``/**`` and
0284 closing ``*/`` are on the same line, and multi-line comments where they are each
0285 on a line of their own, like all other kernel-doc comments::
0286
0287 /**
0288 * struct foo - Brief description.
0289 * @foo: The Foo member.
0290 */
0291 struct foo {
0292 int foo;
0293 /**
0294 * @bar: The Bar member.
0295 */
0296 int bar;
0297 /**
0298 * @baz: The Baz member.
0299 *
0300 * Here, the member description may contain several paragraphs.
0301 */
0302 int baz;
0303 union {
0304 /** @foobar: Single line description. */
0305 int foobar;
0306 };
0307 /** @bar2: Description for struct @bar2 inside @foo */
0308 struct {
0309 /**
0310 * @bar2.barbar: Description for @barbar inside @foo.bar2
0311 */
0312 int barbar;
0313 } bar2;
0314 };
0315
0316 Typedef documentation
0317 ---------------------
0318
0319 The general format of a typedef kernel-doc comment is::
0320
0321 /**
0322 * typedef type_name - Brief description.
0323 *
0324 * Description of the type.
0325 */
0326
0327 Typedefs with function prototypes can also be documented::
0328
0329 /**
0330 * typedef type_name - Brief description.
0331 * @arg1: description of arg1
0332 * @arg2: description of arg2
0333 *
0334 * Description of the type.
0335 *
0336 * Context: Locking context.
0337 * Return: Meaning of the return value.
0338 */
0339 typedef void (*type_name)(struct v4l2_ctrl *arg1, void *arg2);
0340
0341 Highlights and cross-references
0342 -------------------------------
0343
0344 The following special patterns are recognized in the kernel-doc comment
0345 descriptive text and converted to proper reStructuredText markup and `Sphinx C
0346 Domain`_ references.
0347
0348 .. attention:: The below are **only** recognized within kernel-doc comments,
0349 **not** within normal reStructuredText documents.
0350
0351 ``funcname()``
0352 Function reference.
0353
0354 ``@parameter``
0355 Name of a function parameter. (No cross-referencing, just formatting.)
0356
0357 ``%CONST``
0358 Name of a constant. (No cross-referencing, just formatting.)
0359
0360 ````literal````
0361 A literal block that should be handled as-is. The output will use a
0362 ``monospaced font``.
0363
0364 Useful if you need to use special characters that would otherwise have some
0365 meaning either by kernel-doc script or by reStructuredText.
0366
0367 This is particularly useful if you need to use things like ``%ph`` inside
0368 a function description.
0369
0370 ``$ENVVAR``
0371 Name of an environment variable. (No cross-referencing, just formatting.)
0372
0373 ``&struct name``
0374 Structure reference.
0375
0376 ``&enum name``
0377 Enum reference.
0378
0379 ``&typedef name``
0380 Typedef reference.
0381
0382 ``&struct_name->member`` or ``&struct_name.member``
0383 Structure or union member reference. The cross-reference will be to the struct
0384 or union definition, not the member directly.
0385
0386 ``&name``
0387 A generic type reference. Prefer using the full reference described above
0388 instead. This is mostly for legacy comments.
0389
0390 Cross-referencing from reStructuredText
0391 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0392
0393 No additional syntax is needed to cross-reference the functions and types
0394 defined in the kernel-doc comments from reStructuredText documents.
0395 Just end function names with ``()`` and write ``struct``, ``union``, ``enum``
0396 or ``typedef`` before types.
0397 For example::
0398
0399 See foo().
0400 See struct foo.
0401 See union bar.
0402 See enum baz.
0403 See typedef meh.
0404
0405 However, if you want custom text in the cross-reference link, that can be done
0406 through the following syntax::
0407
0408 See :c:func:`my custom link text for function foo <foo>`.
0409 See :c:type:`my custom link text for struct bar <bar>`.
0410
0411 For further details, please refer to the `Sphinx C Domain`_ documentation.
0412
0413 Overview documentation comments
0414 -------------------------------
0415
0416 To facilitate having source code and comments close together, you can include
0417 kernel-doc documentation blocks that are free-form comments instead of being
0418 kernel-doc for functions, structures, unions, enums, or typedefs. This could be
0419 used for something like a theory of operation for a driver or library code, for
0420 example.
0421
0422 This is done by using a ``DOC:`` section keyword with a section title.
0423
0424 The general format of an overview or high-level documentation comment is::
0425
0426 /**
0427 * DOC: Theory of Operation
0428 *
0429 * The whizbang foobar is a dilly of a gizmo. It can do whatever you
0430 * want it to do, at any time. It reads your mind. Here's how it works.
0431 *
0432 * foo bar splat
0433 *
0434 * The only drawback to this gizmo is that is can sometimes damage
0435 * hardware, software, or its subject(s).
0436 */
0437
0438 The title following ``DOC:`` acts as a heading within the source file, but also
0439 as an identifier for extracting the documentation comment. Thus, the title must
0440 be unique within the file.
0441
0442 =============================
0443 Including kernel-doc comments
0444 =============================
0445
0446 The documentation comments may be included in any of the reStructuredText
0447 documents using a dedicated kernel-doc Sphinx directive extension.
0448
0449 The kernel-doc directive is of the format::
0450
0451 .. kernel-doc:: source
0452 :option:
0453
0454 The *source* is the path to a source file, relative to the kernel source
0455 tree. The following directive options are supported:
0456
0457 export: *[source-pattern ...]*
0458 Include documentation for all functions in *source* that have been exported
0459 using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either in *source* or in any
0460 of the files specified by *source-pattern*.
0461
0462 The *source-pattern* is useful when the kernel-doc comments have been placed
0463 in header files, while ``EXPORT_SYMBOL`` and ``EXPORT_SYMBOL_GPL`` are next to
0464 the function definitions.
0465
0466 Examples::
0467
0468 .. kernel-doc:: lib/bitmap.c
0469 :export:
0470
0471 .. kernel-doc:: include/net/mac80211.h
0472 :export: net/mac80211/*.c
0473
0474 internal: *[source-pattern ...]*
0475 Include documentation for all functions and types in *source* that have
0476 **not** been exported using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either
0477 in *source* or in any of the files specified by *source-pattern*.
0478
0479 Example::
0480
0481 .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
0482 :internal:
0483
0484 identifiers: *[ function/type ...]*
0485 Include documentation for each *function* and *type* in *source*.
0486 If no *function* is specified, the documentation for all functions
0487 and types in the *source* will be included.
0488
0489 Examples::
0490
0491 .. kernel-doc:: lib/bitmap.c
0492 :identifiers: bitmap_parselist bitmap_parselist_user
0493
0494 .. kernel-doc:: lib/idr.c
0495 :identifiers:
0496
0497 no-identifiers: *[ function/type ...]*
0498 Exclude documentation for each *function* and *type* in *source*.
0499
0500 Example::
0501
0502 .. kernel-doc:: lib/bitmap.c
0503 :no-identifiers: bitmap_parselist
0504
0505 functions: *[ function/type ...]*
0506 This is an alias of the 'identifiers' directive and deprecated.
0507
0508 doc: *title*
0509 Include documentation for the ``DOC:`` paragraph identified by *title* in
0510 *source*. Spaces are allowed in *title*; do not quote the *title*. The *title*
0511 is only used as an identifier for the paragraph, and is not included in the
0512 output. Please make sure to have an appropriate heading in the enclosing
0513 reStructuredText document.
0514
0515 Example::
0516
0517 .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
0518 :doc: High Definition Audio over HDMI and Display Port
0519
0520 Without options, the kernel-doc directive includes all documentation comments
0521 from the source file.
0522
0523 The kernel-doc extension is included in the kernel source tree, at
0524 ``Documentation/sphinx/kerneldoc.py``. Internally, it uses the
0525 ``scripts/kernel-doc`` script to extract the documentation comments from the
0526 source.
0527
0528 .. _kernel_doc:
0529
0530 How to use kernel-doc to generate man pages
0531 -------------------------------------------
0532
0533 If you just want to use kernel-doc to generate man pages you can do this
0534 from the kernel git tree::
0535
0536 $ scripts/kernel-doc -man \
0537 $(git grep -l '/\*\*' -- :^Documentation :^tools) \
0538 | scripts/split-man.pl /tmp/man
0539
0540 Some older versions of git do not support some of the variants of syntax for
0541 path exclusion. One of the following commands may work for those versions::
0542
0543 $ scripts/kernel-doc -man \
0544 $(git grep -l '/\*\*' -- . ':!Documentation' ':!tools') \
0545 | scripts/split-man.pl /tmp/man
0546
0547 $ scripts/kernel-doc -man \
0548 $(git grep -l '/\*\*' -- . ":(exclude)Documentation" ":(exclude)tools") \
0549 | scripts/split-man.pl /tmp/man