Skip to contentSkip to conversation

genirq/irq_sim: fix a use-after-free bug + some

PATCH In review Bartosz Golaszewski 3 participants 17 messages Aug 12, 2023 – Aug 21, 2023

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

cover genirq/irq_sim: fix a use-after-free bug + some

From: Bartosz Golaszewski <[email protected]>

The first patch is an issue reported by KASAN. We're accessing freed memory if we remove the simulator domain with irs still requested. We must dispose of all existing mappings before we remove the domain.

The following three patches are some improvements to the interrupt simulator. A simple header cleanup and code shrink using the new cleanup.h helpers.

Bartosz Golaszewski (4):
genirq/irq_sim: dispose of remaining mappings before removing the
domain
genirq/irq_sim: order includes alphabetically
bitmap: define a cleanup function for bitmaps genirq/irq_sim: shrink code by using cleanup helpers

 include/linux/bitmap.h |  3 +++
 kernel/irq/irq_sim.c   | 38 +++++++++++++++++++++++---------------
 2 files changed, 26 insertions(+), 15 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/4 genirq/irq_sim: dispose of remaining mappings before removing the domain

From: Bartosz Golaszewski <[email protected]>

If the device providing simulated interrupts is unbound (real life
example: gpio-sim is disabled with users that didn't free their irqs) and removes the simulated domain while interrupts are still requested, we will hit memory issues when they are eventually freed and the mappings destroyed in the process.

Specifically we'll access freed memory in __irq_domain_deactivate_irq().

Dispose of all mappings before removing the simulator domain.

Fixes b19af510e67e ("genirq/irq_sim: Add a simple interrupt simulator framework")
---
 kernel/irq/irq_sim.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
kernel/irq/irq_sim.c +12 -0
index dd76323ea3fd..2c8a9cc1faa6 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2020 Bartosz Golaszewski <[email protected]>
  */
 
+#include <linux/list.h>
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
 #include <linux/irq_work.h>
@@ -16,12 +17,14 @@ struct irq_sim_work_ctx {
 	unsigned int		irq_count;
 	unsigned long		*pending;
 	struct irq_domain	*domain;
+	struct list_head	irqs;
 };
 
 struct irq_sim_irq_ctx {
 	int			irqnum;
 	bool			enabled;
 	struct irq_sim_work_ctx	*work_ctx;
+	struct list_head	siblings;
 };
 
 static void irq_sim_irqmask(struct irq_data *data)
@@ -129,6 +132,8 @@ static int irq_sim_domain_map(struct irq_domain *domain,
 	irq_set_handler(virq, handle_simple_irq);
 	irq_modify_status(virq, IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
 	irq_ctx->work_ctx = work_ctx;
+	irq_ctx->irqnum = virq;
+	list_add_tail(&irq_ctx->siblings, &work_ctx->irqs);
 
 	return 0;
 }
@@ -141,6 +146,7 @@ static void irq_sim_domain_unmap(struct irq_domain *domain, unsigned int virq)
 	irqd = irq_domain_get_irq_data(domain, virq);
 	irq_ctx = irq_data_get_irq_chip_data(irqd);
 
+	list_del(&irq_ctx->siblings);
 	irq_set_handler(virq, NULL);
 	irq_domain_reset_irq_data(irqd);
 	kfree(irq_ctx);
@@ -182,6 +188,7 @@ struct irq_domain *irq_domain_create_sim(struct fwnode_handle *fwnode,
 
 	work_ctx->irq_count = num_irqs;
 	work_ctx->work = IRQ_WORK_INIT_HARD(irq_sim_handle_irq);
+	INIT_LIST_HEAD(&work_ctx->irqs);
 
 	return work_ctx->domain;
 
@@ -203,8 +210,13 @@ EXPORT_SYMBOL_GPL(irq_domain_create_sim);
 void irq_domain_remove_sim(struct irq_domain *domain)
 {
 	struct irq_sim_work_ctx *work_ctx = domain->host_data;
+	struct irq_sim_irq_ctx *irq_ctx, *aux;
 
 	irq_work_sync(&work_ctx->work);
+
+	list_for_each_entry_safe(irq_ctx, aux, &work_ctx->irqs, siblings)
+		irq_dispose_mapping(irq_ctx->irqnum);
+
 	bitmap_free(work_ctx->pending);
 	kfree(work_ctx);
 
Signature · 1 line
2.39.2
#2 lore raw

Re: [PATCH 1/4] genirq/irq_sim: dispose of remaining mappings before removing the domain

Quoted 11 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If the device providing simulated interrupts is unbound (real life
example: gpio-sim is disabled with users that didn't free their irqs) and removes the simulated domain while interrupts are still requested, we will hit memory issues when they are eventually freed and the mappings destroyed in the process.

Specifically we'll access freed memory in __irq_domain_deactivate_irq().

Dispose of all mappings before removing the simulator domain.

...

Quoted 1 line

+#include <linux/list.h>

Maybe ordered?

Quoted 3 lines

#include <linux/irq.h>
#include <linux/irq_sim.h>
#include <linux/irq_work.h>

...

kernel/irq/irq_sim.c struct irq_sim_work_ctx { · line 28

Quoted 13 lines
kernel/irq/irq_sim.c +2 -0
@@ -16,12 +17,14 @@ struct irq_sim_work_ctx {
 	unsigned int		irq_count;
 	unsigned long		*pending;
 	struct irq_domain	*domain;
+	struct list_head	irqs;
 };
 
 struct irq_sim_irq_ctx {
 	int			irqnum;
 	bool			enabled;
 	struct irq_sim_work_ctx	*work_ctx;
 
+	struct list_head	siblings;

You can reduce the code size by moving this to be the first member. Not sure about struct irq_sim_work_ctx, you can play with bloat-o-meter.

Quoted 1 line
(quoted diff) +0 -0
 };
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw

Re: [PATCH 1/4] genirq/irq_sim: dispose of remaining mappings before removing the domain

Quoted 37 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If the device providing simulated interrupts is unbound (real life
example: gpio-sim is disabled with users that didn't free their irqs) and removes the simulated domain while interrupts are still requested, we will hit memory issues when they are eventually freed and the mappings destroyed in the process.

Specifically we'll access freed memory in __irq_domain_deactivate_irq().

Dispose of all mappings before removing the simulator domain.

...

Quoted 1 line

+#include <linux/list.h>

Maybe ordered?

Quoted 3 lines

#include <linux/irq.h>
#include <linux/irq_sim.h>
#include <linux/irq_work.h>

...

Quoted 11 lines
kernel/irq/irq_sim.c +1 -0
@@ -16,12 +17,14 @@ struct irq_sim_work_ctx {
 	unsigned int		irq_count;
 	unsigned long		*pending;
 	struct irq_domain	*domain;
+	struct list_head	irqs;
 };
 
 struct irq_sim_irq_ctx {
 	int			irqnum;
 	bool			enabled;
 	struct irq_sim_work_ctx	*work_ctx;
Quoted 4 lines

kernel/irq/irq_sim.c struct irq_sim_work_ctx { · line 27

Quoted 1 line
kernel/irq/irq_sim.c +1 -0
+	struct list_head	siblings;

You can reduce the code size by moving this to be the first member. Not sure about struct irq_sim_work_ctx, you can play with bloat-o-meter.

Pahole you meant?

  yury:linux$ pahole -C irq_sim_irq_ctx /sys/kernel/btf/vmlinux
  struct irq_sim_irq_ctx {
  	int                        irqnum;               /*     0     4 */
  	bool                       enabled;              /*     4     1 */

/* XXX 3 bytes hole, try to pack */

struct irq_sim_work_ctx * work_ctx; /* 8 8 */

  	/* size: 16, cachelines: 1, members: 3 */
  	/* sum members: 13, holes: 1, sum holes: 3 */
  	/* last cacheline: 16 bytes */
  };

In this particular case, there will be no hole because list head position (16) will be aligned to sizeof(struct list_head) == 16.

But as Bartosz said in the other email, "it's just good practice resulting from years of" kernel coding to have:
- members declared strongly according to the logic of the code, and if no strong preference:
- list head be the first element of the structure, to let compiler avoid generating offsets when traversing lists;
- put elements of greater size at the beginning, so no holes will be emitted like in the example above.

So I'd suggest:

  struct irq_sim_irq_ctx {
     struct list_head        siblings;
     struct irq_sim_work_ctx *work_ctx;
     int                     irqnum;
     bool                    enabled;
  }

Again, if there's NO ANY reason to have the irq number at the beginning.

While here, I wonder, why irqnum is signed? Looking at the very first random function in kernel/irq/irq_sim.c, I see that it's initialized from a function returning unsigned value:

  static void irq_sim_handle_irq(struct irq_work *work)
  {
          struct irq_sim_work_ctx *work_ctx;
          unsigned int offset = 0;
          int irqnum;
          work_ctx = container_of(work, struct irq_sim_work_ctx, work);
  
          while (!bitmap_empty(work_ctx->pending, work_ctx->irq_count)) {
                  offset = find_next_bit(work_ctx->pending,
                                         work_ctx->irq_count, offset);
                  clear_bit(offset, work_ctx->pending);
                  irqnum = irq_find_mapping(work_ctx->domain, offset);
                  handle_simple_irq(irq_to_desc(irqnum));
          }

}

Thanks,
Yury

#4 lore raw

Re: [PATCH 1/4] genirq/irq_sim: dispose of remaining mappings before removing the domain

Quoted 2 lines · Yury Norov
Quoted 1 line · Andy Shevchenko

On Sat, Aug 12, 2023 at 09:44:54PM +0200, Bartosz Golaszewski wrote:

...

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
kernel/irq/irq_sim.c +1 -0
@@ -16,12 +17,14 @@ struct irq_sim_work_ctx {
 	unsigned int		irq_count;
 	unsigned long		*pending;
 	struct irq_domain	*domain;
+	struct list_head	irqs;
 };
 
 struct irq_sim_irq_ctx {
 	int			irqnum;
 	bool			enabled;
 	struct irq_sim_work_ctx	*work_ctx;
Quoted 6 lines
Quoted 4 lines

kernel/irq/irq_sim.c struct irq_sim_work_ctx { · line 27

Quoted 1 line
kernel/irq/irq_sim.c +1 -0
+	struct list_head	siblings;

You can reduce the code size by moving this to be the first member. Not sure about struct irq_sim_work_ctx, you can play with bloat-o-meter.

Pahole you meant?

No. I meant bloat-o-meter.

...

Quoted 7 lines

But as Bartosz said in the other email, "it's just good practice resulting from years of" kernel coding to have:
- members declared strongly according to the logic of the code, and if no strong preference:

- list head be the first element of the structure, to let compiler avoid generating offsets when traversing lists;

Exactly.

Quoted 11 lines

- put elements of greater size at the beginning, so no holes will be emitted like in the example above.

So I'd suggest:

  struct irq_sim_irq_ctx {
     struct list_head        siblings;
     struct irq_sim_work_ctx *work_ctx;
     int                     irqnum;
     bool                    enabled;
  }

Yes, I like this.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH 1/4] genirq/irq_sim: dispose of remaining mappings before removing the domain

In reply to Yury Norov
Quoted 58 lines · Yury Norov
Quoted 37 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If the device providing simulated interrupts is unbound (real life
example: gpio-sim is disabled with users that didn't free their irqs) and removes the simulated domain while interrupts are still requested, we will hit memory issues when they are eventually freed and the mappings destroyed in the process.

Specifically we'll access freed memory in __irq_domain_deactivate_irq().

Dispose of all mappings before removing the simulator domain.

...

Quoted 1 line

+#include <linux/list.h>

Maybe ordered?

Quoted 3 lines

#include <linux/irq.h>
#include <linux/irq_sim.h>
#include <linux/irq_work.h>

...

Quoted 11 lines
kernel/irq/irq_sim.c +1 -0
@@ -16,12 +17,14 @@ struct irq_sim_work_ctx {
    unsigned int            irq_count;
    unsigned long           *pending;
    struct irq_domain       *domain;
+   struct list_head        irqs;
 };
 
 struct irq_sim_irq_ctx {
    int                     irqnum;
    bool                    enabled;
    struct irq_sim_work_ctx *work_ctx;
Quoted 4 lines

kernel/irq/irq_sim.c struct irq_sim_work_ctx { · line 27

Quoted 1 line
kernel/irq/irq_sim.c +1 -0
+   struct list_head        siblings;

You can reduce the code size by moving this to be the first member. Not sure about struct irq_sim_work_ctx, you can play with bloat-o-meter.

Pahole you meant?

  yury:linux$ pahole -C irq_sim_irq_ctx /sys/kernel/btf/vmlinux
  struct irq_sim_irq_ctx {
        int                        irqnum;               /*     0     4 */
        bool                       enabled;              /*     4     1 */
        /* XXX 3 bytes hole, try to pack */

        struct irq_sim_work_ctx *  work_ctx;             /*     8     8 */

        /* size: 16, cachelines: 1, members: 3 */
        /* sum members: 13, holes: 1, sum holes: 3 */
        /* last cacheline: 16 bytes */
Quoted 7 lines

};

In this particular case, there will be no hole because list head position (16) will be aligned to sizeof(struct list_head) == 16.

But as Bartosz said in the other email, "it's just good practice resulting from years of" kernel coding to have:

Did I sound smug or what? I didn't mean to. I was merely pointing to the fact that linux is not the first to use C autopointers. They've been around for years.

Quoted 7 lines

- members declared strongly according to the logic of the code, and if no strong preference:
- list head be the first element of the structure, to let compiler avoid generating offsets when traversing lists;
- put elements of greater size at the beginning, so no holes will be emitted like in the example above.

Quoted 8 lines

So I'd suggest:

  struct irq_sim_irq_ctx {
     struct list_head        siblings;
     struct irq_sim_work_ctx *work_ctx;
     int                     irqnum;
     bool                    enabled;
  }

Sounds good.

Quoted 8 lines

Again, if there's NO ANY reason to have the irq number at the beginning.

While here, I wonder, why irqnum is signed? Looking at the very first random function in kernel/irq/irq_sim.c, I see that it's initialized from a function returning unsigned value:

This field is currently unused. I'm not sure how it ended up there, maybe a leftover from some earlier iterations of the irq_sim. This patch just makes use of it in the end. It may be that it should use unsigned int. Before I change it, I'd like to hear Thomas' comments on these changes in general.

Bart

Quoted 19 lines
  static void irq_sim_handle_irq(struct irq_work *work)
  {
          struct irq_sim_work_ctx *work_ctx;
          unsigned int offset = 0;
          int irqnum;
          work_ctx = container_of(work, struct irq_sim_work_ctx, work);

          while (!bitmap_empty(work_ctx->pending, work_ctx->irq_count)) {
                  offset = find_next_bit(work_ctx->pending,
                                         work_ctx->irq_count, offset);
                  clear_bit(offset, work_ctx->pending);
                  irqnum = irq_find_mapping(work_ctx->domain, offset);
                  handle_simple_irq(irq_to_desc(irqnum));
          }

}

Thanks,
Yury

#6 lore raw

Re: [PATCH 1/4] genirq/irq_sim: dispose of remaining mappings before removing the domain

In reply to Andy Shevchenko
Quoted 12 lines · Andy Shevchenko
Quoted 10 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If the device providing simulated interrupts is unbound (real life
example: gpio-sim is disabled with users that didn't free their irqs) and removes the simulated domain while interrupts are still requested, we will hit memory issues when they are eventually freed and the mappings destroyed in the process.

Specifically we'll access freed memory in __irq_domain_deactivate_irq().

Quoted 8 lines
Quoted 1 line

Dispose of all mappings before removing the simulator domain.

...

Quoted 1 line

+#include <linux/list.h>

Maybe ordered?

This patch comes first in the series as it's meant to be a backportable fix. Header ordering comes later.

Bart

Quoted 30 lines
Quoted 3 lines

#include <linux/irq.h>
#include <linux/irq_sim.h>
#include <linux/irq_work.h>

...

kernel/irq/irq_sim.c struct irq_sim_work_ctx { · line 28

Quoted 13 lines
kernel/irq/irq_sim.c +2 -0
@@ -16,12 +17,14 @@ struct irq_sim_work_ctx {
      unsigned int            irq_count;
      unsigned long           *pending;
      struct irq_domain       *domain;
+     struct list_head        irqs;
 };
 
 struct irq_sim_irq_ctx {
      int                     irqnum;
      bool                    enabled;
      struct irq_sim_work_ctx *work_ctx;
 
+     struct list_head        siblings;

You can reduce the code size by moving this to be the first member. Not sure about struct irq_sim_work_ctx, you can play with bloat-o-meter.

Review struct irq_sim_work_ctx { · line 29

Quoted 1 line
(quoted diff) +0 -0
 };

--
With Best Regards,
Andy Shevchenko

#7 lore raw

Re: [PATCH 1/4] genirq/irq_sim: dispose of remaining mappings before removing the domain

Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Sat, Aug 12, 2023 at 09:44:54PM +0200, Bartosz Golaszewski wrote:

...

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+#include <linux/list.h>

Maybe ordered?

This patch comes first in the series as it's meant to be a backportable fix. Header ordering comes later.

It does not prevent you from putting it in the better place. So the followup will be a less churn.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

Re: [PATCH 1/4] genirq/irq_sim: dispose of remaining mappings before removing the domain

In reply to Bartosz Golaszewski
Quoted 9 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If the device providing simulated interrupts is unbound (real life
example: gpio-sim is disabled with users that didn't free their irqs) and removes the simulated domain while interrupts are still requested, we will hit memory issues when they are eventually freed and the mappings destroyed in the process.

Quoted 7 lines

Specifically we'll access freed memory in __irq_domain_deactivate_irq().

Dispose of all mappings before removing the simulator domain.

Fixes b19af510e67e ("genirq/irq_sim: Add a simple interrupt simulator framework")

---

Please disregard this patch. I realized that it should be up to the owner of the domain to dispose of mappings. Just like core GPIO does for the domains it controls.

I will send the rest separately once they're reworked.

Bart

#9 lore raw

PATCH 2/4 genirq/irq_sim: order includes alphabetically

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

For better maintenance and readability keep the included headers in alphabetical order.

---
 kernel/irq/irq_sim.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/irq/irq_sim.c +2 -2
index 2c8a9cc1faa6..a8b013d0c5be 100644
@@ -4,11 +4,11 @@
  * Copyright (C) 2020 Bartosz Golaszewski <[email protected]>
  */
 
-#include <linux/list.h>
+#include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
 #include <linux/irq_work.h>
-#include <linux/interrupt.h>
+#include <linux/list.h>
 #include <linux/slab.h>
 
 struct irq_sim_work_ctx {
Signature · 1 line
2.39.2
#10 lore raw

Re: [PATCH 2/4] genirq/irq_sim: order includes alphabetically

Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

For better maintenance and readability keep the included headers in alphabetical order.

...

kernel/irq/irq_sim.c

Quoted 3 lines
kernel/irq/irq_sim.c +1 -1
-#include <linux/list.h>
 
+#include <linux/list.h>

This can be done in the previous patch already.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

PATCH 3/4 bitmap: define a cleanup function for bitmaps

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add support for autopointers for bitmaps allocated with bitmap_alloc() et al.

---
 include/linux/bitmap.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/bitmap.h +3 -0
index 03644237e1ef..6709807ebb59 100644
@@ -6,6 +6,7 @@
 
 #include <linux/align.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/find.h>
 #include <linux/limits.h>
 #include <linux/string.h>
@@ -125,6 +126,8 @@ unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
 void bitmap_free(const unsigned long *bitmap);
 
+DEFINE_FREE(bitmap, unsigned long *, if (_T) bitmap_free(_T))
+
 /* Managed variants of the above. */
 unsigned long *devm_bitmap_alloc(struct device *dev,
 				 unsigned int nbits, gfp_t flags);
Signature · 1 line
2.39.2
maintainer
#12 lore raw

Re: [PATCH 3/4] bitmap: define a cleanup function for bitmaps

Quoted 23 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add support for autopointers for bitmaps allocated with bitmap_alloc() et al.

---
 include/linux/bitmap.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/bitmap.h +1 -0
index 03644237e1ef..6709807ebb59 100644
@@ -6,6 +6,7 @@
 
 #include <linux/align.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/find.h>
 #include <linux/limits.h>
 #include <linux/string.h>

include/linux/bitmap.h unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node); · line 129

Quoted 4 lines
include/linux/bitmap.h +1 -0
@@ -125,6 +126,8 @@ unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
 void bitmap_free(const unsigned long *bitmap);
 
+DEFINE_FREE(bitmap, unsigned long *, if (_T) bitmap_free(_T))

bitmap_free() is a wrapper around kfree(), and kfree() checks against NULL, and returns immediately. Why this tests again?

#13 lore raw

Re: [PATCH 3/4] bitmap: define a cleanup function for bitmaps

Quoted 28 lines · Yury Norov
Quoted 26 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add support for autopointers for bitmaps allocated with bitmap_alloc() et al.

---
 include/linux/bitmap.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/bitmap.h +1 -0
index 03644237e1ef..6709807ebb59 100644
@@ -6,6 +6,7 @@
 
 #include <linux/align.h>
 #include <linux/bitops.h>
+#include <linux/cleanup.h>
 #include <linux/find.h>
 #include <linux/limits.h>
 #include <linux/string.h>
@@ -125,6 +126,8 @@ unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node);
 unsigned long *bitmap_zalloc_node(unsigned int nbits, gfp_t flags, int node);
 void bitmap_free(const unsigned long *bitmap);
 
Quoted 4 lines

include/linux/bitmap.h unsigned long *bitmap_alloc_node(unsigned int nbits, gfp_t flags, int node); · line 129

Quoted 1 line
include/linux/bitmap.h +1 -0
+DEFINE_FREE(bitmap, unsigned long *, if (_T) bitmap_free(_T))

bitmap_free() is a wrapper around kfree(), and kfree() checks against NULL, and returns immediately. Why this tests again?

I don't see any kernel docs making it a contract though. :)

It's a good question however. I carried it over from the kfree()'s DEFINE_FREE() itself. I can't find any discussion about it in the thread under Peter Zijlstra's patches.

May be worth revisiting it for free() functions that handle NULL.

Bart

#14 lore raw

PATCH 4/4 genirq/irq_sim: shrink code by using cleanup helpers

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new __free helper from linux/cleanup.h to remove all gotos and simplify the error paths.

---
 kernel/irq/irq_sim.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
kernel/irq/irq_sim.c +10 -14
index a8b013d0c5be..202beb1169c9 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2020 Bartosz Golaszewski <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irq_sim.h>
@@ -170,34 +171,29 @@ static const struct irq_domain_ops irq_sim_domain_ops = {
 struct irq_domain *irq_domain_create_sim(struct fwnode_handle *fwnode,
 					 unsigned int num_irqs)
 {
-	struct irq_sim_work_ctx *work_ctx;
+	struct irq_sim_work_ctx *work_ctx __free(kfree) = NULL;
+	unsigned long *pending __free(bitmap) = NULL;
 
 	work_ctx = kmalloc(sizeof(*work_ctx), GFP_KERNEL);
 	if (!work_ctx)
-		goto err_out;
+		return ERR_PTR(-ENOMEM);
 
-	work_ctx->pending = bitmap_zalloc(num_irqs, GFP_KERNEL);
-	if (!work_ctx->pending)
-		goto err_free_work_ctx;
+	pending = bitmap_zalloc(num_irqs, GFP_KERNEL);
+	if (!pending)
+		return ERR_PTR(-ENOMEM);
 
 	work_ctx->domain = irq_domain_create_linear(fwnode, num_irqs,
 						    &irq_sim_domain_ops,
 						    work_ctx);
 	if (!work_ctx->domain)
-		goto err_free_bitmap;
+		return ERR_PTR(-ENOMEM);
 
 	work_ctx->irq_count = num_irqs;
 	work_ctx->work = IRQ_WORK_INIT_HARD(irq_sim_handle_irq);
 	INIT_LIST_HEAD(&work_ctx->irqs);
+	work_ctx->pending = no_free_ptr(pending);
 
-	return work_ctx->domain;
-
-err_free_bitmap:
-	bitmap_free(work_ctx->pending);
-err_free_work_ctx:
-	kfree(work_ctx);
-err_out:
-	return ERR_PTR(-ENOMEM);
+	return no_free_ptr(work_ctx)->domain;
 }
 EXPORT_SYMBOL_GPL(irq_domain_create_sim);
 
Signature · 1 line
2.39.2
maintainer
#15 lore raw

Re: [PATCH 4/4] genirq/irq_sim: shrink code by using cleanup helpers

Quoted 23 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new __free helper from linux/cleanup.h to remove all gotos and simplify the error paths.

---
 kernel/irq/irq_sim.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
kernel/irq/irq_sim.c +1 -0
index a8b013d0c5be..202beb1169c9 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2020 Bartosz Golaszewski <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irq_sim.h>

kernel/irq/irq_sim.c static const struct irq_domain_ops irq_sim_domain_ops = { · line 175

Quoted 6 lines
kernel/irq/irq_sim.c +2 -1
@@ -170,34 +171,29 @@ static const struct irq_domain_ops irq_sim_domain_ops = {
 struct irq_domain *irq_domain_create_sim(struct fwnode_handle *fwnode,
 					 unsigned int num_irqs)
 {
-	struct irq_sim_work_ctx *work_ctx;
+	struct irq_sim_work_ctx *work_ctx __free(kfree) = NULL;
+	unsigned long *pending __free(bitmap) = NULL;

Why initializing here as NULL ...

kernel/irq/irq_sim.c static const struct irq_domain_ops irq_sim_domain_ops = { · line 183

Quoted 11 lines
kernel/irq/irq_sim.c +4 -4
 	work_ctx = kmalloc(sizeof(*work_ctx), GFP_KERNEL);
 	if (!work_ctx)
-		goto err_out;
+		return ERR_PTR(-ENOMEM);
 
-	work_ctx->pending = bitmap_zalloc(num_irqs, GFP_KERNEL);
-	if (!work_ctx->pending)
-		goto err_free_work_ctx;
+	pending = bitmap_zalloc(num_irqs, GFP_KERNEL);
+	if (!pending)
+		return ERR_PTR(-ENOMEM);

and overriding immediately after that? Not familiar to __free() machinery in details, although. Does it require initialization?

Thank,
Yury

#16 lore raw

Re: [PATCH 4/4] genirq/irq_sim: shrink code by using cleanup helpers

Quoted 46 lines · Yury Norov
Quoted 23 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Use the new __free helper from linux/cleanup.h to remove all gotos and simplify the error paths.

---
 kernel/irq/irq_sim.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
kernel/irq/irq_sim.c +1 -0
index a8b013d0c5be..202beb1169c9 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2020 Bartosz Golaszewski <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irq_sim.h>

kernel/irq/irq_sim.c static const struct irq_domain_ops irq_sim_domain_ops = { · line 175

Quoted 6 lines
kernel/irq/irq_sim.c +2 -1
@@ -170,34 +171,29 @@ static const struct irq_domain_ops irq_sim_domain_ops = {
 struct irq_domain *irq_domain_create_sim(struct fwnode_handle *fwnode,
                                       unsigned int num_irqs)
 {
-     struct irq_sim_work_ctx *work_ctx;
+     struct irq_sim_work_ctx *work_ctx __free(kfree) = NULL;
+     unsigned long *pending __free(bitmap) = NULL;

Why initializing here as NULL ...

kernel/irq/irq_sim.c static const struct irq_domain_ops irq_sim_domain_ops = { · line 183

Quoted 11 lines
kernel/irq/irq_sim.c +4 -4
      work_ctx = kmalloc(sizeof(*work_ctx), GFP_KERNEL);
      if (!work_ctx)
-             goto err_out;
+             return ERR_PTR(-ENOMEM);
 
-     work_ctx->pending = bitmap_zalloc(num_irqs, GFP_KERNEL);
-     if (!work_ctx->pending)
-             goto err_free_work_ctx;
+     pending = bitmap_zalloc(num_irqs, GFP_KERNEL);
+     if (!pending)
+             return ERR_PTR(-ENOMEM);
Quoted 5 lines

and overriding immediately after that? Not familiar to __free() machinery in details, although. Does it require initialization?

Thank,
Yury

For the first variable: it's just good practice resulting from years of user-space coding in GLib which makes heavy use of autopointers. For the 'pending' variable, it's necessary as if the first kmalloc() fails, it will go out of scope with random contents and bitmap_free() will get called on it. Now if we ever add some statement that can return between the start of the function and the first kmalloc() then we'll save ourselves having to modify the variable declaration.

Bart