linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Thumshirn <jthumshirn@suse.de>
To: Nikolay Borisov <nborisov@suse.com>, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v2 1/6] btrfs: Refactor cow_file_range_async
Date: Wed, 20 Feb 2019 16:51:41 +0100	[thread overview]
Message-ID: <262a6971-b2df-9a16-354e-db2270919213@suse.de> (raw)
In-Reply-To: <20190220151143.19057-2-nborisov@suse.com>

On 20/02/2019 16:11, Nikolay Borisov wrote:
> This commit changes the implementation of cow_file_range_async in order
> to get rid of the BUG_ON in the middle of the loop. Additionally it
> reworks the inner loop in the hopes of making it more understandable.
> 
> Main change is that the number of chunks required to handle the given
> range is calculated before going into the loop and the logic of the loop
> just iterates the chunk count. Furthermore, the way memory is allocated
> is reworked and now the code does a single kmalloc with enough space to
> handle all chunks. Depending on whether compression is enabled or not
> chunks are either 1 (in non-compress case) or the range divided by 512k.
> 
> Signed-off-by: Nikolay Borisov <nborisov@suse.com>
> ---
>  fs/btrfs/inode.c | 84 +++++++++++++++++++++++++++++++++---------------
>  1 file changed, 58 insertions(+), 26 deletions(-)
> 
> diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> index 05bbfd02ea49..546000779310 100644
> --- a/fs/btrfs/inode.c
> +++ b/fs/btrfs/inode.c
> @@ -375,6 +375,7 @@ struct async_cow {
>  	unsigned int write_flags;
>  	struct list_head extents;
>  	struct btrfs_work work;
> +	atomic_t *pending;
>  };
>  
>  static noinline int add_async_extent(struct async_cow *cow,
> @@ -1181,7 +1182,12 @@ static noinline void async_cow_free(struct btrfs_work *work)
>  	async_cow = container_of(work, struct async_cow, work);
>  	if (async_cow->inode)
>  		btrfs_add_delayed_iput(async_cow->inode);
> -	kfree(async_cow);
> +	/*
> +	 * Since the pointer to 'pending' is at the beginning of the array of
> +	 * async_cow's, freeing it ensures the whole array has been freed.
> +	 */
> +	if (atomic_dec_and_test(async_cow->pending))
> +		kfree(async_cow->pending);
>  }

[...]

> +	/* Layout is [atomic_t][async_cow1][async_cowN].... */
> +	async_cow = kmalloc(sizeof(atomic_t) + num_chunks*sizeof(*async_cow),
> +			    GFP_NOFS);
> +	if (!async_cow) {
> +		unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
> +			EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
> +			EXTENT_DO_ACCOUNTING;
> +		unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
> +			PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
> +			PAGE_SET_ERROR;
> +		extent_clear_unlock_delalloc(inode, start, end, 0, locked_page,
> +					     clear_bits, page_ops);
> +		return -ENOMEM;
> +	}
> +
> +	p = (atomic_t *)async_cow;
> +	async_cow = (struct async_cow *)((char *)async_cow + sizeof(atomic_t));
> +	atomic_set(p, num_chunks);

Ugh! You're abusing an atomic_t * for something you want to actually
make this:

@@ -375,6 +375,7 @@ struct async_cow {
 	unsigned int write_flags;
 	struct list_head extents;
 	struct btrfs_work work;
+	atomic_t pending;
+	struct async_cow list[0];
};

and then have:

tmp = krealloc(async_cow, num_chunks * sizeof(*async_cow), GFP_NOFS);
if (!tmp) {
[...]
}
kfree(async_cow);
async_cow = tmp;


-- 
Johannes Thumshirn                            SUSE Labs Filesystems
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

  reply	other threads:[~2019-02-20 15:51 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-20 15:11 [PATCH v2 0/6] Compressed path cleanups Nikolay Borisov
2019-02-20 15:11 ` [PATCH v2 1/6] btrfs: Refactor cow_file_range_async Nikolay Borisov
2019-02-20 15:51   ` Johannes Thumshirn [this message]
2019-02-20 15:11 ` [PATCH v2 2/6] btrfs: Remove fs_info from struct async_cow Nikolay Borisov
2019-02-20 15:25   ` Johannes Thumshirn
2019-02-20 15:29     ` Nikolay Borisov
2019-02-20 15:11 ` [PATCH v2 3/6] btrfs: Make compress_file_range take only " Nikolay Borisov
2019-02-20 15:27   ` Johannes Thumshirn
2019-02-21 11:55     ` Nikolay Borisov
2019-02-21 13:08       ` Johannes Thumshirn
2019-02-20 15:11 ` [PATCH v2 4/6] btrfs: Replace clear_extent_bit with unlock_extent Nikolay Borisov
2019-02-20 15:29   ` Johannes Thumshirn
2019-02-20 15:11 ` [PATCH v2 5/6] btrfs: Set iotree only once in submit_compressed_extents Nikolay Borisov
2019-02-20 15:33   ` Johannes Thumshirn
2019-02-20 15:11 ` [PATCH v2 6/6] btrfs: Factor out common extent locking code " Nikolay Borisov
2019-02-20 15:35   ` Johannes Thumshirn

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=262a6971-b2df-9a16-354e-db2270919213@suse.de \
    --to=jthumshirn@suse.de \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=nborisov@suse.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).