From patchwork Mon Mar 21 16:14:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 12787513 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 28C7BC433EF for ; Mon, 21 Mar 2022 16:14:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245536AbiCUQQI (ORCPT ); Mon, 21 Mar 2022 12:16:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351049AbiCUQQC (ORCPT ); Mon, 21 Mar 2022 12:16:02 -0400 Received: from esa1.hgst.iphmx.com (esa1.hgst.iphmx.com [68.232.141.245]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C888D23147 for ; Mon, 21 Mar 2022 09:14:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1647879269; x=1679415269; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o8OCy0g7xB1UK3ZwczxP0PJny07qDFNDokaC0Iq1iSs=; b=UweI23HpWR8pNRzO7QmwhayoQ3Mf1tgqx+Cjyh88uMAxgA4q/o1pJjdF M6cS4RkEUMqT9GIxWJmGpQw78Exj1rUBEx2OJmzMYy6pio2lMPzu8LrwC MYC29KaGV5+sUYgvG5eHconiSXnqV6vcSpboiOIYMbUutmrQNXXVb1fBR OqCwqQDAc5i4vExgi8Mqa7r9qq/VNOHh/ccXhqtlGfDKgMSgIq/PZ8Ut/ Q88dH83XQulpvCQADHiWGwLqokCJyedqmbG2f7H3eFHXfy8DS/NCtZc0Q lBEw1attNfuK27iJz/8w7Jiup3931KA329pdPqPxi5oWDBdnJfD/h+77o Q==; X-IronPort-AV: E=Sophos;i="5.90,199,1643644800"; d="scan'208";a="307836348" Received: from h199-255-45-15.hgst.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 22 Mar 2022 00:14:28 +0800 IronPort-SDR: V+Uxe3JQgV0eV7BFE+hKdON78WEIf4GNRZr7zGiXWcy22TkitL5dLZMm6+48xtsN3UGFVR/b2J Bm4FipOYDKaUe1Aea30WYgS1fa9N3JE7m7bHX8G47m7umO658iKGZUbC6Ucb7hXx/eLyzKjfNH Abk0jd771ePcezaiU4/m5LfrT4H1ccB79FuR3P9vbpTG3cgLhmvO0PCj9L5VSh3vO2D0kIV/XP BSPFFW0YrSq8C2XEAQrtT4gexHTTer9pCRdQhQwgiVgBYt0W/EDuK2wBauno4xpOmtLC3HyavV 4LAwJZSBr994nT+zbCmNuF6C Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 08:45:28 -0700 IronPort-SDR: CDOf1aNlWWM81ye3ib/TtyLzZ/Vtr3EyjC0YKeytfI3R7CZ9Vo9TChgEEbiJ6z6hmimsUiqsG1 7eRq8jxucz1b5IgoqWgt2f6lBocliZqjrZcFCJYKVjjmDRA+Pxj8YvbSRu2E+GOLabQbxt1l+T AImZwyWOwlQylG1pEKHd62W9/n3L4DwOIHzUGCLCD2MhbmEW1Llee7xWlSJcBbZ1vn1YIyywhr aZ6QBl3bj1KRXWgLwpChOiWC6f8MFwUgtTOQcunDrRrnZQ3GoPr5IOpx+OHtqHLbGqvOFm/Ll5 ciQ= WDCIronportException: Internal Received: from unknown (HELO redsun91.ssa.fujisawa.hgst.com) ([10.149.66.72]) by uls-op-cesaip01.wdc.com with ESMTP; 21 Mar 2022 09:14:27 -0700 From: Johannes Thumshirn To: David Sterba Cc: Josef Bacik , linux-btrfs@vger.kernel.org, Pankaj Raghav , Johannes Thumshirn Subject: [PATCH 1/5] btrfs: make the bg_reclaim_threshold per-space info Date: Mon, 21 Mar 2022 09:14:10 -0700 Message-Id: <63d4d206dd2e652aa968ab0fa30dd9aab98a666b.1647878642.git.johannes.thumshirn@wdc.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Josef Bacik For !zoned file systems it's useful to have the auto reclaim feature, however there are different use cases for !zoned, for example we may not want to reclaim metadata chunks ever, only data chunks. Move this sysfs flag to per-space_info. This won't affect current users because this tunable only ever did anything for zoned, and that is currently hidden behind BTRFS_CONFIG_DEBUG. Signed-off-by: Josef Bacik [ jth restore global bg_reclaim_threshold ] Signed-off-by: Johannes Thumshirn --- fs/btrfs/free-space-cache.c | 7 +++++-- fs/btrfs/space-info.c | 9 +++++++++ fs/btrfs/space-info.h | 6 ++++++ fs/btrfs/sysfs.c | 37 +++++++++++++++++++++++++++++++++++++ fs/btrfs/zoned.h | 6 +----- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 01a408db5683..ef84bc5030cd 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -2630,16 +2630,19 @@ int __btrfs_add_free_space(struct btrfs_block_group *block_group, static int __btrfs_add_free_space_zoned(struct btrfs_block_group *block_group, u64 bytenr, u64 size, bool used) { - struct btrfs_fs_info *fs_info = block_group->fs_info; + struct btrfs_space_info *sinfo = block_group->space_info; struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; u64 offset = bytenr - block_group->start; u64 to_free, to_unusable; - const int bg_reclaim_threshold = READ_ONCE(fs_info->bg_reclaim_threshold); + int bg_reclaim_threshold = 0; bool initial = (size == block_group->length); u64 reclaimable_unusable; WARN_ON(!initial && offset + size > block_group->zone_capacity); + if (!initial) + bg_reclaim_threshold = READ_ONCE(sinfo->bg_reclaim_threshold); + spin_lock(&ctl->tree_lock); if (!used) to_free = size; diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c index b87931a458eb..60d0a58c4644 100644 --- a/fs/btrfs/space-info.c +++ b/fs/btrfs/space-info.c @@ -181,6 +181,12 @@ void btrfs_clear_space_info_full(struct btrfs_fs_info *info) found->full = 0; } +/* + * Block groups with more than this value (percents) of unusable space will be + * scheduled for background reclaim. + */ +#define BTRFS_DEFAULT_ZONED_RECLAIM_THRESH 75 + static int create_space_info(struct btrfs_fs_info *info, u64 flags) { @@ -203,6 +209,9 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags) INIT_LIST_HEAD(&space_info->priority_tickets); space_info->clamp = 1; + if (btrfs_is_zoned(info)) + space_info->bg_reclaim_threshold = BTRFS_DEFAULT_ZONED_RECLAIM_THRESH; + ret = btrfs_sysfs_add_space_info_type(info, space_info); if (ret) return ret; diff --git a/fs/btrfs/space-info.h b/fs/btrfs/space-info.h index d841fed73492..0c45f539e3cf 100644 --- a/fs/btrfs/space-info.h +++ b/fs/btrfs/space-info.h @@ -24,6 +24,12 @@ struct btrfs_space_info { the space info if we had an ENOSPC in the allocator. */ + /* + * Once a block group drops below this threshold we'll schedule it for + * reclaim. + */ + int bg_reclaim_threshold; + int clamp; /* Used to scale our threshold for preemptive flushing. The value is >> clamp, so turns out to be a 2^clamp divisor. */ diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 17389a42a3ab..90da1ea0cae0 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -722,6 +722,42 @@ SPACE_INFO_ATTR(bytes_zone_unusable); SPACE_INFO_ATTR(disk_used); SPACE_INFO_ATTR(disk_total); +static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj, + struct kobj_attribute *a, + char *buf) +{ + struct btrfs_space_info *space_info = to_space_info(kobj); + ssize_t ret; + + ret = sysfs_emit(buf, "%d\n", READ_ONCE(space_info->bg_reclaim_threshold)); + + return ret; +} + +static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj, + struct kobj_attribute *a, + const char *buf, size_t len) +{ + struct btrfs_space_info *space_info = to_space_info(kobj); + int thresh; + int ret; + + ret = kstrtoint(buf, 10, &thresh); + if (ret) + return ret; + + if (thresh != 0 && (thresh <= 50 || thresh > 100)) + return -EINVAL; + + WRITE_ONCE(space_info->bg_reclaim_threshold, thresh); + + return len; +} + +BTRFS_ATTR_RW(space_info, bg_reclaim_threshold, + btrfs_sinfo_bg_reclaim_threshold_show, + btrfs_sinfo_bg_reclaim_threshold_store); + /* * Allocation information about block group types. * @@ -738,6 +774,7 @@ static struct attribute *space_info_attrs[] = { BTRFS_ATTR_PTR(space_info, bytes_zone_unusable), BTRFS_ATTR_PTR(space_info, disk_used), BTRFS_ATTR_PTR(space_info, disk_total), + BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold), NULL, }; ATTRIBUTE_GROUPS(space_info); diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h index cbf016a7bb5d..c489c08d7fd5 100644 --- a/fs/btrfs/zoned.h +++ b/fs/btrfs/zoned.h @@ -10,11 +10,7 @@ #include "block-group.h" #include "btrfs_inode.h" -/* - * Block groups with more than this value (percents) of unusable space will be - * scheduled for background reclaim. - */ -#define BTRFS_DEFAULT_RECLAIM_THRESH 75 +#define BTRFS_DEFAULT_RECLAIM_THRESH 75 struct btrfs_zoned_device_info { /* From patchwork Mon Mar 21 16:14:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 12787516 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 57260C43217 for ; Mon, 21 Mar 2022 16:14:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350882AbiCUQQK (ORCPT ); Mon, 21 Mar 2022 12:16:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351076AbiCUQQC (ORCPT ); Mon, 21 Mar 2022 12:16:02 -0400 Received: from esa1.hgst.iphmx.com (esa1.hgst.iphmx.com [68.232.141.245]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97DB32FFF4 for ; Mon, 21 Mar 2022 09:14:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1647879269; x=1679415269; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6oTq/znIoFntHNJj2cxflSZT1FpYRw4I6HbXx7qB18s=; b=f0B3Fjo8FPe2HwewLemVsrfrgLulMPmVYhBShYmgKY25LSaFL9AsUQpb jCocCR4DnukVe/2CZK/OBlLHcTiv1lA29RvHSfc1e4j7H2MANzI4ouvbi 8voZa+ABYrIXeSopV3ENj7zAoQYw8/FTbJu7ollugVXw0D0QyUdOBEQxp cQF5Q/NBuT0xPh2RaTSl9cgLkba/TyERZbtzFVOUAW0e+nsXudMStq5hR OmbCTKYWPjoyXl7qwESM8J2Cin4L+Nlrtgvhawy1Ftjee7rrKcjWbsfxG UcrGABCftrkMwD1jAqLp01CW4iz76+XXVJ7AjQiBeJPzD+z7zd2X19aGQ Q==; X-IronPort-AV: E=Sophos;i="5.90,199,1643644800"; d="scan'208";a="307836351" Received: from h199-255-45-15.hgst.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 22 Mar 2022 00:14:29 +0800 IronPort-SDR: PFPg7fm42T9pctonJgOiLaG4BN2VcOe52//1XHAgUup24p7fOkgDX7p4rAQdls2svj8lvlfE/Z HOY2SrX+7DMLYy5bzFGDI1HPd38FN3Bap0nDl8pQGOCdeYT2QcqAMJV8hn28DKRCB3dM4HJSRJ y7/YxswDUoy2/c5pceIvoVSB+f1ZfZ+L0VU6E7PX93JwtBymQOfWS/5WAXE2+tFSf5qAE3fc0j xVmf/PQfwqbhU9uLeK0UUWDvR7WecW2X0AJMsTLLw8jrI/QmjgAEsYfwK6Xn2hAVlEO/NVrz2i szguVzrHy1mpeK/3QbcDaVYP Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 08:45:29 -0700 IronPort-SDR: mr0tPSzLThJwAgGsjGX9pi8WYNfAtxcT5FTICRNSnPgVh8I0Zdc1wVd+R9nor9lkpiYimdfpEx kYHlqimll7+/sRWzUtr2VsvHAIjICuGRmxFrGY2yp7yUEgosNapjNPzYvlA0UvfO0HE1+8Zzct WUMs8ebkw0G+N7v5nwkAPk+I+iPCfiwHQkEz4VkG+Zt5xobACljt5H/ze0pwysRr47LgdrWgzR ni0gz9cUMFlnVt+YBZILkwB1l5hHXbma8+AlhCLso4I4RvwW7x76cbDKZI3DEvrTfXso5Lc6qv Hpk= WDCIronportException: Internal Received: from unknown (HELO redsun91.ssa.fujisawa.hgst.com) ([10.149.66.72]) by uls-op-cesaip01.wdc.com with ESMTP; 21 Mar 2022 09:14:28 -0700 From: Johannes Thumshirn To: David Sterba Cc: Josef Bacik , linux-btrfs@vger.kernel.org, Pankaj Raghav Subject: [PATCH 2/5] btrfs: allow block group background reclaim for !zoned fs'es Date: Mon, 21 Mar 2022 09:14:11 -0700 Message-Id: X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Josef Bacik We have found this feature invaluable at Facebook due to how our workload interacts with the allocator. We have been using this in production for months with only a single problem that has already been fixed. This will allow us to set a threshold for block groups to be automatically relocated even if we don't have zoned devices. Signed-off-by: Josef Bacik --- fs/btrfs/block-group.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 59f18a10fd5f..628741ecb97b 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -3208,6 +3208,31 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) return ret; } +static inline bool should_reclaim_block_group(struct btrfs_block_group *block_group, + u64 bytes_freed) +{ + const struct btrfs_space_info *space_info = block_group->space_info; + const int reclaim_thresh = READ_ONCE(space_info->bg_reclaim_threshold); + const u64 new_val = block_group->used; + const u64 old_val = new_val + bytes_freed; + u64 thresh; + + if (reclaim_thresh == 0) + return false; + + thresh = div_factor_fine(block_group->length, reclaim_thresh); + + /* + * If we were below the threshold before don't reclaim, we are likely a + * brand new block group and we don't want to relocate new block groups. + */ + if (old_val < thresh) + return false; + if (new_val >= thresh) + return false; + return true; +} + int btrfs_update_block_group(struct btrfs_trans_handle *trans, u64 bytenr, u64 num_bytes, bool alloc) { @@ -3230,6 +3255,8 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, spin_unlock(&info->delalloc_root_lock); while (total) { + bool reclaim; + cache = btrfs_lookup_block_group(info, bytenr); if (!cache) { ret = -ENOENT; @@ -3275,6 +3302,8 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, cache->space_info, num_bytes); cache->space_info->bytes_used -= num_bytes; cache->space_info->disk_used -= num_bytes * factor; + + reclaim = should_reclaim_block_group(cache, num_bytes); spin_unlock(&cache->lock); spin_unlock(&cache->space_info->lock); @@ -3301,6 +3330,8 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans, if (!alloc && old_val == 0) { if (!btrfs_test_opt(info, DISCARD_ASYNC)) btrfs_mark_bg_unused(cache); + } else if (!alloc && reclaim) { + btrfs_mark_bg_to_reclaim(cache); } btrfs_put_block_group(cache); From patchwork Mon Mar 21 16:14:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 12787514 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 A58ECC43219 for ; Mon, 21 Mar 2022 16:14:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350873AbiCUQQK (ORCPT ); Mon, 21 Mar 2022 12:16:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349263AbiCUQQC (ORCPT ); Mon, 21 Mar 2022 12:16:02 -0400 Received: from esa1.hgst.iphmx.com (esa1.hgst.iphmx.com [68.232.141.245]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89DC32314D for ; Mon, 21 Mar 2022 09:14:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1647879270; x=1679415270; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ht3ht7NGdEqqjqWIUOCUfml5Fc2/ivtasHKMfz/HYQI=; b=nN7l5SojrLL8kL7GWnBsDKLWoIdCbqOz1/Uo1K6WcAk3IuN4IFI6wvvv mbJEUydRTVi9Gvr7a3VeoRSIJswE2mIajl1UCTQHOwnp7FmdBzA5Oi8IU pJpjkUJxPMgT0TETA/GYeUvIKgt+pwbxmrzV75u/BETxb9phXt+sAAw31 iHLbJSUzuI6kWxb07w3hvJXcn/eBA5c0p+KxJL1vN6Xy8joBeyr843EEb Vvz/jbBFIdAtaIm8YzzK1QZl2a17qVDD7DXdxy/oo/9RiiWhYBIDnwO6M IvB1sHb6nSkOUVdbMXscDXduD8u1SWovnJFi9PQFC9rc0Dy8f4OLSezbG w==; X-IronPort-AV: E=Sophos;i="5.90,199,1643644800"; d="scan'208";a="307836353" Received: from h199-255-45-15.hgst.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 22 Mar 2022 00:14:30 +0800 IronPort-SDR: WM9AfShHaIHgGqYIRLvO1xTRwUM/PDxmSQJI2+3kN8oRO5Uw1zHNxXrgElgJ21rkqgYh3q3ASX tjmrtOqsSYmTgpm0AJ0WpOFQSSrvOQCtWxf41hD/RL9eeXlCBkMB6gt1KDZqDGvWI9BUcoHMdU ERfTqJKk2Ww0IUnzS1G4GEA7Cw9yzbI3PePh78s28vSjTZW8YLYuhJDEma5Dl3/rGuuHujsuGq AeUf+XQFfDC5I+jt2rexrR1vlL7xZGvqeZYhJxagCEck6dsLhoV4IN0vTcYjzR1T5DMNcAI4E5 iVM3LKTyRvYA4PngdCxL3wTc Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 08:45:30 -0700 IronPort-SDR: VbXmbkN7f/4LJ4z7GTOhBqLU84r7wi1h3vVEwrf64StiX9oO6jTkQSwpxh/jjVgK0IsQROcs/D 82VrNhQBC/bCsW/ohbBXyaq9Kcm808vOiRLZN+HxBTUbSPgM/xCly40lIVUwgshUw0jiriWwqr sim3GvQsximeBwW9dbXVeMtzbHo+zJq0P2Jw48cSQaho42ESkgzj9KJ7agDn3bSpXrhIlhOrf0 XKhBnP+a9ujdDU+Vrz5Ee1akp4toXdVbFWQ7K6joJlPl2wP9NegrYkLvRKi0ewXdNp40gLTYZE DBk= WDCIronportException: Internal Received: from unknown (HELO redsun91.ssa.fujisawa.hgst.com) ([10.149.66.72]) by uls-op-cesaip01.wdc.com with ESMTP; 21 Mar 2022 09:14:29 -0700 From: Johannes Thumshirn To: David Sterba Cc: Josef Bacik , linux-btrfs@vger.kernel.org, Pankaj Raghav Subject: [PATCH 3/5] btrfs: change the bg_reclaim_threshold valid region from 0 to 100 Date: Mon, 21 Mar 2022 09:14:12 -0700 Message-Id: X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Josef Bacik For the !zoned case we may want to set the threshold for reclaim to something below 50%. Change the acceptable threshold from 50-100 to 0-100. Signed-off-by: Josef Bacik --- fs/btrfs/sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 90da1ea0cae0..fdf9bf789528 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -746,7 +746,7 @@ static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj, if (ret) return ret; - if (thresh != 0 && (thresh <= 50 || thresh > 100)) + if (thresh < 0 || thresh > 100) return -EINVAL; WRITE_ONCE(space_info->bg_reclaim_threshold, thresh); From patchwork Mon Mar 21 16:14:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 12787512 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 ADF8AC4332F for ; Mon, 21 Mar 2022 16:14:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348295AbiCUQQJ (ORCPT ); Mon, 21 Mar 2022 12:16:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351078AbiCUQQC (ORCPT ); Mon, 21 Mar 2022 12:16:02 -0400 Received: from esa1.hgst.iphmx.com (esa1.hgst.iphmx.com [68.232.141.245]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85F1726C4 for ; Mon, 21 Mar 2022 09:14:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1647879271; x=1679415271; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=za+nd/YjyhWLL0KnFAqXQ5fGPSN6suf/J77GM2MXiTU=; b=DgHgjzEIo7bjPBjeX9SpqFPpKLjrRadiCtJnL2hvp4Q7I8yL00APoMVJ YSNT71F90S7Kd67KXqUOsDdY4OQ23BiPhlLG+a02CE96ekSGGH7HRH7Vz iFlTADXXW0ikLbQeDq8AybhAnx34H1tGL/GTgYiBT4I2ykiNlZmHeqtEO TlepONXA1VOCPsmmmnPlVlVSuxVeRkcRjtyjv02G/4inLKH+DC4I+ybnk RwxnMw++HnkqF/MbSOCpbn+hUaxXU/TGUSNB6fPF9nQu5dNszY4CtGuU7 pq94X0z/HfA5NWZbeNXdtCa3iGpH7sDTLqDyIFDfLo0HyXFr0jbSI7CvD g==; X-IronPort-AV: E=Sophos;i="5.90,199,1643644800"; d="scan'208";a="307836354" Received: from h199-255-45-15.hgst.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 22 Mar 2022 00:14:31 +0800 IronPort-SDR: 857iaN4GPJqV+66PSZd/FXuCdeqvKqUuD1dEVI5FaBu244RGnnFAwi2CaceY/XS72lKnGQsdsw noJbCNS290k8U91KGXxfXF2eDlc8rILevvhweYknseWl/Bt0Wg9QDVBhc5OJXnvfcMvWAMVxUC QnaQ/sBxp+gd6D00s5oJDSZyYBbbty5sMmHAgOY/02A+NX7M5XhVtgvtqRjmWVVbQAfVqxB5vF F6KmG8ljK3bZQPEx3NtIyv609M9dVQkS9Fez7+vH08RgnGekBVl49/wyKgvXMYqwiqWOmvTakP FyI5nSiV7OMnVNv2mPslMe6A Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 08:45:31 -0700 IronPort-SDR: tW2/Ma4zrOtiZuaMuEcLevm0DVC7WiO/xMTBOT4jqb2cp4t7wAC2djZDmcCscewTVvYy/EYRq1 qWIoysmrXpoI971JRUtrEtPoVuVOV+bNtGjtOxGVRkfM6qbvyIT7513naMAUo1L30WzzgrKiNA QlNh0wQGicU24qXhS/BuXl/6Mid0krikIWL32OcmuGapa3UMV4O4guRHFhlUjh3kQHoVrMk29P gfl8vtGF5MVyXue7fVUK1+GoQAP1KzmoVA3vf7+HteBewWOZuSvRIcJYwR4UTnr1+bTKn2cC/Q Mmc= WDCIronportException: Internal Received: from unknown (HELO redsun91.ssa.fujisawa.hgst.com) ([10.149.66.72]) by uls-op-cesaip01.wdc.com with ESMTP; 21 Mar 2022 09:14:30 -0700 From: Johannes Thumshirn To: David Sterba Cc: Johannes Thumshirn , Josef Bacik , linux-btrfs@vger.kernel.org, Pankaj Raghav Subject: [PATCH 4/5] btrfs: make calc_available_free_space available outside of space-info Date: Mon, 21 Mar 2022 09:14:13 -0700 Message-Id: X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Make calc_available_free_space available outside of space-info.c and rename to btrfs_calc_available_free_space. This is a preparation for the next commit and does not impose any functional changes. Signed-off-by: Johannes Thumshirn Reviewed-by: Josef Bacik --- fs/btrfs/space-info.c | 8 ++++---- fs/btrfs/space-info.h | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c index 60d0a58c4644..e1fd1b1681fd 100644 --- a/fs/btrfs/space-info.c +++ b/fs/btrfs/space-info.c @@ -301,7 +301,7 @@ struct btrfs_space_info *btrfs_find_space_info(struct btrfs_fs_info *info, return NULL; } -static u64 calc_available_free_space(struct btrfs_fs_info *fs_info, +u64 btrfs_calc_available_free_space(struct btrfs_fs_info *fs_info, struct btrfs_space_info *space_info, enum btrfs_reserve_flush_enum flush) { @@ -349,7 +349,7 @@ int btrfs_can_overcommit(struct btrfs_fs_info *fs_info, return 0; used = btrfs_space_info_used(space_info, true); - avail = calc_available_free_space(fs_info, space_info, flush); + avail = btrfs_calc_available_free_space(fs_info, space_info, flush); if (used + bytes < space_info->total_bytes + avail) return 1; @@ -722,7 +722,7 @@ btrfs_calc_reclaim_metadata_size(struct btrfs_fs_info *fs_info, lockdep_assert_held(&space_info->lock); - avail = calc_available_free_space(fs_info, space_info, + avail = btrfs_calc_available_free_space(fs_info, space_info, BTRFS_RESERVE_FLUSH_ALL); used = btrfs_space_info_used(space_info, true); @@ -803,7 +803,7 @@ static bool need_preemptive_reclaim(struct btrfs_fs_info *fs_info, * much delalloc we need for the background flusher to kick in. */ - thresh = calc_available_free_space(fs_info, space_info, + thresh = btrfs_calc_available_free_space(fs_info, space_info, BTRFS_RESERVE_FLUSH_ALL); used = space_info->bytes_used + space_info->bytes_reserved + space_info->bytes_readonly + global_rsv_size; diff --git a/fs/btrfs/space-info.h b/fs/btrfs/space-info.h index 0c45f539e3cf..a957d84d8ff0 100644 --- a/fs/btrfs/space-info.h +++ b/fs/btrfs/space-info.h @@ -151,4 +151,8 @@ static inline void btrfs_space_info_free_bytes_may_use( } int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes, enum btrfs_reserve_flush_enum flush); + +u64 btrfs_calc_available_free_space(struct btrfs_fs_info *fs_info, + struct btrfs_space_info *space_info, + enum btrfs_reserve_flush_enum flush); #endif /* BTRFS_SPACE_INFO_H */ From patchwork Mon Mar 21 16:14:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 12787515 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 0C68EC433F5 for ; Mon, 21 Mar 2022 16:14:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350830AbiCUQQJ (ORCPT ); Mon, 21 Mar 2022 12:16:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351080AbiCUQQC (ORCPT ); Mon, 21 Mar 2022 12:16:02 -0400 Received: from esa1.hgst.iphmx.com (esa1.hgst.iphmx.com [68.232.141.245]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A1DDB7C7 for ; Mon, 21 Mar 2022 09:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1647879272; x=1679415272; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NI+8tL9wkfU4pm1qOj1P3D/xCQVsf5kJ/T73v6NG014=; b=TCgzzFme/9eU2JHHkDt72ZsmOZVZowEnbWx3mTFY6BNagaJibHoWl8Gj v9lyoCSc2pB+RshBTz0gUMz+tm4TvcXwwVppVmGF6h2RjN91aH1Zmfg4N poGh9fIgdgeRZ8b3Px66UdQAQY3gy9z8HZOLdg2p8XSwflX87MufhlkQF cnNnzTyTgXHcoTeQXDdc8eXtx2vNckBdE2qzX3v6A7SWylq3+QabwLUYR x6hHw9Gim5Ld9eIDP9mk6MOZdDdqZaY0IveOWUDadESGtP06BtC0ZLpNc wgVBH230yw14jw+5avIZ+9DzsyP+aNocJ8psmpljqSilclrI4xrfcjoB7 w==; X-IronPort-AV: E=Sophos;i="5.90,199,1643644800"; d="scan'208";a="307836358" Received: from h199-255-45-15.hgst.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 22 Mar 2022 00:14:32 +0800 IronPort-SDR: ug1lvgC+tuCuMjpomI8QOfMSFZdEQxJ82DkZRy87hb2hLWamdjLWfUQ2rGRLMu016OJs0UWiYE N3ma4lgSFCwL67bm4ZFH89GwnTal5gVUapSJZTuZl1boM2O4LyJ0RiwUaqDQ1ZpXDlQgqF52tG suueJe5MUUKjBaX1Nf8V7pdPmEbUEfHLbqNRwMKq1GoILXR6TOCqEVFbIkhYZUirgyw6uG7/cL kPleZNyXWstEw30vv0oKNfW29tYGEvNYBjAFPMoT4evuRNC+YZMUD9DPJlm9GWuM3CFBNjcXww iuXIv6FzD+NiP4mi2RALFWWE Received: from uls-op-cesaip01.wdc.com ([10.248.3.36]) by uls-op-cesaep02.wdc.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2022 08:45:32 -0700 IronPort-SDR: fMXBXthTNN+FrC+x+uAJb2BhEHnnojt/gPpKt5WEWp7hs5cHIUhHV9SQizPJbS7DL3/3ikBTHi zP33kfJkeSKGd1vrekJOyNipD7sUhUo1p/WQL1q63dzUM88TRuVCv7ibYa7NFuMtLIlCZZqIaA CSn98zp7JDUniZzL9DnvSS6UblAO9iri18XpOAN7JhDm1x+xWrGF6c2xeF6twzj7wBFkzRGjPk i74K4Agr6P0BP3iqypPYCpFRfjB1LV1Pii8ISHU5vS+CYHZxOCx2r79gjRvOmu784CA/IgKBT8 uIU= WDCIronportException: Internal Received: from unknown (HELO redsun91.ssa.fujisawa.hgst.com) ([10.149.66.72]) by uls-op-cesaip01.wdc.com with ESMTP; 21 Mar 2022 09:14:31 -0700 From: Johannes Thumshirn To: David Sterba Cc: Johannes Thumshirn , Josef Bacik , linux-btrfs@vger.kernel.org, Pankaj Raghav Subject: [PATCH 5/5] btrfs: zoned: make auto-reclaim less aggressive Date: Mon, 21 Mar 2022 09:14:14 -0700 Message-Id: <89824f8b4ba1ac8f05f74c047333c970049e2815.1647878642.git.johannes.thumshirn@wdc.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org The current auto-reclaim algorithm starts reclaiming all block-group's with a zone_unusable value above a configured threshold. This is causing a lot of reclaim IO even if there would be enough free zones on the device. Instead of only accounting a block-group's zone_unusable value, also take the number of empty zones into account. Cc: Josef Bacik Signed-off-by: Johannes Thumshirn --- Changes since v3: * remove unusedd include (Filipe) * Only call btrfs_zoned_should_reclaim() for zoned FSes (Josef) * Use btrfs_calc_available_free_space() (Josef) Changes since v2: * Use div_u64() Changes since RFC: * Fix logic error * Skip unavailable devices * Use different metric working for non-zoned devices as well --- fs/btrfs/block-group.c | 10 ++++++++++ fs/btrfs/zoned.c | 23 +++++++++++++++++++++++ fs/btrfs/zoned.h | 6 ++++++ 3 files changed, 39 insertions(+) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 628741ecb97b..12454304bb85 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -1512,6 +1512,13 @@ static int reclaim_bgs_cmp(void *unused, const struct list_head *a, return bg1->used > bg2->used; } +static inline bool btrfs_should_reclaim(struct btrfs_fs_info *fs_info) +{ + if (btrfs_is_zoned(fs_info)) + return btrfs_zoned_should_reclaim(fs_info); + return true; +} + void btrfs_reclaim_bgs_work(struct work_struct *work) { struct btrfs_fs_info *fs_info = @@ -1522,6 +1529,9 @@ void btrfs_reclaim_bgs_work(struct work_struct *work) if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) return; + if (!btrfs_should_reclaim(fs_info)) + return; + sb_start_write(fs_info->sb); if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) { diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c index 1b1b310c3c51..f2a412427921 100644 --- a/fs/btrfs/zoned.c +++ b/fs/btrfs/zoned.c @@ -2079,3 +2079,26 @@ void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) } mutex_unlock(&fs_devices->device_list_mutex); } + +bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info) +{ + struct btrfs_space_info *sinfo; + u64 used = 0; + u64 total = 0; + u64 factor; + + if (!btrfs_is_zoned(fs_info)) + return false; + + if (!fs_info->bg_reclaim_threshold) + return false; + + list_for_each_entry(sinfo, &fs_info->space_info, list) { + total += sinfo->total_bytes; + used += btrfs_calc_available_free_space(fs_info, sinfo, + BTRFS_RESERVE_NO_FLUSH); + } + + factor = div_u64(used * 100, total); + return factor >= fs_info->bg_reclaim_threshold; +} diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h index c489c08d7fd5..f2d16395087f 100644 --- a/fs/btrfs/zoned.h +++ b/fs/btrfs/zoned.h @@ -74,6 +74,7 @@ void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 length); void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg); void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info); +bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info); #else /* CONFIG_BLK_DEV_ZONED */ static inline int btrfs_get_dev_zone(struct btrfs_device *device, u64 pos, struct blk_zone *zone) @@ -232,6 +233,11 @@ static inline void btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { } static inline void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info) { } + +static inline bool btrfs_zoned_should_reclaim(struct btrfs_fs_info *fs_info) +{ + return false; +} #endif static inline bool btrfs_dev_is_sequential(struct btrfs_device *device, u64 pos)