Skip to contentSkip to conversation

mm/sparse: optimize sparse_index_alloc

PATCH Superseded Gavin Shan 3 participants 13 messages Jul 2, 2012 – Jul 3, 2012

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

PATCH v3 1/3 mm/sparse: optimize sparse_index_alloc

With CONFIG_SPARSEMEM_EXTREME, the two level of memory section descriptors are allocated from slab or bootmem. When allocating from slab, let slab/bootmem allocator to clear the memory chunk. We needn't clear that explicitly.

---
 mm/sparse.c |   10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
mm/sparse.c +4 -6
index 6a4bf91..781fa04 100644
@@ -65,14 +65,12 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 
 	if (slab_is_available()) {
 		if (node_state(nid, N_HIGH_MEMORY))
-			section = kmalloc_node(array_size, GFP_KERNEL, nid);
+			section = kzalloc_node(array_size, GFP_KERNEL, nid);
 		else
-			section = kmalloc(array_size, GFP_KERNEL);
-	} else
+			section = kzalloc(array_size, GFP_KERNEL);
+	} else {
 		section = alloc_bootmem_node(NODE_DATA(nid), array_size);
-
-	if (section)
-		memset(section, 0, array_size);
+	}
 
 	return section;
 }
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#1 lore raw

PATCH v3 2/3 mm/sparse: fix possible memory leak

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

	mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, the one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

---
 mm/sparse.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
mm/sparse.c +17 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);
+}
+
 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 {
 	static DEFINE_SPINLOCK(index_init_lock);
@@ -102,6 +116,9 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	mem_section[root] = section;
 out:
 	spin_unlock(&index_init_lock);
+	if (ret)
+		sparse_index_free(section);
+
 	return ret;
 }
 #else /* !SPARSEMEM_EXTREME */
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

Quoted 9 lines · Gavin Shan

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

	mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, the one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

I would still like to hear how we can possibly race in this code path. I've thought that memory onlining is done from a single CPU.

Quoted 49 lines
---
 mm/sparse.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
mm/sparse.c +17 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);
+}
+
 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 {
 	static DEFINE_SPINLOCK(index_init_lock);
@@ -102,6 +116,9 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	mem_section[root] = section;
 out:
 	spin_unlock(&index_init_lock);
+	if (ret)
+		sparse_index_free(section);
+
 	return ret;
 }
 #else /* !SPARSEMEM_EXTREME */
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#3 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

Quoted 5 lines · Michal Hocko
Quoted 4 lines · Gavin Shan

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

Quoted 9 lines
Quoted 5 lines
	mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, the one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

I would still like to hear how we can possibly race in this code path. I've thought that memory onlining is done from a single CPU.

Hi Michael, how about to use the following changelog? :-)

-----

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, which is probablly caused by making online for multiple memory sections that depend on same entry of array mem_section[] simultaneously from different CPUs. The one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

-----

Thanks,
Gavin

Quoted 44 lines
Quoted 44 lines
---
 mm/sparse.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
mm/sparse.c +17 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);
+}
+
 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 {
 	static DEFINE_SPINLOCK(index_init_lock);
@@ -102,6 +116,9 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	mem_section[root] = section;
 out:
 	spin_unlock(&index_init_lock);
+	if (ret)
+		sparse_index_free(section);
+
 	return ret;
 }
 #else /* !SPARSEMEM_EXTREME */
Signature · 1 line
1.7.9.5
Quoted 20 lines
Quoted 5 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

Quoted 24 lines · Gavin Shan
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Gavin Shan

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

Quoted 9 lines
Quoted 5 lines
	mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, the one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

I would still like to hear how we can possibly race in this code path. I've thought that memory onlining is done from a single CPU.

Hi Michael, how about to use the following changelog? :-)

-----

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

Quoted 6 lines

mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, which is probablly caused by making online for multiple memory sections that depend on same entry of array mem_section[] simultaneously from different CPUs.

And you really think that this clarified the things? You have just tweaked the comment to sound more obscure.

