[RFC,1/3] init.h: Add __init_str / __exit_str macros
diff mbox series

Message ID 1403477209-14612-2-git-send-email-minipli@googlemail.com
State New, archived
Headers show
Series
  • Mark literal strings in __init / __exit code
Related show

Commit Message

Mathias Krause June 22, 2014, 10:46 p.m. UTC
Add macros to be able to mark string literals used in __init / __exit
functions.

Signed-off-by: Mathias Krause <minipli@googlemail.com>
---
 include/linux/init.h |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Joe Perches June 24, 2014, 7:43 p.m. UTC | #1
On Mon, 2014-06-23 at 00:46 +0200, Mathias Krause wrote:
> Add macros to be able to mark string literals used in __init / __exit
> functions.
[]
> diff --git a/include/linux/init.h b/include/linux/init.h
[]
> +#define __init_str(str)	__mark_str(str, __UNIQUE_ID(_init_str_), __initconst)
> +#define __exit_str(str)	__mark_str(str, __UNIQUE_ID(_exit_str_), __exitdata)
> +#define __mark_str(str, var, __section) \
> +	({ static const char var[] __section __aligned(1) = str; var; })
> +

You probably want to make these strings vanish
completely when !CONFIG_PRINTK.

As is, they will always exist in the image.


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
Mathias Krause June 24, 2014, 8:13 p.m. UTC | #2
On 24 June 2014 21:43, Joe Perches <joe@perches.com> wrote:
> On Mon, 2014-06-23 at 00:46 +0200, Mathias Krause wrote:
>> Add macros to be able to mark string literals used in __init / __exit
>> functions.
> []
>> diff --git a/include/linux/init.h b/include/linux/init.h
> []
>> +#define __init_str(str)      __mark_str(str, __UNIQUE_ID(_init_str_), __initconst)
>> +#define __exit_str(str)      __mark_str(str, __UNIQUE_ID(_exit_str_), __exitdata)
>> +#define __mark_str(str, var, __section) \
>> +     ({ static const char var[] __section __aligned(1) = str; var; })
>> +
>
> You probably want to make these strings vanish
> completely when !CONFIG_PRINTK.
>
> As is, they will always exist in the image.

They will not. They are vanished as printk() is an empty static inline
function for the !CONFIG_PRINTK case. gcc is clever enough to optimize
the variables away in this case.

Thanks,
Mathias
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Patch
diff mbox series

diff --git a/include/linux/init.h b/include/linux/init.h
index 2df8e8dd10..0a425b296e 100644
--- a/include/linux/init.h
+++ b/include/linux/init.h
@@ -35,6 +35,18 @@ 
  * Don't forget to initialize data not at file scope, i.e. within a function,
  * as gcc otherwise puts the data into the bss section and not into the init
  * section.
+ *
+ * For messages printed in __init / __exit functions the __init_str() /
+ * __exit_str() macros will take care of marking the strings accordingly so
+ * they can be freed, too. Otherwise the strings would resist in memory, even
+ * though they are no longer referenced.
+ *
+ * Use them like this:
+ *
+ * static void __init initme(void)
+ * {
+ *    printk(__init_str(KERN_INFO "I am fully initialized, now\n"));
+ * }
  */
 
 /* These are for everybody (although not all archs will actually
@@ -45,6 +57,12 @@ 
 #define __exitdata	__section(.exit.data)
 #define __exit_call	__used __section(.exitcall.exit)
 
+/* Those can be used to mark strings used in __init / __exit functions. */
+#define __init_str(str)	__mark_str(str, __UNIQUE_ID(_init_str_), __initconst)
+#define __exit_str(str)	__mark_str(str, __UNIQUE_ID(_exit_str_), __exitdata)
+#define __mark_str(str, var, __section) \
+	({ static const char var[] __section __aligned(1) = str; var; })
+
 /*
  * Some architecture have tool chains which do not handle rodata attributes
  * correctly. For those disable special sections for const, so that other