Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3879056rwi; Sat, 22 Oct 2022 01:26:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Kl9t/jXlBtXw5klhj4E4uMEI/XtkojlG2VYUd/lpY+TXrO0CPcLGjD04Bbsn0M8wa/EZO X-Received: by 2002:a17:902:8542:b0:179:eb8d:f41d with SMTP id d2-20020a170902854200b00179eb8df41dmr22544897plo.62.1666427182391; Sat, 22 Oct 2022 01:26:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666427182; cv=none; d=google.com; s=arc-20160816; b=wAKW2bweZ3593Tbvq6FfMae5Gp/8ic5j9q/pJGGkaecRuWBXdwXMai4BbW5xUeQmNc LoDoTEQ+Th6ACzYbR3P4dF4E6n4iAVlnodl0a912L29AjsCG1lMK50dQ9FtyL42/F6oA i/LzeOmKx2t5gdMbn7uyAVfeih9GDWU+n7MZv0ruIUcYKa7HYzYKOlsTSRGNB5fcWKPv INIhIfopIQgjJTxDA/jS0d9jb7z9NHHtAFyvb6PDEWGxyQV0iOo1JcLPsFpsCddqFLq5 th7aQK4sPOsR92XEvyfD7lyGeTLtZD3tmpXVb/s6ooQ1zmd12jgZuUVZ67fj1Iw7HWuY ux4w== 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=4fsNrMzV8MFRc5f3fJOQf8AIgom40b2LkD9bVgo6Ajs=; b=f/1GBxwlAHef469wz6bgiCC3EqQSDbmoQxwGYXxwCTgkhp41pB/iwieufmtS6BW5FN ZLcfubyLFMivOpbPhop1LrZr7UcJtZOmem+6PFYjYOVkEXwHUQfFy5I2CUnqKbL+2Xqj eKG45zUp26oQ8honwVGiSdELMCASZ+Yv71srSVILOxvIihxHvKH4kvlvZwNEwqM9cO+8 G/KdQziC/UG5QDMBQ6WoA3FFr7J8/vb0ZXU2jtT9kRVACuhy9fHeFyi0QTVjoS+k4Gcq 0hcoXHnMxqF0z2WleAabL+E1xqB4IQXc3L7iXq8PFZHDySYNZ6hI7REEaOQxqwwfu2df byqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iEzGLTg8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q197-20020a632ace000000b004276c7b2253si28339906pgq.584.2022.10.22.01.26.10; Sat, 22 Oct 2022 01:26:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iEzGLTg8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231248AbiJVHmR (ORCPT + 99 others); Sat, 22 Oct 2022 03:42:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231126AbiJVHls (ORCPT ); Sat, 22 Oct 2022 03:41:48 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C127543E2; Sat, 22 Oct 2022 00:39:52 -0700 (PDT) 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 ams.source.kernel.org (Postfix) with ESMTPS id 5D460B82DFA; Sat, 22 Oct 2022 07:38:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2E19C433C1; Sat, 22 Oct 2022 07:38:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424332; bh=ecbJtZtbTxTy0Xaol6OENYdH2Dt+6dzVljS18GKzyfU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iEzGLTg8kNz3MxHKDmfd2im+pD9sb3wh3MI45pdB0xljEmpFUtgpGqTI/CcEFCtmj n3AiuxKJxxk+8hP/Api/AR9RQWaKgxRUGVN6qZdvbQ/AyCuZV9V7+SzgoLhR4nuCVD iJAb00Mzed5I0lJzGam1FvQyoQK+QrX/WaGh+h18= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexander Zhu , David Sterba Subject: [PATCH 5.19 107/717] btrfs: fix alignment of VMA for memory mapped files on THP Date: Sat, 22 Oct 2022 09:19:46 +0200 Message-Id: <20221022072434.370971359@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexander Zhu commit b0c582233a8563f3c4228df838cdc67a8807ec78 upstream. With CONFIG_READ_ONLY_THP_FOR_FS, the Linux kernel supports using THPs for read-only mmapped files, such as shared libraries. However, the kernel makes no attempt to actually align those mappings on 2MB boundaries, which makes it impossible to use those THPs most of the time. This issue applies to general file mapping THP as well as existing setups using CONFIG_READ_ONLY_THP_FOR_FS. This is easily fixed by using thp_get_unmapped_area for the unmapped_area function in btrfs, which is what ext2, ext4, fuse, and xfs all use. Initially btrfs had been left out in commit 8c07fc452ac0 ("btrfs: fix alignment of VMA for memory mapped files on THP") as btrfs does not support DAX. However, commit 1854bc6e2420 ("mm/readahead: Align file mappings for non-DAX") removed the DAX requirement. We should now be able to call thp_get_unmapped_area() for btrfs. The problem can be seen in /proc/PID/smaps where THPeligible is set to 0 on mappings to eligible shared object files as shown below. Before this patch: 7fc6a7e18000-7fc6a80cc000 r-xp 00000000 00:1e 199856 /usr/lib64/libcrypto.so.1.1.1k Size: 2768 kB THPeligible: 0 VmFlags: rd ex mr mw me With this patch the library is mapped at a 2MB aligned address: fbdfe200000-7fbdfe4b4000 r-xp 00000000 00:1e 199856 /usr/lib64/libcrypto.so.1.1.1k Size: 2768 kB THPeligible: 1 VmFlags: rd ex mr mw me This fixes the alignment of VMAs for any mmap of a file that has the rd and ex permissions and size >= 2MB. The VMA alignment and THPeligible field for anonymous memory is handled separately and is thus not effected by this change. CC: stable@vger.kernel.org # 5.18+ Signed-off-by: Alexander Zhu Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/file.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index 5a3f6e0d9688..8dfce1c40b5f 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -3810,6 +3810,7 @@ const struct file_operations btrfs_file_operations = { .mmap = btrfs_file_mmap, .open = btrfs_file_open, .release = btrfs_release_file, + .get_unmapped_area = thp_get_unmapped_area, .fsync = btrfs_sync_file, .fallocate = btrfs_fallocate, .unlocked_ioctl = btrfs_ioctl, -- 2.38.0