OK, so you have pushed me into the code...
If you had looked into the hotplug callchain up to add_memory you would have seen that the whole arch_add_memory -> __add_pages -> ... -> sparse_index_init is called with lock_memory_hotplug held so the hotplug cannot run from the multiple CPUs.

I do not see any other users apart from boot time sparse_memory_present_with_active_regions and add_memory so I think that the lock is just a heritage from old days.

So please make sure you are fixing a real issue rather than add another code which simply never gets executed.

And no obscuring the changelog doesn't help anybody.

Quoted 79 lines

The one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

-----

Thanks,
Gavin

Quoted 44 lines
Quoted 44 lines
---
 mm/sparse.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
mm/sparse.c +17 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);
+}
+
 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 {
 	static DEFINE_SPINLOCK(index_init_lock);
@@ -102,6 +116,9 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	mem_section[root] = section;
 out:
 	spin_unlock(&index_init_lock);
+	if (ret)
+		sparse_index_free(section);
+
 	return ret;
 }
 #else /* !SPARSEMEM_EXTREME */
Signature · 1 line
1.7.9.5
Quoted 20 lines
Quoted 5 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#5 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

Quoted 41 lines · Michal Hocko
Quoted 24 lines · Gavin Shan
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Gavin Shan

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

Quoted 9 lines
Quoted 5 lines
	mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, the one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

I would still like to hear how we can possibly race in this code path. I've thought that memory onlining is done from a single CPU.

Hi Michael, how about to use the following changelog? :-)

-----

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

Quoted 6 lines

mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, which is probablly caused by making online for multiple memory sections that depend on same entry of array mem_section[] simultaneously from different CPUs.

And you really think that this clarified the things? You have just tweaked the comment to sound more obscure.

OK, so you have pushed me into the code...
If you had looked into the hotplug callchain up to add_memory you would have seen that the whole arch_add_memory -> __add_pages -> ... -> sparse_index_init is called with lock_memory_hotplug held so the hotplug cannot run from the multiple CPUs.

Quoted 4 lines

I do not see any other users apart from boot time sparse_memory_present_with_active_regions and add_memory so I think that the lock is just a heritage from old days.

I just had quick go-through on the source code as you suggested and I think you're right, Michal. So please drop this :-)

With CONFIG_ARCH_MEMORY_PROBE enabled on Power machines, following functions would be included in hotplug path.

memory_probe_store
add_memory
lock_memory_hotplug /* protect the whole hotplug path */
arch_add_memory
__add_pages
__add_section
sparse_add_one_section
sparse_index_init
sparse_index_alloc

The mutex "mem_hotplug_mutex" will be hold by lock_memory_hotplug() to protect the whole hotplug path. However, I'm wandering if we can remove the "index_init_lock" of function sparse_index_init() since that sounds duplicate lock.

static int __meminit sparse_index_init(unsigned long section_nr, int nid)

{
        static DEFINE_SPINLOCK(index_init_lock);
        unsigned long root = SECTION_NR_TO_ROOT(section_nr);
        struct mem_section *section;
        int ret = 0;

        if (mem_section[root])
                return -EEXIST;

        section = sparse_index_alloc(nid);
        if (!section)
                return -ENOMEM;
        /*
         * This lock keeps two different sections from
         * reallocating for the same index
         */
        spin_lock(&index_init_lock);

        if (mem_section[root]) {
                ret = -EEXIST;
                goto out;
        }

        mem_section[root] = section;
out:
        spin_unlock(&index_init_lock);
        if (ret)
                sparse_index_free(section);

        return ret;
}

Thanks,
Gavin

Quoted 79 lines

So please make sure you are fixing a real issue rather than add another code which simply never gets executed.

And no obscuring the changelog doesn't help anybody.

Quoted 74 lines

The one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

-----

Thanks,
Gavin

Quoted 64 lines
Quoted 49 lines
---
 mm/sparse.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
