mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rik van Riel <riel@surriel.com>
To: Robin Murphy <robin.murphy@arm.com>, linux-kernel@vger.kernel.org
Cc: kernel-team@meta.com, joro@8bytes.org, will@kernel.org,
	iommu@lists.linux.dev, 	liam@infradead.org,
	maple-tree@lists.infradead.org, linux-mm@kvack.org,
		ashok.raj@oss.qualcomm.com, jgg@ziepe.ca, kyle@mcmartin.ca
Subject: Re: [RFC PATCH 1/3] iommu/iova: convert from rbtree to maple tree
Date: Mon, 05 Oct 2026 10:54:27 -0400	[thread overview]
Message-ID: <6a5f92b4c152be946c3a8f238197d389b9e87f54.camel@surriel.com> (raw)
In-Reply-To: <9547761f-7688-4351-b172-9e9d5fa28b15@arm.com>

On Thu, 2026-09-24 at 16:26 +0100, Robin Murphy wrote:
> On 18/08/2026 4:25 pm, Rik van Riel wrote:
> > alloc_iova() looks for free space by walking the rbtree linearly.
> > On production workloads at Meta, enough CPUs have ended up in that
> > walk
> > at the same time to trigger soft lockups.
> > 
> > Index the iova ranges in a maple tree instead. Its gap search makes
> > alloc_iova() O(log n).
> > 
> > __alloc_and_insert_iova_range() asks mas_empty_area_rev() for the
> > highest free range below limit_pfn. Alignment is handled by
> > rounding
> > up the allocation size, prioritizing speed over address space
> > waste,
> > with the thought that many iova requests on a system will be
> > similar
> > in size, and reuse the same holes.
> [...]
> >   static int __alloc_and_insert_iova_range(struct iova_domain
> > *iovad,
> >   		unsigned long size, unsigned long limit_pfn,
> >   			struct iova *new, bool size_aligned)
> >   {
> > -	struct rb_node *curr, *prev;
> > -	struct iova *curr_iova;
> >   	unsigned long flags;
> > -	unsigned long new_pfn, retry_pfn;
> > +	unsigned long new_pfn;
> >   	unsigned long align_mask = ~0UL;
> > -	unsigned long high_pfn = limit_pfn, low_pfn = iovad-
> > >start_pfn;
> > +	unsigned long search_size = size;
> > +	MA_STATE(mas, &iovad->mtree, 0, 0);
> > +
> > +	if (size_aligned) {
> > +		unsigned long align = 1UL << fls_long(size - 1);
> >   
> > -	if (size_aligned)
> >   		align_mask <<= fls_long(size - 1);
> > +		search_size = size + align - 1;
> > +	}
> 
> Perhaps it's a bit too much of a cool trick, but I think technically
> we 
> could just do "search_size = size + ~align_mask" unconditionally.
> 
> However, either way I do worry somewhat about the increase in 
> fragmentation and premature failures once the space starts to fill
> up. 
> 
For that use case, I am guessing what we really
want to propagate up the tree is not the maximum
size of a gap, but the maximum power of two size
gap that is also aligned to its own size.

In other words, if we have something like this,
aligned 8, we propagate up a gap size 4:

used used used used gap gap gap gap

But if the usage looks like this, at the same
alignment to the start, we propagate a gap size 2:

used used used gap gap gap gap used

That would allow us to easily find gaps aligned
to their own size. Once the size 2 gap is filled
in, we propagate up the remaining size 1 gaps.

That makes me wonder if we need to go back to
the augmented rbtree, instead of using the
maple tree?


Just let me know your preference.
> 
> > -	/* Walk the tree backwards */
> > -	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
> 
> FWIW I'm not much of a fan of the implicit scoped-cleanup stuff in 
> general, but this seems like an instance where using guard() to
> simplify 
> all the early returns might be worthwhile.

I'm happy to do that.

Are there any other changes I should be making
to get this code ready for merging?

-- 
All Rights Reversed.

  reply	other threads:[~2026-10-05 14:54 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-18 15:25 [PATCH v5 0/3] " Rik van Riel
2026-08-18 15:25 ` [RFC PATCH 1/3] " Rik van Riel
2026-09-24 15:26   ` Robin Murphy
2026-10-05 14:54     ` Rik van Riel [this message]
2026-08-18 15:25 ` [RFC PATCH 2/3] iommu/iova: defer maple tree erase on GFP_ATOMIC failure Rik van Riel
2026-08-18 15:25 ` [RFC PATCH 3/3] iommu/iova: add KUnit test suite Rik van Riel
2026-09-11 17:10 ` [PATCH v5 0/3] iommu/iova: convert from rbtree to maple tree Ashok Raj
2026-09-11 17:27   ` Rik van Riel
2026-09-11 18:26     ` Ashok Raj
2026-09-24 12:52 ` Jörg Rödel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a5f92b4c152be946c3a8f238197d389b9e87f54.camel@surriel.com \
    --to=riel@surriel.com \
    --cc=ashok.raj@oss.qualcomm.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=kernel-team@meta.com \
    --cc=kyle@mcmartin.ca \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=maple-tree@lists.infradead.org \
    --cc=robin.murphy@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®