linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Daniel Mentz <danielmentz@google.com>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	Andi Kleen <ak@linux.intel.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Arnd Bergmann <arnd@arndb.de>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Dan Williams <dan.j.williams@intel.com>,
	David Riley <davidriley@chromium.org>,
	Eric Miao <eric.y.miao@gmail.com>,
	Grant Likely <grant.likely@linaro.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Haojian Zhuang <haojian.zhuang@gmail.com>,
	Huang Ying <ying.huang@intel.com>,
	Jaroslav Kysela <perex@perex.cz>,
	Kevin Hilman <khilman@deeprootsystems.com>,
	Laura Abbott <lauraa@codeaurora.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Mauro Carvalho Chehab <m.chehab@samsung.com>,
	Olof Johansson <olof@lixom.net>,
	Ritesh Harjain <ritesh.harjani@gmail.com>,
	Rob Herring <rob.herring@calxeda.com>,
	Russell King <linux@arm.linux.org.uk>,
	Sekhar Nori <nsekhar@ti.com>, Takashi Iwai <tiwai@suse.de>,
	Thadeu Lima de Souza Cascardo <cascardo@linux.vnet.ibm.com>,
	Thierry Reding <thierry.reding@gmail.com>,
	Vinod Koul <vinod.koul@intel.com>,
	Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>,
	Will Deacon <will.deacon@arm.com>
Subject: Re: [PATCH] lib/genalloc.c: Start search from start of chunk
Date: Tue, 25 Oct 2016 12:29:10 +0000 (UTC)	[thread overview]
Message-ID: <325247067.2674.1477398550882.JavaMail.zimbra@efficios.com> (raw)
In-Reply-To: <1477360693-17645-1-git-send-email-danielmentz@google.com>

----- On Oct 24, 2016, at 9:58 PM, Daniel Mentz danielmentz@google.com wrote:

> gen_pool_alloc_algo() iterates over all chunks of a pool trying to find
> a contiguous block of memory that satisfies the allocation request.
> The search should start at address zero of every chunk. However, as the
> code stands today, this is only true for the first chunk. Due to a bug,
> the search of subsequent chunks starts somewhere else:

So in a situation where a chunk has enough bytes left to fulfill the
request, but they are not contiguous, the check:

                if (size > atomic_read(&chunk->avail))
                        continue;

would not trigger, and we'd end up setting start_bit to the value end_bit
after returning from the algo() call.

So if the following chunks have the same size as the nearly full chunk,
we end up failing memory allocation for all following chunks even
though there is plenty of room left.

I would be tempted to add a bit of explanation on the failure
modes to the commit message (e.g. scenario above).

Other than that:

Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

Thanks!

Mathieu

> 
> The variables start_bit and end_bit are meant to describe the range that
> should be searched and should be reset for every chunk that is searched.
> Today, the code fails to reset start_bit to 0.
> 
> Fixes: 7f184275aa30 ("lib, Make gen_pool memory allocator lockless")
> Cc: Andi Kleen <ak@linux.intel.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Dan Williams <dan.j.williams@intel.com>
> Cc: David Riley <davidriley@chromium.org>
> Cc: Eric Miao <eric.y.miao@gmail.com>
> Cc: Grant Likely <grant.likely@linaro.org>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Haojian Zhuang <haojian.zhuang@gmail.com>
> Cc: Huang Ying <ying.huang@intel.com>
> Cc: Jaroslav Kysela <perex@perex.cz>
> Cc: Kevin Hilman <khilman@deeprootsystems.com>
> Cc: Laura Abbott <lauraa@codeaurora.org>
> Cc: Liam Girdwood <lgirdwood@gmail.com>
> Cc: Mark Brown <broonie@kernel.org>
> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> Cc: Mauro Carvalho Chehab <m.chehab@samsung.com>
> Cc: Olof Johansson <olof@lixom.net>
> Cc: Ritesh Harjain <ritesh.harjani@gmail.com>
> Cc: Rob Herring <rob.herring@calxeda.com>
> Cc: Russell King <linux@arm.linux.org.uk>
> Cc: Sekhar Nori <nsekhar@ti.com>
> Cc: Takashi Iwai <tiwai@suse.de>
> Cc: Thadeu Lima de Souza Cascardo <cascardo@linux.vnet.ibm.com>
> Cc: Thierry Reding <thierry.reding@gmail.com>
> Cc: Vinod Koul <vinod.koul@intel.com>
> Cc: Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>
> Cc: Will Deacon <will.deacon@arm.com>
> Signed-off-by: Daniel Mentz <danielmentz@google.com>
> ---
> lib/genalloc.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/genalloc.c b/lib/genalloc.c
> index 0a11396..144fe6b 100644
> --- a/lib/genalloc.c
> +++ b/lib/genalloc.c
> @@ -292,7 +292,7 @@ unsigned long gen_pool_alloc_algo(struct gen_pool *pool,
> size_t size,
> 	struct gen_pool_chunk *chunk;
> 	unsigned long addr = 0;
> 	int order = pool->min_alloc_order;
> -	int nbits, start_bit = 0, end_bit, remain;
> +	int nbits, start_bit, end_bit, remain;
> 
> #ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
> 	BUG_ON(in_nmi());
> @@ -307,6 +307,7 @@ unsigned long gen_pool_alloc_algo(struct gen_pool *pool,
> size_t size,
> 		if (size > atomic_read(&chunk->avail))
> 			continue;
> 
> +		start_bit = 0;
> 		end_bit = chunk_size(chunk) >> order;
> retry:
> 		start_bit = algo(chunk->bits, end_bit, start_bit,
> --
> 2.8.0.rc3.226.g39d4020

-- 
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com

  reply	other threads:[~2016-10-25 12:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-25  1:58 [PATCH] lib/genalloc.c: Start search from start of chunk Daniel Mentz
2016-10-25 12:29 ` Mathieu Desnoyers [this message]
2016-10-25 18:36   ` Daniel Mentz
2016-10-26 18:09     ` Will Deacon
2016-10-26 19:39       ` Andrew Morton
2016-10-26 22:24         ` Daniel Mentz

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=325247067.2674.1477398550882.JavaMail.zimbra@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=ak@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=broonie@kernel.org \
    --cc=cascardo@linux.vnet.ibm.com \
    --cc=catalin.marinas@arm.com \
    --cc=dan.j.williams@intel.com \
    --cc=danielmentz@google.com \
    --cc=davidriley@chromium.org \
    --cc=eric.y.miao@gmail.com \
    --cc=grant.likely@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=haojian.zhuang@gmail.com \
    --cc=khilman@deeprootsystems.com \
    --cc=lauraa@codeaurora.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@arm.linux.org.uk \
    --cc=m.chehab@samsung.com \
    --cc=nsekhar@ti.com \
    --cc=olof@lixom.net \
    --cc=perex@perex.cz \
    --cc=ritesh.harjani@gmail.com \
    --cc=rob.herring@calxeda.com \
    --cc=thierry.reding@gmail.com \
    --cc=tiwai@suse.de \
    --cc=vinod.koul@intel.com \
    --cc=vladimir_zapolskiy@mentor.com \
    --cc=will.deacon@arm.com \
    --cc=ying.huang@intel.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).