[PATCH] dm: add helper macro for device mapper target driver boilerplate

From: Yangtao Li
Date: Sun Apr 09 2023 - 12:44:03 EST


For simple modules that contain a single device mapper target driver
without any additional setup code then ends up being a block of duplicated
boilerplate. This patch adds a new macro, module_dm(), which replaces
the module_init()/module_exit() registrations with template functions.

Signed-off-by: Yangtao Li <frank.li@xxxxxxxx>
---
drivers/md/dm-crypt.c | 14 +-------------
drivers/md/dm-delay.c | 15 +--------------
drivers/md/dm-dust.c | 14 +-------------
drivers/md/dm-ebs-target.c | 14 +-------------
drivers/md/dm-era-target.c | 14 +-------------
drivers/md/dm-flakey.c | 15 +--------------
drivers/md/dm-log-writes.c | 14 +-------------
drivers/md/dm-raid.c | 18 +-----------------
drivers/md/dm-switch.c | 14 +-------------
drivers/md/dm-target.c | 3 +++
drivers/md/dm-unstripe.c | 14 +-------------
drivers/md/dm-verity-target.c | 14 +-------------
drivers/md/dm-writecache.c | 14 +-------------
drivers/md/dm-zero.c | 14 +-------------
drivers/md/dm-zoned-target.c | 16 ++--------------
include/linux/device-mapper.h | 20 ++++++++++++++++++++
16 files changed, 38 insertions(+), 189 deletions(-)

diff --git a/drivers/md/dm-crypt.c b/drivers/md/dm-crypt.c
index 52615a258e13..8b47b913ee83 100644
--- a/drivers/md/dm-crypt.c
+++ b/drivers/md/dm-crypt.c
@@ -3659,19 +3659,7 @@ static struct target_type crypt_target = {
.iterate_devices = crypt_iterate_devices,
.io_hints = crypt_io_hints,
};
-
-static int __init dm_crypt_init(void)
-{
- return dm_register_target(&crypt_target);
-}
-
-static void __exit dm_crypt_exit(void)
-{
- dm_unregister_target(&crypt_target);
-}
-
-module_init(dm_crypt_init);
-module_exit(dm_crypt_exit);
+module_dm(crypt);

MODULE_AUTHOR("Jana Saout <jana@xxxxxxxx>");
MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
diff --git a/drivers/md/dm-delay.c b/drivers/md/dm-delay.c
index 00d18b2070a5..7433525e5985 100644
--- a/drivers/md/dm-delay.c
+++ b/drivers/md/dm-delay.c
@@ -367,20 +367,7 @@ static struct target_type delay_target = {
.status = delay_status,
.iterate_devices = delay_iterate_devices,
};
-
-static int __init dm_delay_init(void)
-{
- return dm_register_target(&delay_target);
-}
-
-static void __exit dm_delay_exit(void)
-{
- dm_unregister_target(&delay_target);
-}
-
-/* Module hooks */
-module_init(dm_delay_init);
-module_exit(dm_delay_exit);
+module_dm(delay);

MODULE_DESCRIPTION(DM_NAME " delay target");
MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@xxxxxxxxxx>");
diff --git a/drivers/md/dm-dust.c b/drivers/md/dm-dust.c
index 9bf3cdf548de..12a377e06d02 100644
--- a/drivers/md/dm-dust.c
+++ b/drivers/md/dm-dust.c
@@ -570,19 +570,7 @@ static struct target_type dust_target = {
.status = dust_status,
.prepare_ioctl = dust_prepare_ioctl,
};
-
-static int __init dm_dust_init(void)
-{
- return dm_register_target(&dust_target);
-}
-
-static void __exit dm_dust_exit(void)
-{
- dm_unregister_target(&dust_target);
-}
-
-module_init(dm_dust_init);
-module_exit(dm_dust_exit);
+module_dm(dust);

