From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A8C2C47258 for ; Tue, 23 Jan 2024 17:26:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 026646B0080; Tue, 23 Jan 2024 12:26:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ECA4D6B0082; Tue, 23 Jan 2024 12:26:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D92406B0085; Tue, 23 Jan 2024 12:26:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C29C56B0080 for ; Tue, 23 Jan 2024 12:26:57 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 24F8DC0309 for ; Tue, 23 Jan 2024 17:26:57 +0000 (UTC) X-FDA: 81711256074.26.51D432E Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf14.hostedemail.com (Postfix) with ESMTP id 346F4100009 for ; Tue, 23 Jan 2024 17:26:54 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706030815; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=A9Gt/A9I0uhI2DXdN2UvkPoIyqLKDzM6ZuDh5Tpiywg=; b=GSa1QNNytQq5LIPaJJMl2HN+ZWtDQXLOZqQUkvPLZm87lLxF47LuxCgxoZ/Vm6GYWBYZrv V0/M6I47LgI5m0360+Qexc4KlOh410/Sah3TwU+7ikWraGUJ3B9DH2hst79/vfr4TaLPN9 Q2VzymX8UqaFS75VyDm+RR5LEKPZGvc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706030815; a=rsa-sha256; cv=none; b=UNRvCp1nCqSba7DSc5aGbvIe0HVhxAHrIJ9PrPfeUIpc/Vg1nOcLHxzI3ds2cueisPS3e3 duoRWpiSKVXIqRUyBtRJGn2byQ6XfaFolbdb5izfrE7YbSB/hzRs3ckTjHJCBkLzEfGPpr gxd92qanFxKLbraJR4TZxpSosWpAEn8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com 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 788951FB; Tue, 23 Jan 2024 09:27:39 -0800 (PST) Received: from [10.57.77.165] (unknown [10.57.77.165]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 0B9AC3F5A1; Tue, 23 Jan 2024 09:26:52 -0800 (PST) Message-ID: Date: Tue, 23 Jan 2024 17:26:51 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RESEND PATCH] mm: align larger anonymous mappings on THP boundaries Content-Language: en-GB To: Yang Shi Cc: Matthew Wilcox , Yang Shi , riel@surriel.com, cl@linux.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20231214223423.1133074-1-yang@os.amperecomputing.com> <1e8f5ac7-54ce-433a-ae53-81522b2320e1@arm.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 346F4100009 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: fo6sdg1dhqaxn8ec1aih6hdjzd6oryyk X-HE-Tag: 1706030814-301121 X-HE-Meta: U2FsdGVkX1+lcJK7c6GE64vIdwS2hG/qJ+AMaNlOrj5/0cHUKWCS1rjkP8f/SJ6gqGIzaXpFxABMo0OztH3BIHM6UgETCIH0bd+KKpBuZGCykdQteLBF5MwI9vkSjc6A4qFfA/hzPzzfLSEyREHF1ldr73mhtxEhmarli8fDFfRhxlC127JnXU4w5hBhZHyeEG5uxJThWbaZLdVgliwKWGM6E4hZpNaAJgWsgx/gDhv7Rl3sKukPlIwTfmOq8yM9fdRgpeRWWjVy4p8oGTVcxF5z6e15u4SENXsPv/VksdIBE1OBlFeDuQZu/F0y7/x8WRxh3F9B7wWWiOCM5WqZA19k8O1aWn0Gkezr+nVjSKyYM1F+DswFNeKYq/ghI81lJZGdKNTnpA3k+AP3kxXZM3HnAZes4CITQatOZxkzNs5wUEXhvKrpaKL/3xaEdilb182tkApm1wjfy354PWcaTUSOwX/1kK1R4ouBHYfhloGbmyNu/L6tcXEpar7y5fe+aWbNW0uTeibeFjeBoMfEDD6m+m9Uc4dETQxecc5VhMIAWsZbjdeqAL6JlswpCDgkyih9yeuPUh6GxVzPX/rjmu5T5tw5f1ooEuVEIKev4oylAFaiJuqGlLldC8FqMIv51Fws/v03mPKu8f4LzYddR9RklhpEVg6wsIJXDGD7Vhdq1krH27TAJ4aG25/Uhy+3mv81PFEHBvacw7lGC+6HkyAqyhAmOj1E2P69is+oZYf55BWv/Ggn386IyWA0vxsRFYcgg6yxS2TiuvcJvUjwifEjpqmsOsutxHrWItXwqfTbl+fYEE0PWgeaJNExqW383LxPUQcjcfmb4GnCnYgEQgIdjjbFyxIWCLAf4IOIOr24CfsfpnEQaO+B/lum01ApUOTCiMuLH3SyubOuT88heCor3E0t7SzPokq8y8ruikjmYG34T88UKYsu3pOqFOFGTyojFh0d1hKmZfTl3oW IRbgcf7J b/OZUyVtwh68ScgG94lN04Z1CSKONT2CYl2oqeWygH4G4VqdkXEgR9q9NPfyTv01ddmL0pTKKR2f8Es2GhBtPGexXcWEwm2L/aAsiQV+u4BSMoYPU4Q2ukaDrLzG3J2sfeTI63I4qdGy8NaQcEneuSwExvpcr2V2lpaG6QhbMWxzyFLSBsAvfN2+/sq0N+dzQRkJey/sKRIIjqsE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 23/01/2024 17:14, Yang Shi wrote: > On Tue, Jan 23, 2024 at 1:41 AM Ryan Roberts wrote: >> >> On 22/01/2024 19:43, Yang Shi wrote: >>> On Mon, Jan 22, 2024 at 3:37 AM Ryan Roberts wrote: >>>> >>>> On 20/01/2024 16:39, Matthew Wilcox wrote: >>>>> On Sat, Jan 20, 2024 at 12:04:27PM +0000, Ryan Roberts wrote: >>>>>> However, after this patch, each allocation is in its own VMA, and there is a 2M >>>>>> gap between each VMA. This causes 2 problems: 1) mmap becomes MUCH slower >>>>>> because there are so many VMAs to check to find a new 1G gap. 2) It fails once >>>>>> it hits the VMA limit (/proc/sys/vm/max_map_count). Hitting this limit then >>>>>> causes a subsequent calloc() to fail, which causes the test to fail. >>>>>> >>>>>> Looking at the code, I think the problem is that arm64 selects >>>>>> ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT. But __thp_get_unmapped_area() allocates >>>>>> len+2M then always aligns to the bottom of the discovered gap. That causes the >>>>>> 2M hole. As far as I can see, x86 allocates bottom up, so you don't get a hole. >>>>> >>>>> As a quick hack, perhaps >>>>> #ifdef ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT >>>>> take-the-top-half >>>>> #else >>>>> current-take-bottom-half-code >>>>> #endif >>>>> >>>>> ? >>> >>> Thanks for the suggestion. It makes sense to me. Doing the alignment >>> needs to take into account this. >>> >>>> >>>> There is a general problem though that there is a trade-off between abutting >>>> VMAs, and aligning them to PMD boundaries. This patch has decided that in >>>> general the latter is preferable. The case I'm hitting is special though, in >>>> that both requirements could be achieved but currently are not. >>>> >>>> The below fixes it, but I feel like there should be some bitwise magic that >>>> would give the correct answer without the conditional - but my head is gone and >>>> I can't see it. Any thoughts? >>> >>> Thanks Ryan for the patch. TBH I didn't see a bitwise magic without >>> the conditional either. >>> >>>> >>>> Beyond this, though, there is also a latent bug where the offset provided to >>>> mmap() is carried all the way through to the get_unmapped_area() >>>> impelementation, even for MAP_ANONYMOUS - I'm pretty sure we should be >>>> force-zeroing it for MAP_ANONYMOUS? Certainly before this change, for arches >>>> that use the default get_unmapped_area(), any non-zero offset would not have >>>> been used. But this change starts using it, which is incorrect. That said, there >>>> are some arches that override the default get_unmapped_area() and do use the >>>> offset. So I'm not sure if this is a bug or a feature that user space can pass >>>> an arbitrary value to the implementation for anon memory?? >>> >>> Thanks for noticing this. If I read the code correctly, the pgoff used >>> by some arches to workaround VIPT caches, and it looks like it is for >>> shared mapping only (just checked arm and mips). And I believe >>> everybody assumes 0 should be used when doing anonymous mapping. The >>> offset should have nothing to do with seeking proper unmapped virtual >>> area. But the pgoff does make sense for file THP due to the alignment >>> requirements. I think it should be zero'ed for anonymous mappings, >>> like: >>> >>> diff --git a/mm/mmap.c b/mm/mmap.c >>> index 2ff79b1d1564..a9ed353ce627 100644 >>> --- a/mm/mmap.c >>> +++ b/mm/mmap.c >>> @@ -1830,6 +1830,7 @@ get_unmapped_area(struct file *file, unsigned >>> long addr, unsigned long len, >>> pgoff = 0; >>> get_area = shmem_get_unmapped_area; >>> } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { >>> + pgoff = 0; >>> /* Ensures that larger anonymous mappings are THP aligned. */ >>> get_area = thp_get_unmapped_area; >>> } >> >> I think it would be cleaner to just zero pgoff if file==NULL, then it covers the >> shared case, the THP case, and the non-THP case properly. I'll prepare a >> separate patch for this. > > IIUC I don't think this is ok for those arches which have to > workaround VIPT cache since MAP_ANONYMOUS | MAP_SHARED with NULL file > pointer is a common case for creating tmpfs mapping. For example, > arm's arch_get_unmapped_area() has: > > if (aliasing) > do_align = filp || (flags & MAP_SHARED); > > The pgoff is needed if do_align is true. So we should just zero pgoff > iff !file && !MAP_SHARED like what my patch does, we can move the > zeroing to a better place. We crossed streams - I sent out the patch just as you sent this. My patch is implemented as I proposed. I'm not sure I agree with what you are saying. The mmap man page says this: The contents of a file mapping (as opposed to an anonymous mapping; see MAP_ANONYMOUS below), are initialized using length bytes starting at offset offset in the file (or other object) referred to by the file descriptor fd. So that implies offset is only relavent when a file is provided. It then goes on to say: MAP_ANONYMOUS The mapping is not backed by any file; its contents are initialized to zero. The fd argument is ignored; however, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this. The offset argument should be zero. So users are expected to pass offset=0 when mapping anon memory, for both shared and private cases. Infact, in the line above where you made your proposed change, pgoff is also being zeroed for the (!file && (flags & MAP_SHARED)) case. > >> >> >>> >>>> >>>> Finally, the second test failure I reported (ksm_tests) is actually caused by a >>>> bug in the test code, but provoked by this change. So I'll send out a fix for >>>> the test code separately. >>>> >>>> >>>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >>>> index 4f542444a91f..68ac54117c77 100644 >>>> --- a/mm/huge_memory.c >>>> +++ b/mm/huge_memory.c >>>> @@ -632,7 +632,7 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, >>>> { >>>> loff_t off_end = off + len; >>>> loff_t off_align = round_up(off, size); >>>> - unsigned long len_pad, ret; >>>> + unsigned long len_pad, ret, off_sub; >>>> >>>> if (off_end <= off_align || (off_end - off_align) < size) >>>> return 0; >>>> @@ -658,7 +658,13 @@ static unsigned long __thp_get_unmapped_area(struct file *filp, >>>> if (ret == addr) >>>> return addr; >>>> >>>> - ret += (off - ret) & (size - 1); >>>> + off_sub = (off - ret) & (size - 1); >>>> + >>>> + if (current->mm->get_unmapped_area == arch_get_unmapped_area_topdown && >>>> + !off_sub) >>>> + return ret + size; >>>> + >>>> + ret += off_sub; >>>> return ret; >>>> } >>> >>> I didn't spot any problem, would you please come up with a formal patch? >> >> Yeah, I'll aim to post today. > > Thanks! > >> >>