mm/sparse.c +17 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);
+}
+
 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 {
 	static DEFINE_SPINLOCK(index_init_lock);
@@ -102,6 +116,9 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	mem_section[root] = section;
 out:
 	spin_unlock(&index_init_lock);
+	if (ret)
+		sparse_index_free(section);
+
 	return ret;
 }
 #else /* !SPARSEMEM_EXTREME */
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 20 lines
Quoted 5 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

Quoted 47 lines · Gavin Shan
Quoted 41 lines · Michal Hocko
Quoted 24 lines · Gavin Shan
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Gavin Shan

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

Quoted 9 lines
Quoted 5 lines
	mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, the one that loses the race will leak the "section" that sparse_index_alloc() allocated for it. This patch fixes that leak.

I would still like to hear how we can possibly race in this code path. I've thought that memory onlining is done from a single CPU.

Hi Michael, how about to use the following changelog? :-)

-----

sparse_index_init() is designed to be safe if two copies of it race. It uses "index_init_lock" to ensure that, even in the case of a race, only one CPU will manage to do:

Quoted 6 lines

mem_section[root] = section;

However, in the case where two copies of sparse_index_init() _do_ race, which is probablly caused by making online for multiple memory sections that depend on same entry of array mem_section[] simultaneously from different CPUs.

And you really think that this clarified the things? You have just tweaked the comment to sound more obscure.

OK, so you have pushed me into the code...
If you had looked into the hotplug callchain up to add_memory you would have seen that the whole arch_add_memory -> __add_pages -> ... -> sparse_index_init is called with lock_memory_hotplug held so the hotplug cannot run from the multiple CPUs.

Quoted 4 lines

I do not see any other users apart from boot time sparse_memory_present_with_active_regions and add_memory so I think that the lock is just a heritage from old days.

Quoted 5 lines

I just had quick go-through on the source code as you suggested and I think you're right, Michal. So please drop this :-)

With CONFIG_ARCH_MEMORY_PROBE enabled on Power machines, following functions would be included in hotplug path.

I am not sure why you are mentioning Power arch here, add_memory which does the locking is arch independent.

Quoted 11 lines

memory_probe_store
add_memory
lock_memory_hotplug /* protect the whole hotplug path */
arch_add_memory
__add_pages
__add_section
sparse_add_one_section
sparse_index_init
sparse_index_alloc

Quoted 5 lines

The mutex "mem_hotplug_mutex" will be hold by lock_memory_hotplug() to protect the whole hotplug path.

However, I'm wandering if we can remove the "index_init_lock" of function sparse_index_init() since that sounds duplicate lock.

Heh, that's what I am asking from the very beginning... I do not see any purpose of the lock but I might be missing something. So make sure you really understand the locking of this code if you are going to send a patch to remove the lock.

Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

In reply to Gavin Shan
Quoted 10 lines · Gavin Shan
mm/sparse.c +2 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{

mm/sparse.c static struct mem_section noinline __init_refok *sparse_index_alloc(int nid) · line 89

Quoted 10 lines
mm/sparse.c +10 -0
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);

Eek, does that work?

Quoted 6 lines
mm/sparse.c +2 -0
+}
+
 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 {
 	static DEFINE_SPINLOCK(index_init_lock);

mm/sparse.c static int __meminit sparse_index_init(unsigned long section_nr, int nid) · line 124

Quoted 9 lines
mm/sparse.c +3 -0
@@ -102,6 +116,9 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	mem_section[root] = section;
 out:
 	spin_unlock(&index_init_lock);
+	if (ret)
+		sparse_index_free(section);
+
 	return ret;
 }
 #else /* !SPARSEMEM_EXTREME */

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#8 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

Quoted 8 lines
Quoted 8 lines
mm/sparse.c +0 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
Quoted 15 lines

mm/sparse.c static struct mem_section noinline __init_refok *sparse_index_alloc(int nid) · line 89

Quoted 12 lines
mm/sparse.c +12 -0
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);

Eek, does that work?

David, I think it's working fine. If my understanding is wrong, please correct me. Thanks a lot :-)

