From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795110194; b=y9TGGLR2Epfq+GKyj2zuoVj4t2NsuF4DMO4kWZkCtHObCRWqfL40LyGM3pAjwG1T8o2/ KcxzZ1+RAx7qFoSYJhmoXsu6xuHNuNHB76/ag0WeEzhPfq4HrdMbMHMmZ/hjag4DJhF0F djViDEXUT41kWKMLYQ2yJGP6F6RvWNHk01oD3mO5mR+BQJdV8sR6CuxkzlEL04PXaEC1q jlshXcfPXODUR+4kn7rsOy5IjOwOkhpJZpx2jiu/KwPdEFFwf+XXsyYFwrBkpMqFoD65N 0hZEcMk/Vs69bqR1d8YvG4Zb7rwTvN4wna7MexsK50tGEWf7MHt61fqEvBtfZ+FRHBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795110194; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=OP2Z6OwMvYcnCbVikE5bbFucvyV4pc4cThw1oGx4IIM=; b=j2wnsKGIPfYi0r7c/be+//MTuKykxxnb0ffSyA2EOnJZ95AjWxsuUWwohnu0jae8CpeR sY7e/83LKl6BniuAmw+YIHJBNBp+ITPXu8xJKekU8Z8ec42Mj2bbjahbWdq3Jo6+RpWYM 8lWftQb8QUjwgOasY8ckuPmf083oK6O4DOQvdoUuHol7VhnBCHToh4BSnMEAYBDFp9S0I JJjmiDoCGanpIjw5kFszCT/zLf+5cKI3hPvJ7wNGh6IObc6KNZMRk5M4l14vueHdd+dm/ KLEK7z5qS+ZoFg4cvL1DdIvD7oJJJUvQoZ1EGqd7OKxy12b4mOy0BannY9uWEzBAxsQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dNDu9RIL; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5492606A33D for ; Thu, 19 Nov 2026 17:43:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dNDu9RIL Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BAB01F000C4; Thu, 19 Nov 2026 17:43:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795110193; bh=OP2Z6OwMvYcnCbVikE5bbFucvyV4pc4cThw1oGx4IIM=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=dNDu9RILbXjxlt8R4ppKZSkXWe6g/BB83LL6uh8X6lPCUAn9gAW8mERhR4GGBRPf5 doQiFX9utToBExTiRfjinqkm3yFTnopZLd8l1oPjorvcPnAZAT+M09QFiarN/Yqw0j GkV4FHjXkYc2YOk04s19UqNxfB/ZbP54SuIOnWgO/N1aubGa7SqhNEjz2CdT/Q6E+d LoiRDln0arK6nQM1NVFaiD6LwhWhVMXE80W7mN5wgynFo/1a+h0brbNrULpaNPzgxl +0bhuYIopQpzfSD+bFsEz7Ue8t0jafiNi9wlj1Msn9EjQDeuTh8pnYdC7ZwCtiaMq5 2Zj0HTPLBJacg== Date: Thu, 19 Nov 2026 18:43:08 +0100 From: Andi Shyti To: "Kiryl Shutsemau (Meta)" Cc: Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Simona Vetter , Andi Shyti , David Hildenbrand , Hugh Dickins , Baolin Wang , intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: Re: [PATCH] drm/i915/gem: Skip writeback of shmem folios with extra references Message-ID: References: <20261119134243.3745282-1-kas@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261119134243.3745282-1-kas@kernel.org> Hi Kiryl, On Thu, Nov 19, 2026 at 01:42:41PM +0000, Kiryl Shutsemau (Meta) wrote: > __shmem_writeback() writes out every dirty folio that is not mapped. A > folio stops being mapped when munmap() removes its rmap, but the TLB > flush comes later. Until then another thread can still store to it > through a stale TLB entry: > > CPU0 (munmap) CPU1 (same mm) CPU2 (i915 shrinker) > zap_huge_pmd() > folio_remove_rmap_pmd() > __shmem_writeback() > clear dirty > !folio_mapped() > shmem_write_folio() > store via stale TLB > tlb_finish_mmu() > > The store leaves the folio clean in the swap cache. Once reclaim frees > it, the object reads back from swap without that store. > > The legacy mmap ioctl maps the shmem file directly, and i915 mounts it > with huge=within_size on some platforms, so the folio can be PMD-mapped. > The core mm keeps the rmap until after the flush for PTEs, but not for > shmem PMDs: shmem has no writeback, and pageout() refuses a folio with > an extra reference. > > Do the same here. The unmapping side holds its reference until the TLB > is flushed, so a folio with references beyond the page cache and > writeback_iter() is left to reclaim, as a mapped one already is. > > Fixes: 2d6692e642e7 ("drm/i915: Start writeback from the shrinker") > Signed-off-by: Kiryl Shutsemau (Meta) > --- > Found while reviewing David's fix for the same race on file PMDs, which > leaves shmem out because shmem has no writeback: > https://patch.msgid.link/20261116-dirty_file_pmd-v2-2-571883053174@kernel.org > > Build-tested only. > > drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > index bf9e7480f0f5..748772c358ac 100644 > --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c > @@ -316,9 +316,15 @@ void __shmem_writeback(size_t size, struct address_space *mapping) > * leaving only CPU mmapings around) and add those folios to the LRU > * instead of invoking writeback so they are aged and paged out > * as normal. > + * > + * The same goes for a folio with references beyond the page cache > + * and writeback_iter(). A folio that was just unmapped can still be > + * written through a stale TLB entry until the unmapping side flushes > + * the TLB and drops its reference. pageout() checks the same. > */ > while ((folio = writeback_iter(mapping, &wbc, folio, &error))) { > - if (folio_mapped(folio)) > + if (folio_mapped(folio) || > + folio_ref_count(folio) != 1 + folio_nr_pages(folio)) I don't think we need this fix in the current tree. The i915 writeback iterator already skips the shmem folio in the case you describe, so the new refcount check doesn't change the outcome. Thanks, Andi > folio_redirty_for_writepage(&wbc, folio); > else > error = shmem_write_folio(folio); > > base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd > -- > 2.54.0 >