MODULE_DESCRIPTION(DM_NAME " dust test target");
MODULE_AUTHOR("Bryan Gurney <dm-devel@xxxxxxxxxx>");
diff --git a/drivers/md/dm-ebs-target.c b/drivers/md/dm-ebs-target.c
index 38da4de3ecbf..435b45201f4d 100644
--- a/drivers/md/dm-ebs-target.c
+++ b/drivers/md/dm-ebs-target.c
@@ -452,19 +452,7 @@ static struct target_type ebs_target = {
.prepare_ioctl = ebs_prepare_ioctl,
.iterate_devices = ebs_iterate_devices,
};
-
-static int __init dm_ebs_init(void)
-{
- return dm_register_target(&ebs_target);
-}
-
-static void dm_ebs_exit(void)
-{
- dm_unregister_target(&ebs_target);
-}
-
-module_init(dm_ebs_init);
-module_exit(dm_ebs_exit);
+module_dm(ebs);

MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@xxxxxxxxxx>");
MODULE_DESCRIPTION(DM_NAME " emulated block size target");
diff --git a/drivers/md/dm-era-target.c b/drivers/md/dm-era-target.c
index 554d234fca18..0d70914217ee 100644
--- a/drivers/md/dm-era-target.c
+++ b/drivers/md/dm-era-target.c
@@ -1753,19 +1753,7 @@ static struct target_type era_target = {
.iterate_devices = era_iterate_devices,
.io_hints = era_io_hints
};
-
-static int __init dm_era_init(void)
-{
- return dm_register_target(&era_target);
-}
-
-static void __exit dm_era_exit(void)
-{
- dm_unregister_target(&era_target);
-}
-
-module_init(dm_era_init);
-module_exit(dm_era_exit);
+module_dm(era);

MODULE_DESCRIPTION(DM_NAME " era target");
MODULE_AUTHOR("Joe Thornber <ejt@xxxxxxxxxx>");
diff --git a/drivers/md/dm-flakey.c b/drivers/md/dm-flakey.c
index 14179355e6a1..ebcfb99b186b 100644
--- a/drivers/md/dm-flakey.c
+++ b/drivers/md/dm-flakey.c
@@ -506,20 +506,7 @@ static struct target_type flakey_target = {
.prepare_ioctl = flakey_prepare_ioctl,
.iterate_devices = flakey_iterate_devices,
};
-
-static int __init dm_flakey_init(void)
-{
- return dm_register_target(&flakey_target);
-}
-
-static void __exit dm_flakey_exit(void)
-{
- dm_unregister_target(&flakey_target);
-}
-
-/* Module hooks */
-module_init(dm_flakey_init);
-module_exit(dm_flakey_exit);
+module_dm(flakey);

MODULE_DESCRIPTION(DM_NAME " flakey target");
MODULE_AUTHOR("Joe Thornber <dm-devel@xxxxxxxxxx>");
diff --git a/drivers/md/dm-log-writes.c b/drivers/md/dm-log-writes.c
index 6d7436d2fd7f..f17a6cf2284e 100644
--- a/drivers/md/dm-log-writes.c
+++ b/drivers/md/dm-log-writes.c
@@ -937,19 +937,7 @@ static struct target_type log_writes_target = {
.dax_zero_page_range = log_writes_dax_zero_page_range,
.dax_recovery_write = log_writes_dax_recovery_write,
};
-
-static int __init dm_log_writes_init(void)
-{
- return dm_register_target(&log_writes_target);
-}
-
-static void __exit dm_log_writes_exit(void)
-{
- dm_unregister_target(&log_writes_target);
-}
-
-module_init(dm_log_writes_init);
-module_exit(dm_log_writes_exit);
+module_dm(log_writes);

MODULE_DESCRIPTION(DM_NAME " log writes target");
MODULE_AUTHOR("Josef Bacik <jbacik@xxxxxx>");
diff --git a/drivers/md/dm-raid.c b/drivers/md/dm-raid.c
index 2dfd51509647..c8821fcb8299 100644
--- a/drivers/md/dm-raid.c
+++ b/drivers/md/dm-raid.c
@@ -4077,23 +4077,7 @@ static struct target_type raid_target = {
.preresume = raid_preresume,
.resume = raid_resume,
};
-
-static int __init dm_raid_init(void)
-{
- DMINFO("Loading target version %u.%u.%u",
- raid_target.version[0],
- raid_target.version[1],
- raid_target.version[2]);
- return dm_register_target(&raid_target);
-}
-
-static void __exit dm_raid_exit(void)
-{
- dm_unregister_target(&raid_target);
-}
-
-module_init(dm_raid_init);
-module_exit(dm_raid_exit);
+module_dm(raid);

