mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xiaofeng Lian <1198715581lxf@gmail.com>
To: lkp@intel.com
Cc: linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
	oe-kbuild-all@lists.linux.dev, stefani@seibold.net,
	Xiaofeng Lian <1198715581lxf@gmail.com>
Subject: [PATCH v3 1/2] include/linux/kfifo.h: fix some IDEs Intelligence errors
Date: Sat,  5 Oct 2024 21:52:49 +0800	[thread overview]
Message-ID: <20241005135250.121964-1-1198715581lxf@gmail.com> (raw)
In-Reply-To: <202410051712.VDmFpiVZ-lkp@intel.com>

  - In some IDEs, when using the ARRAY_SIZE macro to calculate the
    buf size of kfifo, an error occurs (using incomplete types),
    which belongs to the compiler INTELLIGENCE reporting error,
    and can be compiled normally, specifically due to the gcc
    compiler's __builtin_types_compatible_p function. Because the
    KFIFO_PTR type uses a zero-length array and the macro definition
    is a simple substitution, the ARRAY_SIZE macro will report
    an error because of the zero-length array when calculating
    __kfifo_esize in the INIT_KFIFO macro. Notice that the difference
    between KFIFO and KFIFO_PTR is only whether or not the buf is inlined,
    so (sizeof(KFIFO) - sizeof(KFIFO_PTR)) / sizeof(type_of_kfifo_member)
    will give you the size of the buf of the kfifo, which bypasses the
    compiler! Intelligence and bypasses the possibility that
    ARRAY_SIZE may be passed in as a pointer, resulting in a compilation
    error.

Signed-off-by: Xiaofeng Lian <1198715581lxf@gmail.com>
---
 include/linux/kfifo.h | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/include/linux/kfifo.h b/include/linux/kfifo.h
index 564868bdce89..66eb1b8971f7 100644
--- a/include/linux/kfifo.h
+++ b/include/linux/kfifo.h
@@ -123,6 +123,21 @@ struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
  */
 #define DECLARE_KFIFO(fifo, type, size)	STRUCT_KFIFO(type, size) fifo
 
+/**
+ * get_kfifo_data_type - macro to get type of kfifo's member
+ * @fifo: pointer of kfifo
+ */
+#define get_kfifo_data_type(fifo) typeof(*(fifo)->type)
+
+/**
+ * __KFIFO_SIZE - macro to calculate kfifo's buffer size
+ * @fifo: pointer of kfifo
+ */
+#define __KFIFO_SIZE(fifo)\
+	({\
+		DECLARE_KFIFO_PTR(__tmp_kfifo, get_kfifo_data_type(fifo));\
+		(sizeof(*(fifo)) - sizeof(__tmp_kfifo)) / sizeof(get_kfifo_data_type(fifo));\
+	 })
 /**
  * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
  * @fifo: name of the declared fifo datatype
@@ -133,7 +148,7 @@ struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
 	struct __kfifo *__kfifo = &__tmp->kfifo; \
 	__kfifo->in = 0; \
 	__kfifo->out = 0; \
-	__kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
+	__kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : __KFIFO_SIZE(__tmp->buf) - 1;\
 	__kfifo->esize = sizeof(*__tmp->buf); \
 	__kfifo->data = __is_kfifo_ptr(__tmp) ?  NULL : __tmp->buf; \
 })
-- 
2.45.2


  reply	other threads:[~2024-10-05 13:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-02 17:24 [PATCH v2] include/linux/kfifo.h: fix some IDEs Intelligence errors and the previous patch was misnamed Xiaofeng Lian
2024-10-05 10:00 ` kernel test robot
2024-10-05 13:52   ` Xiaofeng Lian [this message]
2024-10-05 13:52     ` [PATCH v3 2/2] include/linux/kfifo.h:fix errors Xiaofeng Lian
2024-10-08 10:26 ` [PATCH v2] include/linux/kfifo.h: fix some IDEs Intelligence errors and the previous patch was misnamed kernel test robot
2024-10-08 11:48 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241005135250.121964-1-1198715581lxf@gmail.com \
    --to=1198715581lxf@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=llvm@lists.linux.dev \
    --cc=oe-kbuild-all@lists.linux.dev \
    --cc=stefani@seibold.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®