From patchwork Wed Jan 26 18:36:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 12725597 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 5A282C28CF5 for ; Wed, 26 Jan 2022 18:36:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3E5B6B0075; Wed, 26 Jan 2022 13:36:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DEFBF6B008A; Wed, 26 Jan 2022 13:36:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C91DB6B008C; Wed, 26 Jan 2022 13:36:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0157.hostedemail.com [216.40.44.157]) by kanga.kvack.org (Postfix) with ESMTP id A8FFD6B0075 for ; Wed, 26 Jan 2022 13:36:43 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 67C328F6E4 for ; Wed, 26 Jan 2022 18:36:43 +0000 (UTC) X-FDA: 79073294286.08.B6E064B Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) by imf21.hostedemail.com (Postfix) with ESMTP id 051B71C0026 for ; Wed, 26 Jan 2022 18:36:42 +0000 (UTC) Received: by mail-qk1-f173.google.com with SMTP id 71so400579qkf.4 for ; Wed, 26 Jan 2022 10:36:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=a93X1YuUUYjeQ1DmHZzgEs8n16C54TBZM8wi1s2vhvo=; b=N1Vt6B9Sta1FwZczZQvEpySLcRnZhDydeiS6pntiC+WThA+3ZreI3+60RQfO5kdCVX PYbT9ygJ+RpShOQSUmBUF4LeD0OMOmi4BVXaeieVPEDI7qjBopBvxedBObpiCvVEXM19 vIaReulPyOxwwY/oUq54wlge34q8x/ppQVF/brbp0pf/bFE+mP23rJDsUoy6LY1nNdvs h56xvgdY/V+epLYxhiPz25bNyIF3I1Jo2UFm/dTmOhdMEIvwyXt9q4boHCTlKTfyDPb/ FQBTIf+mHIo+LKv2ZXUV+XiQw9lfKjQr1nPDLZz4qcagM/2eqA8M54EazwRtE/p3p7qW J1lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a93X1YuUUYjeQ1DmHZzgEs8n16C54TBZM8wi1s2vhvo=; b=PLAOIe4BK7572Wn0q9dPm81ipVdAOqThBmnHpAMnPmrQksaakRYfVIoE0l4a+LN5BX ZiGCLSaJ0+nYh2U5E+0IhIu4ufHE6r9mrIWMrt4XTgrnpEFUoxpoAfxwrjyxu8/3KG3v 1gtPshG7y3V7WEO9wiWets1sDpcPezKo1fvF+/6b6zVLG3OKKA4kZ03R2lJCo+x4nsEq 9g3VfYbgcaCUtKy8RQasecK5LkYcA4v7WNYupk4v1G2SPQfkoA9gEQi/Z9Yx1QDDfPk4 oANOv7b7DKSxsRVdohu7MgVbv9T4OamNtLvPEPxnso9GIbzbY0mufnJHLDxfGev9iuCu H73Q== X-Gm-Message-State: AOAM532mhXdeIRKy+3TRFxWTujgaSob6Iqr9qWLxvN5BLoyBjT+f1ZSB 9pzOsG1wSOLWoFz4I1St8s0DaQ== X-Google-Smtp-Source: ABdhPJw0PTloyF5Qbm9D+I2W7WmpXBIhuugeFvsdlL3/szCTDEds6C8kR7dEnSFl+43JC4D5IJPKGg== X-Received: by 2002:a05:620a:31a3:: with SMTP id bi35mr100511qkb.198.1643222202238; Wed, 26 Jan 2022 10:36:42 -0800 (PST) Received: from soleen.c.googlers.com.com (189.216.85.34.bc.googleusercontent.com. [34.85.216.189]) by smtp.gmail.com with ESMTPSA id h9sm26226qkp.75.2022.01.26.10.36.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 10:36:41 -0800 (PST) From: Pasha Tatashin To: pasha.tatashin@soleen.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, rientjes@google.com, pjt@google.com, weixugc@google.com, gthelen@google.com, mingo@redhat.com, will@kernel.org, rppt@kernel.org, dave.hansen@linux.intel.com, hpa@zytor.com, aneesh.kumar@linux.ibm.com, jirislaby@kernel.org, songmuchun@bytedance.com, qydwhotmail@gmail.com, hughd@google.com, ziy@nvidia.com, anshuman.khandual@arm.com Subject: [PATCH v4 3/4] mm/khugepaged: unify collapse pmd clear, flush and free Date: Wed, 26 Jan 2022 18:36:36 +0000 Message-Id: <20220126183637.1840960-4-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog In-Reply-To: <20220126183637.1840960-1-pasha.tatashin@soleen.com> References: <20220126183637.1840960-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 051B71C0026 X-Stat-Signature: 9mrs3dthdwuzrawt5tqr5tu5r59etg68 Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=N1Vt6B9S; dmarc=none; spf=pass (imf21.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.222.173 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com X-Rspam-User: nil X-HE-Tag: 1643222202-203579 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: Unify the code that flushes, clears pmd entry, and frees the PTE table level into a new function collapse_and_free_pmd(). This clean-up is useful as in the next patch we will add another call to this function to iterate through PTE prior to freeing the level for page table check. Signed-off-by: Pasha Tatashin Acked-by: David Rientjes --- mm/khugepaged.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 35f14d0a00a6..30e59e4af272 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1416,6 +1416,19 @@ static int khugepaged_add_pte_mapped_thp(struct mm_struct *mm, return 0; } +static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *vma, + unsigned long addr, pmd_t *pmdp) +{ + spinlock_t *ptl; + pmd_t pmd; + + ptl = pmd_lock(vma->vm_mm, pmdp); + pmd = pmdp_collapse_flush(vma, addr, pmdp); + spin_unlock(ptl); + mm_dec_nr_ptes(mm); + pte_free(mm, pmd_pgtable(pmd)); +} + /** * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at * address haddr. @@ -1433,7 +1446,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) struct vm_area_struct *vma = find_vma(mm, haddr); struct page *hpage; pte_t *start_pte, *pte; - pmd_t *pmd, _pmd; + pmd_t *pmd; spinlock_t *ptl; int count = 0; int i; @@ -1509,12 +1522,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) } /* step 4: collapse pmd */ - ptl = pmd_lock(vma->vm_mm, pmd); - _pmd = pmdp_collapse_flush(vma, haddr, pmd); - spin_unlock(ptl); - mm_dec_nr_ptes(mm); - pte_free(mm, pmd_pgtable(_pmd)); - + collapse_and_free_pmd(mm, vma, haddr, pmd); drop_hpage: unlock_page(hpage); put_page(hpage); @@ -1552,7 +1560,7 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) struct vm_area_struct *vma; struct mm_struct *mm; unsigned long addr; - pmd_t *pmd, _pmd; + pmd_t *pmd; i_mmap_lock_write(mapping); vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { @@ -1591,14 +1599,8 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) * reverse order. Trylock is a way to avoid deadlock. */ if (mmap_write_trylock(mm)) { - if (!khugepaged_test_exit(mm)) { - spinlock_t *ptl = pmd_lock(mm, pmd); - /* assume page table is clear */ - _pmd = pmdp_collapse_flush(vma, addr, pmd); - spin_unlock(ptl); - mm_dec_nr_ptes(mm); - pte_free(mm, pmd_pgtable(_pmd)); - } + if (!khugepaged_test_exit(mm)) + collapse_and_free_pmd(mm, vma, addr, pmd); mmap_write_unlock(mm); } else { /* Try again later */