Skip to contentSkip to conversation

Speed up link_mem_sections during boot.

PATCH Applied Robin Holt 4 participants 10 messages Sep 29, 2010 – Oct 5, 2010

Subsystems (4)
lore
Share & tools
lore

Robin Holt

author
#0 lore raw

cover Speed up link_mem_sections during boot.

During boot of a large system, we see a delay in the portion of boot which is calling link_mem_sections(). On a 16TB x86_64 system, it is taking 1 hour 27 minutes. With this patch set applied, it takes 46 seconds.

The speedup is accomplished by simply caching the results from the previous find_memory_block and using that as a quick check to see if the next block happens to be adjacent to the previous. In our testing, that was true 100% of the time.

---

 drivers/base/memory.c   |   28 ++++++++++++++++++----------
 drivers/base/node.c     |    8 +++++---
 include/linux/kobject.h |    2 ++
 include/linux/memory.h  |    2 ++
 lib/kobject.c           |   39 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+), 13 deletions(-)
author
#1 lore raw

PATCH 1/3 Introduce kset_find_obj_hinted.

One call chain getting to kset_find_obj is:
link_mem_sections()

    find_mem_section()
      kset_find_obj()

This is done during boot. The memory sections were added in a linearly increasing order and link_mem_sections tends to utilize them in that same linear order.

Introduce a kset_find_obj_hinted which is passed the result of the previous kset_find_obj which it uses for a quick "is the next object our desired object" check before falling back to the old behavior.

To: Robert P. J. Day <[email protected]>
To: Greg Kroah-Hartman <[email protected]>
Cc: lkml <[email protected]>

---

 include/linux/kobject.h |    2 +
 lib/kobject.c           |   39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
include/linux/kobject.h +2 -0
Index: pv1010359/include/linux/kobject.h
===================================================================
@@ -191,6 +191,8 @@ static inline struct kobj_type *get_ktyp
 }
 
 extern struct kobject *kset_find_obj(struct kset *, const char *);
+extern struct kobject *kset_find_obj_hinted(struct kset *, const char *,
+						struct kobject *);
 
 /* The global /sys/kernel/ kobject for people to chain off of */
 extern struct kobject *kernel_kobj;
lib/kobject.c +39 -0
Index: pv1010359/lib/kobject.c
===================================================================
@@ -746,17 +746,56 @@ void kset_unregister(struct kset *k)
  */
 struct kobject *kset_find_obj(struct kset *kset, const char *name)
 {
+	return kset_find_obj_hinted(kset, name, NULL);
+}
+
+/**
+ * kset_find_obj_hinted - search for object in kset given a predecessor hint.
+ * @kset: kset we're looking in.
+ * @name: object's name.
+ * @hint: hint to possible object's predecessor.
+ *
+ * Check the hint's next object and if it is a match return it directly,
+ * otherwise, fall back to the behavior of kset_find_obj().  Either way
+ * a reference for the returned object is held and the reference on the
+ * hinted object is released.
+ */
+struct kobject *kset_find_obj_hinted(struct kset *kset, const char *name,
+				     struct kobject *hint)
+{
 	struct kobject *k;
 	struct kobject *ret = NULL;
 
 	spin_lock(&kset->list_lock);
+
+	if (!hint)
+		goto slow_search;
+
+	/* end of list detection */
+	if (hint->entry.next == kset->list.next)
+		goto slow_search;
+
+	k = container_of(hint->entry.next, struct kobject, entry);
+	if (!kobject_name(k) || strcmp(kobject_name(k), name))
+		goto slow_search;
+
+	ret = kobject_get(k);
+	goto unlock_exit;
+
+slow_search:
 	list_for_each_entry(k, &kset->list, entry) {
 		if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
 			ret = kobject_get(k);
 			break;
 		}
 	}
+
+unlock_exit:
 	spin_unlock(&kset->list_lock);
+
+	if (hint)
+		kobject_put(hint);
+
 	return ret;
 }
 
#2 lore raw

Re: [Patch 1/3] Introduce kset_find_obj_hinted.

Quoted 36 lines · Robin Holt

