From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A667A198E6F for ; Wed, 18 Dec 2024 09:26:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734514011; cv=none; b=MuylB7jfzfJK7CFgEt6yorhEgwU0kCAQ8Xm9yWsAT5XWWRUdlV/KIwVdxTL64AVvCnOy2kk3erYe3A23ZDM5g9IEO4VKuVDtZWVo79EusFL0/9rM6iDKMPbRd1gMNPXyvqzTnJsKU8Cv27+yygZNQACxvFoJMkUpocx7wRHDJcs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734514011; c=relaxed/simple; bh=d6QycgPu3R24rBXdgU0AlLdMMps/WArzkRenpMIsbOc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=OQnjE4UmnI2NWsQSuL3uxEMefY18tXxSnv+ER96s1us90ckEHWc3Hhbs9Pq6v+xzpvB3yEemvNZhzICl0fy71EzLTCncFbZsK80ZARKq6M3aIHDhk0QlDg3lhEZw53q4Ib9y0xhf5yVCGeA0HWlnItBdnb1aw7NoavOe6aGHaJA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D8D98FEC; Wed, 18 Dec 2024 01:27:16 -0800 (PST) Received: from [10.162.42.42] (K4MQJ0H1H2.blr.arm.com [10.162.42.42]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C5C7B3F7B4; Wed, 18 Dec 2024 01:26:38 -0800 (PST) Message-ID: <999a569d-9424-41b0-b0c8-d028aee2985f@arm.com> Date: Wed, 18 Dec 2024 14:56:35 +0530 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: [RFC PATCH 08/12] khugepaged: Abstract PMD-THP collapse To: Ryan Roberts , akpm@linux-foundation.org, david@redhat.com, willy@infradead.org, kirill.shutemov@linux.intel.com Cc: anshuman.khandual@arm.com, catalin.marinas@arm.com, cl@gentwo.org, vbabka@suse.cz, mhocko@suse.com, apopple@nvidia.com, dave.hansen@linux.intel.com, will@kernel.org, baohua@kernel.org, jack@suse.cz, srivatsa@csail.mit.edu, haowenchao22@gmail.com, hughd@google.com, aneesh.kumar@kernel.org, yang@os.amperecomputing.com, peterx@redhat.com, ioworker0@gmail.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, jglisse@google.com, surenb@google.com, vishal.moola@gmail.com, zokeefe@google.com, zhengqi.arch@bytedance.com, jhubbard@nvidia.com, 21cnbao@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20241216165105.56185-1-dev.jain@arm.com> <20241216165105.56185-9-dev.jain@arm.com> <097741c8-b2ea-4869-8792-f9663019137c@arm.com> Content-Language: en-US From: Dev Jain In-Reply-To: <097741c8-b2ea-4869-8792-f9663019137c@arm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 18/12/24 12:54 am, Ryan Roberts wrote: > On 16/12/2024 16:51, Dev Jain wrote: >> Abstract away taking the mmap_lock exclusively, copying page contents, and >> setting the PMD, into vma_collapse_anon_folio_pmd(). >> >> Signed-off-by: Dev Jain >> --- >> mm/khugepaged.c | 119 +++++++++++++++++++++++++++--------------------- >> 1 file changed, 66 insertions(+), 53 deletions(-) >> >> diff --git a/mm/khugepaged.c b/mm/khugepaged.c >> index 078794aa3335..88beebef773e 100644 >> --- a/mm/khugepaged.c >> +++ b/mm/khugepaged.c >> @@ -1111,58 +1111,17 @@ static int alloc_charge_folio(struct folio **foliop, struct mm_struct *mm, >> return SCAN_SUCCEED; >> } >> >> -static int collapse_huge_page(struct mm_struct *mm, unsigned long address, >> - int referenced, int unmapped, int order, >> - struct collapse_control *cc) >> +static int vma_collapse_anon_folio_pmd(struct mm_struct *mm, unsigned long address, >> + struct vm_area_struct *vma, struct collapse_control *cc, pmd_t *pmd, >> + struct folio *folio) >> { >> + struct mmu_notifier_range range; >> + spinlock_t *pmd_ptl, *pte_ptl; >> LIST_HEAD(compound_pagelist); >> - pmd_t *pmd, _pmd; >> - pte_t *pte; >> pgtable_t pgtable; >> - struct folio *folio; >> - spinlock_t *pmd_ptl, *pte_ptl; >> - int result = SCAN_FAIL; >> - struct vm_area_struct *vma; >> - struct mmu_notifier_range range; >> - >> - VM_BUG_ON(address & ~HPAGE_PMD_MASK); >> - >> - /* >> - * Before allocating the hugepage, release the mmap_lock read lock. >> - * The allocation can take potentially a long time if it involves >> - * sync compaction, and we do not need to hold the mmap_lock during >> - * that. We will recheck the vma after taking it again in write mode. >> - */ >> - mmap_read_unlock(mm); >> - >> - result = alloc_charge_folio(&folio, mm, order, cc); >> - if (result != SCAN_SUCCEED) >> - goto out_nolock; >> - >> - mmap_read_lock(mm); >> - result = hugepage_vma_revalidate(mm, address, true, &vma, order, cc); >> - if (result != SCAN_SUCCEED) { >> - mmap_read_unlock(mm); >> - goto out_nolock; >> - } >> - >> - result = find_pmd_or_thp_or_none(mm, address, &pmd); >> - if (result != SCAN_SUCCEED) { >> - mmap_read_unlock(mm); >> - goto out_nolock; >> - } >> - >> - if (unmapped) { >> - /* >> - * __collapse_huge_page_swapin will return with mmap_lock >> - * released when it fails. So we jump out_nolock directly in >> - * that case. Continuing to collapse causes inconsistency. >> - */ >> - result = __collapse_huge_page_swapin(mm, vma, address, pmd, >> - referenced, order); >> - if (result != SCAN_SUCCEED) >> - goto out_nolock; >> - } >> + int result; >> + pmd_t _pmd; >> + pte_t *pte; >> >> mmap_read_unlock(mm); >> /* >> @@ -1174,7 +1133,8 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, >> * mmap_lock. >> */ >> mmap_write_lock(mm); >> - result = hugepage_vma_revalidate(mm, address, true, &vma, order, cc); >> + >> + result = hugepage_vma_revalidate(mm, address, true, &vma, HPAGE_PMD_ORDER, cc); >> if (result != SCAN_SUCCEED) >> goto out_up_write; >> /* check if the pmd is still valid */ >> @@ -1206,7 +1166,7 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, >> pte = pte_offset_map_lock(mm, &_pmd, address, &pte_ptl); >> if (pte) { >> result = __collapse_huge_page_isolate(vma, address, pte, cc, >> - &compound_pagelist, order); >> + &compound_pagelist, HPAGE_PMD_ORDER); >> spin_unlock(pte_ptl); >> } else { >> result = SCAN_PMD_NULL; >> @@ -1262,11 +1222,64 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, >> deferred_split_folio(folio, false); >> spin_unlock(pmd_ptl); >> >> - folio = NULL; >> - >> result = SCAN_SUCCEED; >> out_up_write: >> mmap_write_unlock(mm); >> + return result; >> +} >> + >> +static int collapse_huge_page(struct mm_struct *mm, unsigned long address, >> + int referenced, int unmapped, int order, >> + struct collapse_control *cc) >> +{ >> + struct vm_area_struct *vma; >> + int result = SCAN_FAIL; >> + struct folio *folio; >> + pmd_t *pmd; >> + >> + /* >> + * Before allocating the hugepage, release the mmap_lock read lock. >> + * The allocation can take potentially a long time if it involves >> + * sync compaction, and we do not need to hold the mmap_lock during >> + * that. We will recheck the vma after taking it again in write mode. >> + */ >> + mmap_read_unlock(mm); >> + >> + result = alloc_charge_folio(&folio, mm, order, cc); >> + if (result != SCAN_SUCCEED) >> + goto out_nolock; >> + >> + mmap_read_lock(mm); >> + result = hugepage_vma_revalidate(mm, address, true, &vma, order, cc); >> + if (result != SCAN_SUCCEED) { >> + mmap_read_unlock(mm); >> + goto out_nolock; >> + } >> + >> + result = find_pmd_or_thp_or_none(mm, address, &pmd); >> + if (result != SCAN_SUCCEED) { >> + mmap_read_unlock(mm); >> + goto out_nolock; >> + } >> + >> + if (unmapped) { >> + /* >> + * __collapse_huge_page_swapin will return with mmap_lock >> + * released when it fails. So we jump out_nolock directly in >> + * that case. Continuing to collapse causes inconsistency. >> + */ >> + result = __collapse_huge_page_swapin(mm, vma, address, pmd, >> + referenced, order); >> + if (result != SCAN_SUCCEED) >> + goto out_nolock; >> + } >> + >> + if (order == HPAGE_PMD_ORDER) >> + result = vma_collapse_anon_folio_pmd(mm, address, vma, cc, pmd, folio); > I think the locking is broken here? collapse_huge_page() used to enter with the > mmamp read lock and exit without the lock held at all. After the change, this is > only true for order == HPAGE_PMD_ORDER. For other orders, you exit with the mmap > read lock still held. Perhaps: > > else > mmap_read_unlock(mm); I have completely messed up sequential building of patches :) I will take care next time. > >> + >> + if (result == SCAN_SUCCEED) >> + folio = NULL; >> + >> out_nolock: >> if (folio) >> folio_put(folio);