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.133.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 76B3514B080 for ; Sat, 21 Dec 2024 14:32:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734791541; cv=none; b=oek1/Plgfirw9icTJCkChE8xRaHLKYXYbUeUD98/zvMUHJmQGzf9lrITML13jhG5PSXlOJf9eO+vEnGI/zwXMCmOQ+xzX2+a4wq08JbOY5fteIep4aJAsymk8HqQUCkfdOeNoH1nThArnRTPrYMR+rZRmTuSYMvzbQwh2Cbqtt0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734791541; c=relaxed/simple; bh=V6aqePbPJihGnM15mCPSwijHPJigUDAJca9491+h02U=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=EY/vlc3id5/k45jaVhxQvEwv7mjecjqIY3H+X/sMPw5L/0x7c2XmcMHi0Z7eVkp0WiVttayGqCGX/mnYIhgVdecy7jbHdWfh3h3zKkFy5vJmb6DAqr9OL4HSogSK57nRx3Y3R79gP5UKcjNzZmH0LckTwSxZYd70wc38W+3hXDk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none 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=dbqBnGKv; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none 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="dbqBnGKv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734791537; 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:autocrypt:autocrypt; bh=glzaZ4GMQ7kx43PeLKg9UKk4Yr/KeQENxPVtTJNPfys=; b=dbqBnGKvMlyPuUnKTzZtu6INUwxYPVF+wwV24i42EabxoCIDJ8+xYr0unhRXUbbJJfWLah 0p5x3gyyt2qjGFs90Uoj8/vISJzqcgezJlzzbiWwm1N5wt29TYhHzNszRGNa4AH8r/ahxM j2x11tDPjEBfG063D+SPG4/w/bRL23g= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-314-5rSn3tOJOIOjohzQoETlWQ-1; Sat, 21 Dec 2024 09:32:15 -0500 X-MC-Unique: 5rSn3tOJOIOjohzQoETlWQ-1 X-Mimecast-MFC-AGG-ID: 5rSn3tOJOIOjohzQoETlWQ Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3862b364578so1965386f8f.1 for ; Sat, 21 Dec 2024 06:32:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734791534; x=1735396334; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=glzaZ4GMQ7kx43PeLKg9UKk4Yr/KeQENxPVtTJNPfys=; b=vIXbKm3Ta7ogU51cjgucp3kWEoQxn4mwcWh56Ct0CPujz5fOH9nN+TQ2x34k35Ya5b 8gxLeJyojhYBkH97/sgK8/Jw5B+QfudPkEsN1KWqTmeySIvtaIuRssIgQDneY1M6ghX3 OhZRLu45LnkbZMtgHMnUTxXkSuZ7+OzrSpt+p/I5KuThoTejTX0zPP47k2nTpmQVs//u hBy+Jj/PfGqbZbCfgA2Npc9MXBQRQeGVrLoyZqBRYXG1+7Nl2irejgSsK7RE6Cu0IIIs ytbrpwuNtKqdIzxY/m1rFc0ZLoD3eYgeyU7ls0uj/OD9sdYIOr7Vx8ZOIegCjcYgPzN8 +Q4g== X-Forwarded-Encrypted: i=1; AJvYcCWFy9l7mdEQ8gWlCp/UuhlACh9BlXT621oQ42fqCYoUJUT9zyQDkSf8lZiImPXXWoIXWxXuJmZOB/ZsffQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzY63QDEZ6u/iIMJ4HIQjUx2t2w77Sph6ugu+GTwQuD7IpamQgc PY2kR45znv1tzDeX68YvQNZRnQHsGQytdtMvUyXh2mV9sO8wMtIvozVmV6h0AHNbQfWSkXGwBM7 +9cNWNh/nznRoHtVjDV7ogLgyfW6LWzPly1QKLyiOVg0LQdqXXL8+M7plYkdr+w== X-Gm-Gg: ASbGnctXm960SxBBqnSAtemasChTPceshF41wZZKM81XyYH3sTnwSzbxNvS7Evmn+82 SZCXfClhPvT0d0R9VeQVdHGTl66Nv5aYabaRrc478Wnk2bbWB0qSNTJkMde7srPuS87c3YxFlqn xNNcgo6gy/77lA4d9K7qWBmGxlSf9RHiK9vyQce8hPTpQ8FX6JcICo/YPqW0r8m3+T1CPzrvByy H5F0B7Rg6bGLT2xVnSx0EnuMNpCnOTXVnjD4mJMuXYPkoek+/c5SEiDDoo2Z3eKfz2s1GDnov7S HdA9ZGDu413H6V95DIrvnApMCFj3qz36kjCoXYqdvXCWDwE2uG09jCOCM8MJr/ouJNMM7YvmXSI Utt4zkV0M X-Received: by 2002:a5d:5d09:0:b0:386:4570:ee3d with SMTP id ffacd0b85a97d-38a22a4c983mr5810359f8f.24.1734791534471; Sat, 21 Dec 2024 06:32:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCMDZk/93Kg60YQwF51fyaC60CAIZCXyZ7BqG62+zVeO+S8Cz54v+G8U7DmDs50F+Vm3RtSg== X-Received: by 2002:a5d:5d09:0:b0:386:4570:ee3d with SMTP id ffacd0b85a97d-38a22a4c983mr5810339f8f.24.1734791534022; Sat, 21 Dec 2024 06:32:14 -0800 (PST) Received: from ?IPV6:2003:cb:c70e:d000:4622:73e7:6184:8f0d? (p200300cbc70ed000462273e761848f0d.dip0.t-ipconnect.de. [2003:cb:c70e:d000:4622:73e7:6184:8f0d]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8ace0esm6517219f8f.106.2024.12.21.06.32.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 21 Dec 2024 06:32:12 -0800 (PST) Message-ID: <433fb64d-80e1-4d96-904e-10b51e40898d@redhat.com> Date: Sat, 21 Dec 2024 15:32:11 +0100 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: [PATCH] replace free hugepage folios after migration To: Ge Yang , 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, muchun.song@linux.dev, liuzixing@hygon.cn, Oscar Salvador , Michal Hocko References: <1734503588-16254-1-git-send-email-yangge1116@126.com> <0b41cc6b-5c93-408f-801f-edd9793cb979@redhat.com> <1241b567-88b6-462c-9088-8f72a45788b7@126.com> <333e584c-2688-4a3f-bc1f-2e84d5215005@126.com> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKo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AAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <333e584c-2688-4a3f-bc1f-2e84d5215005@126.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 21.12.24 13:04, Ge Yang wrote: > > > 在 2024/12/21 0:30, David Hildenbrand 写道: >> On 20.12.24 09:56, Ge Yang wrote: >>> >>> >>> 在 2024/12/20 0:40, David Hildenbrand 写道: >>>> On 18.12.24 07:33, yangge1116@126.com wrote: >>>>> From: yangge >>>> >>>> CCing Oscar, who worked on migrating these pages during memory offlining >>>> and alloc_contig_range(). >>>> >>>>> >>>>> My machine has 4 NUMA nodes, each equipped with 32GB of memory. I >>>>> have configured each NUMA node with 16GB of CMA and 16GB of in-use >>>>> hugetlb pages. The allocation of contiguous memory via the >>>>> cma_alloc() function can fail probabilistically. >>>>> >>>>> The cma_alloc() function may fail if it sees an in-use hugetlb page >>>>> within the allocation range, even if that page has already been >>>>> migrated. When in-use hugetlb pages are migrated, they may simply >>>>> be released back into the free hugepage pool instead of being >>>>> returned to the buddy system. This can cause the >>>>> test_pages_isolated() function check to fail, ultimately leading >>>>> to the failure of the cma_alloc() function: >>>>> cma_alloc() >>>>>       __alloc_contig_migrate_range() // migrate in-use hugepage >>>>>       test_pages_isolated() >>>>>           __test_page_isolated_in_pageblock() >>>>>                PageBuddy(page) // check if the page is in buddy >>>> >>>> I thought this would be working as expected, at least we tested it with >>>> alloc_contig_range / virtio-mem a while ago. >>>> >>>> On the memory_offlining path, we migrate hugetlb folios, but also >>>> dissolve any remaining free folios even if it means that we will going >>>> below the requested number of hugetlb pages in our pool. >>>> >>>> During alloc_contig_range(), we only migrate them, to then free them up >>>> after migration. >>>> >>>> Under which circumstances doe sit apply that "they may simply be >>>> released back into the free hugepage pool instead of being returned to >>>> the buddy system"? >>>> >>> >>> After migration, in-use hugetlb pages are only released back to the >>> hugetlb pool and are not returned to the buddy system. >> >> We had >> >> commit ae37c7ff79f1f030e28ec76c46ee032f8fd07607 >> Author: Oscar Salvador >> Date:   Tue May 4 18:35:29 2021 -0700 >> >>     mm: make alloc_contig_range handle in-use hugetlb pages >>     alloc_contig_range() will fail if it finds a HugeTLB page within the >>     range, without a chance to handle them.  Since HugeTLB pages can be >>     migrated as any LRU or Movable page, it does not make sense to bail >> out >>     without trying.  Enable the interface to recognize in-use HugeTLB >> pages so >>     we can migrate them, and have much better chances to succeed the call. >> >> >> And I am trying to figure out if it never worked correctly, or if >> something changed that broke it. >> >> >> In start_isolate_page_range()->isolate_migratepages_block(), we do the >> >>     ret = isolate_or_dissolve_huge_page(page, &cc->migratepages); >> >> to add these folios to the cc->migratepages list. >> >> In __alloc_contig_migrate_range(), we migrate the pages using >> migrate_pages(). >> >> >> After that, the src hugetlb folios should still be isolated? > Yes. > > But I'm >> getting >> confused when these pages get un-silated and putback to hugetlb/freed. >> > If the migration is successful, call folio_putback_active_hugetlb to > release the src hugetlb folios back to the free hugetlb pool. > > trace: > unmap_and_move_huge_page > folio_putback_active_hugetlb > folio_put > free_huge_folio > > alloc_contig_range_noprof > __alloc_contig_migrate_range > if (test_pages_isolated()) //to determine if hugetlb pages in buddy > isolate_freepages_range //grab isolated pages from freelists. > else > undo_isolate_page_range //undo isolate Ah, now I remember, thanks. So when we free an ordinary page, we put it onto the buddy isolate list, from where we can grab it later and nobody can allocate it in the meantime. In case of hugetlb, we simply free it back to hugetlb, from where it can likely even get allocated immediately again. I think that can actually happen in your proposal: the now-free page will get reallocated, for example for migrating the next folio. Or some concurrent system activity can simply allocate the now-free folio. Or am I missing something that prevents these now-free hugetlb folios from getting re-allocated after migration succeeded? Conceptually, I think we would want migration code in the case of alloc_contig_range() to allocate a new folio from the buddy, and to free the old one back to the buddy immediately, without ever allowing re-allocation of it. What needs to be handled is detecting that (a) we want to allocate a fresh hugetlb folio as migration target (b) if migration succeeds, we have to free the hugetlb folio back to the buddy (c) if migation fails, we have to free the allocated hugetlb foliio back to the buddy We could provide a custom alloc_migration_target that we pass to migrate_page to allocate a fresh hugetlb folio to handle (a). Using the put_new_folio callback we could handle (c). (b) would need some thought. Maybe we can also just mark the source folio as we isolate it, and enlighten migration+freeing code to handle it automatically? Hoping to get some feedback from hugetlb maintainers. -- Cheers, David / dhildenb