From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 7/8] drivers: maintain const when converting from struct driver
Date: Tue, 6 May 2025 08:34:45 +0200 [thread overview]
Message-ID: <20250506063446.3133582-7-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250506063446.3133582-1-a.fatoum@pengutronix.de>
In preparation for making the struct driver parameter to the match
callbacks const, introduce container_of_const and make use of it instead
of normal container in upcasting macros.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/acpi.h | 5 +----
include/efi/efi-device.h | 5 +----
include/linux/container_of.h | 14 ++++++++++++++
include/linux/virtio.h | 10 ++--------
4 files changed, 18 insertions(+), 16 deletions(-)
diff --git a/include/acpi.h b/include/acpi.h
index fc0da30610a6..fa94d972e3fe 100644
--- a/include/acpi.h
+++ b/include/acpi.h
@@ -125,10 +125,7 @@ struct acpi_driver {
extern struct bus_type acpi_bus;
-static inline struct acpi_driver *to_acpi_driver(struct driver *drv)
-{
- return container_of(drv, struct acpi_driver, driver);
-}
+#define to_acpi_driver(drv) container_of_const((drv), struct acpi_driver, driver)
#define device_acpi_driver(drv) \
register_efi_driver_macro(device, acpi, drv)
diff --git a/include/efi/efi-device.h b/include/efi/efi-device.h
index 5d2110356fd4..a8fc99a0e12b 100644
--- a/include/efi/efi-device.h
+++ b/include/efi/efi-device.h
@@ -33,10 +33,7 @@ static inline struct efi_device *to_efi_device(struct device *dev)
return container_of(dev, struct efi_device, dev);
}
-static inline struct efi_driver *to_efi_driver(struct driver *drv)
-{
- return container_of(drv, struct efi_driver, driver);
-}
+#define to_efi_driver(drv) container_of_const((drv), struct efi_driver, driver)
static inline int efi_driver_register(struct efi_driver *efidrv)
{
diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 2f4944b791b8..8669ef0154d9 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -21,6 +21,20 @@
"pointer type mismatch in container_of()"); \
((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
+ * @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.
+ */
+#define container_of_const(ptr, type, member) \
+ _Generic(ptr, \
+ const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
+ default: ((type *)container_of(ptr, type, member)) \
+ )
+
/**
* container_of_safe - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 3d4c88336055..eb4293003294 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -43,10 +43,7 @@ struct virtio_device {
u32 status_param;
};
-static inline struct virtio_device *dev_to_virtio(struct device *_dev)
-{
- return container_of(_dev, struct virtio_device, dev);
-}
+#define dev_to_virtio(_dev) container_of_const(_dev, struct virtio_device, dev)
void virtio_add_status(struct virtio_device *dev, unsigned int status);
int register_virtio_device(struct virtio_device *dev);
@@ -96,10 +93,7 @@ struct virtio_driver {
void (*config_changed)(struct virtio_device *dev);
};
-static inline struct virtio_driver *drv_to_virtio(struct driver *drv)
-{
- return container_of(drv, struct virtio_driver, driver);
-}
+#define drv_to_virtio(__drv) container_of_const(__drv, struct virtio_driver, driver)
int virtio_driver_register(struct virtio_driver *drv);
--
2.39.5
next prev parent reply other threads:[~2025-05-06 6:38 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-06 6:34 [PATCH 1/8] treewide: replace dev_get_drvdata with device_get_match_data Ahmad Fatoum
2025-05-06 6:34 ` [PATCH 2/8] ARM: i.MX: esdctl: " Ahmad Fatoum
2025-05-06 6:34 ` [PATCH 3/8] driver: switch dev_get_drvdata to Linux semantics Ahmad Fatoum
2025-05-06 6:34 ` [PATCH 4/8] gpio: gpio-mxs: replace dev_get_drvdata with device_get_match_data Ahmad Fatoum
2025-05-06 7:42 ` Sascha Hauer
2025-05-06 6:34 ` [PATCH 5/8] mci: am654-sdhci: fix error code printed in error messages Ahmad Fatoum
2025-05-06 6:34 ` [PATCH 6/8] pinctrl: at91: replace dev_get_drvdata with device_get_match_data Ahmad Fatoum
2025-05-06 6:34 ` Ahmad Fatoum [this message]
2025-05-06 6:34 ` [PATCH 8/8] driver: base: invert driver match callback for Linux compatibility Ahmad Fatoum
2025-05-06 7:41 ` [PATCH 1/8] treewide: replace dev_get_drvdata with device_get_match_data Sascha Hauer
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=20250506063446.3133582-7-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.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