The "section" allocated from the bootmem allocator might take following function call path. In the function alloc_bootmem_core(), all online nodes will be checked for the memory allocation. So we could have memory allocated from different node other than the specified one to alloc_bootmem_node()

alloc_bootmem_node(nid, size)
__alloc_bootmem_node()
___alloc_bootmem_node_nopanic()
alloc_bootmem_core()

On the other hand, function free_bootmem() checks which node the memory block belongs to and then free it into that node. That looks reasonable.

Thanks,
Gavin

Quoted 13 lines
Quoted 13 lines
mm/sparse.c +5 -0
+}
+
 static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 {
 	static DEFINE_SPINLOCK(index_init_lock);
@@ -102,6 +116,9 @@ static int __meminit sparse_index_init(unsigned long section_nr, int nid)
 	mem_section[root] = section;
 out:
 	spin_unlock(&index_init_lock);
+	if (ret)
+		sparse_index_free(section);
+
 	return ret;
Quoted 9 lines

mm/sparse.c static int __meminit sparse_index_init(unsigned long section_nr, int nid) · line 124

Quoted 2 lines
mm/sparse.c +0 -0
 }
 #else /* !SPARSEMEM_EXTREME */

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

Quoted 24 lines · Gavin Shan
Quoted 8 lines
Quoted 8 lines
mm/sparse.c +0 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
Quoted 15 lines

mm/sparse.c static struct mem_section noinline __init_refok *sparse_index_alloc(int nid) · line 89

Quoted 12 lines
mm/sparse.c +12 -0
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);

Eek, does that work?

Quoted 3 lines

David, I think it's working fine. If my understanding is wrong, please correct me. Thanks a lot :-)

I'm thinking it should be free_bootmem(__pa(section), size);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#10 lore raw

Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak

Quoted 26 lines · David Rientjes
Quoted 24 lines · Gavin Shan
Quoted 23 lines
Quoted 10 lines
mm/sparse.c +2 -0
index 781fa04..a6984d9 100644
@@ -75,6 +75,20 @@ static struct mem_section noinline __init_refok *sparse_index_alloc(int nid)
 	return section;
 }
 
+static inline void __meminit sparse_index_free(struct mem_section *section)
+{

mm/sparse.c static struct mem_section noinline __init_refok *sparse_index_alloc(int nid) · line 89

Quoted 10 lines
mm/sparse.c +10 -0
+	unsigned long size = SECTIONS_PER_ROOT *
+			     sizeof(struct mem_section);
+
+	if (!section)
+		return;
+
+	if (slab_is_available())
+		kfree(section);
+	else
+		free_bootmem(virt_to_phys(section), size);

Eek, does that work?

Quoted 6 lines
Quoted 3 lines

David, I think it's working fine. If my understanding is wrong, please correct me. Thanks a lot :-)

I'm thinking it should be free_bootmem(__pa(section), size);

Thanks for pointing it out, David.

Thanks,
Gavin

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

author
#11 lore raw

PATCH v3 3/3 mm/sparse: more check on mem_section number

In reply to Gavin Shan

Function __section_nr() was implemented to retrieve the corresponding memory section number according to its descriptor. It's possible that the specified memory section descriptor isn't existing in the global array. So here to add more check on that and report error for wrong case.

---
 mm/sparse.c |    2 ++
 1 file changed, 2 insertions(+)
mm/sparse.c +2 -0
index a6984d9..f2525fd 100644
@@ -147,6 +147,8 @@ int __section_nr(struct mem_section* ms)
 		     break;
 	}
 
+	VM_BUG_ON(root_nr == NR_SECTION_ROOTS);
+
 	return (root_nr * SECTIONS_PER_ROOT) + (ms - root);
 }
 
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v3 3/3
#12 lore raw

Re: [PATCH v3 3/3] mm/sparse: more check on mem_section number

Quoted 7 lines · Gavin Shan

Function __section_nr() was implemented to retrieve the corresponding memory section number according to its descriptor. It's possible that the specified memory section descriptor isn't existing in the global array. So here to add more check on that and report error for wrong case.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>