Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3521779rdh; Thu, 28 Sep 2023 14:24:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGK30tbIUjQgSZBKr6Gibd8RNxaCcYoT8kaYLYE1FB+DZZlf9Bw3TwkNtahN/oSIVK+74Cm X-Received: by 2002:aca:1305:0:b0:3ae:4cad:91a0 with SMTP id e5-20020aca1305000000b003ae4cad91a0mr2403682oii.6.1695936262010; Thu, 28 Sep 2023 14:24:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695936261; cv=none; d=google.com; s=arc-20160816; b=oQy8v7S/breyeafUJLN0MX3sTI0xkx5DHGpjBeruB8R70ovrxwC4/TjG6HrIJ/ZENn UpIyQpw/snBOImSWbOWGbjgIgtLBWxrBMsca+TtvxY7Uf+x32v92+rmU0cKhp/+5E1DY Tsr4DYjyrv4GCJdM+ea9Bihn2Ix6zFcJume2JPc5JnId95knz5Umi0K/ZFG34uyUXzIB NJ2E9cZNnzTlxJsc4If0wCgKhsE1ZJTOAsI/m4VwN3k6GS7lVpBl9ZTExMUHc+UPb2sR NqgdcB/aq3SYZF9bDJq2vqytqRloFnL9g4QIzCiPNTbnxVhpw5lp47mhrEwgAoicxQK/ KscQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=vvaGZz0S2mve5PqT9OEdKb9K0YGDOdRou1VXcHhq0gY=; fh=/0LHPqNkNFqZvsX8E3exOnDdUrFXgD6oAhr5WpRp98k=; b=KbGNv15grlHx3UUkd2RyMcZ4y/bBgs/G1gyIcVcEiW8b4D7rz4LE+0B7+wWm6ZmEZI qwGrArd6H683w20ftK88aeR+fMc6JIViZ0oMSU/nGSOI/ibbk1dABoVZIth/ejrZQusm EpnR5CHqciC4zR8Mk9CYxUEKneI9h101YN7I4UdnByVmlgWQQKAF8q6irHoPc/lwwzTT tqJzldvQ+0ii+r+rSNjF3u3NzwQfXFoi6i50dCPldjaos3oN2AjVc7WWT75xFSTe0I1p vyXIwCIvW5FRt6KHshpZ8h/oznemUfoQj4nl50EN69mNhSB2iOeoyk+Ts8XVdN2ETBOu flDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GifZoq+S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id t5-20020a628105000000b00690d25b198dsi19194894pfd.103.2023.09.28.14.24.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 14:24:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GifZoq+S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id D551582F098A; Thu, 28 Sep 2023 14:20:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230251AbjI1VEl (ORCPT + 99 others); Thu, 28 Sep 2023 17:04:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232215AbjI1VEj (ORCPT ); Thu, 28 Sep 2023 17:04:39 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07FB01AD; Thu, 28 Sep 2023 14:04:37 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EFA80C433C8; Thu, 28 Sep 2023 21:04:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695935077; bh=M/5ZU8g6yA4315it+eEcyz6qGTDOsNSnU7hBp/ukGEI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=GifZoq+S91dlVSa8ba0LZTxOnoK6itojp7KAx0aHws8HEpKAazauz/F7V6ey4j4aZ ReTk2p58whVPBqxUi1p8nECtD32BcYK73OW0ypmbynywHszkqFvjSs9Oanu2jEsaJf G2yVpCNxUtxC8A1bekl8OOXosCFSPGeKVbuU8aa2DjyYdH093YRAdDshO9jwS33at2 oxSPfIWR3UiZOcRFOkd9hnM9jS7+/E7fgt2pcbQ0kh3te4uvyOrY27twx+VsJBFGTG /48cAHA4N/1KsXPkSE+TxA7p91cxibX7Ttw+GQgRBcX8caNe3WmJLWqoObJ0VsYqM2 hLjyiaOnF9dVg== Date: Thu, 28 Sep 2023 14:04:36 -0700 From: "Darrick J. Wong" To: Zach O'Keefe Cc: Ryan Roberts , Bagas Sanjaya , Hugh Dickins , David Hildenbrand , Matthew Wilcox , Chandan Babu R , Linux Memory Management List , Linux XFS , Linux Kernel Mailing List , Yu Zhao Subject: Re: BUG: MADV_COLLAPSE doesn't work for XFS files] Message-ID: <20230928210436.GG11456@frogsfrogsfrogs> References: <4d6c9b19-cdbb-4a00-9a40-5ed5c36332e5@arm.com> <54e5accf-1a56-495a-a4f5-d57504bc2fc8@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 28 Sep 2023 14:20:14 -0700 (PDT) On Thu, Sep 28, 2023 at 12:43:57PM -0700, Zach O'Keefe wrote: > Hey Ryan, > > Thanks for bringing this up. > > On Thu, Sep 28, 2023 at 4:59 AM Ryan Roberts wrote: > > > > On 28/09/2023 11:54, Bagas Sanjaya wrote: > > > On Thu, Sep 28, 2023 at 10:55:17AM +0100, Ryan Roberts wrote: > > >> Hi all, > > >> > > >> I've just noticed that when applied to a file mapping for a file on xfs, MADV_COLLAPSE returns EINVAL. The same test case works fine if the file is on ext4. > > >> > > >> I think the root cause is that the implementation bails out if it finds a (non-PMD-sized) large folio in the page cache for any part of the file covered by the region. XFS does readahead into large folios so we hit this issue. See khugepaged.h:collapse_file(): > > >> > > >> if (PageTransCompound(page)) { > > >> struct page *head = compound_head(page); > > >> > > >> result = compound_order(head) == HPAGE_PMD_ORDER && > > >> head->index == start > > >> /* Maybe PMD-mapped */ > > >> ? SCAN_PTE_MAPPED_HUGEPAGE > > >> : SCAN_PAGE_COMPOUND; > > >> goto out_unlock; > > >> } > > > > > Ya, non-PMD-sized THPs were just barely visible in my peripherals when > writing this, and I'm still woefully behind on your work on them now > (sorry!). > > I'd like to eventually make collapse (not just MADV_COLLAPSE, but > khugepaged too) support arbitrary-sized large folios in general, but > I'm very pressed for time right now. I think M. Wilcox is also > interested in this, given he left the TODO to support it :P Is the point of MADV_COLLAPSE to replace base pages with PMD-sized pages in the pagecache for faster lookups? Or merely to replace them with something larger, even if it's not PMD sized? As of 6.6, XFS asks for folios of size min(read/readahead/write_len, ondisk_mapping_length), so in theory the folio size should roughly follow the access patterns. If the goal is merely larger folios, then we are done here and can move on to some other part of the collapse. OTOH if the goal is TLB savings, then I suppose you'd actually /want/ to select a large (but not PMD) folio for collapsing into a PMD sized folio, right? e.g. if (PageTransCompound(page)) { struct page *head = compound_head(page); if (head->index != start) { /* not sure what _COMPOUND means here... */ result = SCAN_PAGE_COMPOUND; goto out_unlock; } if (compound_order(head) == HPAGE_PMD_ORDER) { result = SCAN_PTE_MAPPED_HUGEPAGE; goto out_unlock; } /* result is still SCAN_SUCCEED, keep going */ } I /think/ that would work? If the largefolio is dirty or not fully uptodate then collapse won't touch it; and I think fs/iomap handles this in a compatible way because it won't mark the folio uptodate until all the blocks have been read, and it marks the folio dirty if any of the blocks are dirty. (says me, who doesn't really understand this part of the code.) --D > Thank you for the reproducer though! I haven't run it, but I'll > probably come back here to steal it when the time comes. > > > > I don't see any hint to -EINVAL above. Am I missing something? > > > > The SCAN_PAGE_COMPOUND result ends up back at madvise_collapse() where it > > eventually gets converted to -EINVAL by madvise_collapse_errno(). > > > > > > > >> > > >> I'm not sure if this is already a known issue? I don't have time to work on a fix for this right now, so thought I would highlight it at least. I might get around to it at some point in the future if nobody else tackles it. > > My guess is Q1 2024 is when I'd be able to look into this, at the > current level of urgency. It doesn't sound like it's blocking anything > for your work right now -- lmk if that changes though! > > Thanks, > Zach > > > > > >> > > >> Thanks, > > >> Ryan > > >> > > >> > > >> Test case I've been using: > > >> > > >> -->8-- > > >> > > >> #include > > >> #include > > >> #include > > >> #include > > >> #include > > >> #include > > >> #include > > >> > > >> #ifndef MADV_COLLAPSE > > >> #define MADV_COLLAPSE 25 > > >> #endif > > >> > > >> #define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0) > > >> > > >> #define SZ_1K 1024 > > >> #define SZ_1M (SZ_1K * SZ_1K) > > >> #define ALIGN(val, align) (((val) + ((align) - 1)) & ~((align) - 1)) > > >> > > >> #if 1 > > >> // ext4 > > >> #define DATA_FILE "/home/ubuntu/data.txt" > > >> #else > > >> // xfs > > >> #define DATA_FILE "/boot/data.txt" > > >> #endif > > >> > > >> int main(void) > > >> { > > >> int fd; > > >> char *mem; > > >> int ret; > > >> > > >> fd = open(DATA_FILE, O_RDONLY); > > >> if (fd == -1) > > >> handle_error("open"); > > >> > > >> mem = mmap(NULL, SZ_1M * 4, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0); > > >> close(fd); > > >> if (mem == MAP_FAILED) > > >> handle_error("mmap"); > > >> > > >> printf("1: pid=%d, mem=%p\n", getpid(), mem); > > >> getchar(); > > >> > > >> mem = (char *)ALIGN((unsigned long)mem, SZ_1M * 2); > > >> ret = madvise(mem, SZ_1M * 2, MADV_COLLAPSE); > > >> if (ret) > > >> handle_error("madvise"); > > >> > > >> printf("2: pid=%d, mem=%p\n", getpid(), mem); > > >> getchar(); > > >> > > >> return 0; > > >> } > > >> > > >> -->8-- > > >> > > > > > > Confused... > > > > This is a user space test case that shows the problem; data.txt needs to be at > > least 4MB and on a mounted ext4 and xfs filesystem. By toggling the '#if 1' to > > 0, you can see the different behaviours for ext4 and xfs - > > handle_error("madvise") fires with EINVAL in the xfs case. The getchar()s are > > leftovers from me looking at the smaps file. > >