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 79AC1C3DA4A for ; Tue, 30 Jul 2024 00:58:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8ECC16B0085; Mon, 29 Jul 2024 20:58:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 89E696B0088; Mon, 29 Jul 2024 20:58:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78AF06B0089; Mon, 29 Jul 2024 20:58:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5B7426B0085 for ; Mon, 29 Jul 2024 20:58:05 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 1534BC018A for ; Tue, 30 Jul 2024 00:58:05 +0000 (UTC) X-FDA: 82394607330.17.CE51B16 Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.7]) by imf01.hostedemail.com (Postfix) with ESMTP id 1E4F440013 for ; Tue, 30 Jul 2024 00:58:01 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=MRM0gzB5; spf=pass (imf01.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.7 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722301079; 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:dkim-signature; bh=gnQaqa4yRlIvwZVmQDp4+I96iPATKoWO64nJkQY4TyU=; b=XbqdYYEKD2tR5jWtBdN4PBjNGD98arqS/8QIzmMHSeEHsb7tLBYH24hY6NdjO1V0PxWkbq 7UCIigQiErcZNwv4Z/NedjnNoLCk1BBANEMx9AtcUKM90CKtDA+KuImQlX8S5yjglrJavO RK3QCdnMDom1eTFCt5YqmSsfze5LnYg= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=MRM0gzB5; spf=pass (imf01.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.7 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722301079; a=rsa-sha256; cv=none; b=Vc3nC1pfpZO+gZuoyt4+pf7kjsucuUDSdulqZYvC1fhKUjrLrMdCfRRm5M3UUtncUNgDzy 1PTVRKj0D1k/2ySxzJBq9ByHNmegNtmq22InUYe2Nbwq9awNm3IT3CEmegDfPBjGqQ++s3 dVk9dX79ZfDi7PUx7WycbjEUdt0eusU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=Message-ID:Date:MIME-Version:Subject:From: Content-Type; bh=gnQaqa4yRlIvwZVmQDp4+I96iPATKoWO64nJkQY4TyU=; b=MRM0gzB5Zgu+ITtWJW+yO6ygMqxVKANYjUW9z9fuc9/0sJUcPZBuG6qZzYxPPn JG4ihN++h7eyHAIaqHJB7Y/2nuzo7bTHqX9+RCFLMAyKO8mGmY7x2ci0U1KkWS6H EOTD6LawM/Sv8LacS3X2aHLcCKPOYFaXNMKbObjgBRLLQ= Received: from [172.21.22.210] (unknown [118.242.3.34]) by gzga-smtp-mta-g1-0 (Coremail) with SMTP id _____wD3_wmQOqhm4E3MAw--.13651S2; Tue, 30 Jul 2024 08:57:54 +0800 (CST) Message-ID: <79234eac-d7cc-424b-984d-b78861a5e862@126.com> Date: Tue, 30 Jul 2024 08:57:52 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH V2] mm/gup: Clear the LRU flag of a page before adding to LRU batch To: David Hildenbrand , akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, 21cnbao@gmail.com, baolin.wang@linux.alibaba.com, liuzixing@hygon.cn References: <1719038884-1903-1-git-send-email-yangge1116@126.com> <3a2ab0ea-3a07-45a0-ae0e-b9d48bf409bd@redhat.com> From: Ge Yang In-Reply-To: <3a2ab0ea-3a07-45a0-ae0e-b9d48bf409bd@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wD3_wmQOqhm4E3MAw--.13651S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3AFW7ZF1kGw1kXFyrGw1Utrb_yoW7ur43pF WxGrnIqFWDJFsrur47Xr15AF1Fk393XF4UAFWxGFy7AFn8Zw1qkF1xKw1UGa9xJr15uF1x Z3WUXFn5W3WUJFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j5CzZUUUUU= X-Originating-IP: [118.242.3.34] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiWRMsG2VLbxqygQABsU X-Rspam-User: X-Stat-Signature: 1buneabigmpczniwkpfydowja3wp6c4d X-Rspamd-Queue-Id: 1E4F440013 X-Rspamd-Server: rspam11 X-HE-Tag: 1722301081-508571 X-HE-Meta: U2FsdGVkX1+WwG/PmHb6o1RPy+M3T+HdCaqYkBBlF9hGRgXw4YNIXQLySPG75DEBkELcVX+yvl+1K6Ps2znV441JtZgA0VGJwXRfcy0yhEsK502sQ344Aho6eN/WMgteUcVwp4t+ixQq9A4ASg2A0OAROd7CTO84iS+hqJiZhKy1e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hBgMRq9Ub3xseM7iCcbTCZP2DbEI6i62KP/yoMCNKLWG/XzxEZf+/Y691/SjNKQkAiRUjzA/BPxsTNf01QEKTDRY7H/URSNTGJpl3Ilt1NVn2PaCvR/UXbKg5ibKfd2wDO1Dh0CymxRNRtFWVrkvz7Rc8OBSj4R3hLaquSwGh6GtvkHU5c+2yjVfjkTqaUW0qyTDTANLSPgRpAOurqf9ylNqWLNYj5jTDqzjdmkS3JWtcBGShtP8VNvsx/19Uhh2xSAKCiyMf/SLmP4euGKfaYUkzm8zYHBs= 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: 在 2024/7/29 22:04, David Hildenbrand 写道: > On 22.06.24 08:48, yangge1116@126.com wrote: >> From: yangge >> >> If a large number of CMA memory are configured in system (for example, >> the >> CMA memory accounts for 50% of the system memory), starting a virtual >> virtual machine, it will call pin_user_pages_remote(..., FOLL_LONGTERM, >> ...) to pin memory.  Normally if a page is present and in CMA area, >> pin_user_pages_remote() will migrate the page from CMA area to non-CMA >> area because of FOLL_LONGTERM flag. But the current code will cause the >> migration failure due to unexpected page refcounts, and eventually cause >> the virtual machine fail to start. >> >> If a page is added in LRU batch, its refcount increases one, remove the >> page from LRU batch decreases one. Page migration requires the page is >> not >> referenced by others except page mapping. Before migrating a page, we >> should try to drain the page from LRU batch in case the page is in it, >> however, folio_test_lru() is not sufficient to tell whether the page is >> in LRU batch or not, if the page is in LRU batch, the migration will >> fail. >> >> To solve the problem above, we modify the logic of adding to LRU batch. >> Before adding a page to LRU batch, we clear the LRU flag of the page so >> that we can check whether the page is in LRU batch by >> folio_test_lru(page). >> Seems making the LRU flag of the page invisible a long time is no >> problem, >> because a new page is allocated from buddy and added to the lru batch, >> its LRU flag is also not visible for a long time. >> >> Cc: >> Signed-off-by: yangge >> --- >>   mm/swap.c | 43 +++++++++++++++++++++++++++++++------------ >>   1 file changed, 31 insertions(+), 12 deletions(-) >> >> diff --git a/mm/swap.c b/mm/swap.c >> index dc205bd..9caf6b0 100644 >> --- a/mm/swap.c >> +++ b/mm/swap.c >> @@ -211,10 +211,6 @@ static void folio_batch_move_lru(struct >> folio_batch *fbatch, move_fn_t move_fn) >>       for (i = 0; i < folio_batch_count(fbatch); i++) { >>           struct folio *folio = fbatch->folios[i]; >> -        /* block memcg migration while the folio moves between lru */ >> -        if (move_fn != lru_add_fn && !folio_test_clear_lru(folio)) >> -            continue; >> - >>           folio_lruvec_relock_irqsave(folio, &lruvec, &flags); >>           move_fn(lruvec, folio); >> @@ -255,11 +251,16 @@ static void lru_move_tail_fn(struct lruvec >> *lruvec, struct folio *folio) >>   void folio_rotate_reclaimable(struct folio *folio) >>   { >>       if (!folio_test_locked(folio) && !folio_test_dirty(folio) && >> -        !folio_test_unevictable(folio) && folio_test_lru(folio)) { >> +        !folio_test_unevictable(folio)) { >>           struct folio_batch *fbatch; >>           unsigned long flags; >>           folio_get(folio); >> +        if (!folio_test_clear_lru(folio)) { >> +            folio_put(folio); >> +            return; >> +        } >> + >>           local_lock_irqsave(&lru_rotate.lock, flags); >>           fbatch = this_cpu_ptr(&lru_rotate.fbatch); >>           folio_batch_add_and_move(fbatch, folio, lru_move_tail_fn); >> @@ -352,11 +353,15 @@ static void folio_activate_drain(int cpu) >>   void folio_activate(struct folio *folio) >>   { >> -    if (folio_test_lru(folio) && !folio_test_active(folio) && >> -        !folio_test_unevictable(folio)) { >> +    if (!folio_test_active(folio) && !folio_test_unevictable(folio)) { >>           struct folio_batch *fbatch; >>           folio_get(folio); >> +        if (!folio_test_clear_lru(folio)) { >> +            folio_put(folio); >> +            return; >> +        } >> + >>           local_lock(&cpu_fbatches.lock); >>           fbatch = this_cpu_ptr(&cpu_fbatches.activate); >>           folio_batch_add_and_move(fbatch, folio, folio_activate_fn); >> @@ -700,6 +705,11 @@ void deactivate_file_folio(struct folio *folio) >>           return; >>       folio_get(folio); >> +    if (!folio_test_clear_lru(folio)) { >> +        folio_put(folio); >> +        return; >> +    } >> + >>       local_lock(&cpu_fbatches.lock); >>       fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate_file); >>       folio_batch_add_and_move(fbatch, folio, lru_deactivate_file_fn); >> @@ -716,11 +726,16 @@ void deactivate_file_folio(struct folio *folio) >>    */ >>   void folio_deactivate(struct folio *folio) >>   { >> -    if (folio_test_lru(folio) && !folio_test_unevictable(folio) && >> -        (folio_test_active(folio) || lru_gen_enabled())) { >> +    if (!folio_test_unevictable(folio) && (folio_test_active(folio) || >> +        lru_gen_enabled())) { >>           struct folio_batch *fbatch; >>           folio_get(folio); >> +        if (!folio_test_clear_lru(folio)) { >> +            folio_put(folio); >> +            return; >> +        } >> + >>           local_lock(&cpu_fbatches.lock); >>           fbatch = this_cpu_ptr(&cpu_fbatches.lru_deactivate); >>           folio_batch_add_and_move(fbatch, folio, lru_deactivate_fn); >> @@ -737,12 +752,16 @@ void folio_deactivate(struct folio *folio) >>    */ >>   void folio_mark_lazyfree(struct folio *folio) >>   { >> -    if (folio_test_lru(folio) && folio_test_anon(folio) && >> -        folio_test_swapbacked(folio) && !folio_test_swapcache(folio) && >> -        !folio_test_unevictable(folio)) { >> +    if (folio_test_anon(folio) && folio_test_swapbacked(folio) && >> +        !folio_test_swapcache(folio) && >> !folio_test_unevictable(folio)) { >>           struct folio_batch *fbatch; >>           folio_get(folio); >> +        if (!folio_test_clear_lru(folio)) { >> +            folio_put(folio); >> +            return; >> +        } > > Looking at this in more detail, I wonder if we can turn that to > > if (!folio_test_clear_lru(folio)) >     return; > folio_get(folio); > > In all cases? The caller must hold a reference, so this should be fine. > Seems the caller madvise_free_pte_range(...), calling folio_mark_lazyfree(...), doesn't hold a reference on folio.