From nobody Fri Nov 29 15:29:44 2024 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C04D187FF5; Tue, 17 Sep 2024 20:33:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726605234; cv=none; b=dDhTtlypUtxww68CDkL74qbKuVkxE0Ep7zMdoWsq4Thfyg8H2PhJVMl2mgIscmBD8izr3xiyhNhSXt/yCYFtRZJuWImZCBDZd0kNyJDj9P4Bp8fIdYJ8EeLPkxpq7UTwYEfihnjlihjc2fFR8gASX7ZiNVT2KSPac8O1TRy89SY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726605234; c=relaxed/simple; bh=/tB73lCEwkWc8PVe9lLkqlvyICzGhv1+4G4VhJnAZCc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YH6bQayAd7vf/17WDgLpASinEC05WuKHrr4iIHSBXyUkPlEGinRIEDIUCJgg054oV97yDoyKa13i8fjDVXyH9tMPZzVNGBmgjLbQSz3KKNg/LGqPFnaNyG20sUZWECTDL9uPG1OvPnIgsIMqaFIdzFnfw470oFBI9j9gmUrNUmg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VimV8Ftm; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VimV8Ftm" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-42cba8340beso550035e9.1; Tue, 17 Sep 2024 13:33:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726605231; x=1727210031; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=v2AT3wnKUqRlT6uiGl5yddAf+ea2CD+q5ywgEsXs62g=; b=VimV8FtmTjIp2oolDLhJ0i9mvudtbuIPjTGQDMiPbxcYtj70X8OHnDnxZwzx8fBBiI pI8rEjQRwPN1sOttkVtezdGYFA2+CKWjZCF3/GMfC7/sQMGOb/tvEh1SjMJnc/S6TGNV tl9Je08YhfnxoLPkPjGSDUtDcVRkv558f0Oe4n1RRQmp1IRuWria7T2FDCdUOSojNfr8 O1J13AY6QjvbzvmZIzgflr4LBqdtV6gVXGRHBfo24wd0KnQUxklRume7wLe+1BXR6iuq XwglaxaRLrJWaujWpgECNC218xn3zHBs07FaIpSSbCuefRNhV2V+4lVbDgqaEseivcs/ xOgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726605231; x=1727210031; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v2AT3wnKUqRlT6uiGl5yddAf+ea2CD+q5ywgEsXs62g=; b=c/kmhKcsIaaRoMsdI853iOiSGVeqZzbEHGTMcqbSUuzYu91vzHZXz/PIaNuwN7jjna iWVZy/OqWM4g/Sgur0nlUDb95cU0i37Jdp+9z0ZOwhPJV0xYFuzT03ScHl0w4gwSPmVk fXTOOCw0v5ss5/DRHB+gpOTA07jFjMHsRmhd0GwWP0RHK/qX2YVqokB3lhQs446AUJc/ 9hYoL+TtSL/bC2SNt/uLTkuyN0BukodMxITiGTAqP2RxnmLlnrr0Zd9JVfJDHM05HAK7 xkJb0INwUSQdFHhMWSO/ZkwnHeIn5ZDrK28TZ22/ONRFWtxd+xGPK+Dl63opeubAtWEQ iQtA== X-Forwarded-Encrypted: i=1; AJvYcCU8ofZ4e/MtdzW4APcj31Qeiur8Kln4TDFpZu2hNcxL0/PG22Gvy3ah72e61FcVf8g/endCSX8RwxFCxg==@vger.kernel.org, AJvYcCXn7FL/Me0toZ2qcr1OKp8X5crZwoPYQrllSHoWSzDinWRUUrB7ERnp4tZuh61A/rFRqICqyVDJou8oR7z0@vger.kernel.org X-Gm-Message-State: AOJu0YxSrXEcNuSsMzrjNO2HpnnoT7rr4+f3Ric1222ed5vmz4pVOZvs YdXLO1fOV2wiybYrKazFrTqwS6FSe6qTzH0jY22ERjCi7ZQs9nlmTyEPD771 X-Google-Smtp-Source: AGHT+IEpXgTw7WNZdI1ghUuNRqg+lU3bnzPkJoNQFhKbunxX9YpI0hVcnd44m53kwjaDV93DDUi2jg== X-Received: by 2002:a05:600c:1c25:b0:42c:ae1d:ea4b with SMTP id 5b1f17b1804b1-42cbde1a51emr156416235e9.13.1726605230625; Tue, 17 Sep 2024 13:33:50 -0700 (PDT) Received: from fedora-thinkpad.lan (net-109-116-17-225.cust.vodafonedsl.it. [109.116.17.225]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-42d9b15c435sm148304545e9.23.2024.09.17.13.33.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2024 13:33:50 -0700 (PDT) From: Luca Stefani To: Cc: Luca Stefani , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 1/2] btrfs: Split remaining space to discard in chunks Date: Tue, 17 Sep 2024 22:33:04 +0200 Message-ID: <20240917203346.9670-2-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240917203346.9670-1-luca.stefani.ge1@gmail.com> References: <20240917203346.9670-1-luca.stefani.ge1@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Per Qu Wenruo in case we have a very large disk, e.g. 8TiB device, mostly empty although we will do the split according to our super block locations, the last super block ends at 256G, we can submit a huge discard for the range [256G, 8T), causing a super large delay. We now split the space left to discard based on BTRFS_MAX_DISCARD_CHUNK_SIZE in preparation of introduction of cancellation signals handling. Link: https://bugzilla.kernel.org/show_bug.cgi?id=3D219180 Link: https://bugzilla.suse.com/show_bug.cgi?id=3D1229737 Signed-off-by: Luca Stefani Reviewed-by: Qu Wenruo --- fs/btrfs/extent-tree.c | 19 +++++++++++++++---- fs/btrfs/volumes.h | 1 + 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index a5966324607d..ad70548d1f72 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -1300,13 +1300,24 @@ static int btrfs_issue_discard(struct block_device = *bdev, u64 start, u64 len, bytes_left =3D end - start; } =20 - if (bytes_left) { + while (bytes_left) { + u64 bytes_to_discard =3D min(BTRFS_MAX_DISCARD_CHUNK_SIZE, bytes_left); + ret =3D blkdev_issue_discard(bdev, start >> SECTOR_SHIFT, - bytes_left >> SECTOR_SHIFT, + bytes_to_discard >> SECTOR_SHIFT, GFP_NOFS); - if (!ret) - *discarded_bytes +=3D bytes_left; + + if (ret) { + if (ret !=3D -EOPNOTSUPP) + break; + continue; + } + + start +=3D bytes_to_discard; + bytes_left -=3D bytes_to_discard; + *discarded_bytes +=3D bytes_to_discard; } + return ret; } =20 diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index 03d2d60afe0c..c15062a435bd 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -29,6 +29,7 @@ struct btrfs_trans_handle; struct btrfs_zoned_device_info; =20 #define BTRFS_MAX_DATA_CHUNK_SIZE (10ULL * SZ_1G) +#define BTRFS_MAX_DISCARD_CHUNK_SIZE (1ULL * SZ_1G) =20 extern struct mutex uuid_mutex; =20 --=20 2.46.0 From nobody Fri Nov 29 15:29:44 2024 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26C4018DF78; Tue, 17 Sep 2024 20:33:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726605235; cv=none; b=ioXPAhbZyesyntSf3GUD1yoYSWB/EFRQsqdNfOu/icnlQv55oNhgMp1BgRwIdALXYVA5uvjmW/8I23KilmURfeOMBZvuincqyjwGxT/va0pTe/yq1NdXjPCdOqXPZex0+OSduGfjNq2GftdNiN1UZXf21Yn9jQ+nKCVvcKNo49s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726605235; c=relaxed/simple; bh=HPnVI1MJuxT5fwRCFOgAZvf4w4V5FkcMCcMT+RHlL30=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FlM46ccprwj5Bm+lES35lLocRVCq3OPPxQVXO98Sqet4LQRHVhvewd+Kt55mL1lZcVZmIM+bEozY2aveeOH7wOqytZR5oVrAAda0Kj8boGFw2bBes7ZIoIenjB6ajhNdCumhUcxwwyxBOUckm6EM6baBU9KN4GKCqRXCYMgXpjE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gez2jX/A; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gez2jX/A" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-42cbe624c59so40381565e9.3; Tue, 17 Sep 2024 13:33:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726605232; x=1727210032; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9XPJtJw1nARsM0r7oxFgXPDKW264HSs0QmHpBBF/tcM=; b=gez2jX/AUQhJy9cMXYTJ7mdkzgMvuwIN5gpYP/WG2iR6gZUtx6F9LuxcxVTYhFHhnQ eViU2wJS3P6DYk5A9Z1B9oOm8+2SMqNs2c9N073QubgMOzm11sg/GS7EPFB5J+Epg3CZ TbTSomvX24TS9570k7XINoagC8iL+z4hUKTP9+NzPnqY7zygao6OovWXyZX1wjXYbCnc w9zERf4ngLgWMhMZu7CISpbicS0KdTXVRUrr1XjlWgP+dUP5CXkx0Zb7QSGNfrvDUSPC 3HUu+FdZuSb1wihnvw0QCwgUMDlnZex6Dhy2k5ruYhZZ84axEUiaX8iGHQF1YmQEDXG4 6GXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726605232; x=1727210032; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9XPJtJw1nARsM0r7oxFgXPDKW264HSs0QmHpBBF/tcM=; b=VLOp5Bup/Jz6+HA1ZjuDDLbyZr3RbS385VnLptJeWhsLVkDwSsuJFrks/F2rHxBEgC v8C2rVAw4A9+hIb6JCGm1usRB2vHZBx6mnVzPR22ymVXwKUI0RSmr90/vo0sXpGHmL8f epJDnlV1cx4wXKxmP9zjW/ltkvv+ktk1/+m5Qb3VBPLRwwFw2vFBdjMYF3EEL82Qo/FX AukMG/OZstr17f2TOXOm4DuqoDzCIvyDbHBo1VTST0Rur8XARj4vFkURuralURifOq9U CmqIOFhsOruwd2wQBH8p+lLW+COu/AKlyE782vLKFs8VHFfNtlc3kbu2huRF5eeuM0lm SZaw== X-Forwarded-Encrypted: i=1; AJvYcCUobLgl4cdyiGgMgFRkvMl781jKcwT4fE6RY94lsDkQfEDGbOaIdwCA6YfY69RpXj/SZAmchEcvUdo167iU@vger.kernel.org, AJvYcCVpC9hXyxe7uH2yvYRKYNaH+qJB4Y+l3y4JluTGlaSXyujqXTmPKrE8lVVUvMkR+6NfrWlxzfxYRZATug==@vger.kernel.org X-Gm-Message-State: AOJu0Yx+c1a+t7GUjV9mTv4DxMxUhJ0ngzpBdT7lfqBX8gZ9wrhH4G9E pT7CPeC7/UcAHpUXcmvZuRmXEeNj9nc9OF+B8lSQAiI15Dp2khCf X-Google-Smtp-Source: AGHT+IEvSEbzEuKn134/9HENsZPfn9s/7CqN/kH99JV+TKU1zyK5eN8Tr+JPc6v9L31a/Mwx8C4dnA== X-Received: by 2002:a05:600c:3ba8:b0:42c:87dc:85e7 with SMTP id 5b1f17b1804b1-42e60b36687mr48905495e9.18.1726605231946; Tue, 17 Sep 2024 13:33:51 -0700 (PDT) Received: from fedora-thinkpad.lan (net-109-116-17-225.cust.vodafonedsl.it. [109.116.17.225]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-42d9b15c435sm148304545e9.23.2024.09.17.13.33.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2024 13:33:51 -0700 (PDT) From: Luca Stefani To: Cc: Luca Stefani , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 2/2] btrfs: Don't block system suspend during fstrim Date: Tue, 17 Sep 2024 22:33:05 +0200 Message-ID: <20240917203346.9670-3-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240917203346.9670-1-luca.stefani.ge1@gmail.com> References: <20240917203346.9670-1-luca.stefani.ge1@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Sometimes the system isn't able to suspend because the task responsible for trimming the device isn't able to finish in time, especially since we have a free extent discarding phase, which can trim a lot of unallocated space, and there is no limits on the trim size (unlike the block group part). Since discard isn't a critical call it can be interrupted at any time, in such cases we stop the trim, report the amount of discarded bytes and return failure. Link: https://bugzilla.kernel.org/show_bug.cgi?id=3D219180 Link: https://bugzilla.suse.com/show_bug.cgi?id=3D1229737 Signed-off-by: Luca Stefani Reviewed-by: Qu Wenruo --- fs/btrfs/extent-tree.c | 7 ++++++- fs/btrfs/free-space-cache.c | 4 ++-- fs/btrfs/free-space-cache.h | 6 ++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index ad70548d1f72..d9f511babd89 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -1316,6 +1316,11 @@ static int btrfs_issue_discard(struct block_device *= bdev, u64 start, u64 len, start +=3D bytes_to_discard; bytes_left -=3D bytes_to_discard; *discarded_bytes +=3D bytes_to_discard; + + if (btrfs_trim_interrupted()) { + ret =3D -ERESTARTSYS; + break; + } } =20 return ret; @@ -6470,7 +6475,7 @@ static int btrfs_trim_free_extents(struct btrfs_devic= e *device, u64 *trimmed) start +=3D len; *trimmed +=3D bytes; =20 - if (fatal_signal_pending(current)) { + if (btrfs_trim_interrupted()) { ret =3D -ERESTARTSYS; break; } diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index eaa1dbd31352..f4bcb2530660 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -3809,7 +3809,7 @@ static int trim_no_bitmap(struct btrfs_block_group *b= lock_group, if (async && *total_trimmed) break; =20 - if (fatal_signal_pending(current)) { + if (btrfs_trim_interrupted()) { ret =3D -ERESTARTSYS; break; } @@ -4000,7 +4000,7 @@ static int trim_bitmaps(struct btrfs_block_group *blo= ck_group, } block_group->discard_cursor =3D start; =20 - if (fatal_signal_pending(current)) { + if (btrfs_trim_interrupted()) { if (start !=3D offset) reset_trimming_bitmap(ctl, offset); ret =3D -ERESTARTSYS; diff --git a/fs/btrfs/free-space-cache.h b/fs/btrfs/free-space-cache.h index 83774bfd7b3b..9f1dbfdee8ca 100644 --- a/fs/btrfs/free-space-cache.h +++ b/fs/btrfs/free-space-cache.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "fs.h" =20 struct inode; @@ -56,6 +57,11 @@ static inline bool btrfs_free_space_trimming_bitmap( return (info->trim_state =3D=3D BTRFS_TRIM_STATE_TRIMMING); } =20 +static inline bool btrfs_trim_interrupted(void) +{ + return fatal_signal_pending(current) || freezing(current); +} + /* * Deltas are an effective way to populate global statistics. Give macro = names * to make it clear what we're doing. An example is discard_extents in --=20 2.46.0