Skip to content

Commit 9821d8d

Browse files
elic307iSaeed Mahameed
authored andcommitted
lib: cpu_rmap: Use allocator for rmap entries
Use a proper allocator for rmap entries using a naive for loop. The allocator relies on whether an entry is NULL to be considered free. Remove the used field of rmap which is not needed. Also, avoid crashing the kernel if an entry is not available. Cc: Thomas Gleixner <[email protected]> Signed-off-by: Eli Cohen <[email protected]> Signed-off-by: Saeed Mahameed <[email protected]> Reviewed-by: Jacob Keller <[email protected]>
1 parent 4e0473f commit 9821d8d

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

include/linux/cpu_rmap.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
* struct cpu_rmap - CPU affinity reverse-map
1717
* @refcount: kref for object
1818
* @size: Number of objects to be reverse-mapped
19-
* @used: Number of objects added
2019
* @obj: Pointer to array of object pointers
2120
* @near: For each CPU, the index and distance to the nearest object,
2221
* based on affinity masks
2322
*/
2423
struct cpu_rmap {
2524
struct kref refcount;
26-
u16 size, used;
25+
u16 size;
2726
void **obj;
2827
struct {
2928
u16 index;

lib/cpu_rmap.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,31 @@ debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
128128
}
129129
#endif
130130

131+
static int get_free_index(struct cpu_rmap *rmap)
132+
{
133+
int i;
134+
135+
for (i = 0; i < rmap->size; i++)
136+
if (!rmap->obj[i])
137+
return i;
138+
139+
return -ENOSPC;
140+
}
141+
131142
/**
132143
* cpu_rmap_add - add object to a rmap
133144
* @rmap: CPU rmap allocated with alloc_cpu_rmap()
134145
* @obj: Object to add to rmap
135146
*
136-
* Return index of object.
147+
* Return index of object or -ENOSPC if no free entry was found
137148
*/
138149
int cpu_rmap_add(struct cpu_rmap *rmap, void *obj)
139150
{
140-
u16 index;
151+
int index = get_free_index(rmap);
152+
153+
if (index < 0)
154+
return index;
141155

142-
BUG_ON(rmap->used >= rmap->size);
143-
index = rmap->used++;
144156
rmap->obj[index] = obj;
145157
return index;
146158
}
@@ -230,7 +242,7 @@ void free_irq_cpu_rmap(struct cpu_rmap *rmap)
230242
if (!rmap)
231243
return;
232244

233-
for (index = 0; index < rmap->used; index++) {
245+
for (index = 0; index < rmap->size; index++) {
234246
glue = rmap->obj[index];
235247
if (glue)
236248
irq_set_affinity_notifier(glue->notify.irq, NULL);
@@ -295,13 +307,22 @@ int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
295307
glue->notify.release = irq_cpu_rmap_release;
296308
glue->rmap = rmap;
297309
cpu_rmap_get(rmap);
298-
glue->index = cpu_rmap_add(rmap, glue);
310+
rc = cpu_rmap_add(rmap, glue);
311+
if (rc < 0)
312+
goto err_add;
313+
314+
glue->index = rc;
299315
rc = irq_set_affinity_notifier(irq, &glue->notify);
300-
if (rc) {
301-
cpu_rmap_put(glue->rmap);
302-
rmap->obj[glue->index] = NULL;
303-
kfree(glue);
304-
}
316+
if (rc)
317+
goto err_set;
318+
319+
return rc;
320+
321+
err_set:
322+
rmap->obj[glue->index] = NULL;
323+
err_add:
324+
cpu_rmap_put(glue->rmap);
325+
kfree(glue);
305326
return rc;
306327
}
307328
EXPORT_SYMBOL(irq_cpu_rmap_add);

0 commit comments

Comments
 (0)