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 3D56EC43217 for ; Thu, 24 Nov 2022 14:09:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7658F6B0072; Thu, 24 Nov 2022 09:09:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6EF336B0073; Thu, 24 Nov 2022 09:09:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 569576B0074; Thu, 24 Nov 2022 09:09:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4182F6B0072 for ; Thu, 24 Nov 2022 09:09:47 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 078221C5E67 for ; Thu, 24 Nov 2022 14:09:47 +0000 (UTC) X-FDA: 80168519214.22.1C3E181 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf22.hostedemail.com (Postfix) with ESMTP id 44F46C0019 for ; Thu, 24 Nov 2022 14:09:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669298984; 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=NJ+D/KTl6NhD/RFPiBvQ0Vau+fFQwZX75ajP08DKdEQ=; b=EDKL+fFAzXTXlEiTQYuuT4g0EV9XPsn6ZoLMBk1GfffO3eQYX0L+2KvReEPncmB1tiAPbF 8lJslOic63a9EYwJMItSiVhpjZ7SP99MpEACGAbQkm155oOuaXJo0yRPsGv18Gpgsmr8sf UIDcd/CtmbreoGLzrxjiF2xry8xE3zs= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-116-q-mIjjazMrSPXk_wsvvZWQ-1; Thu, 24 Nov 2022 09:09:35 -0500 X-MC-Unique: q-mIjjazMrSPXk_wsvvZWQ-1 Received: by mail-wm1-f71.google.com with SMTP id az40-20020a05600c602800b003cfa26c40easo2898171wmb.1 for ; Thu, 24 Nov 2022 06:09:35 -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:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NJ+D/KTl6NhD/RFPiBvQ0Vau+fFQwZX75ajP08DKdEQ=; b=J8mumsJlmKLxMznklbMig2Vu7mrNqlATbVRk7DEdb3ttZJ4Az5DJf38bHBEdfDO9Bm YlfboGBXyGCurrgDnhsPASe+UJjPreI9TnOeMFEMO2ZdJBaIr6lwmXr9n0p6v8YbAZ/b lnHZNVTjdrE+pM552t8+wNIaQkSqHxZSOoHuzP/aEZnCtg4cds7hrgKannGoXOnAORHK usaioqCitnybyk0erNFz3bAD5BLEadRbPFkXzkkG+Nm45gbYFpzg7Mjd54BmVOrKpvU7 fvJG1v9kL1W2x5oBiMybYBY4S8vPEwrtnKiAdaTgej1fi+nKuKq2TH3hPrI6HHTi74HK yaHg== X-Gm-Message-State: ANoB5pmFgHDlS2w9dP/Mxq3RhLijlO/60Tb+i9oO3D68SHMPcqmeY7WO SeKo7cKS/145mWycPl77D1ukAxKG0E0aGzIkCeAF79FptbweklGCwp93QOjcGUdro9dDcMj3vag tD5E8p17VWNY= X-Received: by 2002:adf:d844:0:b0:241:be45:54eb with SMTP id k4-20020adfd844000000b00241be4554ebmr17582608wrl.49.1669298974536; Thu, 24 Nov 2022 06:09:34 -0800 (PST) X-Google-Smtp-Source: AA0mqf77OtRG/YLhx6Vkq6egrXPnulFii95ZIm3sNBSeMylQ39e+7TkHrW908yWtjaOxFDw2vaYSoQ== X-Received: by 2002:adf:d844:0:b0:241:be45:54eb with SMTP id k4-20020adfd844000000b00241be4554ebmr17582585wrl.49.1669298974185; Thu, 24 Nov 2022 06:09:34 -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 p16-20020a05600c1d9000b003b3307fb98fsm1914066wms.24.2022.11.24.06.09.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Nov 2022 06:09:33 -0800 (PST) Message-ID: <31947f33-cd9e-adbb-2dcc-106a464438df@redhat.com> Date: Thu, 24 Nov 2022 15:09:32 +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 From: David Hildenbrand To: Gavin Shan , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, william.kucharski@oracle.com, ziy@nvidia.com, kirill.shutemov@linux.intel.com, zhenyzha@redhat.com, apopple@nvidia.com, hughd@google.com, willy@infradead.org, shan.gavin@gmail.com 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> <759a17cf-e234-2601-bf42-7a40a4d89466@redhat.com> Organization: Red Hat In-Reply-To: <759a17cf-e234-2601-bf42-7a40a4d89466@redhat.com> 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-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EDKL+fFA; spf=pass (imf22.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669298985; a=rsa-sha256; cv=none; b=RhD8V8ZABAlDZjbZ379feLKSseS7GqAYA+KOdUZK9d9XGhQVE448Rvg/agPtAHOKJV6Vhd b272nzr3khY4sacmluPaQNj04pmjvgf14yRNYyKOl41haEZ2ZhaFBW0O5frjob0iH5uwIh jJmsuv8eyeZSXVlhhaXXMr/6QD5j+Sg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669298985; 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=NJ+D/KTl6NhD/RFPiBvQ0Vau+fFQwZX75ajP08DKdEQ=; b=p5iVdtZvByQ0UbC833kqzeXHwmh+80WCbI0za35+VRR5yg2LkPudauxV3GK77vlIQKJWul VTkn+ponacUIK8AQRS8nwK4SFQS2VUOo+s9r1pf+9fopFll9So9YTcmh+wg6u97gIzwByj NwuJSsgeYnrkIXi+FW1zstx3I4vT3GA= X-Stat-Signature: asu73mzq7ui7m8mhyaggu56wyi3r4fqs X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 44F46C0019 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=EDKL+fFA; spf=pass (imf22.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-HE-Tag: 1669298985-263593 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 14:22, David Hildenbrand wrote: > On 24.11.22 13:55, Gavin Shan wrote: >> On 11/24/22 6:43 PM, 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 >>> >>> 6) Read-access to some subpages to fault them in from the swapcache >>> >>> >>> Now you'd have a THP, which >>> >>> 1) Is partially PTE-mapped into the page table >>> 2) Is in the swapcache (each subpage should have one reference from the swapache) >>> >>> >>> Now we could test, if alloc_contig_range() will still succeed (e.g., using virtio-mem). >>> >> >> Thanks for the details. Step (4) and (5) can be actually combined. To swap part of >> the THP (e.g. one sub-page) will force the THP to be split. >> >> I followed your steps in the attached program, there is no issue to do memory hot-remove >> through virtio-mem with or without this patch. > > Interesting. But I don't really see how we could pass this check with a > page that's in the swapcache, maybe I'm missing something else. > > I'll try to see if I can reproduce it. > After some unsuccessful attempts and many head-scratches, I realized that it's quite simple why we don't have to worry about swapcache pages here: page_mapping() is != NULL for pages in the swapcache: folio_mapping() makes this rather obvious: if (unlikely(folio_test_swapcache(folio)) return swap_address_space(folio_swap_entry(folio)); I think the get_page_unless_zero() might also be a fix for the page_mapping() call, smells like something could blow up on concurrent page freeing. (what about concurrent removal from the swapcache? nobody knows :) ) Thanks Gavin! Acked-by: David Hildenbrand -- Thanks, David / dhildenb