From patchwork Thu May 13 21:23:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 12256631 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2A79C433B4 for ; Thu, 13 May 2021 21:23:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 41FAA613F2 for ; Thu, 13 May 2021 21:23:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 41FAA613F2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 64E0A6B0036; Thu, 13 May 2021 17:23:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 625426B006E; Thu, 13 May 2021 17:23:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C68B6B0070; Thu, 13 May 2021 17:23:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0146.hostedemail.com [216.40.44.146]) by kanga.kvack.org (Postfix) with ESMTP id 1D84A6B0036 for ; Thu, 13 May 2021 17:23:39 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id B3AC01802001C for ; Thu, 13 May 2021 21:23:38 +0000 (UTC) X-FDA: 78137484516.28.193EB40 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf17.hostedemail.com (Postfix) with ESMTP id E91DB40002CB for ; Thu, 13 May 2021 21:23:37 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id s20so15049798plr.13 for ; Thu, 13 May 2021 14:23:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zKjERDKMyhua0PjhqCNVM2rE/Zi4pz1d2TcF1K6coiw=; b=vcxAw4es3ae/p0vfEkPw30Wii9+5l8PzPxy1wKHBtSRJW/eMJ4Qzl5Dgp/r1vIpKSc YkPqk09yCEs5j2TwF5MT2QsxblvxARCT799z0pbhJZ6Ry1CsuZoRCZ150z1uOp3FXXg6 WEpmVlXKWbppkCJKn+DlwO7y4n8zFn71kRd/nNE2N5oW0tHB9/w2UvIAfOXIIUA/MdxI YglYroORejTaOQnAv269G7WoimW9L11myTKMZiBMxxw9udK53hQoPWMTikj0/0biq7Ak N53FMd/90Ug8R5PpMwNYSI8nlJZiP7LVIOVs0AppF596MGxevq/ecWRnqM5e0XDu5kFU YIbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zKjERDKMyhua0PjhqCNVM2rE/Zi4pz1d2TcF1K6coiw=; b=XNQzjdtgHiyZRFtab4NoRi0xklLJJAjmnNGawrARjdiY7JbkOElh1PhvvhJPWlrfPf lg+z9xqlhfKlVvyDOzIvNHLsmONx5yMZ57blhb/D1yTtzMrPVqN8xlv7M2B/IN02iNLt GelNx4Qg74Q8dHX7uSR8R4Em6qyhCFZ5Sf7+b/1bif9KkIDA5lK4Gzz514le+IYAFBmM ejKS1FqixMIe3uXwSAvCl1df5+GlKMNW9ayK4T/zzadgdrVZko67CPd6Rc9Szc/HH5VW /E/Lx61kU8nwgYJiiMxvX84O9FPrGZyjGRAWnN3Fbra64MSEFqyjxSQIWBvtMt7NmiTW s4VQ== X-Gm-Message-State: AOAM533tp2CaBWVC1soh11OYcu7K7ap5UAYyBuaqz0wIkfa5hGljPcHq S3r9N2dTzoJRa/oMfw1UsVc= X-Google-Smtp-Source: ABdhPJwFOU23ftSv4xH5XJzisA/8ym0azHja6b7g155adVUUvy2ifygY9HcMcOGq0R+JMRCgLQVEYg== X-Received: by 2002:a17:902:b104:b029:ee:beb3:ef0a with SMTP id q4-20020a170902b104b02900eebeb3ef0amr42241191plr.80.1620941017397; Thu, 13 May 2021 14:23:37 -0700 (PDT) Received: from localhost.localdomain (c-73-93-239-127.hsd1.ca.comcast.net. [73.93.239.127]) by smtp.gmail.com with ESMTPSA id b9sm145926pfo.107.2021.05.13.14.23.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 May 2021 14:23:36 -0700 (PDT) From: Yang Shi To: ziy@nvidia.com, kirill.shutemov@linux.intel.com, wangyugui@e16-tech.com, hughd@google.com, akpm@linux-foundation.org Cc: shy828301@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [v2 PATCH] mm: thp: check total_mapcount instead of page_mapcount Date: Thu, 13 May 2021 14:23:34 -0700 Message-Id: <20210513212334.217424-1-shy828301@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=vcxAw4es; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf17.hostedemail.com: domain of shy828301@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=shy828301@gmail.com X-Stat-Signature: ywtsoe5sd4pxuupjwphh3matgnjn39pz X-Rspamd-Queue-Id: E91DB40002CB X-Rspamd-Server: rspam02 X-HE-Tag: 1620941017-789133 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: When debugging the bug reported by Wang Yugui [1], try_to_unmap() may return false positive for PTE-mapped THP since page_mapcount() is used to check if the THP is unmapped, but it just checks compound mapount and head page's mapcount. If the THP is PTE-mapped and head page is not mapped, it may return false positive. Use total_mapcount() instead of page_mapcount() for try_to_unmap() and do so for the VM_BUG_ON_PAGE in split_huge_page_to_list as well. This changed the semantic of try_to_unmap(), but I don't see there is any usecase that expects try_to_unmap() just unmap one subpage of a huge page. So using page_mapcount() seems like a bug. [1] https://lore.kernel.org/linux-mm/20210412180659.B9E3.409509F4@e16-tech.com/ Signed-off-by: Yang Shi --- v2: Removed dead code and updated the comment of try_to_unmap() per Zi Yan. mm/huge_memory.c | 11 +---------- mm/rmap.c | 10 ++++++---- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 63ed6b25deaa..3b08b9ba1578 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2348,7 +2348,6 @@ static void unmap_page(struct page *page) ttu_flags |= TTU_SPLIT_FREEZE; unmap_success = try_to_unmap(page, ttu_flags); - VM_BUG_ON_PAGE(!unmap_success, page); } static void remap_page(struct page *page, unsigned int nr) @@ -2718,7 +2717,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) } unmap_page(head); - VM_BUG_ON_PAGE(compound_mapcount(head), head); + VM_BUG_ON_PAGE(total_mapcount(head), head); /* block interrupt reentry in xa_lock and spinlock */ local_irq_disable(); @@ -2758,14 +2757,6 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) __split_huge_page(page, list, end); ret = 0; } else { - if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) { - pr_alert("total_mapcount: %u, page_count(): %u\n", - mapcount, count); - if (PageTail(page)) - dump_page(head, NULL); - dump_page(page, "total_mapcount(head) > 0"); - BUG(); - } spin_unlock(&ds_queue->split_queue_lock); fail: if (mapping) xa_unlock(&mapping->i_pages); diff --git a/mm/rmap.c b/mm/rmap.c index 693a610e181d..f52825b1330d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1742,12 +1742,14 @@ static int page_not_mapped(struct page *page) } /** - * try_to_unmap - try to remove all page table mappings to a page - * @page: the page to get unmapped + * try_to_unmap - try to remove all page table mappings to a page and the + * compound page it belongs to + * @page: the page or the subpages of compound page to get unmapped * @flags: action and flags * * Tries to remove all the page table entries which are mapping this - * page, used in the pageout path. Caller must hold the page lock. + * page and the compound page it belongs to, used in the pageout path. + * Caller must hold the page lock. * * If unmap is successful, return true. Otherwise, false. */ @@ -1777,7 +1779,7 @@ bool try_to_unmap(struct page *page, enum ttu_flags flags) else rmap_walk(page, &rwc); - return !page_mapcount(page) ? true : false; + return !total_mapcount(page) ? true : false; } /**