Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25474C48BDF for ; Tue, 15 Jun 2021 15:47:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F159761606 for ; Tue, 15 Jun 2021 15:47:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231303AbhFOPtR (ORCPT ); Tue, 15 Jun 2021 11:49:17 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:55416 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230076AbhFOPtQ (ORCPT ); Tue, 15 Jun 2021 11:49:16 -0400 Received: from imap.suse.de (imap-alt.suse-dmz.suse.de [192.168.254.47]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 75F3E1FD49; Tue, 15 Jun 2021 15:47:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1623772031; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=r9D5PXDpYtbb+NnVCB/tjQxteM1/+o1e56M4qOXMX24=; b=o7Y4kJN4l0cErDJ9F0h2EvpmLlUYGuXHCFfrvnL6ahnSEJoOlG24A2kqHqlrzGco1FPexe 1usXgUWt+UQy/qoWeR8yogu8sb0UsjJ4lo6I6aJcWztVpPQsDpO+B2DWmiaKl61MerxstG 8uxmOx9cAS6iGMA995+VCGz/r+r0gPI= Received: from imap3-int (imap-alt.suse-dmz.suse.de [192.168.254.47]) by imap.suse.de (Postfix) with ESMTP id 3B99D118DD; Tue, 15 Jun 2021 15:47:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1623772031; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=r9D5PXDpYtbb+NnVCB/tjQxteM1/+o1e56M4qOXMX24=; b=o7Y4kJN4l0cErDJ9F0h2EvpmLlUYGuXHCFfrvnL6ahnSEJoOlG24A2kqHqlrzGco1FPexe 1usXgUWt+UQy/qoWeR8yogu8sb0UsjJ4lo6I6aJcWztVpPQsDpO+B2DWmiaKl61MerxstG 8uxmOx9cAS6iGMA995+VCGz/r+r0gPI= Received: from director2.suse.de ([192.168.254.72]) by imap3-int with ESMTPSA id M1giDH/LyGB6ZgAALh3uQQ (envelope-from ); Tue, 15 Jun 2021 15:47:11 +0000 Subject: Re: [RFC PATCH 05/31] btrfs: writepage() lock extent before pages To: Goldwyn Rodrigues , linux-btrfs@vger.kernel.org Cc: Goldwyn Rodrigues References: <412df27129a676f4bca6724960350569573a3791.1623567940.git.rgoldwyn@suse.com> From: Nikolay Borisov Message-ID: <8fd7dce6-43bf-73b7-8943-4ea4e5c2de95@suse.com> Date: Tue, 15 Jun 2021 18:47:10 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <412df27129a676f4bca6724960350569573a3791.1623567940.git.rgoldwyn@suse.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org On 13.06.21 г. 16:39, Goldwyn Rodrigues wrote: > From: Goldwyn Rodrigues > > Lock Order change: Extent locks before page locks > > writepage() is called with the page locked. So make an attempt > to lock the extents and proceed only if successful. > > The new function best_effort_lock_extent() attempts to lock the range > provided. > > If the entire range was not locked, but it still covers the locked > page, work with the reduced range by resetting delalloc_end() > > Signed-off-by: Goldwyn Rodrigues > --- > fs/btrfs/extent_io.c | 66 ++++++++++++++++++++++++++++++++++---------- > 1 file changed, 51 insertions(+), 15 deletions(-) > > diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c > index 9e81d25dea70..75ad809e8c86 100644 > --- a/fs/btrfs/extent_io.c > +++ b/fs/btrfs/extent_io.c > @@ -1488,6 +1488,47 @@ int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end) > return 1; > } > > +/* > + * best_effort_lock_extent() - locks the extent to the best of effort > + * making sure the minimum range is locked and the > + * delalloc bits are set. If the full requested range is not locked, > + * delalloc_end shifts to until the range that can be locked. > + */ > +static bool best_effort_lock_extent(struct extent_io_tree *tree, u64 start, > + u64 *delalloc_end, u64 min_end, struct extent_state **cached_state) > +{ > + u64 failed_start; > + u64 end = *delalloc_end; > + int ret; > + > + ret = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, > + &failed_start, cached_state, GFP_NOFS, NULL); > + > + if (!ret) > + return false; The locking sequence here seems broken. Based on the behavior of lock_extent_bits : 1. ret = 0 means the range was successfully locked instead you return false. 2. ret can be a number of errors, including EEXIST, the original code basically waits for the extents to be unlocked and retries. > + > + if (failed_start < min_end) { > + /* Could not lock the whole range */ > + if (failed_start > start) > + unlock_extent_cached(tree, start, > + failed_start - 1, cached_state); > + return false; > + } else if (end > failed_start) { > + /* Work with what could be locked */ > + end = failed_start - 1; nit: Here instead of assigning *delalloc_end to end and doing the check I think it's more straightforward to perform the check with failed_start and *delalloc_end and only then assign failed_start -1 to end. > + } > + > + /* Check if delalloc bits are set */ > + ret = test_range_bit(tree, start, end, > + EXTENT_DELALLOC, 1, *cached_state); > + if (!ret) { > + unlock_extent_cached(tree, start, end - 1, cached_state); > + return false; > + } > + *delalloc_end = end; > + return true; > +} > + > void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) > { > unsigned long index = start >> PAGE_SHIFT; > @@ -2018,7 +2059,16 @@ noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, > if (delalloc_end + 1 - delalloc_start > max_bytes) > delalloc_end = delalloc_start + max_bytes - 1; > > - /* step two, lock all the pages after the page that has start */ > + /* step two, lock the state bits for the range */ > + found = best_effort_lock_extent(tree, delalloc_start, &delalloc_end, > + ((page_index(locked_page) + 1) << PAGE_SHIFT), page_index(locked_page)+1 means you'd like to ensure you have locked at least 1 page beyond locked page, correct? This looks a bit arbitrary i.e why 1 page and not 2 for example? > + &cached_state); > + if (!found) { > + free_extent_state(cached_state); > + goto out_failed; > + } > + > + /* step three, lock all the pages after the page that has start */ > ret = lock_delalloc_pages(inode, locked_page, > delalloc_start, delalloc_end); So now that this has become step 3 and the extents are locked you should implement relevant error handling which would unlock the extents in case of an error. > ASSERT(!ret || ret == -EAGAIN); > @@ -2038,20 +2088,6 @@ noinline_for_stack bool find_lock_delalloc_range(struct inode *inode, > } > } > > - /* step three, lock the state bits for the whole range */ > - lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state); > - > - /* then test to make sure it is all still delalloc */ > - ret = test_range_bit(tree, delalloc_start, delalloc_end, > - EXTENT_DELALLOC, 1, cached_state); > - if (!ret) { > - unlock_extent_cached(tree, delalloc_start, delalloc_end, > - &cached_state); > - __unlock_for_delalloc(inode, locked_page, > - delalloc_start, delalloc_end); > - cond_resched(); > - goto again; > - } > free_extent_state(cached_state); > *start = delalloc_start; > *end = delalloc_end; >