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 9C69DC46467 for ; Mon, 2 Jan 2023 12:04:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EF248E0002; Mon, 2 Jan 2023 07:04:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 19E498E0001; Mon, 2 Jan 2023 07:04:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 066B98E0002; Mon, 2 Jan 2023 07:04:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id EB6388E0001 for ; Mon, 2 Jan 2023 07:04:22 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BD5B8A0840 for ; Mon, 2 Jan 2023 12:04:22 +0000 (UTC) X-FDA: 80309726364.01.BCAB150 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf29.hostedemail.com (Postfix) with ESMTP id 188CA120012 for ; Mon, 2 Jan 2023 12:04:19 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IB119u7S; spf=pass (imf29.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672661060; a=rsa-sha256; cv=none; b=M9v6kJ/bfMaCqnkzDCQxuS6Ug2ZstAyfZevZ8WE5N6E7zAS93+aGBdF/F4LS16t3FyA93C riyeXoAfhO0+7mHJwXoLeevJCy8rW26i2pNGM0+S/7kIXpUscKMrIF1EBvaV1Nmy+f0P+0 64S48zJ78HPUhCHpzdWRJdfNlCcazxk= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IB119u7S; spf=pass (imf29.hostedemail.com: domain of 42.hyeyoo@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=42.hyeyoo@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672661060; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SWkL+Ay7xGJduzHxgREoGWnEpK2FebgVdJq9e4wRN74=; b=MxKOmj3GpbdsGiwMbcHbc1nSfZaoCvWzStUZOuDtRgX5CcFtltJ4EtUMqv6mYMygO68iua NyjYF4GClu4MbCx23VpcPcRoMvxEHaU8T9VjvbHfZ+m99DUnontg4uxeO53U7Eiy8C1JRr RKH/tm6rvP14jrqo+KPi4CnYeO4DRf8= Received: by mail-pj1-f49.google.com with SMTP id hd14-20020a17090b458e00b0021909875bccso25535775pjb.1 for ; Mon, 02 Jan 2023 04:04:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=SWkL+Ay7xGJduzHxgREoGWnEpK2FebgVdJq9e4wRN74=; b=IB119u7Sbb6z/UYOkoKjjjy1GSSLRU6TTtuM8wlBc1pyTrPjYczTgibOYnNMUCH/tO 1pmS862dpdi6fTEQ14TAz5hwKSjXCotOrLE1Nc6RuILhfzEgp33izj0hbu0tq5+gMDVS O9UizHTR1Pwa9ngdLj1WLxyk/VmTdphDL4YuTxISO8MbXy5OnSoBphVgv2rsTyOygInd u58lgi20ws/UweMAU4HzBlvMvDwPTjzfeyMS8o61dO+cpGwxKFLtjqkmIMjc/oxX71yo N0dLWB312Nw0kTAijIWE87GDVT1rHdji37xM8Z4w4/N6jtNQbwrmKUILgZH7LSUoEcXr P31g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=SWkL+Ay7xGJduzHxgREoGWnEpK2FebgVdJq9e4wRN74=; b=6VBLiE3m3TVOl67wPklVFZGCQqPIxrmtLSEHP+q3kvltCStIshSz9ydAYJW2xFGFcn NlfZaD9QqSiX5ybu8QPwtAkfiSZvk1EpD3xtUnF0cu9MXRLypZ+MVnUY+mNZ+H5/ZZQ0 SIgxKS6Z2N69I47DobJv704A7ObfgX32YsVI2JpvHQXVo4PqCGXtsQkxRh7g5gjw9tUa 9RRxkpraRRtS8s0dyOkoJqd2pIqDNdrcDcGLJpUi6oUVIRmx69mhppxzDZ2f73Ur8fz0 uMsEtQBD7j1cU0qj9uoZr2mcKjwosmWyytsxYR8H+61STYXT2flam33DFJIpf4EfX1C2 7Siw== X-Gm-Message-State: AFqh2krnYEXkf/vNtpwflohL2//s18JC51shIKuhjsUcpVSAIIb318XZ ovlpKjp3rTP73ngFs4FoUz4= X-Google-Smtp-Source: AMrXdXsdAtxobuHHkRUmvju928j1cQhZ7HR9IzeQhw6EW7+Rqjzk/ZZpSduHYIKwxIlyVS7+qjkPsg== X-Received: by 2002:a17:903:2792:b0:192:8c1a:1bea with SMTP id jw18-20020a170903279200b001928c1a1beamr24377662plb.43.1672661058789; Mon, 02 Jan 2023 04:04:18 -0800 (PST) Received: from hyeyoo ([114.29.91.56]) by smtp.gmail.com with ESMTPSA id h6-20020a63c006000000b00478b930f970sm16671838pgg.66.2023.01.02.04.04.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Jan 2023 04:04:17 -0800 (PST) Date: Mon, 2 Jan 2023 21:04:12 +0900 From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Matthew Wilcox Cc: linux-mm@kvack.org, liam.howlett@oracle.com, surenb@google.com, ldufour@linux.ibm.com, michel@lespinasse.org, vbabka@suse.cz, linux-kernel@vger.kernel.org Subject: Re: [QUESTION] about the maple tree and current status of mmap_lock scalability Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Queue-Id: 188CA120012 X-Rspamd-Server: rspam01 X-Stat-Signature: rpgsjbitdjirkokmftcbi8q358iaspj3 X-HE-Tag: 1672661059-701345 X-HE-Meta: U2FsdGVkX18acsQ6Exk/gK6AU+kCUQjebzPszWp6HmcKCBRDaGDVSb9w/moqv7kpFg3DZSXOObNqkN/7xgieM5Ps5GeygYqfjkkRo/wmu1L6eXcPFaFdc3WYkB14dKWjBYEVyFYM4+PxPuCuLOrfJhoIh0N440YbokQVDlhtkekVTTGKjF0KJzj7RgIMz8Hjzy5TnN//4Qf76dn+wwXROFj9n6qVFPFcHBP4GIvmOLTYVtAOeEqEoiqZFr/cz147Oqz4vyXT8wv7xVfhfAvMd8+vNRB/+glqXCI9qAzHuI4lwoqlty9FdQh/6sS0xTUOVevJveMtQbTiqC41i840V4oBjUX2nlsjvpA2EpcvPClYJ+F6EQDHFkH7CWViu1sWFBkPoYUgAdr0EBqjgP/cXcWSRvZqpmJfUpY0nGb9iNEdZbBNehDJAYwQslVcLrij/7DYy4s4IAPzoKewk6XiyRYWTSidoOqd+OwpG4UcBGp+rQ3069W1RgtiqRu/fVqlU5JcoEM73RjOmE/fdarsmg3Mwo0s1Cpkr7dpg/d/erBfiB/Ip7HCzKBTZC+oDLxtptVCpzD2de+CBDW6JlYucafehzJNA4B244hwvCffDZ1G6VKpaiST12WqCPcNVGsWex7/jDB8JVl3H+jfrF1renNqRHdFE4Sr6TqoPlCGBUo4QftM2hOl12sC+MDhd3o9+yOTOeSoMI48cLHnSdm4c7009jQIVs9diOv+e8l9cAtA+DdcuynEV2OfsX1bqkBeMmeWE6dembYmGH/rnz+tVCPAprQVMBmEEjQlqk7JvJ0QN4oBC9EKrZdXbMylhrwETr8YE3+WgkyNoxwgqv8yO2c0nVv+lxYQdUFnhQA+jN7DLL/etWwCq4/4fWArZv7LZB3FK+8fttwkHL3MuLw7Fm/JMxfP1fXd4kizRPpXe6TE2I6r8QUiQ9A4R2JZrdazrTbIFseOJUDED4JTp8c 1kdegTS6 3x7o/wdPdiyeODiwRyVJhTj3DczowiZtXxgqN8uX7xGQBawOECDiy4CLjxnLtBIOMz7vLIjjYWdettmh/WCmS0y5HfHHeM+XFR8qGbdeWGunBPsP5qP6DfbFghljOpcSEwOuLZlnXQ1CMqGzG26rO4I00B4GTMyqExB4oEHLXv80lw2w= 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: From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Matthew Wilcox Cc: linux-mm@kvack.org, liam.howlett@oracle.com, surenb@google.com, ldufour@linux.ibm.com, michel@lespinasse.org, vbabka@suse.cz, linux-kernel@vger.kernel.org Bcc: Subject: Re: [QUESTION] about the maple tree and current status of mmap_lock scalability Reply-To: In-Reply-To: On Thu, Dec 29, 2022 at 04:51:37PM +0000, Matthew Wilcox wrote: > On Thu, Dec 29, 2022 at 11:22:28PM +0900, Hyeonggon Yoo wrote: > > On Wed, Dec 28, 2022 at 08:50:36PM +0000, Matthew Wilcox wrote: > > > The long term goal is even larger than this. Ideally, the VMA tree > > > would be protected by a spinlock rather than a mutex. > > > > You mean replacing mmap_lock rwsem with a spinlock? > > How is that possible if readers can take it for page fault? > > The mmap_lock is taken for many, many things. So the plan was to > have a spinlock in the maple tree (indeed, there's still one there; > it's just in a union with the lockdep_map_p). VMA readers would walk > the tree protected only by RCU; VMA writers would take the spinlock > while modifying the tree. The work Suren, Liam & I are engaged in > still uses the mmap semaphore for writers, but we do walk the tree > under RCU protection. > Thanks, I get it. so it's for less overhead for maple tree modification. > > > While I've read the RCUVM paper, I wouldn't say it was particularly an > > > inspiration. The Maple Tree is independent of the VM; it's a general > > > purpose B-tree. > > > > My intention was to ask how to synchronize with other VMA operations > > after the tree traversal with RCU. (Because it's unreasonable to handle > > page fault in RCU read-side critical section) > > > > Per-VMA lock seem to solve it by taking the VMA lock in read mode within > > RCU read-side critical section. > > Right, but it's a little more complex than that. The real "lock" on > the VMA is actually a sequence count. https://lwn.net/Articles/906852/ > does a good job of explaining it, but the VMA lock is really there as > a convenient way for the writer to wait for readers to be sufficiently > "finished" with handling the page fault that any conflicting changes > will be correctly retired. Oh, thanks, nice article! > https://www.infradead.org/~willy/linux/store-free-page-faults.html > outlines how I intend to proceed from Suren's current scheme (where > RCU is only used to protect the tree walk) to using RCU for the > entire page fault. Thank you for sharing this your outlines. Okay, so the planned scheme is: 1. Try to process entire page fault under RCU protection - if failed, goto 2. if succeeded, goto 4. 2. Fall back to Suren's scheme (try to take VMA rwsem) - if failed, goto 3. if succeeded, goto 4. 3. Fall back to mmap_lock - goto 4. 4. Finish page fault. To implement 1, __p*d_alloc() need to take gfp flags not to sleep in RCU read-side critical section. What about introducing PF_MEMALLOC_NOWAIT process flag forcing GFP_NOWAIT | __GFP_NOWARN similar to PF_MEMALLOC_NO{FS,IO}, looking like this? Will be less churn. diff --git a/include/linux/sched.h b/include/linux/sched.h index 853d08f7562b..77b88f30523b 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1725,7 +1725,7 @@ extern struct pid *cad_pid; #define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized before use */ #define PF__HOLE__00004000 0x00004000 #define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */ -#define PF__HOLE__00010000 0x00010000 +#define PF_MEMALLOC_NOWAIT 0x00010000 /* All allocation requests will force GFP_NOWAIT | __GFP_NOWARN */ #define PF_KSWAPD 0x00020000 /* I am kswapd */ #define PF_MEMALLOC_NOFS 0x00040000 /* All allocation requests will inherit GFP_NOFS */ #define PF_MEMALLOC_NOIO 0x00080000 /* All allocation requests will inherit GFP_NOIO */ diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index 2a243616f222..4a1196646951 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -204,7 +204,8 @@ static inline gfp_t current_gfp_context(gfp_t flags) { unsigned int pflags = READ_ONCE(current->flags); - if (unlikely(pflags & (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS | PF_MEMALLOC_PIN))) { + if (unlikely(pflags & (PF_MEMALLOC_NOIO | PF_MEMALLOC_NOFS + | PF_MEMALLOC_PIN | PF_MEMALLOC_NOWAIT))) { /* * NOIO implies both NOIO and NOFS and it is a weaker context * so always make sure it makes precedence @@ -216,6 +217,8 @@ static inline gfp_t current_gfp_context(gfp_t flags) if (pflags & PF_MEMALLOC_PIN) flags &= ~__GFP_MOVABLE; + if (pflags & PF_MEMALLOC_NOWAIT) + flags = GFP_NOWAIT | __GFP_NOWARN; } return flags; } @@ -305,6 +308,18 @@ static inline void memalloc_noio_restore(unsigned int flags) current->flags = (current->flags & ~PF_MEMALLOC_NOIO) | flags; } +static inline unsigned int memalloc_nowait_save(void) +{ + unsigned int flags = current->flags & PF_MEMALLOC_NOWAIT; + current->flags |= PF_MEMALLOC_NOWAIT; + return flags; +} + +static inline void memalloc_nowait_restore(unsigned int flags) +{ + current->flags = (current->flags & ~PF_MEMALLOC_NOWAIT) | flags; -- Thanks, Hyeonggon