struct kobject *kset_find_obj(struct kset *kset, const char *name) {

lib/kobject.c +29 -0
+       return kset_find_obj_hinted(kset, name, NULL);
+}
+
+/**
+ * kset_find_obj_hinted - search for object in kset given a predecessor hint.
+ * @kset: kset we're looking in.
+ * @name: object's name.
+ * @hint: hint to possible object's predecessor.
+ *
+ * Check the hint's next object and if it is a match return it directly,
+ * otherwise, fall back to the behavior of kset_find_obj().  Either way
+ * a reference for the returned object is held and the reference on the
+ * hinted object is released.
+ */
+struct kobject *kset_find_obj_hinted(struct kset *kset, const char *name,
+                                    struct kobject *hint)
+{
        struct kobject *k;
        struct kobject *ret = NULL;
 
        spin_lock(&kset->list_lock);
+
+       if (!hint)
+               goto slow_search;
+
+       /* end of list detection */
+       if (hint->entry.next == kset->list.next)
+               goto slow_search;
+
+       k = container_of(hint->entry.next, struct kobject, entry);
+       if (!kobject_name(k) || strcmp(kobject_name(k), name))
+               goto slow_search;
+

lib/kobject.c void kset_unregister(struct kset *k) · line 791

Quoted 10 lines
lib/kobject.c +4 -0
+       ret = kobject_get(k);
+       goto unlock_exit;
+
+slow_search:
        list_for_each_entry(k, &kset->list, entry) {
                if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
                        ret = kobject_get(k);
                        break;
                }
        }

So, the slow search ends up looking through _all_ of the sections in the order they got registered, which _guarantees_ that we'll go all the way through the list every time. Right?

I guess we could also add to the list using list_add_tail(), iterate during the search using list_for_each_tail(), or even just look at the last entry by looking at kset->list.prev directly. Those would have the advantage of not having to have several levels in the call chain. We also wouldn't have to change anything if and when this gets fixed properly.

-- Dave

author
#3 lore raw

Re: [Patch 1/3] Introduce kset_find_obj_hinted.

Quoted 52 lines · Dave Hansen
Quoted 36 lines · Robin Holt

struct kobject *kset_find_obj(struct kset *kset, const char *name) {

lib/kobject.c +29 -0
+       return kset_find_obj_hinted(kset, name, NULL);
+}
+
+/**
+ * kset_find_obj_hinted - search for object in kset given a predecessor hint.
+ * @kset: kset we're looking in.
+ * @name: object's name.
+ * @hint: hint to possible object's predecessor.
+ *
+ * Check the hint's next object and if it is a match return it directly,
+ * otherwise, fall back to the behavior of kset_find_obj().  Either way
+ * a reference for the returned object is held and the reference on the
+ * hinted object is released.
+ */
+struct kobject *kset_find_obj_hinted(struct kset *kset, const char *name,
+                                    struct kobject *hint)
+{
        struct kobject *k;
        struct kobject *ret = NULL;
 
        spin_lock(&kset->list_lock);
+
+       if (!hint)
+               goto slow_search;
+
+       /* end of list detection */
+       if (hint->entry.next == kset->list.next)
+               goto slow_search;
+
+       k = container_of(hint->entry.next, struct kobject, entry);
+       if (!kobject_name(k) || strcmp(kobject_name(k), name))
+               goto slow_search;
+

lib/kobject.c void kset_unregister(struct kset *k) · line 791

Quoted 10 lines
lib/kobject.c +4 -0
+       ret = kobject_get(k);
+       goto unlock_exit;
+
+slow_search:
        list_for_each_entry(k, &kset->list, entry) {
                if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
                        ret = kobject_get(k);
                        break;
                }
        }

So, the slow search ends up looking through _all_ of the sections in the order they got registered, which _guarantees_ that we'll go all the way through the list every time. Right?

Quoted 6 lines

I guess we could also add to the list using list_add_tail(), iterate during the search using list_for_each_tail(), or even just look at the last entry by looking at kset->list.prev directly. Those would have the advantage of not having to have several levels in the call chain. We also wouldn't have to change anything if and when this gets fixed properly.

Except that drivers/base/memory.c's memory_dev_init registers all the memory sections and then later, drivers/base/node.c's node_dev_init looks for memory sections on each node and does the symlink.

