From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F27B30C359 for ; Sat, 1 Aug 2026 18:49:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785610194; cv=none; b=kOkAnixznsL13f1v0a6e1vnrmhVVvn6y+oxc74uDO29lgrRddHDE0WJqqShyrHF4PQ+DBhZIY68x2RPqhAv6i+lNe1EKv/1RDbU+jHgph0l50iZSTKL1mmXwqt3zW+NiYgSPTEZKGzef6EnUMwYPji9AvalKDVal9RhEQyWKto4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785610194; c=relaxed/simple; bh=q+w5zSyWj6u44VZF4B6C74cnfsThIHmTShnVuEudu9o=; h=Date:From:To:cc:Subject:In-Reply-To:Message-ID:References: MIME-Version:Content-Type; b=HflM1+rakZoDEtmtc3cbtsfVlYrVwgJNvW4egyrw0PlL7J1KYKaJix4LQL1Tpn/37yT7kq48ywftUEDfEq+UVzj+ZqFtIynuniwEBY78ofWCdUPFsHtn/xMzxn/IGmNNz3t/cgDQzvJmKVWx97jffdU48n/XJiWe6en/XMQQfC8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OVYEidit; arc=none smtp.client-ip=209.85.128.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OVYEidit" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-81e6f2ee60bso41958447b3.1 for ; Sat, 01 Aug 2026 11:49:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785610192; x=1786214992; darn=vger.kernel.org; h=content-type:mime-version:references:message-id:in-reply-to:subject :cc:to:from:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NBwyWL5vh2enJmrBMYpGdLkECX4vKqcM+TreBJTlvII=; b=OVYEiditompA+dDqDpdOA/BGAW/6ZPyNpL5gdTGNm8PW2cVJ3OZLHNKHYTWSrbwJSp /qcSLjUTY/VRek0UeJYsqy4yi5N5mkBZJhF4f8GGZ8tY1SL1pZmzyz+Ubzh4ez1na6K5 Xssgcfhh8bMPlh/XbZyg0/UeVhGWmnqITBE4EdvrPsTxX39dTrv/8HFzFdKbNuJKxDMT ZBFNAZAsW3tJkmPeMw55Zj/3pSO6qLU+T/rhI+Hbmj8X7higOoVCMMW7Ydm76FxJ9WoT nQH7amC4xZ5pP+rrQ547dFdBvT5ADO7XgJ9LgxhVSVZ5j3hQOc81S7eEQqdUIkCGLQnx zvtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785610192; x=1786214992; h=content-type:mime-version:references:message-id:in-reply-to:subject :cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NBwyWL5vh2enJmrBMYpGdLkECX4vKqcM+TreBJTlvII=; b=hrFvuAAljz/xvn4SGSYfG5//32vL7AqkqPI4N65LvwG1rxhRHcivcM3gIX2YI9Kmv0 jkklOwOqCsw64KcKO+AL0/uJxijCCr7CK/kWoY+MfbiY8/rfEK38VN2mfTyUtwViCCeq 64oMvxfPOJLQHYH4jp2Wj/M/yFrDt5kPAuDP8YADH46d8/5G/6bxL4wQqfIx5fv2Tjp9 lG8zK6jkdjcPxOujxNKqqBYeMZ5/xsMkZCLhYzauZgwJgZCnJeJpzaCXOPoWdqtrxyfD Exy1LKrHnofmTlHRde0fUsd5Z8v/hy20L/FkLRKPmA+XqWqjCqPCEncIB6m017n86iRv ognQ== X-Forwarded-Encrypted: i=1; AHgh+RoKtj/yY4py9kwAUowaWJ0Rjh5L7dD5JmcN/Lqnk0m7AxR+vIgpKLu4qYrFxQsYiY+wCd1Qp5AqPelS4nw=@vger.kernel.org X-Gm-Message-State: AOJu0YxEHHqpcz1fnXWxc51HTfRG/nrFs0KFD5MM8H+h8ZeaLO5BmRY9 GMT1ZJFq75potJjiYHeOMosSXvNejB1S2ZsWl17Xf2MM7dJV3mkbKB6hSOXTXmBhscnhlpcu2aZ Ej94Gs+is X-Gm-Gg: AR+sD12vGUbuJ5kwX9Jc6c0DCBEU8UTkYEDihf8Outzm+ooF/YAePLPXkd1/lSws27w kZT8tpLiFFIqgyrkcGfExcIdPSVijgqz+JErDEBTkdOw9JXGymQ5qL4Mc8pMqjBeUf+hAdGKEfG NpmDrOquCtYdJ0jfWW+2ojUeuV0sgLACFsYi2prbKoYYy7OVYWnqDMwQUL+vCGYN6EeuwgHbGoe x12/hqZREjyZux7t7VRtaRU36PB1E1oPsWKf3l40sd+ql3QHPxeZvckChuFiPhgewTb835WmRjR unMfTjmTrOYeTi8Tnws7Cug7ocGKzOwZMKLu28YWx6qffGtP2U3Y81CFWwSum798K+sa33geTlD ArByoVps5/FC/ys3HRWKZJFszzibW4MjFk/ylkXUEl+6aUG220JxVnqVZCyFERt00DOneCOnCea ZOWb8ip4EnM6rNMXAKyElOXPGaE/rgstctVxJJzrXoR6RvHiXhyF4NYUm/rK2y9UrvnYgDOtMnH 0FOPo1sk/gTHrAB8T+HX/qJ2Vm+zzZ3YT3/CFgYwqE= X-Received: by 2002:a05:690c:b86:b0:81e:4acb:c0a with SMTP id 00721157ae682-81fd4d31d4bmr66094927b3.2.1785610191764; Sat, 01 Aug 2026 11:49:51 -0700 (PDT) Received: from [192.168.1.163] (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id 00721157ae682-81fccf2e4f5sm28773407b3.3.2026.08.01.11.49.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 Aug 2026 11:49:50 -0700 (PDT) Date: Sat, 1 Aug 2026 11:49:39 -0700 (PDT) From: Hugh Dickins To: "David Hildenbrand (Arm)" cc: Andrew Morton , Jason Gunthorpe , John Hubbard , Peter Xu , Kiryl Shutsemau , Hugh Dickins , Ackerley Tng , Frank van der Linden , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] mm/gup: fix always draining LRU caches in collect_longterm_unpinnable_folios() In-Reply-To: <20260731-check_and_migrate_movable_folios-v1-1-e0002d7b791e@kernel.org> Message-ID: References: <20260731-check_and_migrate_movable_folios-v1-1-e0002d7b791e@kernel.org> 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 On Fri, 31 Jul 2026, David Hildenbrand (Arm) wrote: > folio_may_be_lru_cached() is currently only true for small folios, and > for small folios FOLL_PIN adds GUP_PIN_COUNTING_BIAS references instead > of 1 in try_grab_folio()/try_grab_folio_fast(). > > Consequently, our > > folio_ref_count(folio) != folio_expected_ref_count(folio) + 1 > > check in collect_longterm_unpinnable_folios() will currently always > identify "reference mismatch" and first drain the local LRU cache to then > drain the LRU cache on all CPUs, as collect_longterm_unpinnable_folios() > is really called after pinning the folios with FOLL_PIN. > > Add a comment because the current code is not quite intuitive: we used to > drain only to make sure the folio_isolate_lru() would succeed. But then we > also started draining to make later migration more reliable. > > We'll refactor that code soon a bit, to also make it usable in other > context where we really want to remove any references from LRU caches. > > Let's add CC stable, because having an easy way for excessive LRU cache > draining on all CPUs does not sound right. In common scenarios we > don't expect to every have to drain. > > Fixes: 98c6d259319e ("mm/gup: check ref_count instead of lru before migration") > Fixes: a09a8a1fbb37 ("mm/gup: local lru_add_drain() to avoid lru_add_drain_all()") > Cc: stable@vger.kernel.org > Signed-off-by: David Hildenbrand (Arm) Ughh! And I was so proud of it. Now I'm ashamed. It just never occurred to me, that the unpinnable pages were pinned there. Thanks for the fix (but please don't tell anyone ;) Acked-by: Hugh Dickins > --- > Found by code inspection. If someone has a testcase that can easily > trigger this and result in migration problems, please test! But this > change seems to be "obvious the right thing to do". Sorry, I don't (as perhaps is too obvious). And I'm a wee bit afraid that some of the problems which those changes happened to succeed in fixing, might now reappear once the drains are finely targeted. Am I arguing for a delay before advancing to stable? Perhaps, but I think not: it's probably best to get that working as intended, then deal with any fallout if it arises: just be on guard. > --- > mm/gup.c | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > diff --git a/mm/gup.c b/mm/gup.c > index 99902c15703b0..41c3317e0f0f4 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -2273,6 +2273,7 @@ static unsigned long collect_longterm_unpinnable_folios( > > for (folio = pofs_get_folio(pofs, i); folio; > folio = pofs_next_folio(folio, pofs, &i)) { > + const int pin_refs = folio_has_pincount(folio) ? 1 : GUP_PIN_COUNTING_BIAS; > > if (folio_is_longterm_pinnable(folio)) > continue; > @@ -2287,15 +2288,20 @@ static unsigned long collect_longterm_unpinnable_folios( > continue; > } > > + /* > + * We drain not only to make the folio_isolate_lru() succeed, > + * but also to remove any other folio references from LRU > + * caches. > + */ > if (drained == 0 && folio_may_be_lru_cached(folio) && > folio_ref_count(folio) != > - folio_expected_ref_count(folio) + 1) { > + folio_expected_ref_count(folio) + pin_refs) { > lru_add_drain(); > drained = 1; > } > if (drained == 1 && folio_may_be_lru_cached(folio) && > folio_ref_count(folio) != > - folio_expected_ref_count(folio) + 1) { > + folio_expected_ref_count(folio) + pin_refs) { > lru_add_drain_all(); > drained = 2; > } > > --- > > base-commit: e5492213654050379e78ec6f9acfd6c9fe00f334 > > change-id: 20260731-check_and_migrate_movable_folios-43500556943d > > -- > > Cheers, > > David