Skip to content

Commit bef69ea

Browse files
committed
Resource handling: add 'insert_resource_expand_to_fit()' function
Not used anywhere yet, but this complements the existing plain 'insert_resource()' functionality with a version that can expand the resource we are adding in order to fix up any conflicts it has with existing resources. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 00aeb42 commit bef69ea

2 files changed

Lines changed: 64 additions & 25 deletions

File tree

‎include/linux/ioport.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ extern struct resource iomem_resource;
109109
extern int request_resource(struct resource *root, struct resource *new);
110110
extern int release_resource(struct resource *new);
111111
extern int insert_resource(struct resource *parent, struct resource *new);
112+
extern void insert_resource_expand_to_fit(struct resource *root, struct resource *new);
112113
extern int allocate_resource(struct resource *root, struct resource *new,
113114
resource_size_t size, resource_size_t min,
114115
resource_size_t max, resource_size_t align,

‎kernel/resource.c‎

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -362,35 +362,21 @@ int allocate_resource(struct resource *root, struct resource *new,
362362

363363
EXPORT_SYMBOL(allocate_resource);
364364

365-
/**
366-
* insert_resource - Inserts a resource in the resource tree
367-
* @parent: parent of the new resource
368-
* @new: new resource to insert
369-
*
370-
* Returns 0 on success, -EBUSY if the resource can't be inserted.
371-
*
372-
* This function is equivalent to request_resource when no conflict
373-
* happens. If a conflict happens, and the conflicting resources
374-
* entirely fit within the range of the new resource, then the new
375-
* resource is inserted and the conflicting resources become children of
376-
* the new resource.
365+
/*
366+
* Insert a resource into the resource tree. If successful, return NULL,
367+
* otherwise return the conflicting resource (compare to __request_resource())
377368
*/
378-
int insert_resource(struct resource *parent, struct resource *new)
369+
static struct resource * __insert_resource(struct resource *parent, struct resource *new)
379370
{
380-
int result;
381371
struct resource *first, *next;
382372

383-
write_lock(&resource_lock);
384-
385373
for (;; parent = first) {
386-
result = 0;
387374
first = __request_resource(parent, new);
388375
if (!first)
389-
goto out;
376+
return first;
390377

391-
result = -EBUSY;
392378
if (first == parent)
393-
goto out;
379+
return first;
394380

395381
if ((first->start > new->start) || (first->end < new->end))
396382
break;
@@ -401,15 +387,13 @@ int insert_resource(struct resource *parent, struct resource *new)
401387
for (next = first; ; next = next->sibling) {
402388
/* Partial overlap? Bad, and unfixable */
403389
if (next->start < new->start || next->end > new->end)
404-
goto out;
390+
return next;
405391
if (!next->sibling)
406392
break;
407393
if (next->sibling->start > new->end)
408394
break;
409395
}
410396

411-
result = 0;
412-
413397
new->parent = parent;
414398
new->sibling = next->sibling;
415399
new->child = first;
@@ -426,10 +410,64 @@ int insert_resource(struct resource *parent, struct resource *new)
426410
next = next->sibling;
427411
next->sibling = new;
428412
}
413+
return NULL;
414+
}
429415

430-
out:
416+
/**
417+
* insert_resource - Inserts a resource in the resource tree
418+
* @parent: parent of the new resource
419+
* @new: new resource to insert
420+
*
421+
* Returns 0 on success, -EBUSY if the resource can't be inserted.
422+
*
423+
* This function is equivalent to request_resource when no conflict
424+
* happens. If a conflict happens, and the conflicting resources
425+
* entirely fit within the range of the new resource, then the new
426+
* resource is inserted and the conflicting resources become children of
427+
* the new resource.
428+
*/
429+
int insert_resource(struct resource *parent, struct resource *new)
430+
{
431+
struct resource *conflict;
432+
433+
write_lock(&resource_lock);
434+
conflict = __insert_resource(parent, new);
435+
write_unlock(&resource_lock);
436+
return conflict ? -EBUSY : 0;
437+
}
438+
439+
/**
440+
* insert_resource_expand_to_fit - Insert a resource into the resource tree
441+
* @parent: parent of the new resource
442+
* @new: new resource to insert
443+
*
444+
* Insert a resource into the resource tree, possibly expanding it in order
445+
* to make it encompass any conflicting resources.
446+
*/
447+
void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
448+
{
449+
if (new->parent)
450+
return;
451+
452+
write_lock(&resource_lock);
453+
for (;;) {
454+
struct resource *conflict;
455+
456+
conflict = __insert_resource(root, new);
457+
if (!conflict)
458+
break;
459+
if (conflict == root)
460+
break;
461+
462+
/* Ok, expand resource to cover the conflict, then try again .. */
463+
if (conflict->start < new->start)
464+
new->start = conflict->start;
465+
if (conflict->end > new->end)
466+
new->end = conflict->end;
467+
468+
printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
469+
}
431470
write_unlock(&resource_lock);
432-
return result;
433471
}
434472

435473
/**

0 commit comments

Comments
 (0)