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 B8F38E77173 for ; Fri, 6 Dec 2024 08:37:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 357F66B01B1; Fri, 6 Dec 2024 03:37:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 305E76B01F0; Fri, 6 Dec 2024 03:37:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A7D06B01F1; Fri, 6 Dec 2024 03:37:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EC88B6B01B1 for ; Fri, 6 Dec 2024 03:37:29 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 607571218C7 for ; Fri, 6 Dec 2024 08:37:29 +0000 (UTC) X-FDA: 82863879546.07.0401D26 Received: from mail-vs1-f53.google.com (mail-vs1-f53.google.com [209.85.217.53]) by imf27.hostedemail.com (Postfix) with ESMTP id 70BE340002 for ; Fri, 6 Dec 2024 08:37:07 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf27.hostedemail.com: domain of geert.uytterhoeven@gmail.com designates 209.85.217.53 as permitted sender) smtp.mailfrom=geert.uytterhoeven@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733474239; a=rsa-sha256; cv=none; b=I2csMWrQLgkr+5WBQPRknCPsLf/B9oTTVI2Lfrt+bTzSevbKoULhnFnxUQxIjuzbHtzXsH 0cFr32TGntvqXLJVuQln/xbT51OBR+dB/5uSLEeOyUEfm9YGbN3ivE6bYixn/1wJK8T+i1 /jISGAZ/9MXfKu9YI3cVZE/JCofrPvg= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf27.hostedemail.com: domain of geert.uytterhoeven@gmail.com designates 209.85.217.53 as permitted sender) smtp.mailfrom=geert.uytterhoeven@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733474239; 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=p52l+EzfUjM05v2ng7V2LMMN7gKNSNOhsoEffeujg0Y=; b=2Aa3PsqP4WuMQSAKT0kYbg+pSzCk9zANmZ/U1maTSA4KZHxpdULuvjdL6+/u/0SKVUnP7y 7mGCe/FNhJXFWhzZ0qwclZdh3KCWCXmhJbIolZ0zTzwj1e4crGBXyRTi70xmW7OdgeefAt UVomGAritG/Yj93/jjYti89I3qppVDM= Received: by mail-vs1-f53.google.com with SMTP id ada2fe7eead31-4afc6ce5d94so332103137.0 for ; Fri, 06 Dec 2024 00:37:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733474245; x=1734079045; 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=p52l+EzfUjM05v2ng7V2LMMN7gKNSNOhsoEffeujg0Y=; b=kLHyE5IlfhvlgsobLRZBIZHwChQO55NW2nUVIB0m1EAmPuuoad1DEmTc0Wk/LdUQiz hntKsfCC6fU/YSwYJSCg1EEaiAI/kW2hVHjEkkuN4/BjKTwIymsWKXMzkihws5Fvx1jL ck1/nZYmEa6UPWido40KGXliBjPknL7Cr0d0OZ4PsEQ5iNg/02QD2lpG4/jp7Aa4jMaG ov5q1RGrQQ6lJid/KmF5Zgpcx7byE0q/jXma8LqmW1GsSZ1DOWlK5f7LRzd0axVxE2x5 E+yvS02FHp09pMg0wzWCItOfn6/UKfA0tFMzGm32ffGdHt9aBiGoAuC13FAkSCyYMpdp iGrA== X-Forwarded-Encrypted: i=1; AJvYcCVAjigoPPeHtOQhITAV8CAfdXf8B/qo25/j0tLvXKzmlAL/BKnofm0B1tMWEAiNkzEIoycuupsyIg==@kvack.org X-Gm-Message-State: AOJu0Yw9bD/ZYDhsW3QLyjbnbL0xmmcJNrv9WqZGIfaVXCP0VPXhnGDq j5ah//pPM6LDXNzgT2XnOuCsveusq74Jwj926KvJ3CaY0WkMi31wzyNkI0b7 X-Gm-Gg: ASbGncuxJy1DcM64m/9ddc/r7b3eZ9+Zi7KSUP3qSo5SbnfLiWkfjKNLOpKLrDN1txf /bYGjTva+e6mPn4f7Th1a0yMOmr2C5yizRbVvkPkgMtpJ78QW2ILQpms8psZjnH7VV38CS8T8Y+ wAHkw9eDWHGeSIot0Cb9psde09pKcotv46mpjHmQNfGDN7Mg3Q8Zlrh9xwjqjnBnOid74evM57L z1eSTeJdEqoYVUDFrg+WqjyrkisiBDRcKIgjahgrcVLMNy67DrQ8lYlcMGIHzh5bj3XYg8Wb7PK yYmTFFNJ/pXf X-Google-Smtp-Source: AGHT+IFaFSCzm/G9Cpn0K8V17Qk91DRKzAq4boVgg4d1lLA57HabLnpIj8smh9czP9YoDqeBJpJ6Qw== X-Received: by 2002:a05:6102:5491:b0:4af:cbf7:979c with SMTP id ada2fe7eead31-4afcbf79b1bmr2460709137.3.1733474245381; Fri, 06 Dec 2024 00:37:25 -0800 (PST) Received: from mail-ua1-f46.google.com (mail-ua1-f46.google.com. [209.85.222.46]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-4afd5ee97ecsm12625137.12.2024.12.06.00.37.24 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 06 Dec 2024 00:37:24 -0800 (PST) Received: by mail-ua1-f46.google.com with SMTP id a1e0cc1a2514c-85c4d855fafso58275241.2 for ; Fri, 06 Dec 2024 00:37:24 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCW6KQUZl4D5iI0gXhPALpn/xC6HSesccsshhuIepPcpJBM66rVcUHQxtZSdoYyQpE70iNqiQrsbag==@kvack.org X-Received: by 2002:a05:6102:dcd:b0:4af:cba5:e496 with SMTP id ada2fe7eead31-4afcba5e701mr2269468137.5.1733474244664; Fri, 06 Dec 2024 00:37:24 -0800 (PST) MIME-Version: 1.0 References: <20241011150304.709590-1-ziy@nvidia.com> <9942C08D-C188-461C-B731-F08DE294CD2B@nvidia.com> In-Reply-To: From: Geert Uytterhoeven Date: Fri, 6 Dec 2024 09:37:12 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] mm: avoid zeroing user movable page twice with init_on_alloc=1 To: Zi Yan Cc: Matthew Wilcox , Vlastimil Babka , linux-mm@kvack.org, Andrew Morton , David Hildenbrand , Miaohe Lin , Kefeng Wang , John Hubbard , "Huang, Ying" , Ryan Roberts , Alexander Potapenko , Kees Cook , linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 70BE340002 X-Stat-Signature: dhctojc986up5n1gqgfudxx1eitjngmt X-HE-Tag: 1733474227-118564 X-HE-Meta: U2FsdGVkX1+YdwuIbSArG0BPjUrl0UEEb9L4pNjHR7c9ieE2dYclV92E/kEZ+4/ro4gp4n2/sNmNBccviGZVNLjKPF6Zy1OUqqGgeQ+OjE1Oo7gG7p/laPv0v/XzrSqWMYE6x+cgTMLiGMcFuNaRlsL4g74r5UG4QevZHgFA0D/A85FCaMAxetr0G/KYWVRdYXyucNCIVN082S6k+L/rpt0iNcf36Amflv4IN7aWkjWscegsbbXgRT4cO3/tMOi6aOR/V6PiQVgoD2B8OHnQsFpxMmIJow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ZP7Z7Bu5h+Hhl14brGDe9pMupssWRXwnHDLjid1EvdLivRxkq7jnS4afmpvO8kize4Kvu8WTb49SE7RDtbXdNgl15pIcy5tayuKP 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: Hi Zi, On Thu, Dec 5, 2024 at 6:33=E2=80=AFPM Zi Yan wrote: > Can you try the patch below (it compiles locally for mips and x86) to see > if your issue is fixed? Can you please make THP always on in your config, > since THP is also affected by the same issue? The patch you tested only > fixed non THP config. Thanks, this works both without THP, and with CONFIG_TRANSPARENT_HUGEPAGE=3Dy CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS=3Dy Tested-by: Geert Uytterhoeven > Thanks. I appreciate your help. :) Thanks, I appreciate your quick responses and solutions! > --- a/include/linux/highmem.h > +++ b/include/linux/highmem.h > @@ -224,7 +224,13 @@ static inline > struct folio *vma_alloc_zeroed_movable_folio(struct vm_area_struct *vma, > unsigned long vaddr) > { > - return vma_alloc_folio(GFP_HIGHUSER_MOVABLE | __GFP_ZERO, 0, vma,= vaddr); > + struct folio *folio; > + > + folio =3D vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vaddr); > + if (folio && alloc_need_zeroing()) > + clear_user_highpage(&folio->page, vaddr); > + > + return folio; > } > #endif > > diff --git a/include/linux/mm.h b/include/linux/mm.h > index c39c4945946c..6ac0308c4380 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -4175,6 +4175,23 @@ static inline int do_mseal(unsigned long start, si= ze_t len_in, unsigned long fla > } > #endif > > +/* > + * alloc_need_zeroing checks if a user folio from page allocator needs t= o be > + * zeroed or not. > + */ > +static inline bool alloc_need_zeroing(void) > +{ > + /* > + * for user folios, arch with cache aliasing requires cache flush= and > + * arc sets folio->flags, so always return false to make caller u= se > + * clear_user_page()/clear_user_highpage() > + */ > + return (IS_ENABLED(CONFIG_ARCH_HAS_CPU_CACHE_ALIASING) || > + IS_ENABLED(CONFIG_ARC)) || > + !static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, > + &init_on_alloc); > +} > + > int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __= user *status); > int arch_set_shadow_stack_status(struct task_struct *t, unsigned long st= atus); > int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long s= tatus); > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index ee335d96fc39..107130a5413a 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -1176,11 +1176,12 @@ static struct folio *vma_alloc_anon_folio_pmd(str= uct vm_area_struct *vma, > folio_throttle_swaprate(folio, gfp); > > /* > - * When a folio is not zeroed during allocation (__GFP_ZERO not us= ed), > - * folio_zero_user() is used to make sure that the page correspond= ing > - * to the faulting address will be hot in the cache after zeroing. > + * When a folio is not zeroed during allocation (__GFP_ZERO not us= ed) > + * or user folios require special handling, folio_zero_user() is u= sed to > + * make sure that the page corresponding to the faulting address w= ill be > + * hot in the cache after zeroing. > */ > - if (!alloc_zeroed()) > + if (alloc_need_zeroing()) > folio_zero_user(folio, addr); > /* > * The memory barrier inside __folio_mark_uptodate makes sure tha= t > diff --git a/mm/internal.h b/mm/internal.h > index cb8d8e8e3ffa..3bd08bafad04 100644 > --- a/mm/internal.h > +++ b/mm/internal.h > @@ -1285,12 +1285,6 @@ void touch_pud(struct vm_area_struct *vma, unsigne= d long addr, > void touch_pmd(struct vm_area_struct *vma, unsigned long addr, > pmd_t *pmd, bool write); > > -static inline bool alloc_zeroed(void) > -{ > - return static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, > - &init_on_alloc); > -} > - > /* > * Parses a string with mem suffixes into its order. Useful to parse ker= nel > * parameters. > diff --git a/mm/memory.c b/mm/memory.c > index 75c2dfd04f72..cf1611791856 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -4733,12 +4733,12 @@ static struct folio *alloc_anon_folio(struct vm_f= ault *vmf) > folio_throttle_swaprate(folio, gfp); > /* > * When a folio is not zeroed during allocation > - * (__GFP_ZERO not used), folio_zero_user() is us= ed > - * to make sure that the page corresponding to th= e > - * faulting address will be hot in the cache afte= r > - * zeroing. > + * (__GFP_ZERO not used) or user folios require s= pecial > + * handling, folio_zero_user() is used to make su= re > + * that the page corresponding to the faulting ad= dress > + * will be hot in the cache after zeroing. > */ > - if (!alloc_zeroed()) > + if (alloc_need_zeroing()) > folio_zero_user(folio, vmf->address); > return folio; > } Gr{oetje,eeting}s, Geert --=20 Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k= .org In personal conversations with technical people, I call myself a hacker. Bu= t when I'm talking to journalists I just say "programmer" or something like t= hat. -- Linus Torvalds