mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
To: Greg KH <gregkh@linuxfoundation.org>,
	Dan Carpenter <dan.carpenter@linaro.org>
Cc: linux-kernel@vger.kernel.org,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-hardening@vger.kernel.org
Subject: [PATCH][next] container_of: add container_first() macro
Date: Fri, 20 Sep 2024 14:50:02 +0200	[thread overview]
Message-ID: <Zu1vekikKNR5oUoM@elsanto> (raw)

This is like container_of() but it has an assert to ensure that it's
using the first struct member.

Co-developed-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 include/linux/container_of.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 713890c867be..928c5865c1b4 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -22,6 +22,24 @@
 		      "pointer type mismatch in container_of()");	\
 	((type *)(__mptr - offsetof(type, member))); })
 
+/**
+ * container_first - cast first member of a structure out to the containing
+ *		     structure
+ * @ptr:	     the pointer to the member.
+ * @type:	     the type of the container struct this is embedded in.
+ * @member:	     the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.
+ */
+#define container_first(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);                                   \
+	static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
+		      __same_type(*(ptr), void),                        \
+		      "pointer type mismatch in container_of()");       \
+	static_assert(offsetof(type, member) == 0, "not first member");	\
+	((type *)(__mptr - offsetof(type, member))); })
+
+
 /**
  * container_of_const - cast a member of a structure out to the containing
  *			structure and preserve the const-ness of the pointer
-- 
2.34.1


             reply	other threads:[~2024-09-20 12:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-20 12:50 Gustavo A. R. Silva [this message]
2024-09-21  5:45 ` Dan Carpenter
2024-09-21  5:52   ` Greg KH
2024-09-21  7:10     ` Gustavo A. R. Silva

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=Zu1vekikKNR5oUoM@elsanto \
    --to=gustavoars@kernel.org \
    --cc=dan.carpenter@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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®