From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98AE82904 for ; Tue, 5 Aug 2025 05:19:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754371175; cv=none; b=MIfgO+J0IMiy3SMdKMZX9LQpjVdssj+rm3h9smSuyXliOFsMfmIdTHB8zQr0OWj/dmR/SZQVE9iC71vZIhB7e1ZAauX9hm4ArAczlFRY60T+/AQ6/xqYnKHcz/FfAlLv6j6TyV2YCkO/YKovmn4Io7gj+vEByj2BYrnkfoLOOec= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754371175; c=relaxed/simple; bh=RmNMPhi4cCI92F++nLv+tRJhUPDqRtXfZIt0Id2evKI=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=bGAzPf1rnCRnp5kimUXaE8glQ7LmT4cfL+tsb3LOH9vUIrf05R9dnDVRqZdkUX6LHNM6BVnVssWt6kIKamyxVB+1FBf+Obpvo8TyAFGSjy09r96i43vd8bXCeMfyFCEyqEm+x8fEpNjVwupps5SkYOELengNKJfF+R4yy8tUY5g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Och5M2j1; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Och5M2j1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1754371172; 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=LGLPKOHCE4WHIZo2zl9wNosGejx/J5pqoktUTOJ9DEQ=; b=Och5M2j1R2WrJNQ02Y++odj7+0myYeoLsta3iheLKWNBLw0wJKdhHmt7MecT1H8JPj8AVn AuxOaB42gxGvzt8GuFFCBAeImOHW0hW+QaERb+4z/UBep7IKtI3QVoPugp/Fun3zY/JqOC HAKG/2qp460pQh7WBdR1SOttT2ArtRI= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-187-Yb7CTsuFMeqCjh0fR6wd5w-1; Tue, 05 Aug 2025 01:19:30 -0400 X-MC-Unique: Yb7CTsuFMeqCjh0fR6wd5w-1 X-Mimecast-MFC-AGG-ID: Yb7CTsuFMeqCjh0fR6wd5w_1754371169 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-55ba1981389so1518671e87.2 for ; Mon, 04 Aug 2025 22:19:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754371169; x=1754975969; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LGLPKOHCE4WHIZo2zl9wNosGejx/J5pqoktUTOJ9DEQ=; b=kjGbdo0Bfbh3V2tYcyFjTBEDYuN/Hn2IB/ac6nU663QPmVwNrpTGwA60GkcBrrUwEh zCpNahUmFFnFN+AInDTOScckRAB9TUOCEOu7ZLjXqX22fe1V8urNZojLalLV19xDcSBJ 275OjnTyXJyFyVlqPUnbcCr9vm/+1MEKWao3MkA3nYLJ1ncfJVliKVbFERVlylLk1fEv mcDuOE4PmYPiw/0mFHzjvsDzlrLaN5TtBvCBJrG3oHvMaWczuZF8PUJ5lXidcZKi302p YKEC8nWT8ryzbhufo59i3MiNZSBuUQdSpcZYthjBdkzlv3qbQjUEzMeWu/+vUfydpekN f3Cw== X-Forwarded-Encrypted: i=1; AJvYcCXVF/idPX++8ZiTVsM7BVqjnFJs3QHm5It0VM9FjvvQ0JU0Z7Uiy89Iu8wBUQVylAlD2lTqe62jAh11MFA=@vger.kernel.org X-Gm-Message-State: AOJu0YzuIW/piAgxyfbD+eq0Ujkb5Cuelt+wBtlxiy6+XfULeYDiuXgW djQOMgihbZ8f7A9BFEQ6ItUilJGwzGNYbsjW8tc/KxwwPay2e4D9Y8KjjBy3ik+PbEFX+f3lCmJ SDDvilog8r5j6mgT46jl6QpJfDafNWatwQCk3xXEOR6jLwVfSmWWA9uoqlCm24oG2 X-Gm-Gg: ASbGncveDD4qdZbP5SW3BJOrkywurwORTtcXDmswOLrX32zfb3vPCFybc5txzma/L3Z K2p+m1DTvllIl6uWk9YTqSngiIgScqY0rnL7WMiO+1Lr8hdcI9B8oaEx75CIA/1gj3Ov2FJbpVn cfzQ6xwJ2d4WHxMK25FJQco2YqzAUH+nt+sPDcjvXaP+8pdP7RrG9uNhfcmpIkhtVpa3/M8XoE3 nV/Cv5JOnF1O8ZAaZp2ZPcknEMz7Q7ce7dfTzLSKCTfnV3Twt1MRsFJLciKCfTVlLD6eO+3GjVm H8/x5JgkyGuJ9NaMuM/nyG2OPoZQdRnmeNgyUUkaiZRGIAR46GMGfpd44FRl2cdAJQ== X-Received: by 2002:a05:6512:3991:b0:553:65bc:4232 with SMTP id 2adb3069b0e04-55b97b50600mr3043103e87.31.1754371169055; Mon, 04 Aug 2025 22:19:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7ovAchybYJl5BUGKvnbj7PL2Is1KUsQyv2BFYPbPag/hcXmOub4Fc8vBEeILkJIX7hJjvYQ== X-Received: by 2002:a05:6512:3991:b0:553:65bc:4232 with SMTP id 2adb3069b0e04-55b97b50600mr3043080e87.31.1754371168498; Mon, 04 Aug 2025 22:19:28 -0700 (PDT) Received: from [192.168.1.86] (85-23-48-6.bb.dnainternet.fi. [85.23.48.6]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-55b8898bdcfsm1813347e87.32.2025.08.04.22.19.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Aug 2025 22:19:27 -0700 (PDT) Message-ID: <4fa4f448-ebef-47dd-ba99-6ef6e5862fda@redhat.com> Date: Tue, 5 Aug 2025 08:19:27 +0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code From: =?UTF-8?Q?Mika_Penttil=C3=A4?= To: Balbir Singh , Zi Yan Cc: David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Karol Herbst , Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Shuah Khan , Barry Song , Baolin Wang , Ryan Roberts , Matthew Wilcox , Peter Xu , Kefeng Wang , Jane Chu , Alistair Popple , Donet Tom , Matthew Brost , Francois Dugast , Ralph Campbell References: <20250730092139.3890844-1-balbirs@nvidia.com> <11ee9c5e-3e74-4858-bf8d-94daf1530314@redhat.com> <14aeaecc-c394-41bf-ae30-24537eb299d9@nvidia.com> <71c736e9-eb77-4e8e-bd6a-965a1bbcbaa8@nvidia.com> <47BC6D8B-7A78-4F2F-9D16-07D6C88C3661@nvidia.com> <2406521e-f5be-474e-b653-e5ad38a1d7de@redhat.com> <920a4f98-a925-4bd6-ad2e-ae842f2f3d94@redhat.com> <196f11f8-1661-40d2-b6b7-64958efd8b3b@redhat.com> <087e40e6-3b3f-4a02-8270-7e6cfdb56a04@redhat.com> Content-Language: en-US In-Reply-To: <087e40e6-3b3f-4a02-8270-7e6cfdb56a04@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 8/5/25 07:24, Mika Penttilä wrote: > Hi, > > On 8/5/25 07:10, Balbir Singh wrote: >> On 8/5/25 09:26, Mika Penttilä wrote: >>> Hi, >>> >>> On 8/5/25 01:46, Balbir Singh wrote: >>>> On 8/2/25 22:13, Mika Penttilä wrote: >>>>> Hi, >>>>> >>>>> On 8/2/25 13:37, Balbir Singh wrote: >>>>>> FYI: >>>>>> >>>>>> I have the following patch on top of my series that seems to make it work >>>>>> without requiring the helper to split device private folios >>>>>> >>>>> I think this looks much better! >>>>> >>>> Thanks! >>>> >>>>>> Signed-off-by: Balbir Singh >>>>>> --- >>>>>> include/linux/huge_mm.h | 1 - >>>>>> lib/test_hmm.c | 11 +++++- >>>>>> mm/huge_memory.c | 76 ++++------------------------------------- >>>>>> mm/migrate_device.c | 51 +++++++++++++++++++++++++++ >>>>>> 4 files changed, 67 insertions(+), 72 deletions(-) >>>>>> >>>>>> diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h >>>>>> index 19e7e3b7c2b7..52d8b435950b 100644 >>>>>> --- a/include/linux/huge_mm.h >>>>>> +++ b/include/linux/huge_mm.h >>>>>> @@ -343,7 +343,6 @@ unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long add >>>>>> vm_flags_t vm_flags); >>>>>> >>>>>> bool can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins); >>>>>> -int split_device_private_folio(struct folio *folio); >>>>>> int __split_huge_page_to_list_to_order(struct page *page, struct list_head *list, >>>>>> unsigned int new_order, bool unmapped); >>>>>> int min_order_for_split(struct folio *folio); >>>>>> diff --git a/lib/test_hmm.c b/lib/test_hmm.c >>>>>> index 341ae2af44ec..444477785882 100644 >>>>>> --- a/lib/test_hmm.c >>>>>> +++ b/lib/test_hmm.c >>>>>> @@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf) >>>>>> * the mirror but here we use it to hold the page for the simulated >>>>>> * device memory and that page holds the pointer to the mirror. >>>>>> */ >>>>>> - rpage = vmf->page->zone_device_data; >>>>>> + rpage = folio_page(page_folio(vmf->page), 0)->zone_device_data; >>>>>> dmirror = rpage->zone_device_data; >>>>>> >>>>>> /* FIXME demonstrate how we can adjust migrate range */ >>>>>> order = folio_order(page_folio(vmf->page)); >>>>>> nr = 1 << order; >>>>>> >>>>>> + /* >>>>>> + * When folios are partially mapped, we can't rely on the folio >>>>>> + * order of vmf->page as the folio might not be fully split yet >>>>>> + */ >>>>>> + if (vmf->pte) { >>>>>> + order = 0; >>>>>> + nr = 1; >>>>>> + } >>>>>> + >>>>>> /* >>>>>> * Consider a per-cpu cache of src and dst pfns, but with >>>>>> * large number of cpus that might not scale well. >>>>>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >>>>>> index 1fc1efa219c8..863393dec1f1 100644 >>>>>> --- a/mm/huge_memory.c >>>>>> +++ b/mm/huge_memory.c >>>>>> @@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, >>>>>> struct shrink_control *sc); >>>>>> static unsigned long deferred_split_scan(struct shrinker *shrink, >>>>>> struct shrink_control *sc); >>>>>> -static int __split_unmapped_folio(struct folio *folio, int new_order, >>>>>> - struct page *split_at, struct xa_state *xas, >>>>>> - struct address_space *mapping, bool uniform_split); >>>>>> - >>>>>> static bool split_underused_thp = true; >>>>>> >>>>>> static atomic_t huge_zero_refcount; >>>>>> @@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, >>>>>> pmd_populate(mm, pmd, pgtable); >>>>>> } >>>>>> >>>>>> -/** >>>>>> - * split_huge_device_private_folio - split a huge device private folio into >>>>>> - * smaller pages (of order 0), currently used by migrate_device logic to >>>>>> - * split folios for pages that are partially mapped >>>>>> - * >>>>>> - * @folio: the folio to split >>>>>> - * >>>>>> - * The caller has to hold the folio_lock and a reference via folio_get >>>>>> - */ >>>>>> -int split_device_private_folio(struct folio *folio) >>>>>> -{ >>>>>> - struct folio *end_folio = folio_next(folio); >>>>>> - struct folio *new_folio; >>>>>> - int ret = 0; >>>>>> - >>>>>> - /* >>>>>> - * Split the folio now. In the case of device >>>>>> - * private pages, this path is executed when >>>>>> - * the pmd is split and since freeze is not true >>>>>> - * it is likely the folio will be deferred_split. >>>>>> - * >>>>>> - * With device private pages, deferred splits of >>>>>> - * folios should be handled here to prevent partial >>>>>> - * unmaps from causing issues later on in migration >>>>>> - * and fault handling flows. >>>>>> - */ >>>>>> - folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio)); >>>>>> - ret = __split_unmapped_folio(folio, 0, &folio->page, NULL, NULL, true); >>>>>> - VM_WARN_ON(ret); >>>>>> - for (new_folio = folio_next(folio); new_folio != end_folio; >>>>>> - new_folio = folio_next(new_folio)) { >>>>>> - zone_device_private_split_cb(folio, new_folio); >>>>>> - folio_ref_unfreeze(new_folio, 1 + folio_expected_ref_count( >>>>>> - new_folio)); >>>>>> - } >>>>>> - >>>>>> - /* >>>>>> - * Mark the end of the folio split for device private THP >>>>>> - * split >>>>>> - */ >>>>>> - zone_device_private_split_cb(folio, NULL); >>>>>> - folio_ref_unfreeze(folio, 1 + folio_expected_ref_count(folio)); >>>>>> - return ret; >>>>>> -} >>>>>> - >>>>>> static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, >>>>>> unsigned long haddr, bool freeze) >>>>>> { >>>>>> @@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, >>>>>> freeze = false; >>>>>> if (!freeze) { >>>>>> rmap_t rmap_flags = RMAP_NONE; >>>>>> - unsigned long addr = haddr; >>>>>> - struct folio *new_folio; >>>>>> - struct folio *end_folio = folio_next(folio); >>>>>> >>>>>> if (anon_exclusive) >>>>>> rmap_flags |= RMAP_EXCLUSIVE; >>>>>> >>>>>> - folio_lock(folio); >>>>>> - folio_get(folio); >>>>>> - >>>>>> - split_device_private_folio(folio); >>>>>> - >>>>>> - for (new_folio = folio_next(folio); >>>>>> - new_folio != end_folio; >>>>>> - new_folio = folio_next(new_folio)) { >>>>>> - addr += PAGE_SIZE; >>>>>> - folio_unlock(new_folio); >>>>>> - folio_add_anon_rmap_ptes(new_folio, >>>>>> - &new_folio->page, 1, >>>>>> - vma, addr, rmap_flags); >>>>>> - } >>>>>> - folio_unlock(folio); >>>>>> - folio_add_anon_rmap_ptes(folio, &folio->page, >>>>>> - 1, vma, haddr, rmap_flags); >>>>>> + folio_ref_add(folio, HPAGE_PMD_NR - 1); >>>>>> + if (anon_exclusive) >>>>>> + rmap_flags |= RMAP_EXCLUSIVE; >>>>>> + folio_add_anon_rmap_ptes(folio, page, HPAGE_PMD_NR, >>>>>> + vma, haddr, rmap_flags); >>>>>> } >>>>>> } >>>>>> >>>>>> @@ -4065,7 +4001,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order, >>>>>> if (nr_shmem_dropped) >>>>>> shmem_uncharge(mapping->host, nr_shmem_dropped); >>>>>> >>>>>> - if (!ret && is_anon) >>>>>> + if (!ret && is_anon && !folio_is_device_private(folio)) >>>>>> remap_flags = RMP_USE_SHARED_ZEROPAGE; >>>>>> >>>>>> remap_page(folio, 1 << order, remap_flags); >>>>>> diff --git a/mm/migrate_device.c b/mm/migrate_device.c >>>>>> index 49962ea19109..4264c0290d08 100644 >>>>>> --- a/mm/migrate_device.c >>>>>> +++ b/mm/migrate_device.c >>>>>> @@ -248,6 +248,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, >>>>>> * page table entry. Other special swap entries are not >>>>>> * migratable, and we ignore regular swapped page. >>>>>> */ >>>>>> + struct folio *folio; >>>>>> + >>>>>> entry = pte_to_swp_entry(pte); >>>>>> if (!is_device_private_entry(entry)) >>>>>> goto next; >>>>>> @@ -259,6 +261,55 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, >>>>>> pgmap->owner != migrate->pgmap_owner) >>>>>> goto next; >>>>>> >>>>>> + folio = page_folio(page); >>>>>> + if (folio_test_large(folio)) { >>>>>> + struct folio *new_folio; >>>>>> + struct folio *new_fault_folio; >>>>>> + >>>>>> + /* >>>>>> + * The reason for finding pmd present with a >>>>>> + * device private pte and a large folio for the >>>>>> + * pte is partial unmaps. Split the folio now >>>>>> + * for the migration to be handled correctly >>>>>> + */ >>>>>> + pte_unmap_unlock(ptep, ptl); >>>>>> + >>>>>> + folio_get(folio); >>>>>> + if (folio != fault_folio) >>>>>> + folio_lock(folio); >>>>>> + if (split_folio(folio)) { >>>>>> + if (folio != fault_folio) >>>>>> + folio_unlock(folio); >>>>>> + ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl); >>>>>> + goto next; >>>>>> + } >>>>>> + >>>>> The nouveau migrate_to_ram handler needs adjustment also if split happens. >>>>> >>>> test_hmm needs adjustment because of the way the backup folios are setup. >>> nouveau should check the folio order after the possible split happens. >>> >> You mean the folio_split callback? > no, nouveau_dmem_migrate_to_ram(): > .. > sfolio = page_folio(vmf->page); > order = folio_order(sfolio); > ... > migrate_vma_setup() > .. > if sfolio is split order still reflects the pre-split order > >>>>>> + /* >>>>>> + * After the split, get back the extra reference >>>>>> + * on the fault_page, this reference is checked during >>>>>> + * folio_migrate_mapping() >>>>>> + */ >>>>>> + if (migrate->fault_page) { >>>>>> + new_fault_folio = page_folio(migrate->fault_page); >>>>>> + folio_get(new_fault_folio); >>>>>> + } >>>>>> + >>>>>> + new_folio = page_folio(page); >>>>>> + pfn = page_to_pfn(page); >>>>>> + >>>>>> + /* >>>>>> + * Ensure the lock is held on the correct >>>>>> + * folio after the split >>>>>> + */ >>>>>> + if (folio != new_folio) { >>>>>> + folio_unlock(folio); >>>>>> + folio_lock(new_folio); >>>>>> + } >>>>> Maybe careful not to unlock fault_page ? >>>>> >>>> split_page will unlock everything but the original folio, the code takes the lock >>>> on the folio corresponding to the new folio >>> I mean do_swap_page() unlocks folio of fault_page and expects it to remain locked. >>> >> Not sure I follow what you're trying to elaborate on here > Actually fault_folio should be fine but should we have: if (fault_folio) if(folio != new_folio)) { folio_unlock(folio); folio_lock(new_folio); } else folio_unlock(folio); >> Balbir >> > --Mika >