Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp4124447imc; Thu, 14 Mar 2019 12:56:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqwmXeXdmOFGtdJQ7s1HqipmBEQ3DtDt2/jOr8Z7kw/Vr8MpSHq/hHB32ajLxEmOoATVcg/r X-Received: by 2002:a62:6046:: with SMTP id u67mr71885pfb.46.1552593367296; Thu, 14 Mar 2019 12:56:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552593367; cv=none; d=google.com; s=arc-20160816; b=ImfW7G6BNzCrktSUlXlcMWvESuqgC4vU9UiyGcqLoj4PVZv6TYp6tmBikkr8vOqCil iM5x5wa+MU75wJjMzAsWVR699I83vW+oRFlg5E+gAp6T/7r8g43j63GGXlT1MXhLv8kv VgTpKXhWwdqw/893dJlYTRbmabpOCRP1GH1OjSfN6rmu1ixXaaiIAJwCvbRDlOB2Xl/e 7OPUKwWEw9fg6cLHbEcm078Jox3arBdv8VH8cV1Cy4Mrt/2bi9d2lp/EH3POoQzYwF2n GXMR5nopgzMsrjsDb+J+pcniDmv/LwRU1uQq6cOt/OFKb84dC+T0Rmy2LxYQDT5mkzef rgsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:to :from:date:dkim-signature; bh=vZNuEUuNICS2Gm4IYHhAJQIWK7u5E9v+XO+j854JpXQ=; b=Z4PCeJLuDeVJtNjS8xlsyna+MP/jPd+PzGvmwUTzPn2/C6JqYbm+77qxsc4rOk6K/T x6JSqlZ9Ftx8qfjrb7SHU169WoTT5rlcR2d5OGS0Fa3B4kKYzhMf3sSQr77WVeDjdmos 8oj9wx1I4s7kbN3yHNQPsIGkLAfIXxN1KZcPRHTNQHdRuW8NIHhku+SoB5VG9K+moFtq jkuDtoWbbR+64kiReM/x39JBzgEWEhIe3tFM1UzZzELfluaBoos2QWYcV2tujLJ+NfKK 0m1UM4VQ7sVe8f5MHgZzgtoYPwC9onFsFWnMgH3x2Y/biF/TdFIv12m4KiWRfVCjA6GS X+7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@infradead.org header.s=bombadil.20170209 header.b=A8tVvGpd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ci1si15471827plb.352.2019.03.14.12.55.43; Thu, 14 Mar 2019 12:56:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@infradead.org header.s=bombadil.20170209 header.b=A8tVvGpd; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727246AbfCNTyy (ORCPT + 99 others); Thu, 14 Mar 2019 15:54:54 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:49236 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726890AbfCNTyy (ORCPT ); Thu, 14 Mar 2019 15:54:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:To:From:Date:Sender:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=hOVUCp5SVXqYeCVElU5+KNwIZohVlf3Zu5f4b0Ps8p4=; b=A8tVvGpde8o3Yq8dzeRzjiya/ 6d8j+QyDyLUzjRLq0m7jo4Ld90pNTFVjjyY3AxJmzN7fRNS4F4W4af4inV0CZcWLuT1ZCXKisqE5d leUQ9G+rkRCS2LXqKMm1rQA5GpZop/W9KNKuSjxpAfAbtyAk4EKkkO4PesQr+Rn/nArwTadLuV4lj M3hUyAi5SYmjTQmeDNHHXrfmyjQEQSX+RuZn2izaU2lgMvn307wZYa1KtXtmrmXXHg8BySzSdsHXX b/iESJnKe27AzX1PiJ80fRLWUDX8+TeFt7lbfYW6eaSinRYZyqIG7KphsuDzVZGP4aT9wlpLCOx3Y xJmQpWQTw==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1h4WRI-0003kC-9H; Thu, 14 Mar 2019 19:54:52 +0000 Date: Thu, 14 Mar 2019 12:54:52 -0700 From: Matthew Wilcox To: Laurent Dufour , lsf-pc@lists.linux-foundation.org, Linux-MM , linux-kernel@vger.kernel.org Subject: Re: [LSF/MM TOPIC] Using XArray to manage the VMA Message-ID: <20190314195452.GN19508@bombadil.infradead.org> References: <7da20892-f92a-68d8-4804-c72c1cb0d090@linux.ibm.com> <20190313210603.fguuxu3otj5epk3q@linux-r8p5> <20190314023910.GL19508@bombadil.infradead.org> <20190314164343.owsgnldxk7qr363q@linux-r8p5> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190314164343.owsgnldxk7qr363q@linux-r8p5> User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 14, 2019 at 09:43:43AM -0700, Davidlohr Bueso wrote: > On Wed, 13 Mar 2019, Matthew Wilcox wrote: > > > It's probably worth listing the advantages of the Maple Tree over the > > rbtree. > > I'm not familiar with maple trees, are they referred to by another name? > (is this some sort of B-tree?). Google just shows me real trees. It is a B-tree variant which supports ranges as a first-class citizen (single elements are ranges of length 1). It optimises for ranges which are adjacent to each other, and does not support overlapping ranges. It supports RCU lookup and embeds a spinlock which must be held for modification. There's a lot of detail I can go into, but let's leave it at that for an introduction. > > - Shallower tree. A 1000-entry rbtree is 10 levels deep. A 1000-entry > > Maple Tree is 5 levels deep (I did a more detailed analysis in an > > earlier email thread with Laurent and I can present it if needed). > > I'd be interested in reading on that. (see the last two paragraphs of the mail for that analysis) > > - O(1) prev/next > > - Lookups under the RCU lock > > > > There're some second-order effects too; by using externally allocated > > nodes, we avoid disturbing other VMAs when inserting/deleting, and we > > avoid bouncing cachelines around (eg the VMA which happens to end up > > at the head of the tree is accessed by every lookup in the tree because > > it's on the way to every other node). > > How would maple trees deal with the augmented vma tree (vma gaps) trick > we use to optimize get_unmapped_area? The fundamental unit of the Maple Tree is a 128-byte node. A leaf node is laid out like this: struct maple_range_64 { struct maple_node *parent; void __rcu *slot[8]; u64 pivot[7]; }; The pivots are stored in ascending order; if the search index is less than pivot[i], then the value (ie the vma pointer) you are searching for is stored in slot[i]. Non-leaf nodes (for trees which support range allocations) are laid out like this: struct maple_arange_64 { struct maple_node *parent; u64 gaps[5]; void __rcu *slot[5]; u64 pivot[4]; }; gaps[i] stores the largest run of NULL pointers in the subtree rooted at slot[i]. When searching for an empty range of at least N, you can skip any subtree which has gaps[i] < N. Here's a simple case: $ ldd `which cat` linux-vdso.so.1 (0x00007ffc867fc000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2c8cc6e000) /lib64/ld-linux-x86-64.so.2 (0x00007f2c8ce69000) 'cat /proc/self/maps | wc' gives me 25 mappings. They look like this: $ cat /proc/self/maps |cut -f 1 -d ' ' 55c414785000-55c414787000 55c414787000-55c41478c000 55c41478c000-55c41478e000 55c41478f000-55c414790000 55c414790000-55c414791000 55c4159dd000-55c4159fe000 7fa5f6527000-7fa5f680c000 7fa5f680c000-7fa5f682e000 7fa5f682e000-7fa5f6976000 7fa5f6976000-7fa5f69c2000 7fa5f69c2000-7fa5f69c3000 7fa5f69c3000-7fa5f69c7000 7fa5f69c7000-7fa5f69c9000 7fa5f69c9000-7fa5f69cd000 7fa5f69cd000-7fa5f69cf000 7fa5f69d9000-7fa5f69fb000 7fa5f69fb000-7fa5f69fc000 7fa5f69fc000-7fa5f6a1a000 7fa5f6a1a000-7fa5f6a22000 7fa5f6a22000-7fa5f6a23000 7fa5f6a23000-7fa5f6a24000 7fa5f6a24000-7fa5f6a25000 7ffe54a3c000-7ffe54a5d000 7ffe54a7b000-7ffe54a7e000 7ffe54a7e000-7ffe54a80000 We'd represent this in the Maple Tree as: 0-55c414785000 -> NULL 55c414785000-55c414787000 -> vma 55c414787000-55c41478c000 -> vma ... 55c414790000-55c414791000 -> vma 55c414791000-7fa5f6527000 -> NULL 7fa5f6527000-7fa5f680c000 -> vma ... 7fa5f69cd000-7fa5f69cf000 -> vma 7fa5f69cf000-7fa5f69d9000 -> NULL 7fa5f69d9000-7fa5f69fb000 -> vma ... 7fa5f6a24000-7fa5f6a25000 -> vma 7fa5f6a25000-7ffe54a3c000 -> NULL 7ffe54a3c000-7ffe54a5d000 -> vma 7ffe54a5d000-7ffe54a7b000 -> NULL 7ffe54a7b000-7ffe54a7e000 -> vma 7ffe54a7e000-7ffe54a80000 -> vma 7ffe54a80000-ffffffffffff -> NULL so the maple tree stores 6 ranges that point to NULL in addition to the 25 that're stored by the rbtree. Because they're allocated sequentially, there won't be any wastage in the maple tree caused by items shifting around. That means we'll get 8 per leaf node, so just 4 leaf nodes needed to store 31 ranges, all stored in a single root node. That means to get from root to an arbitrary VMA is just 3 pointer dereferences, versus 3.96 pointer dereferences for an optimally balanced rbtree. For a process with 1000 VMAs, we'll have approximately 167 leaf nodes (assuming approximately 6 of the 8 pointers are used per node) arranged into a tree of height 5, with about 44 non-leaf nodes needed to manage those 166 leaf-nodes. That'll be 6 pointers to follow per walk of the tree (if it were optimally arranged, it'd be 125 leaf nodes plus 25 + 5 + 1 non-leaf nodes and 5 pointers to follow, but it's unrealistic to assume it'll be optimally arranged, and this neglects the NULL ranges which will also need to be stored). The rbtree has a 1/1000 chance of 1 pointer dereference, a 2/1000 chance of 2 pointers, 4/1000 chance of 3 pointers, 8/1000 4 pointers, 16/1000 5 pointers, 32/1000 6 pointers, 64/1000 7 pointers, 128/1000 8 pointers, 256/1000 9 pointers, 489/1000 10 pointers. Amortised, that's 8.987 pointers to look up a random VMA (assuming the rbtree is fully balanced; I haven't checked how unbalanced the rbtree can actually become).