Skip to contentSkip to conversation

genirq: Properly pair kobject_del with kobject_add

PATCH [email protected] 4 participants 5 messages Aug 2, 2019 – Aug 19, 2019 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

[email protected]

author maintainer
#1 lore raw
Quoted 22 lines · Michael Kelley

If alloc_descs fails before irq_sysfs_init has run, free_desc in the cleanup path will call kobject_del even though the kobject has not been added with kobject_add. Fix this by making the call to kobject_del conditional on whether irq_sysfs_init has run.

This problem surfaced because commit aa30f47cf666 ("kobject: Add support for default attribute groups to kobj_type") makes kobject_del stricter about pairing with kobject_add. If the pairing is incorrrect, a WARNING and backtrace occur in sysfs_remove_group because there is no parent.

Fixes ecb3f394c5db ("genirq: Expose interrupt information through sysfs")
---
 kernel/irq/irqdesc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/irq/irqdesc.c +0 -0
index 9484e88..5447760 100644

kernel/irq/irqdesc.c static void free_desc(unsigned int irq) · line 442

Quoted 6 lines
kernel/irq/irqdesc.c +2 -1
@@ -438,7 +438,8 @@ static void free_desc(unsigned int irq)
 	 * The sysfs entry must be serialized against a concurrent
 	 * irq_sysfs_init() as well.
 	 */
-	kobject_del(&desc->kobj);
+	if (irq_kobj_base)
+		kobject_del(&desc->kobj);

But now you leak the memory of desc as there is no chance it could be freed, because the kobject release function is never called :(

Relying on irq_kobj_bas to be present or not seems like an odd test here.

thanks,

greg k-h

A message that is not in the archive (sent off-list or not yet received): MWHPR21MB078463AB854A336842118405D7D90@MWHPR21MB0784.namprd21.prod.outlook.com

author maintainer Acked-by
#3 lore raw
Quoted 50 lines · Michael Kelley

From: [email protected] <[email protected]> Sent: Thursday, August 1, 2019 11:34 PM

Quoted 18 lines · Michael Kelley
Quoted 17 lines

If alloc_descs fails before irq_sysfs_init has run, free_desc in the cleanup path will call kobject_del even though the kobject has not been added with kobject_add. Fix this by making the call to kobject_del conditional on whether irq_sysfs_init has run.

This problem surfaced because commit aa30f47cf666 ("kobject: Add support for default attribute groups to kobj_type") makes kobject_del stricter about pairing with kobject_add. If the pairing is incorrrect, a WARNING and backtrace occur in sysfs_remove_group because there is no parent.

Fixes ecb3f394c5db ("genirq: Expose interrupt information through sysfs")
---
 kernel/irq/irqdesc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 14 lines

kernel/irq/irqdesc.c static void free_desc(unsigned int irq) · line 442

Quoted 11 lines
kernel/irq/irqdesc.c +2 -1
index 9484e88..5447760 100644
@@ -438,7 +438,8 @@ static void free_desc(unsigned int irq)
 	 * The sysfs entry must be serialized against a concurrent
 	 * irq_sysfs_init() as well.
 	 */
-	kobject_del(&desc->kobj);
+	if (irq_kobj_base)
+		kobject_del(&desc->kobj);

But now you leak the memory of desc as there is no chance it could be freed, because the kobject release function is never called :(

In the alloc_descs error path, when irq_kobj_base is still NULL, the kobject code sequence is:
kobject_init() [as called by alloc_desc]
kobject_put() [as called by delayed_free_desc]

So I don't think anything leaks.

If irq_kobj_base is not NULL, the kobject code sequence is:
kobject_init() [as called by alloc_desc]
kobject_add() [as called by irq_sysfs_add]
kobject_del() [as called by free_desc]
kobject_put() [as called by delayed_free_desc]

Again, everything is paired up properly.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Relying on irq_kobj_base to be present or not seems like an odd test here.

It's the same test that is used in irq_sysfs_add to decide whether to call kobject_add. So it makes everything paired up and symmetrical.

Ugh, that's a tangled mess and totally not obvious at all. I'm sure there's a good reason for all of that, and I really don't want to know :)

Anyway, yes, you are right, the patch is fine, sorry for the noise.

greg k-h

#4 lore raw
Quoted 11 lines · On Sat, 3 Aug 2019, [email protected] wrote:
Quoted 6 lines · Michael Kelley
Quoted 3 lines

Relying on irq_kobj_base to be present or not seems like an odd test here.

It's the same test that is used in irq_sysfs_add to decide whether to call kobject_add. So it makes everything paired up and symmetrical.

Ugh, that's a tangled mess and totally not obvious at all. I'm sure there's a good reason for all of that, and I really don't want to know :)

