Skip to contentSkip to conversation

Input: amijoy - mark amijoy_phys as __initconst

PATCH In review Thorsten Blum 2 participants 3 messages Nov 7

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: amijoy - mark amijoy_phys as __initconst

amijoy_phys is only read by the __init function amijoy_init().

Also declare amijoy_phys as const.

---
 drivers/input/joystick/amijoy.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/input/joystick/amijoy.c +4 -1
index 3752dc2a2086..cdb2b8f15348 100644
@@ -30,7 +30,10 @@ MODULE_PARM_DESC(map, "Map of attached joysticks in form of <a>,<b> (default is
 static int amijoy_used;
 static DEFINE_MUTEX(amijoy_mutex);
 static struct input_dev *amijoy_dev[2];
-static char *amijoy_phys[2] = { "amijoy/input0", "amijoy/input1" };
+static const char * const amijoy_phys[2] __initconst = {
+	"amijoy/input0",
+	"amijoy/input1",
+};
 
 static irqreturn_t amijoy_interrupt(int irq, void *dummy)
 {
maintainer
#1 lore raw

Hi Thorsten,

Quoted 3 lines · Thorsten Blum

amijoy_phys is only read by the __init function amijoy_init().

Also declare amijoy_phys as const.

...

drivers/input/joystick/amijoy.c MODULE_PARM_DESC(map, "Map of attached joysticks in form of <a>,<b> (default is · line 36

Quoted 5 lines
drivers/input/joystick/amijoy.c +4 -1
-static char *amijoy_phys[2] = { "amijoy/input0", "amijoy/input1" };
+static const char * const amijoy_phys[2] __initconst = {
+	"amijoy/input0",
+	"amijoy/input1",
+};

While this technically works because __initconst only places the pointer array itself into .init.rodata and leaves the string literals in .rodata, I find it too subtle and fragile. input_register_device() retains the dev->phys pointer for the entire lifetime of the input device, so if anyone later turns amijoy_phys into a 2D char array to avoid pointer indirection, the strings themselves will move into .init.rodata and dev->phys will become a dangling pointer after init (with no warning from modpost since the reference is inside amijoy_init()).

Could we either just mark amijoy_phys as const without __initconst (it is only 8 bytes on m68k), or drop amijoy_phys altogether and do:

	amijoy_dev[i]->phys = i ? "amijoy/input1" : "amijoy/input0";

in amijoy_init() (which stays in .init.text anyway)?

Thanks.

Signature · 1 line
Dmitry
#2 lore raw

Hi Dmitry,

Quoted 7 lines · Dmitry Torokhov

Hi Thorsten,

Quoted 3 lines · Thorsten Blum

amijoy_phys is only read by the __init function amijoy_init().

Also declare amijoy_phys as const.

...

Quoted 14 lines

drivers/input/joystick/amijoy.c MODULE_PARM_DESC(map, "Map of attached joysticks in form of <a>,<b> (default is · line 36

Quoted 5 lines
drivers/input/joystick/amijoy.c +4 -1
-static char *amijoy_phys[2] = { "amijoy/input0", "amijoy/input1" };
+static const char * const amijoy_phys[2] __initconst = {
+	"amijoy/input0",
+	"amijoy/input1",
+};

While this technically works because __initconst only places the pointer array itself into .init.rodata and leaves the string literals in .rodata, I find it too subtle and fragile. input_register_device() retains the dev->phys pointer for the entire lifetime of the input device, so if anyone later turns amijoy_phys into a 2D char array to avoid pointer indirection, the strings themselves will move into .init.rodata and dev->phys will become a dangling pointer after init (with no warning from modpost since the reference is inside amijoy_init()).

Thanks for the feedback.

Quoted 6 lines

Could we either just mark amijoy_phys as const without __initconst (it is only 8 bytes on m68k), or drop amijoy_phys altogether and do:

	amijoy_dev[i]->phys = i ? "amijoy/input1" : "amijoy/input0";

in amijoy_init() (which stays in .init.text anyway)?

I prefer to drop it then. v2 is here:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Thorsten