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 7FCECC43217 for ; Thu, 24 Nov 2022 13:20:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E8EFC6B0071; Thu, 24 Nov 2022 08:20:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E3F786B0072; Thu, 24 Nov 2022 08:20:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE09A6B0074; Thu, 24 Nov 2022 08:20:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C0DFB6B0071 for ; Thu, 24 Nov 2022 08:20:44 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 954941C69B7 for ; Thu, 24 Nov 2022 13:20:44 +0000 (UTC) X-FDA: 80168395608.01.41707A9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 09A5D40018 for ; Thu, 24 Nov 2022 13:20:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669296043; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zKVh5LMLpMEzx628uR7TstqMz9DC8m5tstRZUPhlS3M=; b=copgreWoFxBynzOdfJW/2dFrZllg6HHMUi3Li/4ToGYbOB52xfi4P0kJLwbv4ptH/p223H gNaZZEgWS8kT0BriQ40rlUpisVYFsvMOPCJ3A/IUg7hJMQnuHFTAr1bbwt/ucrSHZuSDKs tunlq0cZ679GVb794th3RFaMLjSvzto= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-621-yWMerXgqMs2s7n2f1YlAPA-1; Thu, 24 Nov 2022 08:20:41 -0500 X-MC-Unique: yWMerXgqMs2s7n2f1YlAPA-1 Received: by mail-wm1-f72.google.com with SMTP id c187-20020a1c35c4000000b003cfee3c91cdso947950wma.6 for ; Thu, 24 Nov 2022 05:20:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zKVh5LMLpMEzx628uR7TstqMz9DC8m5tstRZUPhlS3M=; b=xCqacyu4/hcOw8/rTQQGFbWmXBhwXcgeXFj9oVvgGJOW56cS26xyorB6ui3KFMVXPt hB5MzkFsUxUEU5hRNQK7cpajpo46y0/UFdzVtYJUesM5j5Migyv4UBMC9CDJbTHia2f+ nDVAYbNnilpD/DquYOy/gocKLmTA6ELEfsiIxYyB8Yz6z3/UeP9GoIlMtFiymYCv5Ywy yLVIj1KRxXD+Q74ovOB67lsaH2zkKxsis3qorLDXdrov1l120IsVWX7BdfKTgvJ2s4Hn B2/FQAdh0yXZUZE/fHsHGwQEbbwjPV4bBybtoERfwwbGe1nDwrEYH9LrUKt3/OIeGeQv OcWw== X-Gm-Message-State: ANoB5pnSswg5d6sNUqaiwZexkpaDGoaftFtYI3gyQ3HlOEQALAPyUp/1 TlsnJNgEythD5QiIuu20q3ZT39HZX73ugh8kr696Dqt13DOQ+d+/mZrODRAXNhtlF4novjHfUfW MAZ7+pMOcycY= X-Received: by 2002:a5d:5a8b:0:b0:241:b92b:d895 with SMTP id bp11-20020a5d5a8b000000b00241b92bd895mr14315196wrb.449.1669296040270; Thu, 24 Nov 2022 05:20:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf7fbE6DnOhP990SWIzCZp2wuhX/dzyJ5UGMSxijAD+AF4Cy7yZbdWHbiYCcaV2k7ZnOQ6VLug== X-Received: by 2002:a5d:5a8b:0:b0:241:b92b:d895 with SMTP id bp11-20020a5d5a8b000000b00241b92bd895mr14315178wrb.449.1669296039926; Thu, 24 Nov 2022 05:20:39 -0800 (PST) Received: from ?IPV6:2003:cb:c704:2200:bfcb:7212:1370:de13? (p200300cbc7042200bfcb72121370de13.dip0.t-ipconnect.de. [2003:cb:c704:2200:bfcb:7212:1370:de13]) by smtp.gmail.com with ESMTPSA id bi25-20020a05600c3d9900b003cf4eac8e80sm2075927wmb.23.2022.11.24.05.20.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Nov 2022 05:20:39 -0800 (PST) Message-ID: <5bafb39c-4365-b691-c516-4ce595af7ef7@redhat.com> Date: Thu, 24 Nov 2022 14:20:38 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH v2] mm: migrate: Fix THP's mapcount on isolation To: Zi Yan Cc: Gavin Shan , linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, william.kucharski@oracle.com, kirill.shutemov@linux.intel.com, zhenyzha@redhat.com, apopple@nvidia.com, hughd@google.com, willy@infradead.org, shan.gavin@gmail.com, Huang Ying References: <20221124095523.31061-1-gshan@redhat.com> <3c584ce6-dc8c-e0e4-c78f-b59dfff1fc13@redhat.com> <22407f18-0406-6ede-ef1e-592f03d3699e@redhat.com> <31bda0ab-a185-340d-b96b-b1cfed7c3910@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669296044; a=rsa-sha256; cv=none; b=Aqv+Qld2MfmOBXG6dXDuNO3232tMA+y7xk3y/5YuIPFy2Q2s1U6G3/EGWsEpOdd3+s6mb4 gnH5FOLCSddTfCfvPRmFfQCcss7HVvR8xZXVgqJVzmc1ACaK23/Y6I8S3ZJLvnnL35L+f+ iJWWXVRfByLg8wwkpKvx+aoq9bjnqjk= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=copgreWo; spf=pass (imf17.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669296044; 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=zKVh5LMLpMEzx628uR7TstqMz9DC8m5tstRZUPhlS3M=; b=kOcLKscBJnHwGwjiLXmQCRdEjmx0VSqoJ4OLY9Ta1Xes3oaDnkiEzlubs2t5wtEEG5j16B WiHgmMjCJgWcDosmE6XBQuM9rO3w3dNpoS0zM5pY80OLhrUpKAPgBDauu/YLvWMSeszNXo aE5gB0+NmhEulVsLi/994YDHBqqpjnE= X-Stat-Signature: a1chx15r4yxbnqy8wkemu45epc43ubgf X-Rspamd-Queue-Id: 09A5D40018 X-Rspam-User: Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=copgreWo; spf=pass (imf17.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam02 X-HE-Tag: 1669296043-694114 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: On 24.11.22 13:38, Zi Yan wrote: > > On 24 Nov 2022, at 5:43, David Hildenbrand wrote: > >> On 24.11.22 11:21, Gavin Shan wrote: >>> On 11/24/22 6:09 PM, David Hildenbrand wrote: >>>> On 24.11.22 10:55, Gavin Shan wrote: >>>>> The issue is reported when removing memory through virtio_mem device. >>>>> The transparent huge page, experienced copy-on-write fault, is wrongly >>>>> regarded as pinned. The transparent huge page is escaped from being >>>>> isolated in isolate_migratepages_block(). The transparent huge page >>>>> can't be migrated and the corresponding memory block can't be put >>>>> into offline state. >>>>> >>>>> Fix it by replacing page_mapcount() with total_mapcount(). With this, >>>>> the transparent huge page can be isolated and migrated, and the memory >>>>> block can be put into offline state. Besides, The page's refcount is >>>>> increased a bit earlier to avoid the page is released when the check >>>>> is executed. >>>> >>>> Did you look into handling pages that are in the swapcache case as well? >>>> >>>> See is_refcount_suitable() in mm/khugepaged.c. >>>> >>>> Should be easy to reproduce, let me know if you need inspiration. >>>> >>> >>> Nope, I didn't look into the case. Please elaborate the details so that >>> I can reproduce it firstly. >> >> >> A simple reproducer would be (on a system with ordinary swap (not zram)) >> >> 1) mmap a region (MAP_ANON|MAP_PRIVATE) that can hold a THP >> >> 2) Enable THP for that region (MADV_HUGEPAGE) >> >> 3) Populate a THP (e.g., write access) >> >> 4) PTE-map the THP, for example, using MADV_FREE on the last subpage >> >> 5) Trigger swapout of the THP, for example, using MADV_PAGEOUT > > Added the original THP swapout code author, Ying. > > At this step, the THP will be split, right? > > https://elixir.bootlin.com/linux/latest/source/mm/vmscan.c#L1786 > > Even if a THP has PMD mapping, IIRC, it is split in the add_to_swap() > then swapped out. But I cannot find that split code now. I recall there was some sequence to achieve it. Maybe it was swapping out the PMD first and not triggering a PTE-mapping first. mm/vmscan.c:shrink_folio_list() if (folio_test_large(folio)) { /* cannot split folio, skip it */ if (!can_split_folio(folio, NULL)) goto activate_locked; /* * Split folios without a PMD map right * away. Chances are some or all of the * tail pages can be freed without IO. */ if (!folio_entire_mapcount(folio) && split_folio_to_list(folio, folio_list)) goto activate_locked; } } So the sequence might have to be 1) mmap a region (MAP_ANON|MAP_PRIVATE) that can hold a THP 2) Enable THP for that region (MADV_HUGEPAGE) 3) Populate a THP (e.g., write access) 4) Trigger swapout of the THP, for example, using MADV_PAGEOUT 5) Access some subpage As we don't have PMD swap entries, we will PTE-map the THP during try_to_unmap() IIRC. Independent of that, the check we have here also doesn't consider ordinary order-0 pages that might be in the swapache. -- Thanks, David / dhildenb