From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47ED91E8850 for ; Sat, 25 Jan 2025 01:24:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737768243; cv=none; b=Q00NTmoUlApb9Dm/HZkZ4LGLDovOFnBu7lnyfn3C6MAqVdp1ZufCAms0S537Moj3NYanAv/PIEJHX2UMsA0rxK+RhXM7C9UK3dlUIqIjhRerdHLskJHcJrEq6lxU1yNUIXNLD473MfRqDpTOWQakz3fP2ljQghY9a/RvwfdfOGY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737768243; c=relaxed/simple; bh=FYVfMxPYc+OFMvEkABiP1w05Cizf7xW4POCl2ZmnkxI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Yfz8D1V5cbA8z1bSSwFxfV2qfOB1GGkn9tDk9DV4VZ9oHabU/3or5JFxslmk0+FBmqMXEb90m5JV8/ZB3mob2yUjk6jn28JXzrgene3u0pq/1Et8NRlnsiJJnJxE0fR4l3/rf8VaZWwe9bgIEI1XgS4gIqt6yJLSODVVFgbuxGE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=unLAAWB2; arc=none smtp.client-ip=95.215.58.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="unLAAWB2" Date: Sat, 25 Jan 2025 01:23:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1737768239; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=/pSbQD6T7cVkyHZaa4OI7IbTRYzdcQFCXIisyqs6sDM=; b=unLAAWB20cFTyKOtxmF44nr4maB8AeQkhvOjERFWEZKOP1qB1ZW9wA8TkoaVA1GZSUFjSF tbR2i3YhDxfsMGAdCkq4D5ukUiD8CNyvG8V9DBWKSOUezwkIZ0UDB3GJQpsp1rLAtVxwMd ZFHNa5MFYur4wlTDVCLjuzS0gqWCJTI= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Roman Gushchin To: Peter Zijlstra Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Jann Horn , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Hugh Dickins , linux-arch@vger.kernel.org Subject: Re: [PATCH v2] mmu_gather: move tlb flush for VM_PFNMAP/VM_MIXEDMAP vmas into free_pgtables() Message-ID: References: <20250122232716.1321171-1-roman.gushchin@linux.dev> <20250123214531.GA969@noisy.programming.kicks-ass.net> <20250124082250.GA13226@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250124082250.GA13226@noisy.programming.kicks-ass.net> X-Migadu-Flow: FLOW_OUT On Fri, Jan 24, 2025 at 09:22:50AM +0100, Peter Zijlstra wrote: > On Thu, Jan 23, 2025 at 11:12:33PM +0000, Roman Gushchin wrote: > > > > +static inline void tlb_free_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) > > > { > > > if (tlb->fullmm) > > > return; > > > > > > /* > > > * VM_PFNMAP is more fragile because the core mm will not track the > > > + * page mapcount -- there might not be page-frames for these PFNs > > > + * after all. > > > + * > > > + * Specifically() there is a race between munmap() and > > > + * unmap_mapping_range(), where munmap() will unlink the VMA, such > > > + * that unmap_mapping_range() will no longer observe the VMA and > > > + * no-op, without observing the TLBI, returning prematurely. > > > + * > > > + * So if we're about to unlink such a VMA, and we have pending > > > + * TLBI for such a vma, flush things now. > > > */ > > > + if ((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) && tlb->vma_pfn) > > > tlb_flush_mmu_tlbonly(tlb); > > > > Why do we need to re-check vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP) here? > > No need, but an opportunity. > > > In free_pgtables() we're iterating over multiple vma's. What if the first has > > no VM_PFNMAP set, but some other do? Idk if it's even possible, but it's not > > obvious that it's not possible either. > > If we only need to flush PFN entries before unlinking PFN VMAs, then: > > - if there are no PFNs pending (vma_pfn), we don't need to flush; > - if no PFN vma is being freed (vm_flags), we don't need to flush. Right, but if I understand the code correctly, more than one vma can be freed by a single free_pgtables() invocation. Should we then check each vma's flags in the while loop in free_pgtables()? But then we're back to where we're now with multiple flushes. Do I misunderstand this? Thanks