Since the two are separated in time, either the head or tail based search is going to end up with last or first part of the search sequence being much slower.

Robin

Reviewed-by on v1 1/3
#4 lore raw

Re: [Patch 1/3] Introduce kset_find_obj_hinted.

In reply to Robin Holt
Quoted 9 lines · On Wed, 29 Sep 2010 14:00:54 -0500 Robin Holt <[email protected]> wrote:

One call chain getting to kset_find_obj is:
link_mem_sections()

    find_mem_section()
      kset_find_obj()

This is done during boot. The memory sections were added in a linearly increasing order and link_mem_sections tends to utilize them in that same linear order.

Quoted 9 lines

Introduce a kset_find_obj_hinted which is passed the result of the previous kset_find_obj which it uses for a quick "is the next object our desired object" check before falling back to the old behavior.

To: Robert P. J. Day <[email protected]>
To: Greg Kroah-Hartman <[email protected]>
Cc: lkml <[email protected]>

Interesting. Thank you.

Quoted 87 lines

---

 include/linux/kobject.h |    2 +
 lib/kobject.c           |   39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

Index: pv1010359/include/linux/kobject.h
===================================================================

include/linux/kobject.h +2 -0
@@ -191,6 +191,8 @@ static inline struct kobj_type *get_ktyp
 }
 
 extern struct kobject *kset_find_obj(struct kset *, const char *);
+extern struct kobject *kset_find_obj_hinted(struct kset *, const char *,
+						struct kobject *);
 
 /* The global /sys/kernel/ kobject for people to chain off of */
 extern struct kobject *kernel_kobj;

Index: pv1010359/lib/kobject.c
===================================================================

lib/kobject.c +39 -1
@@ -746,17 +746,56 @@ void kset_unregister(struct kset *k)
  */
 struct kobject *kset_find_obj(struct kset *kset, const char *name)
 {
+	return kset_find_obj_hinted(kset, name, NULL);
+}
+
+/**
+ * kset_find_obj_hinted - search for object in kset given a predecessor hint.
+ * @kset: kset we're looking in.
+ * @name: object's name.
+ * @hint: hint to possible object's predecessor.
+ *
+ * Check the hint's next object and if it is a match return it directly,
+ * otherwise, fall back to the behavior of kset_find_obj().  Either way
+ * a reference for the returned object is held and the reference on the
+ * hinted object is released.
+ */
+struct kobject *kset_find_obj_hinted(struct kset *kset, const char *name,
+				     struct kobject *hint)
+{
 	struct kobject *k;
 	struct kobject *ret = NULL;
 
 	spin_lock(&kset->list_lock);
+
+	if (!hint)
+		goto slow_search;
+
+	/* end of list detection */
+	if (hint->entry.next == kset->list.next)
+		goto slow_search;
+
+	k = container_of(hint->entry.next, struct kobject, entry);
+	if (!kobject_name(k) || strcmp(kobject_name(k), name))
+		goto slow_search;
+
+	ret = kobject_get(k);
+	goto unlock_exit;
+
+slow_search:
 	list_for_each_entry(k, &kset->list, entry) {
 		if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
 			ret = kobject_get(k);
 			break;
 		}
 	}
+
+unlock_exit:
 	spin_unlock(&kset->list_lock);
+
+	if (hint)
+		kobject_put(hint);
+
 	return ret;
 }
 
 
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author
#5 lore raw

PATCH 2/3 Introduce find_memory_block_hinted which utilizes kset_find_obj_hinted.

In reply to Robin Holt

Introduce a find_memory_block_hinted() which utilizes the recently added kset_find_obj_hinted().

To: Dave Hansen <[email protected]>
To: Matt Tolentino <[email protected]>
Cc: lkml <[email protected]>

---

 drivers/base/memory.c  |   28 ++++++++++++++++++----------
 include/linux/memory.h |    2 ++
 2 files changed, 20 insertions(+), 10 deletions(-)
drivers/base/memory.c +18 -10
Index: pv1010359/drivers/base/memory.c
===================================================================
@@ -468,28 +468,23 @@ static int add_memory_block(int nid, str
 	return ret;
 }
 
