1. 25 2月, 2022 9 次提交
  2. 02 11月, 2021 1 次提交
  3. 19 10月, 2021 1 次提交
    • K
      stddef: Introduce DECLARE_FLEX_ARRAY() helper · 3080ea55
      Kees Cook 提交于
      There are many places where kernel code wants to have several different
      typed trailing flexible arrays. This would normally be done with multiple
      flexible arrays in a union, but since GCC and Clang don't (on the surface)
      allow this, there have been many open-coded workarounds, usually involving
      neighboring 0-element arrays at the end of a structure. For example,
      instead of something like this:
      
      struct thing {
      	...
      	union {
      		struct type1 foo[];
      		struct type2 bar[];
      	};
      };
      
      code works around the compiler with:
      
      struct thing {
      	...
      	struct type1 foo[0];
      	struct type2 bar[];
      };
      
      Another case is when a flexible array is wanted as the single member
      within a struct (which itself is usually in a union). For example, this
      would be worked around as:
      
      union many {
      	...
      	struct {
      		struct type3 baz[0];
      	};
      };
      
      These kinds of work-arounds cause problems with size checks against such
      zero-element arrays (for example when building with -Warray-bounds and
      -Wzero-length-bounds, and with the coming FORTIFY_SOURCE improvements),
      so they must all be converted to "real" flexible arrays, avoiding warnings
      like this:
      
      fs/hpfs/anode.c: In function 'hpfs_add_sector_to_btree':
      fs/hpfs/anode.c:209:27: warning: array subscript 0 is outside the bounds of an interior zero-length array 'struct bplus_internal_node[0]' [-Wzero-length-bounds]
        209 |    anode->btree.u.internal[0].down = cpu_to_le32(a);
            |    ~~~~~~~~~~~~~~~~~~~~~~~^~~
      In file included from fs/hpfs/hpfs_fn.h:26,
                       from fs/hpfs/anode.c:10:
      fs/hpfs/hpfs.h:412:32: note: while referencing 'internal'
        412 |     struct bplus_internal_node internal[0]; /* (internal) 2-word entries giving
            |                                ^~~~~~~~
      
      drivers/net/can/usb/etas_es58x/es58x_fd.c: In function 'es58x_fd_tx_can_msg':
      drivers/net/can/usb/etas_es58x/es58x_fd.c:360:35: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds]
        360 |  tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len];
            |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:22,
                       from drivers/net/can/usb/etas_es58x/es58x_fd.c:17:
      drivers/net/can/usb/etas_es58x/es58x_fd.h:231:6: note: while referencing 'raw_msg'
        231 |   u8 raw_msg[0];
            |      ^~~~~~~
      
      However, it _is_ entirely possible to have one or more flexible arrays
      in a struct or union: it just has to be in another struct. And since it
      cannot be alone in a struct, such a struct must have at least 1 other
      named member -- but that member can be zero sized. Wrap all this nonsense
      into the new DECLARE_FLEX_ARRAY() in support of having flexible arrays
      in unions (or alone in a struct).
      
      As with struct_group(), since this is needed in UAPI headers as well,
      implement the core there, with a non-UAPI wrapper.
      
      Additionally update kernel-doc to understand its existence.
      
      https://github.com/KSPP/linux/issues/137
      
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
      Signed-off-by: NKees Cook <keescook@chromium.org>
      3080ea55
  4. 13 10月, 2021 1 次提交
  5. 25 9月, 2021 1 次提交
    • K
      stddef: Introduce struct_group() helper macro · 50d7bd38
      Kees Cook 提交于
      Kernel code has a regular need to describe groups of members within a
      structure usually when they need to be copied or initialized separately
      from the rest of the surrounding structure. The generally accepted design
      pattern in C is to use a named sub-struct:
      
      	struct foo {
      		int one;
      		struct {
      			int two;
      			int three, four;
      		} thing;
      		int five;
      	};
      
      This would allow for traditional references and sizing:
      
      	memcpy(&dst.thing, &src.thing, sizeof(dst.thing));
      
      However, doing this would mean that referencing struct members enclosed
      by such named structs would always require including the sub-struct name
      in identifiers:
      
      	do_something(dst.thing.three);
      
      This has tended to be quite inflexible, especially when such groupings
      need to be added to established code which causes huge naming churn.
      Three workarounds exist in the kernel for this problem, and each have
      other negative properties.
      
      To avoid the naming churn, there is a design pattern of adding macro
      aliases for the named struct:
      
      	#define f_three thing.three
      
      This ends up polluting the global namespace, and makes it difficult to
      search for identifiers.
      
      Another common work-around in kernel code avoids the pollution by avoiding
      the named struct entirely, instead identifying the group's boundaries using
      either a pair of empty anonymous structs of a pair of zero-element arrays:
      
      	struct foo {
      		int one;
      		struct { } start;
      		int two;
      		int three, four;
      		struct { } finish;
      		int five;
      	};
      
      	struct foo {
      		int one;
      		int start[0];
      		int two;
      		int three, four;
      		int finish[0];
      		int five;
      	};
      
      This allows code to avoid needing to use a sub-struct named for member
      references within the surrounding structure, but loses the benefits of
      being able to actually use such a struct, making it rather fragile. Using
      these requires open-coded calculation of sizes and offsets. The efforts
      made to avoid common mistakes include lots of comments, or adding various
      BUILD_BUG_ON()s. Such code is left with no way for the compiler to reason
      about the boundaries (e.g. the "start" object looks like it's 0 bytes
      in length), making bounds checking depend on open-coded calculations:
      
      	if (length > offsetof(struct foo, finish) -
      		     offsetof(struct foo, start))
      		return -EINVAL;
      	memcpy(&dst.start, &src.start, offsetof(struct foo, finish) -
      				       offsetof(struct foo, start));
      
      However, the vast majority of places in the kernel that operate on
      groups of members do so without any identification of the grouping,
      relying either on comments or implicit knowledge of the struct contents,
      which is even harder for the compiler to reason about, and results in
      even more fragile manual sizing, usually depending on member locations
      outside of the region (e.g. to copy "two" and "three", use the start of
      "four" to find the size):
      
      	BUILD_BUG_ON((offsetof(struct foo, four) <
      		      offsetof(struct foo, two)) ||
      		     (offsetof(struct foo, four) <
      		      offsetof(struct foo, three));
      	if (length > offsetof(struct foo, four) -
      		     offsetof(struct foo, two))
      		return -EINVAL;
      	memcpy(&dst.two, &src.two, length);
      
      In order to have a regular programmatic way to describe a struct
      region that can be used for references and sizing, can be examined for
      bounds checking, avoids forcing the use of intermediate identifiers,
      and avoids polluting the global namespace, introduce the struct_group()
      macro. This macro wraps the member declarations to create an anonymous
      union of an anonymous struct (no intermediate name) and a named struct
      (for references and sizing):
      
      	struct foo {
      		int one;
      		struct_group(thing,
      			int two;
      			int three, four;
      		);
      		int five;
      	};
      
      	if (length > sizeof(src.thing))
      		return -EINVAL;
      	memcpy(&dst.thing, &src.thing, length);
      	do_something(dst.three);
      
      There are some rare cases where the resulting struct_group() needs
      attributes added, so struct_group_attr() is also introduced to allow
      for specifying struct attributes (e.g. __align(x) or __packed).
      Additionally, there are places where such declarations would like to
      have the struct be tagged, so struct_group_tagged() is added.
      
      Given there is a need for a handful of UAPI uses too, the underlying
      __struct_group() macro has been defined in UAPI so it can be used there
      too.
      
      To avoid confusing scripts/kernel-doc, hide the macro from its struct
      parsing.
      Co-developed-by: NKeith Packard <keithp@keithp.com>
      Signed-off-by: NKeith Packard <keithp@keithp.com>
      Acked-by: NGustavo A. R. Silva <gustavoars@kernel.org>
      Link: https://lore.kernel.org/lkml/20210728023217.GC35706@embeddedorEnhanced-by: NRasmus Villemoes <linux@rasmusvillemoes.dk>
      Link: https://lore.kernel.org/lkml/41183a98-bdb9-4ad6-7eab-5a7292a6df84@rasmusvillemoes.dkEnhanced-by: NDan Williams <dan.j.williams@intel.com>
      Link: https://lore.kernel.org/lkml/1d9a2e6df2a9a35b2cdd50a9a68cac5991e7e5f0.camel@intel.comEnhanced-by: NDaniel Vetter <daniel.vetter@ffwll.ch>
      Link: https://lore.kernel.org/lkml/YQKa76A6XuFqgM03@phenom.ffwll.localAcked-by: NDan Williams <dan.j.williams@intel.com>
      Signed-off-by: NKees Cook <keescook@chromium.org>
      50d7bd38
  6. 12 8月, 2021 1 次提交
  7. 18 5月, 2021 1 次提交
  8. 27 4月, 2021 1 次提交
  9. 16 4月, 2021 1 次提交
  10. 30 3月, 2021 1 次提交
  11. 27 3月, 2021 1 次提交
  12. 26 3月, 2021 1 次提交
  13. 09 3月, 2021 2 次提交
  14. 07 3月, 2021 1 次提交
  15. 23 2月, 2021 1 次提交
  16. 29 1月, 2021 1 次提交
  17. 19 1月, 2021 1 次提交
    • M
      scripts: kernel-doc: validate kernel-doc markup with the actual names · 52042e2d
      Mauro Carvalho Chehab 提交于
      Kernel-doc currently expects that the kernel-doc markup to come
      just before the function/enum/struct/union/typedef prototype.
      
      Yet, if it find things like:
      
      	/**
      	 * refcount_add - add a value to a refcount
      	 * @i: the value to add to the refcount
      	 * @r: the refcount
      	 */
      	static inline void __refcount_add(int i, refcount_t *r, int *oldp);
      	static inline void refcount_add(int i, refcount_t *r);
      
      Kernel-doc will do the wrong thing:
      
      	foobar.h:6: warning: Function parameter or member 'oldp' not described in '__refcount_add'
      	.. c:function:: void __refcount_add (int i, refcount_t *r, int *oldp)
      
      	   add a value to a refcount
      
      	**Parameters**
      
      	``int i``
      	  the value to add to the refcount
      
      	``refcount_t *r``
      	  the refcount
      
      	``int *oldp``
      	  *undescribed*
      
      Basically, it will document "__refcount_add" with the kernel-doc
      markup for refcount_add.
      
      If both functions have the same arguments, this won't even
      produce any warning!
      
      Add a logic to check if the kernel-doc identifier matches the actual
      name of the C function or data structure that will be documented.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      Link: https://lore.kernel.org/r/081546f141a496d6cabb99a4adc140444c705e93.1610610937.git.mchehab+huawei@kernel.orgSigned-off-by: NJonathan Corbet <corbet@lwn.net>
      52042e2d
  18. 04 12月, 2020 1 次提交
  19. 14 11月, 2020 1 次提交
  20. 29 10月, 2020 3 次提交
  21. 15 10月, 2020 9 次提交
    • M
      scripts: kernel-doc: try to use c:function if possible · 6e9e4158
      Mauro Carvalho Chehab 提交于
      There are a few namespace clashes by using c:macro everywhere:
      
      basically, when using it, we can't have something like:
      
      	.. c:struct:: pwm_capture
      
      	.. c:macro:: pwm_capture
      
      So, we need to use, instead:
      
      	.. c:function:: int pwm_capture (struct pwm_device * pwm, struct pwm_capture * result, unsigned long timeout)
      
      for the function declaration.
      
      The kernel-doc change was proposed by Jakob Lykke Andersen here:
      
      	https://github.com/jakobandersen/linux_docs/commit/6fd2076ec001cca7466857493cd678df4dfe4a65
      
      Although I did a different implementation.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      6e9e4158
    • M
      scripts: kernel-doc: fix line number handling · 5ef09c96
      Mauro Carvalho Chehab 提交于
      Address several issues related to pointing to the wrong line
      number:
      
      1) ensure that line numbers will always be initialized
      
         When section is the default (Description), the line number
         is not initializing, producing this:
      
      	$ ./scripts/kernel-doc --enable-lineno ./drivers/media/v4l2-core/v4l2-mem2mem.c|less
      
      	**Description**
      
      	#define LINENO 0
      	In case of streamoff or release called on any context,
      	1] If the context is currently running, then abort job will be called
      	2] If the context is queued, then the context will be removed from
      	   the job_queue
      
        Which is not right. Ensure that the line number will always
        be there. After applied, the result now points to the right location:
      
      	**Description**
      
      	#define LINENO 410
      	In case of streamoff or release called on any context,
      	1] If the context is currently running, then abort job will be called
      	2] If the context is queued, then the context will be removed from
      	   the job_queue
      
      2) The line numbers for function prototypes are always + 1,
         because it is taken at the line after handling the prototype.
         Change the logic to point to the next line after the /** */
         block;
      
      3) The "DOC:" line number should point to the same line as this
         markup is found, and not to the next one.
      
      Probably part of the issues were due to a but that was causing
      the line number offset to be incremented by one, if --export
      were used.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      5ef09c96
    • M
      scripts: kernel-doc: allow passing desired Sphinx C domain dialect · 93351d41
      Mauro Carvalho Chehab 提交于
      When kernel-doc is called via kerneldoc.py, there's no need to
      auto-detect the Sphinx version, as the Sphinx module already
      knows it. So, add an optional parameter to allow changing the
      Sphinx dialect.
      
      As kernel-doc can also be manually called, keep the auto-detection
      logic if the parameter was not specified. On such case, emit
      a warning if sphinx-build can't be found at PATH.
      
      I ended using a suggestion from Joe for using a more readable
      regex, instead of using a complex one with a hidden group like:
      
      	m/^(\d+)\.(\d+)(?:\.?(\d+)?)/
      
      in order to get the optional <patch> argument.
      
      Thanks-to: Joe Perches <joe@perches.com>
      Suggested-by: NJonathan Corbet <corbet@lwn.net>
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      93351d41
    • M
      scripts: kernel-doc: don't mangle with parameter list · ed8348e2
      Mauro Carvalho Chehab 提交于
      While kernel-doc needs to parse parameters in order to
      identify its name, it shouldn't be touching the type,
      as parsing it is very difficult, and errors happen.
      
      One current error is when parsing this parameter:
      
      	const u32 (*tab)[256]
      
      Found at ./lib/crc32.c, on this function:
      
      	u32 __pure crc32_be_generic (u32 crc, unsigned char const *p, size_t len, const u32 (*tab)[256], u32 polynomial);
      
      The current logic mangles it, producing this output:
      
      	const u32 ( *tab
      
      That's something that it is not recognizeable.
      
      So, instead, let's push the argument as-is, and use it
      when printing the function prototype and when describing
      each argument.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      ed8348e2
    • M
      scripts: kernel-doc: fix typedef identification · 47bcacfd
      Mauro Carvalho Chehab 提交于
      Some typedef expressions are output as normal functions.
      
      As we need to be clearer about the type with Sphinx 3.x,
      detect such cases.
      
      While here, fix a wrongly-indented block.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      47bcacfd
    • M
      scripts: kernel-doc: reimplement -nofunction argument · eab795dd
      Mauro Carvalho Chehab 提交于
      Right now, the build system doesn't use -nofunction, as
      it is pretty much useless, because it doesn't consider
      the other output modes (extern, internal), working only
      with all.
      
      Also, it is limited to exclude functions.
      
      Re-implement it in order to allow excluding any symbols from
      the document output, no matter what mode is used.
      
      The parameter was also renamed to "-nosymbol", as it express
      better its meaning.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      eab795dd
    • M
      scripts: kernel-doc: fix troubles with line counts · dbe8ba00
      Mauro Carvalho Chehab 提交于
      There's currently a bug with the way kernel-doc script
      counts line numbers that can be seen with:
      
      	$ ./scripts/kernel-doc -rst  -enable-lineno include/linux/math64.h >all && ./scripts/kernel-doc -rst -internal -enable-lineno include/linux/math64.h >int && diff -U0 int all
      
      	--- int	2020-09-28 12:58:08.927486808 +0200
      	+++ all	2020-09-28 12:58:08.905486845 +0200
      	@@ -1 +1 @@
      	-#define LINENO 27
      	+#define LINENO 26
      	@@ -3 +3 @@
      	-#define LINENO 16
      	+#define LINENO 15
      	@@ -9 +9 @@
      	-#define LINENO 17
      	+#define LINENO 16
      	...
      
      This is happening with perl version 5.30.3, but I'm not
      so sure if this is a perl bug, or if this is due to something
      else.
      
      In any case, fixing it is easy. Basically, when "-internal"
      parameter is used, the process_export_file() function opens the
      handle "IN". This makes the line number to be incremented, as the
      handler for the main open is also "IN".
      
      Fix the problem by using a different handler for the
      main open().
      
      While here, add a missing close for it.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      dbe8ba00
    • M
      scripts: kernel-doc: use a less pedantic markup for funcs on Sphinx 3.x · e3ad05fe
      Mauro Carvalho Chehab 提交于
      Unfortunately, Sphinx 3.x parser for c functions is too pedantic:
      
      	https://github.com/sphinx-doc/sphinx/issues/8241
      
      While it could be relaxed with some configurations, there are
      several corner cases that it would make it hard to maintain,
      and will require teaching conf.py about several macros.
      
      So, let's instead use the :c:macro notation. This will
      produce an output that it is not as nice as currently, but it
      should still be acceptable, and will provide cross-references,
      removing thousands of warnings when building with newer
      versions of Sphinx.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      e3ad05fe
    • M
      scripts: kernel-doc: make it more compatible with Sphinx 3.x · efa44475
      Mauro Carvalho Chehab 提交于
      With Sphinx 3.x, the ".. c:type:" tag was changed to accept either:
      
      	.. c:type:: typedef-like declaration
      	.. c:type:: name
      
      Using it for other types (including functions) don't work anymore.
      
      So, there are newer tags for macro, enum, struct, union, and others,
      which doesn't exist on older versions.
      
      Add a check for the Sphinx version and change the produced tags
      accordingly.
      Signed-off-by: NMauro Carvalho Chehab <mchehab+huawei@kernel.org>
      efa44475