Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp635015rdb; Tue, 23 Jan 2024 09:53:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgDJVMBoOa4DEVXJs3BF1YvugJKIHFowUxoiTDU/nLrpIVR6njLaIjObCDbiaVt5LAI2JX X-Received: by 2002:a05:6358:7f18:b0:176:5b5:39b4 with SMTP id p24-20020a0563587f1800b0017605b539b4mr5959711rwn.37.1706032404661; Tue, 23 Jan 2024 09:53:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706032404; cv=pass; d=google.com; s=arc-20160816; b=DSMnskJYpT2tr/1CNeJwtzUxwE260eEKKggcWOjUgSptmY0N5aPZDnFcUICrKDMd8S pdR6xIUrIsA6cRMWMpHqlkkHD/aOZ06kloMYtJutnuworJV+oHU07XAyb2Gl0UBFL53U IQYjmD7+iE6fmy1QFVDKIU1CvLcUYRs5P5qceV06n8hsnHwzSokgILyBh5AHGz8GS6DV l2XHCNIC0I3TBmHK17cKVOacw7ZeP9Pu43haz5j9jvE63qUh45FSTZZJZQkM0M9frRU8 Hw9zw99xE5kiPnYfunqJEnF/D4ROUN3JNjd464/MrUq3XYPwEs0EEnufN1WuFkIg6/dl KuOA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=DLN6EqySpNHRi+yFbUimnsqhdqYk4vMvEH74xI9UcaY=; fh=wpqtphakQ77pet8pldvYE9PwKD0iaws66TpwsvHezug=; b=CFoVtzJJQHd9ehtMyPXykb0qbxH1LhfQd7DNeIVBIFge7HyNpW9NbiAO1jvbsvjNMK r12Euh5kFUfyhoeSo5XWxD1m/63Lu9lbifP2K7xUXr51BXUzRhBlfBgLUazbyoS9/Ae5 GLrFAzukMaMA+39ufRwhMcBzt45diuwesBhga9aeI8dN1JAYckeey6f08z2zbnBZHlmM uRHAI55lQjJ7Oz3o3rYCWFVphCY2EDjyobrfOnGTxQ/bVMCmzDc4fMzyl9fR/bsSTrQP lMZKzn5z0xXQSpqYEf+BNxD4gt7ju91eOzJoArDb1xAGVWAiGXvREm0MD7mh+DuZG+FV NHOg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Ofcwva9U; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-35777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35777-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 35-20020a631463000000b005cdd7f6482fsi10114315pgu.5.2024.01.23.09.53.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 09:53:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Ofcwva9U; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-35777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35777-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 20AF6B22444 for ; Tue, 23 Jan 2024 17:27:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 26A567FBDC; Tue, 23 Jan 2024 17:26:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ofcwva9U" Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABD8B7F7F0 for ; Tue, 23 Jan 2024 17:26:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706030812; cv=none; b=kPV7emTxPzm/uIjMAWB1fpgnWxCHyGMjWMgaMYI2idj2DTdEXsAMwudwsjeRf/S+u5IWxZixZ1310vJQUN4aR0Uun1FjMZPANARxuLP2SoPIS3NI/toQ/FrLuu7WhxygF9HE0M5GNA1d+IKxtMRRmucJ7LW4jjIYfZoKXlYxa1c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706030812; c=relaxed/simple; bh=HtgaEazjRyizXbtpsY6ycAq04h3P2hVjFbZkBcbdjVA=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=IrjacktS1KUrfyILJbIafJhg+G/swABq1oWLAK5qF/oNCD/IXNR94t/sk94WDIxPvCBCZDsIDniyslISIyYRACoga0DW4mfIkHZ+mxaWwxWucnoVtbLsCE8ovMhT/8p97/m6PdihrB6kE/ksp1GZt/EJ2F7978mQyme5thfJKJk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ofcwva9U; arc=none smtp.client-ip=209.85.215.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-5cdbc4334edso2194765a12.3 for ; Tue, 23 Jan 2024 09:26:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706030810; x=1706635610; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=DLN6EqySpNHRi+yFbUimnsqhdqYk4vMvEH74xI9UcaY=; b=Ofcwva9UwTt2JOsnE4W633FHLO3oGSyPdnhNz+1Wpm6ZPSOwbA0r1yDtKWja0V4jZ3 WUdmskvp3HgZYVdC8PLzXVQicw/jFaNGuQP0uIXAhd2U1Bw9PTR737mkoDFBna9NBtis MobmHqKw0JO3bLHLlW+dKd4Wa2vh7v8QNNZHyCxJF5mnnS/e5jEtOd/q3/uIO6amOdMw 6QvWamuDk0WEsQfpKBwF2sRlSgxIbqjw9ZEthV3NVmbU1xvGSLumQHXCDqbDJsBgDMuk /0neXJSC9kfHEXT7qWJxLl3aArWf0bXCCpTmbHg2fTVSOmmK6XA9lptQgvXq8ZKm9gL4 EX8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706030810; x=1706635610; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DLN6EqySpNHRi+yFbUimnsqhdqYk4vMvEH74xI9UcaY=; b=RddPjDLeA9kwLqyOUd/5sLVXRPkgXEPjysItYK9wK7YsC/kY3DPDn5f8hHP8IrRpM1 HMZqU9wQ5AHo3j+C6nJ/CBIJV7v2nWO45m+9/dgH4Yf6PB5vzWggZSzwrb/oI9+6z/NI IZ8ozpbOhAC/ox8CNe9jjqGenJYAxOFY2L2HrPgdKC/1Zh4/SZCbzyI7/sa/+m64dqEu JQlOUyvt8DqRsHv8Ylr1BJNPzceFA3pvQWGRocSttWvc4I97p9zx+mU7gK+hCV7wOIg8 633Z7ZUj3tRzShxoE5BKEOfdZ8M8FRw9ocgVZVXvOJINUVroUReJSV9qwXmdQ8nDZqUo 2oFA== X-Gm-Message-State: AOJu0YxEInzyWxKvzJUqknXyT+fnAdnAkrhY+B2i8ylCWHdZEzBTadX0 YTAcoS4LZwgIO3xnh37VFbtT5KUFa26W/5SqWQdXc1Hv0BeKsaCf+b5iGaZ9FJtGlBobJhM/p9x 9zLx7QepkHHJKEZuYKas4hn77cpCzd+XH X-Received: by 2002:a05:6a20:a128:b0:19c:5cca:e98 with SMTP id q40-20020a056a20a12800b0019c5cca0e98mr746651pzk.4.1706030809998; Tue, 23 Jan 2024 09:26:49 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231214223423.1133074-1-yang@os.amperecomputing.com> <1e8f5ac7-54ce-433a-ae53-81522b2320e1@arm.com> In-Reply-To: From: Yang Shi Date: Tue, 23 Jan 2024 09:26:37 -0800 Message-ID: Subject: Re: [RESEND PATCH] mm: align larger anonymous mappings on THP boundaries To: Ryan Roberts Cc: Matthew Wilcox , Yang Shi , riel@surriel.com, cl@linux.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Jan 23, 2024 at 9:14=E2=80=AFAM Yang Shi wrot= e: > > On Tue, Jan 23, 2024 at 1:41=E2=80=AFAM Ryan Roberts wrote: > > > > On 22/01/2024 19:43, Yang Shi wrote: > > > On Mon, Jan 22, 2024 at 3:37=E2=80=AFAM 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) I= t fails once > > >>>> it hits the VMA limit (/proc/sys/vm/max_map_count). Hitting this l= imit then > > >>>> causes a subsequent calloc() to fail, which causes the test to fai= l. > > >>>> > > >>>> 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. Tha= t 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 th= at in > > >> general the latter is preferable. The case I'm hitting is special th= ough, in > > >> that both requirements could be achieved but currently are not. > > >> > > >> The below fixes it, but I feel like there should be some bitwise mag= ic 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 pro= vided 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, fo= r 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 spac= e can pass > > >> an arbitrary value to the implementation for anon memory?? > > > > > > Thanks for noticing this. If I read the code correctly, the pgoff use= d > > > 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 =3D 0; > > > get_area =3D shmem_get_unmapped_area; > > > } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { > > > + pgoff =3D 0; > > > /* Ensures that larger anonymous mappings are THP ali= gned. */ > > > get_area =3D thp_get_unmapped_area; > > > } > > > > I think it would be cleaner to just zero pgoff if file=3D=3DNULL, 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 =3D 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. Rethinking this... zeroing pgoff when file is NULL should be ok since MAP_ANOYMOUS | MAP_SHARED mapping should typically have zero offset. I'm not aware of any usecase with non-zero offset, or sane usecase... > > > > > > > > > > >> > > >> 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(str= uct file *filp, > > >> { > > >> loff_t off_end =3D off + len; > > >> loff_t off_align =3D round_up(off, size); > > >> - unsigned long len_pad, ret; > > >> + unsigned long len_pad, ret, off_sub; > > >> > > >> if (off_end <=3D off_align || (off_end - off_align) < size) > > >> return 0; > > >> @@ -658,7 +658,13 @@ static unsigned long __thp_get_unmapped_area(st= ruct file *filp, > > >> if (ret =3D=3D addr) > > >> return addr; > > >> > > >> - ret +=3D (off - ret) & (size - 1); > > >> + off_sub =3D (off - ret) & (size - 1); > > >> + > > >> + if (current->mm->get_unmapped_area =3D=3D arch_get_unmapped_= area_topdown && > > >> + !off_sub) > > >> + return ret + size; > > >> + > > >> + ret +=3D off_sub; > > >> return ret; > > >> } > > > > > > I didn't spot any problem, would you please come up with a formal pat= ch? > > > > Yeah, I'll aim to post today. > > Thanks! > > > > >