Skip to content

Commit

Permalink
drivers/base: reorder consumer and its children behind suppliers
Browse files Browse the repository at this point in the history
commit 52cdbdd ("driver core: correct device's shutdown order")
introduces supplier<-consumer order in devices_kset. The commit tries
to cleverly maintain both parent<-child and supplier<-consumer order by
reordering a device when probing. This method makes things simple and
clean, but unfortunately, breaks parent<-child order in some case,
which is described in next patch in this series.
Here this patch tries to resolve supplier<-consumer by only reordering a
device when it has suppliers, and takes care of the following scenario:
    [consumer, children] [ ... potential ... ] supplier
                         ^                   ^
After moving the consumer and its children after the supplier, the
potentail section may contain consumers whose supplier is inside
children, and this poses the requirement to dry out all consumpers in
the section recursively.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Grygorii Strashko <grygorii.strashko@ti.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Bjorn Helgaas <helgaas@kernel.org>
Cc: Dave Young <dyoung@redhat.com>
Cc: linux-pci@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Signed-off-by: Pingfan Liu <kernelfans@gmail.com>
  • Loading branch information
liupingfan authored and intel-lab-lkp committed Jun 25, 2018
1 parent 67013cc commit 1b2a1e6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/base/base.h
Expand Up @@ -135,6 +135,7 @@ extern void device_unblock_probing(void);

/* /sys/devices directory */
extern struct kset *devices_kset;
extern int device_reorder_consumer(struct device *dev);
extern void devices_kset_move_last(struct device *dev);

#if defined(CONFIG_MODULES) && defined(CONFIG_SYSFS)
Expand Down
97 changes: 97 additions & 0 deletions drivers/base/core.c
Expand Up @@ -161,6 +161,103 @@ static bool is_consumer(struct device *query, struct device *supplier)
return false;
}

/* recursively move the potential consumers in open section (left, right)
* after the barrier
*/
static int __device_reorder_consumer(struct device *consumer,
struct list_head *left, struct list_head *right,
struct pos_info *p)
{
struct list_head *iter;
struct device *c_dev, *s_dev, *tail_dev;

descendants_reorder_after_pos(consumer, p);
tail_dev = p->tail;
/* (left, right) may contain consumers, hence checking if any moved
* child serving as supplier. The reversing order help us to meet
* the last supplier of a consumer.
*/
list_opensect_for_each_reverse(iter, left, right) {
struct list_head *l_iter, *moved_left, *moved_right;

moved_left = (&consumer->kobj.entry)->prev;
moved_right = tail_dev->kobj.entry.next;
/* the moved section may contain potential suppliers */
list_opensect_for_each_reverse(l_iter, moved_left,
moved_right) {
s_dev = list_entry(l_iter, struct device, kobj.entry);
c_dev = list_entry(iter, struct device, kobj.entry);
/* to fix: this poses extra effort for locking */
if (is_consumer(c_dev, s_dev)) {
p->tail = NULL;
/* to fix: lock issue */
p->pos = s_dev;
/* reorder after the last supplier */
__device_reorder_consumer(c_dev,
l_iter, right, p);
}
}
}
return 0;
}

static int find_last_supplier(struct device *dev, struct device *supplier)
{
struct device_link *link;

list_for_each_entry_reverse(link, &dev->links.suppliers, c_node) {
if (link->supplier == supplier)
return 1;
}
if (dev == supplier)
return -1;
return 0;
}

/* When reodering, take care of the range of (old_pos(dev), new_pos(dev)),
* there may be requirement to recursively move item.
*/
int device_reorder_consumer(struct device *dev)
{
struct list_head *iter, *left, *right;
struct device *cur_dev;
struct pos_info info;
int ret, idx;

idx = device_links_read_lock();
if (list_empty(&dev->links.suppliers)) {
device_links_read_unlock(idx);
return 0;
}
spin_lock(&devices_kset->list_lock);
list_for_each_prev(iter, &devices_kset->list) {
cur_dev = list_entry(iter, struct device, kobj.entry);
ret = find_last_supplier(dev, cur_dev);
switch (ret) {
case -1:
goto unlock;
case 1:
break;
case 0:
continue;
}
}
BUG_ON(!ret);

/* record the affected open section */
left = dev->kobj.entry.prev;
right = iter;
info.pos = list_entry(iter, struct device, kobj.entry);
info.tail = NULL;
/* dry out the consumers in (left,right) */
__device_reorder_consumer(dev, left, right, &info);

unlock:
spin_unlock(&devices_kset->list_lock);
device_links_read_unlock(idx);
return 0;
}

static int device_reorder_to_tail(struct device *dev, void *not_used)
{
struct device_link *link;
Expand Down

0 comments on commit 1b2a1e6

Please sign in to comment.