From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB4B8C531DA for ; Sat, 10 Aug 2024 18:43:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EF036B008A; Sat, 10 Aug 2024 14:43:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 09F126B0092; Sat, 10 Aug 2024 14:43:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECF5B6B0095; Sat, 10 Aug 2024 14:43:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CF7A46B008A for ; Sat, 10 Aug 2024 14:43:04 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7AF51A0409 for ; Sat, 10 Aug 2024 18:43:04 +0000 (UTC) X-FDA: 82437207888.18.763D422 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf30.hostedemail.com (Postfix) with ESMTP id 4752280012 for ; Sat, 10 Aug 2024 18:43:02 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf30.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723315316; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fPZUlKUBg9K/W0PNqqUtaR3E+gi8CthNO97T1kXJIik=; b=DTzK8wj0zdNm1Ij/GPV7D03f25LRfRrv8/le6QRGSVvQQKzHkAjcOcu3KToI5A6z82iJlj ThMvUV1d4BWwUaXIrB8xCbMpT98zFNuhYi3mMLJQNESIkY4CeJ7b5Zc8/auMqOO3QAO7vG rUOTD8HXbRsyULwZ1xrEADdOAl3FK7Q= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723315316; a=rsa-sha256; cv=none; b=4PZVUrDIVH0JWYeddWOscQovXnriuJ1m6wrYST2zYkXo/VxiJEhBamBaQ8Wy0Bcem+N3Rd 01MABZW05mw4lmyYtjZidB2uurbTcZAKgXeTVQheJPvMP0S+p4dqp0LxMhh3GVcyyWhT07 q9W48cJ9BwS5PEwcsAliyDPRR7waIWI= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf30.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A76C3FEC; Sat, 10 Aug 2024 11:43:26 -0700 (PDT) Received: from [10.163.56.130] (unknown [10.163.56.130]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 995183F71E; Sat, 10 Aug 2024 11:42:51 -0700 (PDT) Message-ID: <5a4ae1d3-d753-4261-97a8-926e44d4217a@arm.com> Date: Sun, 11 Aug 2024 00:12:43 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] mm: Retry migration earlier upon refcount mismatch To: David Hildenbrand , akpm@linux-foundation.org, shuah@kernel.org, willy@infradead.org Cc: ryan.roberts@arm.com, anshuman.khandual@arm.com, catalin.marinas@arm.com, cl@gentwo.org, vbabka@suse.cz, mhocko@suse.com, apopple@nvidia.com, osalvador@suse.de, baolin.wang@linux.alibaba.com, dave.hansen@linux.intel.com, will@kernel.org, baohua@kernel.org, ioworker0@gmail.com, gshan@redhat.com, mark.rutland@arm.com, kirill.shutemov@linux.intel.com, hughd@google.com, aneesh.kumar@kernel.org, yang@os.amperecomputing.com, peterx@redhat.com, broonie@kernel.org, mgorman@techsingularity.net, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org References: <20240809103129.365029-1-dev.jain@arm.com> <20240809103129.365029-2-dev.jain@arm.com> <761ba58e-9d6f-4a14-a513-dcc098c2aa94@redhat.com> Content-Language: en-US From: Dev Jain In-Reply-To: <761ba58e-9d6f-4a14-a513-dcc098c2aa94@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4752280012 X-Stat-Signature: 3fhp5fczmaoeao5hm1ergy7o8ebo8wbg X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1723315382-565582 X-HE-Meta: U2FsdGVkX18q3C+FJmGUhDQw0Q2iaJEObB1bjXUBb3EIwjVHjgxpmZNHc6Fr+/Qi5pTNtmwZ3ElkJkxjaQPtocayyCMhBRvQmxcZX925WhK6hzQTP/h8/FMC0eRSS47Cj8twRetPtW4QNdNPBSa4Q4PK8JFqKoFyOdiMiRmBIrhG+JDI5HvJY8rSO3ZcKcYN5lqJSpPzxGZ1ojrdronLk0myeWHmP66UfBnpHkZ3UFEkQ3LgsPFxwl/mAsQGNw497WmejXswd3ADQSfliee47npf+kLMwcsxf/iJDiuu5LBlIgjvp4HsLaJHKw1iorHrPxqBWmzuDKFmqBUJDfhIvO6MUBP1oqMW6QxIS3nAp+oEyFYS0URggTPjPYrjp+Bb4wbciXMBgVEvr/JSvq0107xLyTCg2dZtKxmznWoeQOhg0HRTcvNwCYne96b+muxMhcIGSbPFPrwrsQglNYtvxtLFDZgJ9dlfY4u6IuggdY8kXSKN6o9OMNT3AhMVi4hN3kP1M5OYSaLzlU/zh+i9Q0aCgq30gmhleEpWB221AVExatFvXm/rxnjq0huj8jDAi2VE0vbWE5o5F/ivLuyD/e6ROs5fzsy8n70Hp3mq5Zqaa82Cy0PWBq6wCJ29tr3uOM5YU4FilqVVEYBS25ZTFa5kpS9gqxmcr5wPm1M2zsCJ9DDyOobImaY3p8LO6c3Rb4hsFPcBKZwXBDFEFf2yxOEbPp+RFJ9tWUcWEQXNG58NDvVgsCzWyhrSAcg42JWiJCk767VK0ah7YJPxb7Mog59j2L1HxYQFuRtjT2RFqFW1Im9oAyQf6/412/Dk/U7J1/Ru5Qe6ZCcoFUu1rrCDoJMjuXmC+93P3FMcllW5MzgkUXSyU9KCCV9X9aL4p19r/vVW2q+Prruo26HJibVqI6ccgtzLhNeCYKkOqVcTOEdL2R593h4LGDvH+6oCO34imkfMv3WsW+txD2xblu2 abkzVa4n ySiQZtnstJ3wGoiZBS1z+rirso+yS4ul3iztD3MZtB9r4uSWJ0MxzD+0aPsLy7V7WC2s6gnaY6qlAot4Olqisqs4i2Mx4KIU52/mxGuj3R40FFHpqTydX6BnY78O4qiOaCfC01Wcz2x6X3fgPm+1tGJwS+nBHsDACL/E4d1SNaIOAasxNlvbQkmQJpw5zObZb3i5QM3mMLqSksiSpCkkpL4WTPaR4vQ3FEZcl5tIXseWam3t7T9PpgeJt9jqx8GIaGxrTa4zCJ4RZE5A= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 8/9/24 19:17, David Hildenbrand wrote: > On 09.08.24 12:31, Dev Jain wrote: >> As already being done in __migrate_folio(), wherein we backoff if the >> folio refcount is wrong, make this check during the unmapping phase, >> upon >> the failure of which, the original state of the PTEs will be restored >> and >> the folio lock will be dropped via migrate_folio_undo_src(), any racing >> thread will make progress and migration will be retried. >> >> Signed-off-by: Dev Jain >> --- >>   mm/migrate.c | 9 +++++++++ >>   1 file changed, 9 insertions(+) >> >> diff --git a/mm/migrate.c b/mm/migrate.c >> index e7296c0fb5d5..477acf996951 100644 >> --- a/mm/migrate.c >> +++ b/mm/migrate.c >> @@ -1250,6 +1250,15 @@ static int migrate_folio_unmap(new_folio_t >> get_new_folio, >>       } >>         if (!folio_mapped(src)) { >> +        /* >> +         * Someone may have changed the refcount and maybe sleeping >> +         * on the folio lock. In case of refcount mismatch, bail out, >> +         * let the system make progress and retry. >> +         */ >> +        struct address_space *mapping = folio_mapping(src); >> + >> +        if (folio_ref_count(src) != folio_expected_refs(mapping, src)) >> +            goto out; > > This really seems to be the latest point where we can "easily" back > off and unlock the source folio -- in this function :) > > I wonder if we should be smarter in the migrate_pages_batch() loop > when we start the actual migrations via migrate_folio_move(): if we > detect that a folio has unexpected references *and* it has waiters > (PG_waiters), back off then and retry the folio later. If it only has > unexpected references, just keep retrying: no waiters -> nobody is > waiting for the lock to make progress. The patch currently retries migration irrespective of the reason of refcount change. If you are suggesting that, break the retrying according to two conditions: 1. If the folio has waiters, retry according to NR_MAX_MIGRATE_PAGES_RETRY = 10. 2. If not, retry for a large number of iterations, say 10,000, since we just need to keep retrying till the racer finishes reading the folio/failing on folio_trylock(), and decrementing refcount. If so, we will have to make the check as a refcount freeze(with xas_lock()); if we don't do that, anyone can increase the refcount again, reading data from a stale reference to the folio, making our check futile (which begs the question: is commit 0609139 correct? Checking refcount mismatch in __migrate_folio() is ineffective since after that, and before folio_ref_freeze() in __folio_migrate_mapping(), the refcount may change.) As a result, the freeze will have to take place immediately after we unmap the folios from everyone's address space, something like: while (!folio_ref_freeze(src, expected_count) && ++retries < 10000) {         if (folio has waiters)                 break;    /* will be retried by the outer loop giving us 10 chances in total */ } > This really seems to be the latest point where we can "easily" back > off and unlock the source folio -- in this function :) > For example, when migrate_folio_move() fails with -EAGAIN, check if > there are waiters (PG_waiter?) and undo+unlock to try again later. Currently, on -EAGAIN, migrate_folio_move() returns without undoing src and dst; even if we were to fall through to _undo_src/dst, the folios will not be unmapped again since _unmap() and _move() are wrapped around different loops. This is what I was hinting to when I wrote in the cover letter: "...there is no way the refcount would be decremented; as a result, this renders the retrying useless" since upon the failure of _move(), the lock will not be dropped (which is dropped through undo_src()), rendering the _move() loop useless. Sorry, should have noted this there.