module_param(devices_handle_discard_safely, bool, 0644);
MODULE_PARM_DESC(devices_handle_discard_safely,
diff --git a/drivers/md/dm-switch.c b/drivers/md/dm-switch.c
index 5a5976b0cfb8..dfd9fb52a6f3 100644
--- a/drivers/md/dm-switch.c
+++ b/drivers/md/dm-switch.c
@@ -565,19 +565,7 @@ static struct target_type switch_target = {
.prepare_ioctl = switch_prepare_ioctl,
.iterate_devices = switch_iterate_devices,
};
-
-static int __init dm_switch_init(void)
-{
- return dm_register_target(&switch_target);
-}
-
-static void __exit dm_switch_exit(void)
-{
- dm_unregister_target(&switch_target);
-}
-
-module_init(dm_switch_init);
-module_exit(dm_switch_exit);
+module_dm(switch);

MODULE_DESCRIPTION(DM_NAME " dynamic path switching target");
MODULE_AUTHOR("Kevin D. O'Kelley <Kevin_OKelley@xxxxxxxx>");
diff --git a/drivers/md/dm-target.c b/drivers/md/dm-target.c
index 9e9871361440..a6381cf376c1 100644
--- a/drivers/md/dm-target.c
+++ b/drivers/md/dm-target.c
@@ -84,6 +84,9 @@ int dm_register_target(struct target_type *tt)
{
int rv = 0;

+ DMINFO("Loading '%s' target version %u.%u.%u",
+ tt->name, tt->version[0], tt->version[1], tt->version[2]);
+
down_write(&_lock);
if (__find_target_type(tt->name)) {
DMERR("%s: cannot find '%s' target",
diff --git a/drivers/md/dm-unstripe.c b/drivers/md/dm-unstripe.c
index e7b7d5983a16..48587c16c445 100644
--- a/drivers/md/dm-unstripe.c
+++ b/drivers/md/dm-unstripe.c
@@ -192,19 +192,7 @@ static struct target_type unstripe_target = {
.iterate_devices = unstripe_iterate_devices,
.io_hints = unstripe_io_hints,
};
-
-static int __init dm_unstripe_init(void)
-{
- return dm_register_target(&unstripe_target);
-}
-
-static void __exit dm_unstripe_exit(void)
-{
- dm_unregister_target(&unstripe_target);
-}
-
-module_init(dm_unstripe_init);
-module_exit(dm_unstripe_exit);
+module_dm(unstripe);

MODULE_DESCRIPTION(DM_NAME " unstriped target");
MODULE_ALIAS("dm-unstriped");
diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
index 260f04d12982..e35c16e06d06 100644
--- a/drivers/md/dm-verity-target.c
+++ b/drivers/md/dm-verity-target.c
@@ -1514,19 +1514,7 @@ static struct target_type verity_target = {
.iterate_devices = verity_iterate_devices,
.io_hints = verity_io_hints,
};
-
-static int __init dm_verity_init(void)
-{
- return dm_register_target(&verity_target);
-}
-
-static void __exit dm_verity_exit(void)
-{
- dm_unregister_target(&verity_target);
-}
-
-module_init(dm_verity_init);
-module_exit(dm_verity_exit);
+module_dm(verity);

MODULE_AUTHOR("Mikulas Patocka <mpatocka@xxxxxxxxxx>");
MODULE_AUTHOR("Mandeep Baines <msb@xxxxxxxxxxxx>");
diff --git a/drivers/md/dm-writecache.c b/drivers/md/dm-writecache.c
index 81b60b75a9fa..074cb785eafc 100644
--- a/drivers/md/dm-writecache.c
+++ b/drivers/md/dm-writecache.c
@@ -2773,19 +2773,7 @@ static struct target_type writecache_target = {
.iterate_devices = writecache_iterate_devices,
.io_hints = writecache_io_hints,
};
-
-static int __init dm_writecache_init(void)
-{
- return dm_register_target(&writecache_target);
-}
-
-static void __exit dm_writecache_exit(void)
-{
- dm_unregister_target(&writecache_target);
-}
-
-module_init(dm_writecache_init);
-module_exit(dm_writecache_exit);
+module_dm(writecache);

MODULE_DESCRIPTION(DM_NAME " writecache target");
MODULE_AUTHOR("Mikulas Patocka <dm-devel@xxxxxxxxxx>");
diff --git a/drivers/md/dm-zero.c b/drivers/md/dm-zero.c
index 884ac726a743..3b13e6eb1aa4 100644
--- a/drivers/md/dm-zero.c
+++ b/drivers/md/dm-zero.c
@@ -75,19 +75,7 @@ static struct target_type zero_target = {
.map = zero_map,
.io_hints = zero_io_hints,
};
-
-static int __init dm_zero_init(void)
-{
- return dm_register_target(&zero_target);
-}
-
-static void __exit dm_zero_exit(void)
-{
- dm_unregister_target(&zero_target);
-}
-
-module_init(dm_zero_init)
-module_exit(dm_zero_exit)
+module_dm(zero);

MODULE_AUTHOR("Jana Saout <jana@xxxxxxxx>");
MODULE_DESCRIPTION(DM_NAME " dummy target returning zeros");
diff --git a/drivers/md/dm-zoned-target.c b/drivers/md/dm-zoned-target.c
index ad4764dcd013..ad8e670a2f9b 100644
--- a/drivers/md/dm-zoned-target.c
+++ b/drivers/md/dm-zoned-target.c
@@ -1138,7 +1138,7 @@ static int dmz_message(struct dm_target *ti, unsigned int argc, char **argv,
return r;
}

-static struct target_type dmz_type = {
+static struct target_type zoned_target = {
.name = "zoned",
.version = {2, 0, 0},
.features = DM_TARGET_SINGLETON | DM_TARGET_MIXED_ZONED_MODEL,
@@ -1154,19 +1154,7 @@ static struct target_type dmz_type = {
.status = dmz_status,
.message = dmz_message,
};
-
-static int __init dmz_init(void)
-{
- return dm_register_target(&dmz_type);
-}
-
-static void __exit dmz_exit(void)
-{
- dm_unregister_target(&dmz_type);
-}
-
-module_init(dmz_init);
-module_exit(dmz_exit);
+module_dm(zoned);

MODULE_DESCRIPTION(DM_NAME " target for zoned block devices");
MODULE_AUTHOR("Damien Le Moal <damien.lemoal@xxxxxxx>");
diff --git a/include/linux/device-mapper.h b/include/linux/device-mapper.h
index 8aa6b3ea91fa..9d79ce428b62 100644
--- a/include/linux/device-mapper.h
+++ b/include/linux/device-mapper.h
@@ -631,6 +631,26 @@ void dm_destroy_crypto_profile(struct blk_crypto_profile *profile);
DMEMIT("target_name=%s,target_version=%u.%u.%u", \
(y)->name, (y)->version[0], (y)->version[1], (y)->version[2])

+/**
+ * module_dm() - Helper macro for drivers that don't do anything
+ * special in module init/exit. This eliminates a lot of boilerplate.
+ * Each module may only use this macro once, and calling it replaces
+ * module_init() and module_exit().
+ *
+ * @name: device mapper name
+ */
+#define module_dm(name) \
+static int __init dm_##name##_init(void) \
+{ \
+ return dm_register_target(&(name##_target)); \
+} \
+module_init(dm_##name##_init) \
+static void __exit dm_##name##_exit(void) \
+{ \
+ dm_unregister_target(&(name##_target)); \
+} \
+module_exit(dm_##name##_exit)
+
/*
* Definitions of return values from target end_io function.
*/
--
2.35.1