Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3176585rdh; Thu, 28 Sep 2023 05:09:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHKZ+k6GJU5DDIYUQzxYeYDP2nf8J6ZfjLQ1KgBPsX6RXMUFLzguLY4CSIfUHRttci8Hvv2 X-Received: by 2002:a17:90b:1b52:b0:276:86e0:2e45 with SMTP id nv18-20020a17090b1b5200b0027686e02e45mr828998pjb.43.1695902964788; Thu, 28 Sep 2023 05:09:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695902964; cv=none; d=google.com; s=arc-20160816; b=azL1akwhcO/WN/e3ntpC+LksR4nOi+Ort2Jw+hvmEXh91sLQOt7KNmbZrpeRftZb0W Ol4j5Qgf/xJgayBpVLi1j6HI2KRPYm5unfWq0PRh9IkK1t72kEnn5qztwAu0velFRMym bAC7Nfl+a6Oyz20CTb+7OxoN4ZzKXSTiRlLfz9AwOojF3u+FhIEssWkRS61KO7Vz5IAs hS7dZqIUoFhXy8ZvZeNURbnmXrSgry7+cbe66C+W6Wgmye/dWV3iatOMz0Pw2mGTeo1f DfWrNAsj5/knNwd1KYNJh5+ythR/wY98t8Nhzt0ZrOa3nqaEYL29HM0rAi+m6Hmi89oZ 9W2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=eErpPbAfD0HzNbDWCSq0NqTvXCcidCAJPs5lDgztij4=; fh=wPnFwHQ6dVTwJg/H83xno86dkueQfF3lpy8S4wSH8h4=; b=o7MWW1bHptJJSEcPIV/Y2ZC/Nyj6kUmZhx4xQaw2ixt2WBfJ0HFByy09gC/oBeU1Nk YGf5THsmxQkNMoIsSrmbC9oGx+uHSPe06rR/umNT14X+Q9pHWLOKMTTlkP6+hElul7No h8GE6uw9s+qFPia4YjIBTolg/USNwGskclossdFaSx36/3owRHHtzHKkRXeqjIvzHThB mzkSIzHxysr9J2EjAnfQVMOV7xj8W1tIuKUdxJ7Oo76IPLRrf/DxguTSDA6S6Zb3THgc kylhUhV8RyfHoBOyl8HQHVcqfrFcLKdtB31+kMHXdJTKLPegb7tMFGvYvCpAEbqciXLl 7hUg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id t1-20020a17090aba8100b00268278c51e3si19537655pjr.49.2023.09.28.05.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 05:09:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id F20AB830C336; Thu, 28 Sep 2023 04:59:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232108AbjI1L7t (ORCPT + 99 others); Thu, 28 Sep 2023 07:59:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229980AbjI1L7s (ORCPT ); Thu, 28 Sep 2023 07:59:48 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9DA0A121; Thu, 28 Sep 2023 04:59:46 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A25D51FB; Thu, 28 Sep 2023 05:00:24 -0700 (PDT) Received: from [10.1.37.161] (XHFQ2J9959.cambridge.arm.com [10.1.37.161]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id EF6F23F5A1; Thu, 28 Sep 2023 04:59:44 -0700 (PDT) Message-ID: <54e5accf-1a56-495a-a4f5-d57504bc2fc8@arm.com> Date: Thu, 28 Sep 2023 12:59:43 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG: MADV_COLLAPSE doesn't work for XFS files Content-Language: en-GB To: Bagas Sanjaya , Hugh Dickins , David Hildenbrand , Matthew Wilcox , zokeefe@google.com, Chandan Babu R , "Darrick J. Wong" Cc: Linux Memory Management List , Linux XFS , Linux Kernel Mailing List References: <4d6c9b19-cdbb-4a00-9a40-5ed5c36332e5@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 28 Sep 2023 04:59:56 -0700 (PDT) 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; >> } > > 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. >> >> 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. >