All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs: raid56: Remove VLA usage
@ 2018-05-29 23:44 Kees Cook
  2018-05-30 15:14 ` David Sterba
  0 siblings, 1 reply; 4+ messages in thread
From: Kees Cook @ 2018-05-29 23:44 UTC (permalink / raw)
  To: David Sterba; +Cc: Liu Bo, Chris Mason, Josef Bacik, linux-btrfs, linux-kernel

In the quest to remove all stack VLA usage from the kernel[1], this
allocates the working buffers during regular init, instead of using stack
space. This refactors the allocation code a bit to make it easier
to review.

[1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 fs/btrfs/raid56.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
index 9abd950e7f78..5e4ad134b9ad 100644
--- a/fs/btrfs/raid56.c
+++ b/fs/btrfs/raid56.c
@@ -163,6 +163,12 @@ struct btrfs_raid_bio {
 	 * bitmap to record which horizontal stripe has data
 	 */
 	unsigned long *dbitmap;
+
+	/* allocated with real_stripes-many pointers for finish_*() calls */
+	void **finish_pointers;
+
+	/* allocated with stripe_npages-many bits for finish_*() calls */
+	unsigned long *finish_pbitmap;
 };
 
 static int __raid56_parity_recover(struct btrfs_raid_bio *rbio);
@@ -981,9 +987,14 @@ static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
 	int stripe_npages = DIV_ROUND_UP(stripe_len, PAGE_SIZE);
 	void *p;
 
-	rbio = kzalloc(sizeof(*rbio) + num_pages * sizeof(struct page *) * 2 +
-		       DIV_ROUND_UP(stripe_npages, BITS_PER_LONG) *
-		       sizeof(long), GFP_NOFS);
+	rbio = kzalloc(sizeof(*rbio) +
+		       sizeof(*rbio->stripe_pages) * num_pages +
+		       sizeof(*rbio->bio_pages) * num_pages +
+		       sizeof(*rbio->finish_pointers) * real_stripes +
+		       sizeof(*rbio->dbitmap) * BITS_TO_LONGS(stripe_npages) +
+		       sizeof(*rbio->finish_pbitmap) *
+				BITS_TO_LONGS(stripe_npages),
+		       GFP_NOFS);
 	if (!rbio)
 		return ERR_PTR(-ENOMEM);
 
@@ -1005,13 +1016,20 @@ static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
 	atomic_set(&rbio->stripes_pending, 0);
 
 	/*
-	 * the stripe_pages and bio_pages array point to the extra
+	 * the stripe_pages, bio_pages, etc arrays point to the extra
 	 * memory we allocated past the end of the rbio
 	 */
 	p = rbio + 1;
-	rbio->stripe_pages = p;
-	rbio->bio_pages = p + sizeof(struct page *) * num_pages;
-	rbio->dbitmap = p + sizeof(struct page *) * num_pages * 2;
+#define CONSUME_ALLOC(ptr, count)	do {				\
+		ptr = p;						\
+		p = (unsigned char *)p + sizeof(*(ptr)) * (count);	\
+	} while (0)
+	CONSUME_ALLOC(rbio->stripe_pages, num_pages);
+	CONSUME_ALLOC(rbio->bio_pages, num_pages);
+	CONSUME_ALLOC(rbio->finish_pointers, real_stripes);
+	CONSUME_ALLOC(rbio->dbitmap, BITS_TO_LONGS(stripe_npages));
+	CONSUME_ALLOC(rbio->finish_pbitmap, BITS_TO_LONGS(stripe_npages));
+#undef  CONSUME_ALLOC
 
 	if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5)
 		nr_data = real_stripes - 1;
@@ -1180,7 +1198,7 @@ static void index_rbio_pages(struct btrfs_raid_bio *rbio)
 static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
 {
 	struct btrfs_bio *bbio = rbio->bbio;
-	void *pointers[rbio->real_stripes];
+	void **pointers = rbio->finish_pointers;
 	int nr_data = rbio->nr_data;
 	int stripe;
 	int pagenr;
@@ -2350,8 +2368,8 @@ static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
 					 int need_check)
 {
 	struct btrfs_bio *bbio = rbio->bbio;
-	void *pointers[rbio->real_stripes];
-	DECLARE_BITMAP(pbitmap, rbio->stripe_npages);
+	void **pointers = rbio->finish_pointers;
+	unsigned long *pbitmap = rbio->finish_pbitmap;
 	int nr_data = rbio->nr_data;
 	int stripe;
 	int pagenr;
-- 
2.17.0


-- 
Kees Cook
Pixel Security

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] btrfs: raid56: Remove VLA usage
  2018-05-29 23:44 [PATCH] btrfs: raid56: Remove VLA usage Kees Cook
@ 2018-05-30 15:14 ` David Sterba
  2018-05-30 16:08   ` David Sterba
  0 siblings, 1 reply; 4+ messages in thread
From: David Sterba @ 2018-05-30 15:14 UTC (permalink / raw)
  To: Kees Cook
  Cc: David Sterba, Liu Bo, Chris Mason, Josef Bacik, linux-btrfs,
	linux-kernel

On Tue, May 29, 2018 at 04:44:59PM -0700, Kees Cook wrote:
> In the quest to remove all stack VLA usage from the kernel[1], this
> allocates the working buffers during regular init, instead of using stack
> space. This refactors the allocation code a bit to make it easier
> to review.
> 
> [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> 
> Signed-off-by: Kees Cook <keescook@chromium.org>

Reviewed-by: David Sterba <dsterba@suse.com>

Queued for 4.18, thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] btrfs: raid56: Remove VLA usage
  2018-05-30 15:14 ` David Sterba
