Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA97AC433F5 for ; Mon, 24 Jan 2022 12:36:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239497AbiAXMgw (ORCPT ); Mon, 24 Jan 2022 07:36:52 -0500 Received: from de-smtp-delivery-102.mimecast.com ([194.104.109.102]:42784 "EHLO de-smtp-delivery-102.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238400AbiAXMgw (ORCPT ); Mon, 24 Jan 2022 07:36:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1643027810; h=from:from:reply-to:subject:subject: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=TRlt5hAYd/12/TipCXMjFMvKC2cwDzPN9HMeBgBoJYM=; b=f7AAdSpUQLJu5VwPEIvMDqzPzRpS4AhE+I3lJRHc759Kc9oFIu4O05Z1HN20ffDPQEWCeL 9QEh62gQ1i54g4jod7qBZO7ZeB8fx2TehCcWOWbzeIBA93lsPWHHwhXXSRgNlAnXA4+mFv sUauXLatHc7kRsMwhBH+WNV98b8G7r0= Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-he1eur01lp2051.outbound.protection.outlook.com [104.47.0.51]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-35-zbVD2jHINA2A5SKg0cxGVw-1; Mon, 24 Jan 2022 13:36:49 +0100 X-MC-Unique: zbVD2jHINA2A5SKg0cxGVw-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U5inT1Mqmo28jn1yRcCFFl+f5xWvXBlUTSxuM1kvecMCPlTO8EzDB3C/zskqMXCniPt5UhsODyA5HKmSXuMdKiiivR2GR9wYNh1jhA5oHk2JC1NA0Os+/ko0FHsI/mdx6hkD+u1D4tTwmJ+zkXi4dvKDKSevfWh/V4qIUib+trV0Ud3YGn0Hk84opjexX893yg3Vcl7IJSyV1/+KGzGkdWQEhvBIaaQfxAdYpanRtpCFKUe8jEjWxSOal41Dmb4uVfMmcNvWf/TnTPfOh7hOppHpDm/9X3Nt9pjyrs+VFK6snhJtF+M1NOho7iXnB9lLCpalulnMrlVcYoZaNkhN9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=TRlt5hAYd/12/TipCXMjFMvKC2cwDzPN9HMeBgBoJYM=; b=keI7/OJ52QKyfzYbFcLQlNYA3/IxZswAAYjKpAt2mEGd1U4TFCJHMpBl8STXDk6EOxIypaE+IiFFEkMA9ymjDL8AtWJ96/vADol85gHdZARehBDAi17zWqb2+EaeqSeUPY/nvZNDGyDnVD0p0iv5rXtNwovXjC9K8CYgbQCx0ZHoOJqNq8On9q6qZAPCL+xLEmev2WGTuIGeRUBwPy5QjeMnal5hrcfDjSNg0XCaXaD0t/t4sMzXTC8+tF10q3ggGWuIp3e0y9NaPQ0r6W+BaegFvDpJD8eu1XJqn8liDxjUXFcwV3olx5QvoaOrKgXM3vm4VOCiVoHIYBY5yGAewg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from DB9PR04MB9426.eurprd04.prod.outlook.com (2603:10a6:10:36a::14) by AS8PR04MB8387.eurprd04.prod.outlook.com (2603:10a6:20b:3f7::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4909.17; Mon, 24 Jan 2022 12:36:48 +0000 Received: from DB9PR04MB9426.eurprd04.prod.outlook.com ([fe80::7547:9d8a:d148:986e]) by DB9PR04MB9426.eurprd04.prod.outlook.com ([fe80::7547:9d8a:d148:986e%5]) with mapi id 15.20.4909.012; Mon, 24 Jan 2022 12:36:47 +0000 Message-ID: <04c0145a-57f2-4e1b-b898-ebccc92b6b59@suse.com> Date: Mon, 24 Jan 2022 20:36:39 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US To: Filipe Manana Cc: linux-btrfs@vger.kernel.org References: <20220123045242.25247-1-wqu@suse.com> From: Qu Wenruo Subject: Re: [PATCH] btrfs: defrag: don't try to merge regular extents with preallocated extents In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: BY3PR05CA0010.namprd05.prod.outlook.com (2603:10b6:a03:254::15) To DB9PR04MB9426.eurprd04.prod.outlook.com (2603:10a6:10:36a::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7486ec88-2a3d-43e5-4e1e-08d9df362fe1 X-MS-TrafficTypeDiagnostic: AS8PR04MB8387:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: R2LckfjnlrAZtpW/DpHVNmaUWnOxUd5J80CVBR5u2bikLB9MZ2Lvfk+J9hCJhqICk5yDcKriwwRxCLRLZqlbljjx4Vv9QQSTX6GW3PcAtNNG9LR0Tb5ffp0SqxUscerfZmEOetepzRcDk8U8DmBsD0w2+8kAUo+OUBkqJu2N7tv3Nr852gXIGLRxavAaY9+SiKvEPCBT5Yn3u1nai8IRY4UJr+/V7voqENI4SbPcaQWMg5dqk/LNkL8mIHMG+1qdIhYu1hV9Y6B9S1AoMzsAaPg5xsLTTRRonF8puxQTgx7tbUgJA1NPFPx64QL0Wo04FfBH/AM6nqTk7h3srZhR03CKQx5zY1mEqb4CG+6jdc1gt/16bcZkPhV3J2z7ZDLxUoSxTWGeD+aAwbLpg/fizSrY+So7LSzfwkqxazQhESnLYF5HUURHOmkddOmr78CanLqvpB8MmdpnZWu2rFPBspKAYZYgBp3pGGjpojU1rettG28wWQNRfl8bKbmNE+xI9F4fOTu3OyCJEdqRmVgaTMBLBF9VxkbEqC7mB/i5sGHWOIKJIVOaIiRHzDJRHfPm6ujv+UVkLuNtKf/68tQ11hAfWzeT2gYPoMDFbr0uxBVSLfxvN5AeFnzYT4Ex1t5w0Dv21IhhJ5XUkpB7Uuxbse4mu/DHC6r8a735afVO5gEOjiwXvsLoo+ryIdXJpxI/ITvypjGZY7O6mOVK3/74J78tsXIaVIkXa7YY2NGuTQw= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB9PR04MB9426.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(2906002)(6512007)(86362001)(31696002)(8936002)(8676002)(31686004)(6666004)(83380400001)(66556008)(66476007)(53546011)(66946007)(316002)(6506007)(6916009)(6486002)(2616005)(36756003)(26005)(38100700002)(4326008)(5660300002)(508600001)(186003)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?OERrbVhvVFU5YmtNU0pGdmhCM0UxRGlhdFpmWnJkNGdYdnNxYjJKeHVjd0Jo?= =?utf-8?B?YitrTndVL2NaS2Q5Y2NUTm1WbUFrUUZ5ZU5NOSsvdkR1V3NOY3VGU3hOSEly?= =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aMjg4bmRaU2pDQnVEZEdLSXkwVSs3R0t0S1hSV3dC?= =?utf-8?B?TnlJU21hZ3NUV1V5Q21adEcrOERjZmQ3K1k2ZXYyd0h4OXU3czQvMkpSVWli?= =?utf-8?B?dGFNWVR1dGZpM0ZmWGhiSmoyUFdyazBRYjNobTBoZDFOcXJOSzdiZEJtSmhG?= =?utf-8?B?K1V6cS93QWJOc2Myb3d2OFdhV0M1c0Y5bldNelVhenZDYkgzKzNjTmo3R3Fl?= =?utf-8?Q?mAz8=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7486ec88-2a3d-43e5-4e1e-08d9df362fe1 X-MS-Exchange-CrossTenant-AuthSource: DB9PR04MB9426.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2022 12:36:47.7595 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: DPk1UZZRPYumzhRo9dhly8nQ3qdGyURKWzWBpDq+fO59vnnAj6Qa2rPPmH54/NHx X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR04MB8387 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org On 2022/1/24 20:19, Filipe Manana wrote: > On Sun, Jan 23, 2022 at 12:52:42PM +0800, Qu Wenruo wrote: >> [BUG] >> With older kernels (before v5.16), btrfs will defrag preallocated extents. >> While with newer kernels (v5.16 and newer) btrfs will not defrag >> preallocated extents, but it will defrag the extent just before the >> preallocated extent, even it's just a single sector. >> >> This can be exposed by the following small script: >> >> mkfs.btrfs -f $dev > /dev/null >> >> mount $dev $mnt >> xfs_io -f -c "pwrite 0 4k" -c sync -c "falloc 4k 16K" $mnt/file >> xfs_io -c "fiemap -v" $mnt/file >> btrfs fi defrag $mnt/file >> sync >> xfs_io -c "fiemap -v" $mnt/file >> >> The output looks like this on older kernels: >> >> /mnt/btrfs/file: >> EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS >> 0: [0..7]: 26624..26631 8 0x0 >> 1: [8..39]: 26632..26663 32 0x801 >> /mnt/btrfs/file: >> EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS >> 0: [0..39]: 26664..26703 40 0x1 >> >> Which defrags the single sector along with the preallocated extent, and >> replace them with an regular extent into a new location (caused by data >> COW). >> This wastes most of the data IO just for the preallocated range. >> >> On the other hand, v5.16 is slightly better: >> >> /mnt/btrfs/file: >> EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS >> 0: [0..7]: 26624..26631 8 0x0 >> 1: [8..39]: 26632..26663 32 0x801 >> /mnt/btrfs/file: >> EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS >> 0: [0..7]: 26664..26671 8 0x0 >> 1: [8..39]: 26632..26663 32 0x801 >> >> The preallocated range is not defragged, but the sector before it still >> gets defragged, which has no need for it. >> >> [CAUSE] >> One of the function reused by the old and new behavior is >> defrag_check_next_extent(), it will determine if we should defrag >> current extent by checking the next one. >> >> It only checks if the next extent is a hole or inlined, but it doesn't >> check if it's preallocated. >> >> On the other hand, out of the function, both old and new kernel will >> reject preallocated extents. >> >> Such inconsistent behavior causes above behavior. >> >> [FIX] >> - Also check if next extent is preallocated >> If so, don't defrag current extent >> >> - Add comments on each case we don't defrag >> >> This will reduce the IO caused by defrag ioctl and autodefrag. >> >> Signed-off-by: Qu Wenruo >> --- >> fs/btrfs/ioctl.c | 29 +++++++++++++++++++++++------ >> 1 file changed, 23 insertions(+), 6 deletions(-) >> >> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c >> index 91ba2efe9792..dfa81b377e89 100644 >> --- a/fs/btrfs/ioctl.c >> +++ b/fs/btrfs/ioctl.c >> @@ -1049,23 +1049,40 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start, >> return em; >> } >> >> +/* >> + * Return if current extent @em is a good candidate for defrag. >> + * >> + * This is done by checking against the next extent after @em. >> + */ >> static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em, >> bool locked) >> { >> struct extent_map *next; >> - bool ret = true; >> + bool ret = false; >> >> /* this is the last extent */ >> if (em->start + em->len >= i_size_read(inode)) >> - return false; >> + return ret; >> >> next = defrag_lookup_extent(inode, em->start + em->len, locked); >> + /* No next extent or a hole, no way to merge */ >> if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) >> - ret = false; >> - else if ((em->block_start + em->block_len == next->block_start) && >> - (em->block_len > SZ_128K && next->block_len > SZ_128K)) >> - ret = false; >> + goto out; >> >> + /* Next extent is preallocated, no sense to defrag current extent */ >> + if (test_bit(EXTENT_FLAG_PREALLOC, &next->flags)) >> + goto out; >> + >> + /* >> + * Next extent are not only mergable but also adjacent in their > > are not -> is not > mergable -> mergeable > their -> its > >> + * logical address, normally an excellent candicate, but if they > > candicate -> candidate > >> + * are already large enough, then no need to defrag current extent. >> + */ > > It still sounds a bit odd to me, maybe: > > Next extent is mergeable and its logical address is contiguous with this > extent, so normally an excellent candidate, but if this extent or the next > one is already large enough, then we don't need to defrag. We use SZ_128K > because in case of enabled compression, extents can never be larger than > that. In fact, I'm a little more concerned of the original condition now. One thing is, the threshold here, it's hard coded 128K, while the extent_threshold for defrag can be specified by the ioctl caller. Another thing is, the original condition is using block_start, I'm not sure if we really need to check that. As long as the next extent is not a hole/preallocated one, we're completely happy to defrag. In fact, if the disk bytenr/num_bytes of @em is not adjacent to the next extent, it's even better, we can merge it into one extent without an extra seek. So I tend to change the condition more, like this: - Skip holes/preallocated That's already here in the patch - Skip large extents, using @threshold passed into this function No longer hard coded values, let the defrag caller to have more control, and have more consistent behavior. - No more check on em::block_start There are some pros and cons of defragging already physically adjacent file extents: Pros: * Reduces the number of extents Which may be what the defrag users want, to defrag extents caused by small but sequential direct IO. With reduced number of extents, there is a slight chance to reduce mount time by a little. Cons: * Extra IO and no saving in seeking time So why the existing code checks on the em::block_start is already questionable, as it's not a clear win. I know this sounds weird especially after I have broken so many defrag code, but I still want to remove the checks, replacing with more reasonable checks, even it means it will change the behavior again. Thanks, Qu > > Adding this comment is unrelated to this fix about prealloc extents, but I'm > fine with it. > > Other than that it looks fine. > > Reviewed-by: Filipe Manana > > Thanks. > >> + if ((em->block_start + em->block_len == next->block_start) && >> + (em->block_len > SZ_128K && next->block_len > SZ_128K)) >> + goto out; >> + ret = true; >> +out: >> free_extent_map(next); >> return ret; >> } >> -- >> 2.34.1 >> >