-/*
- * For now, we have a linear search to go find the appropriate
- * memory_block corresponding to a particular phys_index. If
- * this gets to be a real problem, we can always use a radix
- * tree or something here.
- *
- * This could be made generic for all sysdev classes.
- */
-struct memory_block *find_memory_block(struct mem_section *section)
+struct memory_block *find_memory_block_hinted(struct mem_section *section,
+					      struct memory_block *hint)
 {
 	struct kobject *kobj;
 	struct sys_device *sysdev;
 	struct memory_block *mem;
 	char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
 
+	kobj = hint ? &hint->sysdev.kobj : NULL;
+
 	/*
 	 * This only works because we know that section == sysdev->id
 	 * slightly redundant with sysdev_register()
 	 */
 	sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
 
-	kobj = kset_find_obj(&memory_sysdev_class.kset, name);
+	kobj = kset_find_obj_hinted(&memory_sysdev_class.kset, name, kobj);
 	if (!kobj)
 		return NULL;
 
@@ -499,6 +494,19 @@ struct memory_block *find_memory_block(s
 	return mem;
 }
 
+/*
+ * For now, we have a linear search to go find the appropriate
+ * memory_block corresponding to a particular phys_index. If
+ * this gets to be a real problem, we can always use a radix
+ * tree or something here.
+ *
+ * This could be made generic for all sysdev classes.
+ */
+struct memory_block *find_memory_block(struct mem_section *section)
+{
+	return find_memory_block_hinted(section, NULL);
+}
+
 int remove_memory_block(unsigned long node_id, struct mem_section *section,
 		int phys_device)
 {
include/linux/memory.h +2 -0
Index: pv1010359/include/linux/memory.h
===================================================================
@@ -113,6 +113,8 @@ extern int memory_dev_init(void);
 extern int remove_memory_block(unsigned long, struct mem_section *, int);
 extern int memory_notify(unsigned long val, void *v);
 extern int memory_isolate_notify(unsigned long val, void *v);
+extern struct memory_block *find_memory_block_hinted(struct mem_section *,
+							struct memory_block *);
 extern struct memory_block *find_memory_block(struct mem_section *);
 #define CONFIG_MEM_BLOCK_SIZE	(PAGES_PER_SECTION<<PAGE_SHIFT)
 enum mem_add_context { BOOT, HOTPLUG };
Reviewed-by on v1 2/3
#6 lore raw

Re: [Patch 2/3] Introduce find_memory_block_hinted which utilizes kset_find_obj_hinted.

Quoted 9 lines · On Wed, 29 Sep 2010 14:00:55 -0500 Robin Holt <[email protected]> wrote:

Introduce a find_memory_block_hinted() which utilizes the recently added kset_find_obj_hinted().

To: Dave Hansen <[email protected]>
To: Matt Tolentino <[email protected]>
Cc: lkml <[email protected]>

author
#7 lore raw

PATCH 3/3 Convert link_mem_sections to use find_memory_block_hinted.

In reply to Robin Holt

Modify link_mem_sections() to pass in the previous mem_block as a hint to locating the next mem_block. Since they are typically added in order this results in a massive saving in time during boot of a very large system. For example, on a 16TB x86_64 machine, it reduced the total time spent linking all node's memory sections from 1 hour, 27 minutes to 46 seconds.

To: Gary Hade <[email protected]>
To: Badari Pulavarty <[email protected]>
To: Ingo Molnar <[email protected]>
Cc: lkml <[email protected]>

---

 drivers/base/node.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/base/node.c +5 -3
Index: pv1010359/drivers/base/node.c
===================================================================
@@ -409,25 +409,27 @@ static int link_mem_sections(int nid)
 	unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
 	unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
 	unsigned long pfn;
+	struct memory_block *mem_blk = NULL;
 	int err = 0;
 
 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
 		unsigned long section_nr = pfn_to_section_nr(pfn);
 		struct mem_section *mem_sect;
-		struct memory_block *mem_blk;
 		int ret;
 
 		if (!present_section_nr(section_nr))
 			continue;
 		mem_sect = __nr_to_section(section_nr);
-		mem_blk = find_memory_block(mem_sect);
+		mem_blk = find_memory_block_hinted(mem_sect, mem_blk);
 		ret = register_mem_sect_under_node(mem_blk, nid);
 		if (!err)
 			err = ret;
 
 		/* discard ref obtained in find_memory_block() */
-		kobject_put(&mem_blk->sysdev.kobj);
 	}
+
+	if (mem_blk)
+		kobject_put(&mem_blk->sysdev.kobj);
 	return err;
 }
 
