> The reason of this optimization is that umount() can hold namespace_sem
> for a long time, this semaphore is global, so it affects all users. 
> Recently Eric W. Biederman added a per mount namespace limit on the
> number of mounts.  The default number of mounts allowed per mount
> namespace at 100,000.  Currently this value is allowed to construct a tree
> which requires hours to be umounted. 
>
> In a worse case the current complexity of umount_tree() is O(n^3). 
> * Enumirate all mounts in a target tree (propagate_umount)
> * Enumirate mounts to find where these changes have to
> be propagated (mark_umount_candidates)
> * Enumirate mounts to find a requered mount by parent and dentry
> (__lookup_mnt_lat)
>
> The worse case is when all mounts from the tree live in the same shared
> group.  In this case we have to enumirate all mounts on each step. 
>
> Here we can optimize the second step.  We don't need to make it for
> mounts which we already met when we did this step for previous mounts. 
> It reduces the complexity of umount_tree() to O(n^2). 

To O(n) not O(n^2). 

A hash table lookup (aka __lookup_mnt() and friends) is O(1) or the hash
table is malfunctioning.  Please don't call

Arguably we are getting into sizes where
the mount hash table fills up and is on the edge of malfunctioning, but
that is not particularly relevant to this case. 

What your patch is aiming to do is to take a O(n^2) algorithm and
make it O(n).  That is very much worth doing. 

However your patch confuses two separate issues.  Marking mounts that
may be unmounted.  Marking pieces of the propagation tree that have
already been traversed. 

I do not see anything requiring propagation trees to intersect at the
set of mounts that are unmounted in umount_tree before propagate_umount
is called.  Which means there are topologies where we can and should do
better than your patch. 

I am also bothered that your patch changes how we look up the mount
mounted on a mount point (aka playing with __lookup_mnt_last).  There
is no reason to do that to solve the problem, and I think it obscures
what is actually going on. 

I am going to see if I can rework your basic concept with explicit
marking of the propagation tree.  In the meantime for people
who are want to see what your patch is doing the version below
essentially does the same thing, without the extra essentially
meaningless loop.