diff mbox series

[02/12] percpu: do not search past bitmap when allocating an area

Message ID 20190228021839.55779-3-dennis@kernel.org (mailing list archive)
State New, archived
Headers show
Series introduce percpu block scan_hint | expand

Commit Message

Dennis Zhou Feb. 28, 2019, 2:18 a.m. UTC
pcpu_find_block_fit() guarantees that a fit is found within
PCPU_BITMAP_BLOCK_BITS. Iteration is used to determine the first fit as
it compares against the block's contig_hint. This can lead to
incorrectly scanning past the end of the bitmap. The behavior was okay
given the check after for bit_off >= end and the correctness of the
hints from pcpu_find_block_fit().

This patch fixes this by bounding the end offset by the number of bits
in a chunk.

Signed-off-by: Dennis Zhou <dennis@kernel.org>
---
 mm/percpu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Peng Fan March 2, 2019, 1:32 p.m. UTC | #1
Hi Dennis,

> -----Original Message-----
> From: owner-linux-mm@kvack.org [mailto:owner-linux-mm@kvack.org] On
> Behalf Of Dennis Zhou
> Sent: 2019年2月28日 10:18
> To: Dennis Zhou <dennis@kernel.org>; Tejun Heo <tj@kernel.org>; Christoph
> Lameter <cl@linux.com>
> Cc: Vlad Buslov <vladbu@mellanox.com>; kernel-team@fb.com;
> linux-mm@kvack.org; linux-kernel@vger.kernel.org
> Subject: [PATCH 02/12] percpu: do not search past bitmap when allocating an
> area
> 
> pcpu_find_block_fit() guarantees that a fit is found within
> PCPU_BITMAP_BLOCK_BITS. Iteration is used to determine the first fit as it
> compares against the block's contig_hint. This can lead to incorrectly scanning
> past the end of the bitmap. The behavior was okay given the check after for
> bit_off >= end and the correctness of the hints from pcpu_find_block_fit().
> 
> This patch fixes this by bounding the end offset by the number of bits in a
> chunk.
> 
> Signed-off-by: Dennis Zhou <dennis@kernel.org>
> ---
>  mm/percpu.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/percpu.c b/mm/percpu.c
> index 53bd79a617b1..69ca51d238b5 100644
> --- a/mm/percpu.c
> +++ b/mm/percpu.c
> @@ -988,7 +988,8 @@ static int pcpu_alloc_area(struct pcpu_chunk *chunk,
> int alloc_bits,
>  	/*
>  	 * Search to find a fit.
>  	 */
> -	end = start + alloc_bits + PCPU_BITMAP_BLOCK_BITS;
> +	end = min_t(int, start + alloc_bits + PCPU_BITMAP_BLOCK_BITS,
> +		    pcpu_chunk_map_bits(chunk));
>  	bit_off = bitmap_find_next_zero_area(chunk->alloc_map, end, start,
>  					     alloc_bits, align_mask);
>  	if (bit_off >= end)
> --

From pcpu_alloc_area itself, I think this is correct to avoid bitmap_find_next_zero_area
scan past the boundaries of alloc_map, so

Reviewed-by: Peng Fan <peng.fan@nxp.com>

There are a few points I did not understand well,
Per understanding pcpu_find_block_fit is to find the first bit off in a chunk which could satisfy
the bits allocation, so bits might be larger than PCPU_BITMAP_BLOCK_BITS. And if
pcpu_find_block_fit returns a good off, it means there is a area in the chunk could satisfy
the bits allocation, then the following pcpu_alloc_area will not scan past the boundaries of
alloc_map, right?

Thanks,
Peng.

> 2.17.1
Dennis Zhou March 2, 2019, 10:23 p.m. UTC | #2
On Sat, Mar 02, 2019 at 01:32:04PM +0000, Peng Fan wrote:
> Hi Dennis,
> 
> > -----Original Message-----
> > From: owner-linux-mm@kvack.org [mailto:owner-linux-mm@kvack.org] On
> > Behalf Of Dennis Zhou
> > Sent: 2019年2月28日 10:18
> > To: Dennis Zhou <dennis@kernel.org>; Tejun Heo <tj@kernel.org>; Christoph
> > Lameter <cl@linux.com>
> > Cc: Vlad Buslov <vladbu@mellanox.com>; kernel-team@fb.com;
> > linux-mm@kvack.org; linux-kernel@vger.kernel.org
> > Subject: [PATCH 02/12] percpu: do not search past bitmap when allocating an
> > area
> > 
> > pcpu_find_block_fit() guarantees that a fit is found within
> > PCPU_BITMAP_BLOCK_BITS. Iteration is used to determine the first fit as it
> > compares against the block's contig_hint. This can lead to incorrectly scanning
> > past the end of the bitmap. The behavior was okay given the check after for
> > bit_off >= end and the correctness of the hints from pcpu_find_block_fit().
> > 
> > This patch fixes this by bounding the end offset by the number of bits in a
> > chunk.
> > 
> > Signed-off-by: Dennis Zhou <dennis@kernel.org>
> > ---
> >  mm/percpu.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/mm/percpu.c b/mm/percpu.c
> > index 53bd79a617b1..69ca51d238b5 100644
> > --- a/mm/percpu.c
> > +++ b/mm/percpu.c
> > @@ -988,7 +988,8 @@ static int pcpu_alloc_area(struct pcpu_chunk *chunk,
> > int alloc_bits,
> >  	/*
> >  	 * Search to find a fit.
> >  	 */
> > -	end = start + alloc_bits + PCPU_BITMAP_BLOCK_BITS;
> > +	end = min_t(int, start + alloc_bits + PCPU_BITMAP_BLOCK_BITS,
> > +		    pcpu_chunk_map_bits(chunk));
> >  	bit_off = bitmap_find_next_zero_area(chunk->alloc_map, end, start,
> >  					     alloc_bits, align_mask);
> >  	if (bit_off >= end)
> > --
> 
> From pcpu_alloc_area itself, I think this is correct to avoid bitmap_find_next_zero_area
> scan past the boundaries of alloc_map, so
> 
> Reviewed-by: Peng Fan <peng.fan@nxp.com>
> 
> There are a few points I did not understand well,
> Per understanding pcpu_find_block_fit is to find the first bit off in a chunk which could satisfy
> the bits allocation, so bits might be larger than PCPU_BITMAP_BLOCK_BITS. And if
> pcpu_find_block_fit returns a good off, it means there is a area in the chunk could satisfy
> the bits allocation, then the following pcpu_alloc_area will not scan past the boundaries of
> alloc_map, right?
> 

pcpu_find_block_fit() finds the chunk offset corresponding to the block
that will be able to fit the chunk. Allocations are done by first fit,
so scanning begins from the first_free of a block. Because the hints are
always accurate, you never fail to find a fit in pcpu_alloc_area() if
pcpu_find_block_fit() gives you an offset. This means you never scan
past the end anyway.

Thanks,
Dennis
Peng Fan March 3, 2019, 8:41 a.m. UTC | #3
> -----Original Message-----
> From: Dennis Zhou [mailto:dennis@kernel.org]
> Sent: 2019年3月3日 6:24
> To: Peng Fan <peng.fan@nxp.com>
> Cc: Tejun Heo <tj@kernel.org>; Christoph Lameter <cl@linux.com>; Vlad
> Buslov <vladbu@mellanox.com>; kernel-team@fb.com; linux-mm@kvack.org;
> linux-kernel@vger.kernel.org
> Subject: Re: [PATCH 02/12] percpu: do not search past bitmap when allocating
> an area
> 
> On Sat, Mar 02, 2019 at 01:32:04PM +0000, Peng Fan wrote:
> > Hi Dennis,
> >
> > > -----Original Message-----
> > > From: owner-linux-mm@kvack.org [mailto:owner-linux-mm@kvack.org]
> On
> > > Behalf Of Dennis Zhou
> > > Sent: 2019年2月28日 10:18
> > > To: Dennis Zhou <dennis@kernel.org>; Tejun Heo <tj@kernel.org>;
> > > Christoph Lameter <cl@linux.com>
> > > Cc: Vlad Buslov <vladbu@mellanox.com>; kernel-team@fb.com;
> > > linux-mm@kvack.org; linux-kernel@vger.kernel.org
> > > Subject: [PATCH 02/12] percpu: do not search past bitmap when
> > > allocating an area
> > >
> > > pcpu_find_block_fit() guarantees that a fit is found within
> > > PCPU_BITMAP_BLOCK_BITS. Iteration is used to determine the first fit
> > > as it compares against the block's contig_hint. This can lead to
> > > incorrectly scanning past the end of the bitmap. The behavior was
> > > okay given the check after for bit_off >= end and the correctness of the
> hints from pcpu_find_block_fit().
> > >
> > > This patch fixes this by bounding the end offset by the number of
> > > bits in a chunk.
> > >
> > > Signed-off-by: Dennis Zhou <dennis@kernel.org>
> > > ---
> > >  mm/percpu.c | 3 ++-
> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/mm/percpu.c b/mm/percpu.c index
> > > 53bd79a617b1..69ca51d238b5 100644
> > > --- a/mm/percpu.c
> > > +++ b/mm/percpu.c
> > > @@ -988,7 +988,8 @@ static int pcpu_alloc_area(struct pcpu_chunk
> > > *chunk, int alloc_bits,
> > >  	/*
> > >  	 * Search to find a fit.
> > >  	 */
> > > -	end = start + alloc_bits + PCPU_BITMAP_BLOCK_BITS;
> > > +	end = min_t(int, start + alloc_bits + PCPU_BITMAP_BLOCK_BITS,
> > > +		    pcpu_chunk_map_bits(chunk));
> > >  	bit_off = bitmap_find_next_zero_area(chunk->alloc_map, end,
> start,
> > >  					     alloc_bits, align_mask);
> > >  	if (bit_off >= end)
> > > --
> >
> > From pcpu_alloc_area itself, I think this is correct to avoid
> > bitmap_find_next_zero_area scan past the boundaries of alloc_map, so
> >
> > Reviewed-by: Peng Fan <peng.fan@nxp.com>
> >
> > There are a few points I did not understand well, Per understanding
> > pcpu_find_block_fit is to find the first bit off in a chunk which
> > could satisfy the bits allocation, so bits might be larger than
> > PCPU_BITMAP_BLOCK_BITS. And if pcpu_find_block_fit returns a good off,
> > it means there is a area in the chunk could satisfy the bits
> > allocation, then the following pcpu_alloc_area will not scan past the
> boundaries of alloc_map, right?
> >
> 
> pcpu_find_block_fit() finds the chunk offset corresponding to the block that
> will be able to fit the chunk. Allocations are done by first fit, so scanning begins
> from the first_free of a block. Because the hints are always accurate, you
> never fail to find a fit in pcpu_alloc_area() if
> pcpu_find_block_fit() gives you an offset. This means you never scan past the
> end anyway.

Thanks for explanation.

Thanks,
Peng.

> 
> Thanks,
> Dennis
diff mbox series

Patch

diff --git a/mm/percpu.c b/mm/percpu.c
index 53bd79a617b1..69ca51d238b5 100644
--- a/mm/percpu.c
+++ b/mm/percpu.c
@@ -988,7 +988,8 @@  static int pcpu_alloc_area(struct pcpu_chunk *chunk, int alloc_bits,
 	/*
 	 * Search to find a fit.
 	 */
-	end = start + alloc_bits + PCPU_BITMAP_BLOCK_BITS;
+	end = min_t(int, start + alloc_bits + PCPU_BITMAP_BLOCK_BITS,
+		    pcpu_chunk_map_bits(chunk));
 	bit_off = bitmap_find_next_zero_area(chunk->alloc_map, end, start,
 					     alloc_bits, align_mask);
 	if (bit_off >= end)