It's all your fault that the sysfs stuff does not work in very early boot.

/me runs

#5 lore raw

[tip:irq/urgent] genirq: Properly pair kobject_del() with kobject_add()

In reply to a message not in the archive
genirq: Properly pair kobject_del() with kobject_add()
Commit-ID:  e1ee29624746fbf667f80e8ae3815a76e4d1bd5b
Gitweb:     https://git.kernel.org/tip/e1ee29624746fbf667f80e8ae3815a76e4d1bd5b
Author:     Michael Kelley <[email protected]>
AuthorDate: Thu, 1 Aug 2019 23:53:53 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Mon, 19 Aug 2019 15:56:28 +0200

genirq: Properly pair kobject_del() with kobject_add()

If alloc_descs() fails before irq_sysfs_init() has run, free_desc() in the cleanup path will call kobject_del() even though the kobject has not been added with kobject_add().

Fix this by making the call to kobject_del() conditional on whether irq_sysfs_init() has run.

This problem surfaced because commit aa30f47cf666 ("kobject: Add support for default attribute groups to kobj_type") makes kobject_del() stricter about pairing with kobject_add(). If the pairing is incorrrect, a WARNING and backtrace occur in sysfs_remove_group() because there is no parent.

[ tglx: Add a comment to the code ]

---
 kernel/irq/irqdesc.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/irq/irqdesc.c +7 -1
index 9484e88dabc2..51f42f3caf09 100644
@@ -437,8 +437,14 @@ static void free_desc(unsigned int irq)
 	 *
 	 * The sysfs entry must be serialized against a concurrent
 	 * irq_sysfs_init() as well.
+	 *
+	 * If irq_sysfs_init() has not yet been invoked (early boot), then
+	 * irq_kobj_base is NULL and the descriptor was never added.
+	 * kobject_del() complains about a object with no parent, so make
+	 * it conditional.
 	 */
-	kobject_del(&desc->kobj);
+	if (irq_kobj_base)
+		kobject_del(&desc->kobj);
 	delete_irq_desc(irq);
 
 	/*

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

RE: [tip:irq/urgent] genirq: Properly pair kobject_del() with kobject_add()

From: Sasha Levin <[email protected]> Sent: Monday, August 19, 2019 2:28 PM

Quoted 18 lines

This commit has been processed because it contains a "Fixes:" tag, fixing commit: ecb3f394c5db genirq: Expose interrupt information through sysfs.

The bot has tested the following trees: v5.2.9, v4.19.67, v4.14.139, v4.9.189.

v5.2.9: Build failed! Errors:

    kernel/irq/irqdesc.c:446:6: error: ‘irq_kobj_base’ undeclared (first use in this function);

did you mean ‘irq_kobj_type’?

v4.19.67: Build failed! Errors:

    kernel/irq/irqdesc.c:445:6: error: ‘irq_kobj_base’ undeclared (first use in this function);

did you mean ‘irq_kobj_type’?

v4.14.139: Build failed! Errors:

    kernel/irq/irqdesc.c:428:6: error: ‘irq_kobj_base’ undeclared (first use in this function);

did you mean ‘irq_kobj_type’?

Quoted 8 lines

v4.9.189: Build failed! Errors:

    kernel/irq/irqdesc.c:414:6: error: ‘irq_kobj_base’ undeclared (first use in this function);

did you mean ‘irq_kobj_type’?

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Compile error occurs when CONFIG_SYSFS is not selected. It's probably cleanest to
revert the current patch. I'll send out a new version that fixes the problem.

Michael