Skip to content

Commit 1bf2c92

Browse files
committed
irqdomain: Cleanup domain name allocation
irq_domain_set_name() is truly unreadable gunk. Clean it up before adding more. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Matti Vaittinen <[email protected]> Link: https://lore.kernel.org/all/874j7uvkbm.ffs@tglx
1 parent 70114e7 commit 1bf2c92

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

kernel/irq/irqdomain.c

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -128,72 +128,76 @@ void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
128128
}
129129
EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
130130

131-
static int irq_domain_set_name(struct irq_domain *domain,
132-
const struct fwnode_handle *fwnode,
133-
enum irq_domain_bus_token bus_token)
131+
static int alloc_name(struct irq_domain *domain, char *base, enum irq_domain_bus_token bus_token)
132+
{
133+
domain->name = bus_token ? kasprintf(GFP_KERNEL, "%s-%d", base, bus_token) :
134+
kasprintf(GFP_KERNEL, "%s", base);
135+
if (!domain->name)
136+
return -ENOMEM;
137+
138+
domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
139+
return 0;
140+
}
141+
142+
static int alloc_fwnode_name(struct irq_domain *domain, const struct fwnode_handle *fwnode,
143+
enum irq_domain_bus_token bus_token)
144+
{
145+
char *name = bus_token ? kasprintf(GFP_KERNEL, "%pfw-%d", fwnode, bus_token) :
146+
kasprintf(GFP_KERNEL, "%pfw", fwnode);
147+
148+
if (!name)
149+
return -ENOMEM;
150+
151+
/*
152+
* fwnode paths contain '/', which debugfs is legitimately unhappy
153+
* about. Replace them with ':', which does the trick and is not as
154+
* offensive as '\'...
155+
*/
156+
domain->name = strreplace(name, '/', ':');
157+
domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
158+
return 0;
159+
}
160+
161+
static int alloc_unknown_name(struct irq_domain *domain, enum irq_domain_bus_token bus_token)
134162
{
135163
static atomic_t unknown_domains;
136-
struct irqchip_fwid *fwid;
164+
int id = atomic_inc_return(&unknown_domains);
165+
166+
domain->name = bus_token ? kasprintf(GFP_KERNEL, "unknown-%d-%d", id, bus_token) :
167+
kasprintf(GFP_KERNEL, "unknown-%d", id);
137168

169+
if (!domain->name)
170+
return -ENOMEM;
171+
domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
172+
return 0;
173+
}
174+
175+
static int irq_domain_set_name(struct irq_domain *domain, const struct fwnode_handle *fwnode,
176+
enum irq_domain_bus_token bus_token)
177+
{
138178
if (is_fwnode_irqchip(fwnode)) {
139-
fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
179+
struct irqchip_fwid *fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
140180

141181
switch (fwid->type) {
142182
case IRQCHIP_FWNODE_NAMED:
143183
case IRQCHIP_FWNODE_NAMED_ID:
144-
domain->name = bus_token ?
145-
kasprintf(GFP_KERNEL, "%s-%d",
146-
fwid->name, bus_token) :
147-
kstrdup(fwid->name, GFP_KERNEL);
148-
if (!domain->name)
149-
return -ENOMEM;
150-
domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
151-
break;
184+
return alloc_name(domain, fwid->name, bus_token);
152185
default:
153186
domain->name = fwid->name;
154-
if (bus_token) {
155-
domain->name = kasprintf(GFP_KERNEL, "%s-%d",
156-
fwid->name, bus_token);
157-
if (!domain->name)
158-
return -ENOMEM;
159-
domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
160-
}
161-
break;
187+
if (bus_token)
188+
return alloc_name(domain, fwid->name, bus_token);
162189
}
163-
} else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
164-
is_software_node(fwnode)) {
165-
char *name;
166190

167-
/*
168-
* fwnode paths contain '/', which debugfs is legitimately
169-
* unhappy about. Replace them with ':', which does
170-
* the trick and is not as offensive as '\'...
171-
*/
172-
name = bus_token ?
173-
kasprintf(GFP_KERNEL, "%pfw-%d", fwnode, bus_token) :
174-
kasprintf(GFP_KERNEL, "%pfw", fwnode);
175-
if (!name)
176-
return -ENOMEM;
177-
178-
domain->name = strreplace(name, '/', ':');
179-
domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
191+
} else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) || is_software_node(fwnode)) {
192+
return alloc_fwnode_name(domain, fwnode, bus_token);
180193
}
181194

182-
if (!domain->name) {
183-
if (fwnode)
184-
pr_err("Invalid fwnode type for irqdomain\n");
185-
domain->name = bus_token ?
186-
kasprintf(GFP_KERNEL, "unknown-%d-%d",
187-
atomic_inc_return(&unknown_domains),
188-
bus_token) :
189-
kasprintf(GFP_KERNEL, "unknown-%d",
190-
atomic_inc_return(&unknown_domains));
191-
if (!domain->name)
192-
return -ENOMEM;
193-
domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
194-
}
195+
if (domain->name)
196+
return 0;
195197

196-
return 0;
198+
if (fwnode)
199+
pr_err("Invalid fwnode type for irqdomain\n");
200+
return alloc_unknown_name(domain, bus_token);
197201
}
198202

199203
static struct irq_domain *__irq_domain_create(const struct irq_domain_info *info)

0 commit comments

Comments
 (0)