linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Gavin Shan <shangw@linux.vnet.ibm.com>
To: Michal Hocko <mhocko@suse.cz>
Cc: Gavin Shan <shangw@linux.vnet.ibm.com>,
	linux-mm@kvack.org, dave@linux.vnet.ibm.com, rientjes@google.com,
	akpm@linux-foundation.org
Subject: Re: [PATCH v3 2/3] mm/sparse: fix possible memory leak
Date: Tue, 3 Jul 2012 11:38:23 +0800	[thread overview]
Message-ID: <20120703033823.GA497@shangw> (raw)
In-Reply-To: <20120702154628.GE8050@tiehlicka.suse.cz>

On Mon, Jul 02, 2012 at 05:46:28PM +0200, Michal Hocko wrote:
>On Mon 02-07-12 21:40:53, Gavin Shan wrote:
>> On Mon, Jul 02, 2012 at 11:43:31AM +0200, Michal Hocko wrote:
>> >On Mon 02-07-12 17:28:56, Gavin Shan wrote:
>> >> 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.
>> >
>> 
>> 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. 
>
>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.
>

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




>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.
>
>> The one that loses the race will leak the "section" that
>> sparse_index_alloc() allocated for it. This patch fixes that leak.
>
>> 
>> -----
>> 
>> Thanks,
>> Gavin
>> 
>> >> 
>> >> Signed-off-by: Gavin Shan <shangw@linux.vnet.ibm.com>
>> >> ---
>> >>  mm/sparse.c |   17 +++++++++++++++++
>> >>  1 file changed, 17 insertions(+)
>> >> 
>> >> diff --git a/mm/sparse.c b/mm/sparse.c
>> >> index 781fa04..a6984d9 100644
>> >> --- a/mm/sparse.c
>> >> +++ b/mm/sparse.c
>> >> @@ -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 */
>> >> -- 
>> >> 1.7.9.5
>> >> 
>> >> --
>> >> To unsubscribe, send a message with 'unsubscribe linux-mm' in
>> >> the body to majordomo@kvack.org.  For more info on Linux MM,
>> >> see: http://www.linux-mm.org/ .
>> >> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
>> >
>> >-- 
>> >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 majordomo@kvack.org.  For more info on Linux MM,
>> >see: http://www.linux-mm.org/ .
>> >Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
>> >
>> 
>> --
>> To unsubscribe, send a message with 'unsubscribe linux-mm' in
>> the body to majordomo@kvack.org.  For more info on Linux MM,
>> see: http://www.linux-mm.org/ .
>> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
>
>-- 
>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 majordomo@kvack.org.  For more info on Linux MM,
>see: http://www.linux-mm.org/ .
>Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
>

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

  reply	other threads:[~2012-07-03  3:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-02  9:28 [PATCH v3 1/3] mm/sparse: optimize sparse_index_alloc Gavin Shan
2012-07-02  9:28 ` [PATCH v3 2/3] mm/sparse: fix possible memory leak Gavin Shan
2012-07-02  9:43   ` Michal Hocko
2012-07-02 13:40     ` Gavin Shan
2012-07-02 15:46       ` Michal Hocko
2012-07-03  3:38         ` Gavin Shan [this message]
2012-07-03 12:51           ` Michal Hocko
2012-07-02 11:04   ` David Rientjes
2012-07-02 13:28     ` Gavin Shan
2012-07-02 21:19       ` David Rientjes
2012-07-03  1:19         ` Gavin Shan
2012-07-02  9:28 ` [PATCH v3 3/3] mm/sparse: more check on mem_section number Gavin Shan
2012-07-02 11:05   ` David Rientjes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120703033823.GA497@shangw \
    --to=shangw@linux.vnet.ibm.com \
    --cc=akpm@linux-foundation.org \
    --cc=dave@linux.vnet.ibm.com \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.cz \
    --cc=rientjes@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).