@ 2018-05-30 16:08   ` David Sterba
  2018-05-30 20:49     ` Kees Cook
  0 siblings, 1 reply; 4+ messages in thread
From: David Sterba @ 2018-05-30 16:08 UTC (permalink / raw)
  To: dsterba, Kees Cook, David Sterba, Liu Bo, Chris Mason,
	Josef Bacik, linux-btrfs, linux-kernel

On Wed, May 30, 2018 at 05:14:56PM +0200, David Sterba wrote:
> On Tue, May 29, 2018 at 04:44:59PM -0700, Kees Cook wrote:
> > In the quest to remove all stack VLA usage from the kernel[1], this
> > allocates the working buffers during regular init, instead of using stack
> > space. This refactors the allocation code a bit to make it easier
> > to review.
> > 
> > [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com

For the record, so this was the last use of VLA in fs/btrfs/ .

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] btrfs: raid56: Remove VLA usage
  2018-05-30 16:08   ` David Sterba
@ 2018-05-30 20:49     ` Kees Cook
  0 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2018-05-30 20:49 UTC (permalink / raw)
  To: dsterba, Kees Cook, David Sterba, Liu Bo, Chris Mason,
	Josef Bacik, Linux Btrfs, LKML

On Wed, May 30, 2018 at 9:08 AM, David Sterba <dsterba@suse.cz> wrote:
> On Wed, May 30, 2018 at 05:14:56PM +0200, David Sterba wrote:
>> On Tue, May 29, 2018 at 04:44:59PM -0700, Kees Cook wrote:
>> > In the quest to remove all stack VLA usage from the kernel[1], this
>> > allocates the working buffers during regular init, instead of using stack
>> > space. This refactors the allocation code a bit to make it easier
>> > to review.
>> >
>> > [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
>
> For the record, so this was the last use of VLA in fs/btrfs/ .

Correct. And fs/ntfs/ has the last remaining VLAs in fs/.

-Kees

-- 
Kees Cook
Pixel Security

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-05-30 20:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-29 23:44 [PATCH] btrfs: raid56: Remove VLA usage Kees Cook
2018-05-30 15:14 ` David Sterba
2018-05-30 16:08   ` David Sterba
2018-05-30 20:49     ` Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.