Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1084146pxk; Fri, 25 Sep 2020 05:58:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw9qC+Ek6T2zZ0jntUHjnVd0C5bnrYoMalwpjJhnONMxESzOMhPU8KXJRn0tqP4VYobE9ke X-Received: by 2002:a17:906:3e0c:: with SMTP id k12mr2532690eji.189.1601038718752; Fri, 25 Sep 2020 05:58:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601038718; cv=none; d=google.com; s=arc-20160816; b=FrpGDQj/bSnUfcQMkNgnHw5PNdC8OfBmnL4wM3Yenrd8LzQVlp2fcodzNAmIeUyEYR mrAZlaAj1Qp51iSQlQFwp41qDo6H9y3brX4oURiA/h2dCIYR1fnsN3QUGf6x/JXNX8Gb OUvzH3pDdEuHbctBn8gJ+75su9ydgbObKpygHEeyGi0C7QLTfaDOltSQ1hiJ//uC9vXe Ij7ui5YJNhqa9b045AO6ImKmpexfWJhGAhfTVi27fYr5NTV5BXgKVcWGazMy6sdyp7jP MUXFoOJvUUlKvtaQsJL/WfHF3UrujLlS/FN+ADLOe7sXsDJHg8eCXHTVK9JCjBqU1Lxm Oxvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=8pJyQXoJyAL092vCTw+tbBbtj6Jqf/6ehmLF6Ln9q2k=; b=YOOk/TzfuPZRdoK1NDe/0u+Hk9/D1Krds3x669Ib2AD07XyzKGT2V0oG323pmsAhHT gQtvnhluH6hVTpfabDTUroTuvfHhjd0lx9VWlz2wc8iKykUBbB5+CdDYbnV8OMob0fiU Fh3mDxm6DgX53D/1DquwQB638ycAOhHDRo3b9yJxgpZ54NbimI3b3tK9trIDxWSR/KzI 9LQKdxQkFFYCtaAoZYlGeBe6A8QeZpkgvRXzdZ3LvB7CdJQl+pR2czocOpcOYWLj4Vlv viRV3j+Hlcpm6uStfw4o7ianUtZ1CuvQhphrScr4j8hvPeb0y6pam/7bS57WzAnTTkmd ygtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=z+wUp9Mv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n15si2006800edy.595.2020.09.25.05.58.15; Fri, 25 Sep 2020 05:58:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=z+wUp9Mv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728912AbgIYM4F (ORCPT + 99 others); Fri, 25 Sep 2020 08:56:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:59486 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729460AbgIYMxm (ORCPT ); Fri, 25 Sep 2020 08:53:42 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3DEFF2072E; Fri, 25 Sep 2020 12:53:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601038420; bh=ijm+fS2MNLDNyGJf/n1YBkhk3QwMXeiOI52TRRaygOs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z+wUp9Mvo5wySBu1h88IZ7WFKbBrXBSTI91ExL8RQHb3wZQ23W20eM/n7fal2tWAd 2d3fp9pZx7KtSed/SBtc1nTKqViCZT7qC137kIEtgX8L9l8qwPFCY5YHMEcwiU8dyd Is5wwfwsM+sIr4UhxfUdz0HZJB9f7mr2RJ9bzz64= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ralph Campbell , Andrew Morton , Yang Shi , Zi Yan , Jerome Glisse , John Hubbard , Alistair Popple , Christoph Hellwig , Jason Gunthorpe , Bharata B Rao , Ben Skeggs , Shuah Khan , Linus Torvalds , Sasha Levin Subject: [PATCH 4.19 04/37] mm/thp: fix __split_huge_pmd_locked() for migration PMD Date: Fri, 25 Sep 2020 14:48:32 +0200 Message-Id: <20200925124721.618613840@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200925124720.972208530@linuxfoundation.org> References: <20200925124720.972208530@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ralph Campbell [ Upstream commit ec0abae6dcdf7ef88607c869bf35a4b63ce1b370 ] A migrating transparent huge page has to already be unmapped. Otherwise, the page could be modified while it is being copied to a new page and data could be lost. The function __split_huge_pmd() checks for a PMD migration entry before calling __split_huge_pmd_locked() leading one to think that __split_huge_pmd_locked() can handle splitting a migrating PMD. However, the code always increments the page->_mapcount and adjusts the memory control group accounting assuming the page is mapped. Also, if the PMD entry is a migration PMD entry, the call to is_huge_zero_pmd(*pmd) is incorrect because it calls pmd_pfn(pmd) instead of migration_entry_to_pfn(pmd_to_swp_entry(pmd)). Fix these problems by checking for a PMD migration entry. Fixes: 84c3fc4e9c56 ("mm: thp: check pmd migration entry in common path") Signed-off-by: Ralph Campbell Signed-off-by: Andrew Morton Reviewed-by: Yang Shi Reviewed-by: Zi Yan Cc: Jerome Glisse Cc: John Hubbard Cc: Alistair Popple Cc: Christoph Hellwig Cc: Jason Gunthorpe Cc: Bharata B Rao Cc: Ben Skeggs Cc: Shuah Khan Cc: [4.14+] Link: https://lkml.kernel.org/r/20200903183140.19055-1-rcampbell@nvidia.com Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/huge_memory.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2145,7 +2145,7 @@ static void __split_huge_pmd_locked(stru put_page(page); add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR); return; - } else if (is_huge_zero_pmd(*pmd)) { + } else if (pmd_trans_huge(*pmd) && is_huge_zero_pmd(*pmd)) { /* * FIXME: Do we want to invalidate secondary mmu by calling * mmu_notifier_invalidate_range() see comments below inside @@ -2233,27 +2233,33 @@ static void __split_huge_pmd_locked(stru pte = pte_offset_map(&_pmd, addr); BUG_ON(!pte_none(*pte)); set_pte_at(mm, addr, pte, entry); - atomic_inc(&page[i]._mapcount); - pte_unmap(pte); - } - - /* - * Set PG_double_map before dropping compound_mapcount to avoid - * false-negative page_mapped(). - */ - if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) { - for (i = 0; i < HPAGE_PMD_NR; i++) + if (!pmd_migration) atomic_inc(&page[i]._mapcount); + pte_unmap(pte); } - if (atomic_add_negative(-1, compound_mapcount_ptr(page))) { - /* Last compound_mapcount is gone. */ - __dec_node_page_state(page, NR_ANON_THPS); - if (TestClearPageDoubleMap(page)) { - /* No need in mapcount reference anymore */ + if (!pmd_migration) { + /* + * Set PG_double_map before dropping compound_mapcount to avoid + * false-negative page_mapped(). + */ + if (compound_mapcount(page) > 1 && + !TestSetPageDoubleMap(page)) { for (i = 0; i < HPAGE_PMD_NR; i++) - atomic_dec(&page[i]._mapcount); + atomic_inc(&page[i]._mapcount); + } + + lock_page_memcg(page); + if (atomic_add_negative(-1, compound_mapcount_ptr(page))) { + /* Last compound_mapcount is gone. */ + __dec_lruvec_page_state(page, NR_ANON_THPS); + if (TestClearPageDoubleMap(page)) { + /* No need in mapcount reference anymore */ + for (i = 0; i < HPAGE_PMD_NR; i++) + atomic_dec(&page[i]._mapcount); + } } + unlock_page_memcg(page); } smp_wmb(); /* make pte visible before pmd */