From patchwork Mon Sep 2 20:56:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13787697 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 998841A4E6C; Mon, 2 Sep 2024 21:00:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725310803; cv=none; b=lbkWbfEbgZKpnoXNR0aonkKdicRXa85z4pJztvDNkmAKNBUz7ODHunI3ko3tnnIXibdfiouJPfagftJlmm5eTiY45ZzTx9JBxTJb3UrQUt9qhfKOWCBG/4zfGz18ZRRZMckh230Hf8gpBSlnaxuuxBBvHxv1k3i4Vt6/JdzCvt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725310803; c=relaxed/simple; bh=Jppyu6S2zJuNblX7X/9ir0VSqKk+Z3e44Jf07nJ5r2E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q/ZPKIylArg4YdclQegD2iiofHSMXgu5ba/EktgAh9NZ8TN7bl4CXmzGVKYri2N8h9t+RVbtlYNh72la/w5JMc8uI69lJs2RGcnYSRW5b7ABzbXZQ3XKGfrc2sgjPKxZNsquyULmo/Cs+z9ObdwpEwCpL+dGx1UJBO4T8cBedl8= 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=ZPY5kJC3; arc=none smtp.client-ip=209.85.128.42 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="ZPY5kJC3" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-42bbdf7f860so29595475e9.3; Mon, 02 Sep 2024 14:00:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725310800; x=1725915600; 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=iJ2NVfTjqOzglJM6TtSPuUyDiNFczs8rfNMhTWfviss=; b=ZPY5kJC3yCo5xR1TBZOwmwzhmRAAEaVmYCnoHRq3I6Udnf18RPf2gH7lWCmd0byPI+ xJXnBoe6NszZqONxkSgXUJ4y5CEXjEMu5geetZ+B3OLpJ9xEOsuEVGX8PXE0ICFWmfQJ OL6Xm/4LvJhosF4x8M3FykybV8sJ1Qgt1HFLGgy2Ce1YXttTYQFzW3OV05zBL2Hmbhe8 WtCVbe3fpYRO2Ob8jLljg9wCCDI4nA/MANByoMXWv/rgUh4I2TAfZKow06KAAOqnH0Jy J9YselbOPtsYIE+CpCZH2pBFkgUHmE3QVMXjlyYVX87CT9/FuI2IDDvu1Ntcpvr89wFV mNPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725310800; x=1725915600; 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=iJ2NVfTjqOzglJM6TtSPuUyDiNFczs8rfNMhTWfviss=; b=UF76k93NbWjZSC1NsJGA9AfGV5XW5m5HejK9+b/xJMZtDbiuaWaXtb0wET8EhMspDB PAi8BweMy+LjLalSkF4SuY48XlBGjoADxIlRniceCDswtr+yNd8DiIGG+1WFZjgyr1XK qcLJDprZoLSjhoMuWhKJzNk+0RxOaarAp/DN9NEmzCaXygyDIqt/J/Cz4yNetEDekZfh +xDnytz7yHlASv+F9hOUlKFjd4URezXDhC2rYhKavrUVQ5KaBRAAXLwhIN4GyAUAEHMb y5xwOm+msdPS+n4lc3ZjqPcBodmebA6PnHRaGtzrmmfIKtAVeTvjGW3jryUcrVbV8qU6 WE5A== X-Forwarded-Encrypted: i=1; AJvYcCUBwhTf2Wzp6780H6TesXaGVtXRH8/tXrSSaovloTGZ+/LM6DIv3QH3CM2mOfMm0SK1fWLG0uCyyZupSub2@vger.kernel.org, AJvYcCVgbg8zqVmInSYunJM/t3Wj+MWNRqs3zlmXn0qwbt8YdTuSGXNsjX6uFs2XT/Xxb4EnAHKPA87yrWu4vA==@vger.kernel.org, AJvYcCWjvxjjcHwj/c5+F3RfCFuvldIQWQCX1I+izu3Yk402enNi1qpKKXvBcJfeaVAsb11d3on/84bPBRdQVAo=@vger.kernel.org X-Gm-Message-State: AOJu0YyW/D4gih5+1AoB2egIii42R5QyeFq2YDlwVYSpBj1nVO00xU4I B2XZOFRSk+bHWdleD1JNT4l0fqFDgAnDcadLNcvBAdG0/aUWtyYv X-Google-Smtp-Source: AGHT+IEsj8k3Ky5VdKV3IY10D71TbBEZck9sSkM+NmDRHUsGhfElw8XJqq84ljk7MDPqC9MlEfxAOQ== X-Received: by 2002:a05:600c:4455:b0:425:5ec3:570b with SMTP id 5b1f17b1804b1-42bb01fcfb6mr107126595e9.35.1725310799827; Mon, 02 Sep 2024 13:59:59 -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-42c89dca8absm2913705e9.27.2024.09.02.13.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 13:59:59 -0700 (PDT) From: Luca Stefani To: Cc: Luca Stefani , Jens Axboe , Chris Mason , Josef Bacik , David Sterba , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-btrfs@vger.kernel.org Subject: [PATCH v2 1/3] block: Export bio_discard_limit Date: Mon, 2 Sep 2024 22:56:10 +0200 Message-ID: <20240902205828.943155-2-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240902205828.943155-1-luca.stefani.ge1@gmail.com> References: <20240902205828.943155-1-luca.stefani.ge1@gmail.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 It can be used to calculate the sector size limit of each discard call allowing filesystem to implement their own chunked discard logic with customized behavior, for example cancellation due to signals. Signed-off-by: Luca Stefani --- block/blk-lib.c | 3 ++- include/linux/blkdev.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 4c9f20a689f7..501cbfc4f112 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -10,7 +10,7 @@ #include "blk.h" -static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) +sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) { unsigned int discard_granularity = bdev_discard_granularity(bdev); sector_t granularity_aligned_sector; @@ -34,6 +34,7 @@ static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) */ return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT; } +EXPORT_SYMBOL(bio_discard_limit); struct bio *blk_alloc_discard_bio(struct block_device *bdev, sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask) diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index b7664d593486..42d63400025b 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1088,6 +1088,7 @@ static inline long nr_blockdev_pages(void) extern void blk_io_schedule(void); +sector_t bio_discard_limit(struct block_device *bdev, sector_t sector); int blkdev_issue_discard(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask); int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, From patchwork Mon Sep 2 20:56:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13787698 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 582AF21019F; Mon, 2 Sep 2024 21:00:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725310805; cv=none; b=FEtPlYLJQNJuuoBHZsUjmsX1s9StN0et/cWfCK/c6omCR3v1XZoV/6M+Zc/7NNrLb+kpRiNnjmotKkZ+Xs+F/Yku4ZYpWEHDTN7YLxIck1gC59aPyO/lJG5FBVG4XkA8wE9CZ0VM5BuIJwIk7HPNPwkanYer5/PGgUyQymOzxDs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725310805; c=relaxed/simple; bh=SEj7IYFl7zC2aHPGx2VgmF2xom1uHPUOY6jDG+3Ax7A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Gr1Q3JP91ObWrZf/OTcYQEC7IWt8EWFNBDrkdpRlIbMVdNw3VYTQglHPliAqQAxnDJUDFFXRFuRwxzA8hDLWZAc2u+L5Cw5RXSq13BPBmjZk1bT0ZyOmWAnz2DuPri5twE+QozRzbjwMNNf4C03bVpFMuLdGCTkMNGQgtqOvnkk= 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=C6vgVZkN; arc=none smtp.client-ip=209.85.128.44 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="C6vgVZkN" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-42c7a49152aso25434695e9.2; Mon, 02 Sep 2024 14:00:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725310801; x=1725915601; 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=Ny4ikVUi/TH1pl766OKNCnGhf37BcCI9Wb18E5567Uw=; b=C6vgVZkNEVjgLIHqtefcVz7uohpjzqrGBG4N/wOD4LTsW8MdDcaLyZzGRzfMlJZWin 7gxlVHhSn48hzsjErDvMK6tzm+9i2ewJlTH6/SJbBi3aJ6RKtDXyg1Kb/tmZyjlzkdi+ PPfC42T832B6EDGFAxtrafdM9+XVdTYTzAZJDbt5arm0AsPB8wjrA1VNbpibb1v57jWC 5G/0kxdoaq32TpRzloCPkezbLpbGD7z3uoLZsVZXAcwUAaD1zIrjtD64FUpbNkO9qYfW 6FmkeDDPMrwapBwfRAWbUqlKuxmRTJnRRZk/6yz57PEjbtTzW5AZ9qzYGD8dArWCiQcf 102g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725310801; x=1725915601; 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=Ny4ikVUi/TH1pl766OKNCnGhf37BcCI9Wb18E5567Uw=; b=KmI8jrrQ7NqK/+v+bYSZqOtbIU90c7Jkvvv67FaGndkLzWgFYmw1kAJrQz8WypX9RW w6JX7JAv6kolxcaenqzscEZ6+AdbJD6jrHZH/MiKUPSZods2onBaIeShOV6wL9QPxqds +K+RN9WLbXugY4UTsiMSSk2CzHq4JWzVl5frHwBx4fLm50GwoRtyHnvSNNMiZtGAdO79 9V5+qCdL1LVIPg4YqP5qwlH9/+3q14h1dKSfvLdDUz4oO1dXngxo8uUDXPQJ6g2u4shU Kwgv21vStxkc11Mf7r3tj8KeBvKN+m6O3i4LOspkowAHorvQ+k+vIdviO6p2dR2SHgxN wECg== X-Forwarded-Encrypted: i=1; AJvYcCU1gLJbLaDUYesTwhKF7OehLTGFd8B/OGYJZt0GrlurkO6ykc035FJhITbk5RMZEm1bshyocy46YPYq8/4=@vger.kernel.org, AJvYcCUu64X+MJaGj/UOFISRfiW7p6RaKwaVT7XAW3ha3Vxoa3kxEBodha3uuQnZX9c2S1C164NyK2rVlXlsnA==@vger.kernel.org, AJvYcCXVzdXncb47C64vn0UvJmegIKRe+xiJdfiNPJKtP6u+ybbr7jD+ocSaoXu4XI5TbtqaG55SBMPeHV5GG1Ny@vger.kernel.org X-Gm-Message-State: AOJu0Yz1wE0NmQ3pQnXRG656SzyS4yP3NrLVGt0ghyM9NW1P37Rcf42J AqvSw6rhb3rmGGNaZzyAI4MsQdHsczpiWbxATcatFQElqymAzf/0 X-Google-Smtp-Source: AGHT+IHNKdoo6+620GdTdBdCtV5ntCT1U3cEt865NmyUtuJlPODQMIJJQAGQPSNMoHXvK+gq8Ij5HA== X-Received: by 2002:a05:600c:cc3:b0:426:63f1:9a1b with SMTP id 5b1f17b1804b1-42bb020ba4cmr117166285e9.33.1725310801142; Mon, 02 Sep 2024 14:00:01 -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-42c89dca8absm2913705e9.27.2024.09.02.14.00.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 14:00:00 -0700 (PDT) From: Luca Stefani To: Cc: Luca Stefani , Qu Wenruo , Jens Axboe , Chris Mason , Josef Bacik , David Sterba , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-btrfs@vger.kernel.org Subject: [PATCH v2 2/3] btrfs: Split remaining space to discard in chunks Date: Mon, 2 Sep 2024 22:56:11 +0200 Message-ID: <20240902205828.943155-3-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240902205828.943155-1-luca.stefani.ge1@gmail.com> References: <20240902205828.943155-1-luca.stefani.ge1@gmail.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 the block discard limit in preparation of introduction of cancellation signals handling. Reported-by: Qu Wenruo Closes: https://lore.kernel.org/lkml/2e15214b-7e95-4e64-a899-725de12c9037@gmail.com/T/#mdfef1d8b36334a15c54cd009f6aadf49e260e105 Signed-off-by: Luca Stefani --- fs/btrfs/extent-tree.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index feec49e6f9c8..894684f4f497 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -1239,8 +1239,9 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, u64 *discarded_bytes) { int j, ret = 0; - u64 bytes_left, end; + u64 bytes_left, bytes_to_discard, end; u64 aligned_start = ALIGN(start, 1 << SECTOR_SHIFT); + sector_t sector, nr_sects, bio_sects; /* Adjust the range to be aligned to 512B sectors if necessary. */ if (start != aligned_start) { @@ -1300,13 +1301,23 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, bytes_left = end - start; } - if (bytes_left) { - ret = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT, - bytes_left >> SECTOR_SHIFT, - GFP_NOFS); + while (bytes_left) { + sector = start >> SECTOR_SHIFT; + nr_sects = bytes_left >> SECTOR_SHIFT; + bio_sects = min(nr_sects, bio_discard_limit(bdev, sector)); + bytes_to_discard = bio_sects << SECTOR_SHIFT; + + ret = blkdev_issue_discard(bdev, sector, bio_sects, GFP_NOFS); + if (!ret) - *discarded_bytes += bytes_left; + *discarded_bytes += bytes_to_discard; + else if (ret != -EOPNOTSUPP) + return ret; + + start += bytes_to_discard; + bytes_left -= bytes_to_discard; } + return ret; } From patchwork Mon Sep 2 20:56:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Stefani X-Patchwork-Id: 13787699 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 6E6C52139C7; Mon, 2 Sep 2024 21:00:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725310806; cv=none; b=kHwVNyBqg4sDICbLS5LgBD6SvntcE579aTcudck1bcA4wZ3GM6+moefabRFuf710Vjy+ojHgACag1PZ4KmPIOi6LeXTZv+q1ZOa9o0yUXGjoIGifkfp9JQXvOfsG3ZIPkMoLQP/Y6ZEdcQHi+kg8LTljFRRVs+FA1CWFjD0kpp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725310806; c=relaxed/simple; bh=pkpET/uZFHN7xCawNWO7nY5MStK547dU2c9KvFYWEv4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fCBk1/OGRSJVXE1IvL8hRqOvM3g1RnYzvNeemSpvq8XD6yymGLlU2Zthrq0OJrpa92sy8XyRLsagMcxxj2iQ5IJ8b0+DmvaLnwN3NOxF0FZoQFcAjLLDM2oIJ2G597LBkDohOlqshfSeZUFaqs82XuvL0oyY9+x+97M7vBg8Hdc= 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=Kblt3aUK; arc=none smtp.client-ip=209.85.128.51 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="Kblt3aUK" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-42bc19e94bdso23015685e9.3; Mon, 02 Sep 2024 14:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725310803; x=1725915603; 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=HlxzsdvXEzZryfS+C92F0IMet4pQ2CFegtQZuGlbV9A=; b=Kblt3aUKM+p919M32cTyx1PKmBJZEbpcfGU6UhDrWbMc39LEZ7NyonSv5hy9jK8Ogn NHZz5NwmESy2USVWrPr6eDki7570uDDMv6PO51qX9O+70htEbOpQZVIvj2JXCj1TmF7O YCbUWfxt3c6ieyc6rI+vEvxShDXzflB7H9VoFIUlV8U9JXqbkOiB5dHHsnyJw/fwpB9n IFe2TDFNQi58Wm0u+/sf9lIi5v5O+jDz/mQoUZZzLEPCV/SO8BCYVjBUMieF04H1JyfT tYvzmSXQaHWs57pZXIiNbVBj87hk0PaMkiQwgZovvYFrbuKr37RjQtRwLDPYLXlvV4Ld 5A4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725310803; x=1725915603; 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=HlxzsdvXEzZryfS+C92F0IMet4pQ2CFegtQZuGlbV9A=; b=p5A1YbTfjibi6mCTUyj1aIu2ZTiQek6nRWpQaG6dOg97EkP19ZgFI/+vZLVW+RCHTd QV5hKli8Lloq4WrMtv1zXlzmzoUvBr93xvOUFCHuy+HtIIuVAuuNyDJ3jJkCrxoEtVod v9J/FCKVCYI0iV2cQ3+j6klj7RlUgsLXaXBff5NEaadHWkxEjwCJxhNixmCCmOcJ6iJz kZWorQy2rG86vkNiQAUrKSqz6++aK0q9j3sWQw6vFZ1c7IyACSe8NM4pz1J3VRsYFoBw z5hUSsK8oxRcO2QftQ9KGkmy1bBWLizqdB0kXPQkiVZV32PXZISw6/iy2WWpHU6D8NgF XC8w== X-Forwarded-Encrypted: i=1; AJvYcCWH813CcuGQj0ydKEBbYHF8IOpKIUHA5uPs1J7CBk9ysj+xyWj68pKWdEAfZ+hQZHX3FxRJzfhf+fRiOA==@vger.kernel.org, AJvYcCXkRd5yjJt39ISDpaPV+fpLzauxayG3sfAHJzJzE7FLb+JsmENlnrhGteu7ZTvfVDgX4drE7mUh9vhAkQQ=@vger.kernel.org, AJvYcCXnnLLsancRzpAPY3EroCaSlufBij/GpEhsdbijZ9vftC0HjkvJumoSlj3avFZrRf8kZiQket47I8zUEcfD@vger.kernel.org X-Gm-Message-State: AOJu0YyrOlMGMNURBP7PmejX0PSU5m/0DOMVI0b2/ohE6lXr9oUazBkU XnhAwYkqbKuPTDOZZ+HP1skurzxl4knA5wWSDnEbzosNzxcBHzwm X-Google-Smtp-Source: AGHT+IFtjjxmX/D2XNy5vajIk5D1o0OYAnOsZdshyL6WHLN4dfHZLBBUQb5XzmYRI6QBDtrgfHcG5A== X-Received: by 2002:a05:600c:1e15:b0:426:5dde:627a with SMTP id 5b1f17b1804b1-42bb01e5b62mr112014445e9.23.1725310802534; Mon, 02 Sep 2024 14:00:02 -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-42c89dca8absm2913705e9.27.2024.09.02.14.00.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2024 14:00:02 -0700 (PDT) From: Luca Stefani To: Cc: Luca Stefani , Jens Axboe , Chris Mason , Josef Bacik , David Sterba , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-btrfs@vger.kernel.org Subject: [PATCH v2 3/3] btrfs: Don't block system suspend during fstrim Date: Mon, 2 Sep 2024 22:56:12 +0200 Message-ID: <20240902205828.943155-4-luca.stefani.ge1@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240902205828.943155-1-luca.stefani.ge1@gmail.com> References: <20240902205828.943155-1-luca.stefani.ge1@gmail.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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=219180 Link: https://bugzilla.suse.com/show_bug.cgi?id=1229737 Signed-off-by: Luca Stefani --- fs/btrfs/extent-tree.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 894684f4f497..7c78ed4044db 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "ctree.h" #include "extent-tree.h" #include "transaction.h" @@ -1235,6 +1236,11 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans, return ret; } +static bool btrfs_trim_interrupted(void) +{ + return fatal_signal_pending(current) || freezing(current); +} + static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, u64 *discarded_bytes) { @@ -1302,6 +1308,9 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, } while (bytes_left) { + if (btrfs_trim_interrupted()) + break; + sector = start >> SECTOR_SHIFT; nr_sects = bytes_left >> SECTOR_SHIFT; bio_sects = min(nr_sects, bio_discard_limit(bdev, sector)); @@ -6470,7 +6479,7 @@ static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed) start += len; *trimmed += bytes; - if (fatal_signal_pending(current)) { + if (btrfs_trim_interrupted()) { ret = -ERESTARTSYS; break; } @@ -6519,6 +6528,9 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) cache = btrfs_lookup_first_block_group(fs_info, range->start); for (; cache; cache = btrfs_next_block_group(cache)) { + if (btrfs_trim_interrupted()) + break; + if (cache->start >= range_end) { btrfs_put_block_group(cache); break; @@ -6558,6 +6570,9 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) mutex_lock(&fs_devices->device_list_mutex); list_for_each_entry(device, &fs_devices->devices, dev_list) { + if (btrfs_trim_interrupted()) + break; + if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) continue;