Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3688575pxb; Mon, 24 Jan 2022 15:24:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJz8s32Wg4slWM+Zhe7F2I/ozigpSZgIr9r/SqPjpzYcJgif5GJX3QQ5+iRXviKcyvngerE/ X-Received: by 2002:a63:f201:: with SMTP id v1mr4869254pgh.250.1643066691849; Mon, 24 Jan 2022 15:24:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643066691; cv=none; d=google.com; s=arc-20160816; b=Xb5d07SY8br/JjiD+NIpkU5MaiyUV6hUyZTnHeyV8/sZMKZXzqnxWSrU54n5Cv15Jx hRm3NAS1ZbmWD0R/kU9/nphH6Ov0RerwiMzuA9YHg75UXBE/UUJaCkz4vDyVa4byYcQD t9AO1pV78YPUc+kQ3XG1aBfQtad4TRwnBdEA9Q8n9XX6N68tmw3leUUuwq/9ITwe1ufi 8KjQ2ZsLDVS6psY3s8GJLF++oM1Q2l+OLpFD1HkhGbSM2BReBv6E7x76zZxqSCtEUssa BUinBSbJF984YCvaoie5WCq8XcMPXniRQI6sPxUfM006+G9AT+3ZaInW5+to2TKvgcs3 eBlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=TGRS4OHXyenjyEYw3yR6DyKVPBYT5EbU/POY1uQYspw=; b=cS0dAjUqgDtPzlTupNdV0pTjkjCguKbXSMjxECpecD8tFIn1FmjrCtMaQmwvp7b7Vg J3UzgsjinqTkyQ5dIxbsM1vbULEqKSKmJcRjfdsgwQw3590HfokA4Vmgz6PlLzXC+/KD O5TTXgVT+6RYTFNreAFVNrzk+00ZkKYOwwFT2lGou64K8jU9ZQGu7VL9T9eXH40Y3ixA 2raSyxdtqorustIx6Dr1X+RTPbG/ouzM2xyydwL7WPBZ/q+1qIQUgCOwtPYWoM2I4k00 y4repklowIxSfq+2NhXNnAbzx4oWoQXKAVyZKwWqNgr5iu6qsd4kKVN2IMG4ZbrmAW1w BVsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HFyxJ+Z6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e11si583801pjj.25.2022.01.24.15.24.40; Mon, 24 Jan 2022 15:24:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HFyxJ+Z6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1846882AbiAXXRX (ORCPT + 99 others); Mon, 24 Jan 2022 18:17:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1581212AbiAXWRS (ORCPT ); Mon, 24 Jan 2022 17:17:18 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37BCCC04A2D2; Mon, 24 Jan 2022 12:45:37 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C933060C17; Mon, 24 Jan 2022 20:45:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5B27C340E8; Mon, 24 Jan 2022 20:45:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643057136; bh=nmiMrpAAXAP5bidkMoGHx7ZEzwNtUKKPQpg/JF9EprQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HFyxJ+Z6jpTxPJF86HnfXEG7TbM/7v4PAyHw+6T3UeL7XyL9i03cWXoLolUj/i+ez MWXSV72qYvaodmZHhUy05URspfACXySxGM85/B1GxINqqLLMKFPRw53yjLGq4zxVXS KuQHZHvDb1h5qcvVbLcRRHp2Vcpwoukjs7tbdLts= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , David Sterba Subject: [PATCH 5.15 712/846] btrfs: respect the max size in the header when activating swap file Date: Mon, 24 Jan 2022 19:43:49 +0100 Message-Id: <20220124184125.586556723@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184100.867127425@linuxfoundation.org> References: <20220124184100.867127425@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Filipe Manana commit c2f822635df873c510bda6fb7fd1b10b7c31be2d upstream. If we extended the size of a swapfile after its header was created (by the mkswap utility) and then try to activate it, we will map the entire file when activating the swap file, instead of limiting to the max size defined in the swap file's header. Currently test case generic/643 from fstests fails because we do not respect that size limit defined in the swap file's header. So fix this by not mapping file ranges beyond the max size defined in the swap header. This is the same type of bug that iomap used to have, and was fixed in commit 36ca7943ac18ae ("mm/swap: consider max pages in iomap_swapfile_add_extent"). Fixes: ed46ff3d423780 ("Btrfs: support swap files") CC: stable@vger.kernel.org # 5.4+ Reviewed-and-tested-by: Josef Bacik Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/inode.c | 11 +++++++++++ 1 file changed, 11 insertions(+) --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -10586,9 +10586,19 @@ static int btrfs_add_swap_extent(struct struct btrfs_swap_info *bsi) { unsigned long nr_pages; + unsigned long max_pages; u64 first_ppage, first_ppage_reported, next_ppage; int ret; + /* + * Our swapfile may have had its size extended after the swap header was + * written. In that case activating the swapfile should not go beyond + * the max size set in the swap header. + */ + if (bsi->nr_pages >= sis->max) + return 0; + + max_pages = sis->max - bsi->nr_pages; first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT; next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len, PAGE_SIZE) >> PAGE_SHIFT; @@ -10596,6 +10606,7 @@ static int btrfs_add_swap_extent(struct if (first_ppage >= next_ppage) return 0; nr_pages = next_ppage - first_ppage; + nr_pages = min(nr_pages, max_pages); first_ppage_reported = first_ppage; if (bsi->start == 0)