Reviewed-by on v1 3/3
#8 lore raw

Re: [Patch 3/3] Convert link_mem_sections to use find_memory_block_hinted.

Quoted 7 lines · On Wed, 29 Sep 2010 14:00:56 -0500 Robin Holt <[email protected]> wrote:

Modify link_mem_sections() to pass in the previous mem_block as a hint to locating the next mem_block. Since they are typically added in order this results in a massive saving in time during boot of a very large system. For example, on a 16TB x86_64 machine, it reduced the total time spent linking all node's memory sections from 1 hour, 27 minutes to 46 seconds.

Quoted 6 lines

To: Gary Hade <[email protected]>
To: Badari Pulavarty <[email protected]>
To: Ingo Molnar <[email protected]>
Cc: lkml <[email protected]>

#9 lore raw

patch "driver core: Convert link_mem_sections to use find_memory_block_hinted." added to gregkh-2.6 tree

In reply to Robin Holt

This is a note to let you know that I've just added the patch titled

driver core: Convert link_mem_sections to use find_memory_block_hinted.

to my gregkh-2.6 tree which can be found in directory form at:
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/
and in git form at:
git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/patches.git

The filename of this patch is:
driver-core-convert-link_mem_sections-to-use-find_memory_block_hinted.patch

The patch will show up in the next release of the linux-next tree (usually sometime within the next 24 hours during the week.)

If this patch meets the merge guidelines for a bugfix, it should be merged into Linus's tree before the next major kernel release. If not, it will be merged into Linus's tree during the next merge window.

Either way, you will probably be copied on the patch when it gets sent to Linus for merging so that others can see what is happening in kernel development.

If you have any questions about this process, please let me know.

Quoted 1 line

From [email protected] Tue Oct 5 15:51:00 2010

Message-Id: <[email protected]>
Date: Wed, 29 Sep 2010 14:00:56 -0500
From: Robin Holt <[email protected]>
To: lkml <[email protected]>,
Gary Hade <[email protected]>,
Badari Pulavarty <[email protected]>, Ingo Molnar <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>,
Dave Hansen <[email protected]>,
Matt Tolentino <[email protected]>,
"Robert P. J. Day" <[email protected]>
Subject: driver core: Convert link_mem_sections to use find_memory_block_hinted.

Modify link_mem_sections() to pass in the previous mem_block as a hint to locating the next mem_block. Since they are typically added in order this results in a massive saving in time during boot of a very large system. For example, on a 16TB x86_64 machine, it reduced the total time spent linking all node's memory sections from 1 hour, 27 minutes to 46 seconds.

To: Gary Hade <[email protected]>
To: Badari Pulavarty <[email protected]>
To: Ingo Molnar <[email protected]>

---
 drivers/base/node.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/base/node.c +5 -3
@@ -409,25 +409,27 @@ static int link_mem_sections(int nid)
 	unsigned long start_pfn = NODE_DATA(nid)->node_start_pfn;
 	unsigned long end_pfn = start_pfn + NODE_DATA(nid)->node_spanned_pages;
 	unsigned long pfn;
+	struct memory_block *mem_blk = NULL;
 	int err = 0;
 
 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
 		unsigned long section_nr = pfn_to_section_nr(pfn);
 		struct mem_section *mem_sect;
-		struct memory_block *mem_blk;
 		int ret;
 
 		if (!present_section_nr(section_nr))
 			continue;
 		mem_sect = __nr_to_section(section_nr);
-		mem_blk = find_memory_block(mem_sect);
+		mem_blk = find_memory_block_hinted(mem_sect, mem_blk);
 		ret = register_mem_sect_under_node(mem_blk, nid);
 		if (!err)
 			err = ret;
 
 		/* discard ref obtained in find_memory_block() */
-		kobject_put(&mem_blk->sysdev.kobj);
 	}
+
+	if (mem_blk)
+		kobject_put(&mem_blk->sysdev.kobj);
 	return err;
 }