From patchwork Mon Jan 31 20:32:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 12731116 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 603AEC433FE for ; Mon, 31 Jan 2022 20:33:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE8116B00F6; Mon, 31 Jan 2022 15:33:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D636B6B00F8; Mon, 31 Jan 2022 15:33:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB83B6B00F9; Mon, 31 Jan 2022 15:33:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0070.hostedemail.com [216.40.44.70]) by kanga.kvack.org (Postfix) with ESMTP id A4C9F6B00F6 for ; Mon, 31 Jan 2022 15:33:00 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6597B181F0FE6 for ; Mon, 31 Jan 2022 20:33:00 +0000 (UTC) X-FDA: 79091731320.21.86B5CF8 Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) by imf15.hostedemail.com (Postfix) with ESMTP id 036A4A0006 for ; Mon, 31 Jan 2022 20:32:58 +0000 (UTC) Received: by mail-qv1-f44.google.com with SMTP id b4so9130141qvf.0 for ; Mon, 31 Jan 2022 12:32:58 -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=fF5QFjKZhDTZd5B31TRW94GY69vdDJby6v3gBBayCwY=; b=HMakM+xY1YLPhqv6PfRXrYF6lEKAz8ldAQlsUVG77GLVRPT4XgfUdXYBbDqjbHerrt xlAlPDccJTu54jM1IX3IhH9eY7bHfcLScHm8cpZdvEAtBUEe0OwdWTmm3OpsgrHQOxKc p9fJbv6S+USh2jyF9NwYK2PuMR4blz+hZQUSoTJxMiyTUL+opiBq+wcMSSz/d0GmBIHw F8lNPaRz+Uaj2G743xUJPbnHO5FCBhIIRR5PK9Mt7vGZv7uHbvALBbVPi2mD/ESWMX4Q 9JSwCrXHiq/NqgjmRJ945HfsESAs/4CAd03ax9ZYClBPRTsNg5k7euUEIDikB6zCMR8T Za3Q== 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=fF5QFjKZhDTZd5B31TRW94GY69vdDJby6v3gBBayCwY=; b=XGtjj2iYgQttlzpzJbMDVfi1a6a69x1zjRnVSdOip5sKTQ+FdHRr7kkRJ7STt+AHje LzaV+Uz1OVTQU73kbBWiGpEd9XWNn19eRx61q42SGed4OjVo5zWkFAixcesriUTaD/Kw xw0a8xodMaHyr6QxGeIdX9q0J0LIsNacaP6qGtQclguvhU75lIbiDMF5fiKFbetTfog4 ln6wXC7W6ODhhMdusUhIpwhfDRxPux+m7MShrLrgG/J61Yw5oIL5Sd9ZL5kfjc18hBZb seTWsqS3sJvM3UO6BOvwQysS/vYzw4Jke7Z3Njb/BHFXZxBnjoFLV7ms8SupkvTma0yF 7LVw== X-Gm-Message-State: AOAM533ndduNL9iYTHWRMauWm75fmcbljA/2fkAd5vcL/A3J5YyrP3R0 D//00jKYuYiwbw7rRcOURR6hiQ== X-Google-Smtp-Source: ABdhPJx9Y6qc+SG5iROabTkXGy2K2xhjBhgrJadrpqmMWIheHQtwTINxgKfqF3vSMeC0FLwOvq+rfw== X-Received: by 2002:a05:6214:5014:: with SMTP id jo20mr19417458qvb.28.1643661178443; Mon, 31 Jan 2022 12:32:58 -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 j14sm5349661qko.10.2022.01.31.12.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 12:32:57 -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 v5 3/4] mm/khugepaged: unify collapse pmd clear, flush and free Date: Mon, 31 Jan 2022 20:32:48 +0000 Message-Id: <20220131203249.2832273-4-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.35.0.rc2.247.g8bbb082509-goog In-Reply-To: <20220131203249.2832273-1-pasha.tatashin@soleen.com> References: <20220131203249.2832273-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 036A4A0006 X-Stat-Signature: bn1kofg9wmqu39apc5wrf9cb5eq3odoj Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=soleen.com header.s=google header.b=HMakM+xY; dmarc=none; spf=pass (imf15.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.219.44 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com X-Rspam-User: nil X-HE-Tag: 1643661178-925497 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 */