[PATCH] mm/rmap: avoid reusing batched TLB flush generations
Harry Yoo
harry at kernel.org
Tue Nov 24 13:54:21 PST 2026
On Tue, Nov 24, 2026 at 10:16:25PM +0100, David Hildenbrand (Arm) wrote:
> On 11/24/26 21:50, Harry Yoo wrote:
> > On Tue, Nov 24, 2026 at 10:39:11AM +0100, David Hildenbrand (Arm) wrote:
> Hi Harry,
>
> thanks for all that effort!
No problem, thanks for looking at the issue ;)
> > Uh, I spent a couple of hours to review and make sure this is correct.
> > I thought this is correct but then my claude flagged a race during fork.
> >
> > dup_mm() copies the counters with memcpy() and it doesn't know the
> > ordering. When flushed and pending fields sit in separate words,
> > dup_mm() could read them in two separate loads, but in the wrong order.
> >
> > It's especially problematic in a very rare (IMO) case where
> > flushed == pending + 1 becomes true in the child (which means the first
> > pending flush in the child might not be flushed properly).
> >
> > fork reclaim another process
> > memcpy reads pending = X
> > pending++ (X + 1)
> > truncate()
> > -> flush_tlb_batched_pending()
> > -> flushed = X + 1
> > memcpy reads flushed = X + 1
> >
> > Maybe reset the counters after copying mm_struct?
>
> Resetting sounds in general like the right thing to do. Ugh, this copying of
> random state is nasty.
Indeed.
> The child never ran, so there isn't anything to flush for it.
>
> So what you say is that the first deferred flush in the child will be missed.
Yes, I exactly meant that.
> I agree that it's extremely unlikely, but we should handle it.
Agreed.
> We currently have:
>
> if (unlikely(pending - flushed > INT_MAX))
>
> So I guess an alternative would be:
>
> if (unlikely(pending - flushed - 1 >= INT_MAX))
>
>
> pending = x + 1
> flushed = x + 1
>
> (x + 1 - (x + 1 ) - 1) = (unsigned)-1 >= INT_MAX
Oh that's subtle :)
> But yeah, let's just initialize it during fork(), thanks!
Sounds good, thanks!
--
Cheers,
Harry / Hyeonggon
More information about the linux-riscv
mailing list