From patchwork Fri Dec 17 06:25:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Xinhai X-Patchwork-Id: 12683731 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 3C131C433EF for ; Fri, 17 Dec 2021 06:26:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ABB616B0071; Fri, 17 Dec 2021 01:26:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A6A646B0072; Fri, 17 Dec 2021 01:26:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 959206B0073; Fri, 17 Dec 2021 01:26:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0131.hostedemail.com [216.40.44.131]) by kanga.kvack.org (Postfix) with ESMTP id 87D4D6B0071 for ; Fri, 17 Dec 2021 01:26:20 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 47F2C180ACC20 for ; Fri, 17 Dec 2021 06:26:10 +0000 (UTC) X-FDA: 78926301300.26.45A8F14 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by imf11.hostedemail.com (Postfix) with ESMTP id DEE774001C for ; Fri, 17 Dec 2021 06:26:07 +0000 (UTC) Received: by mail-lf1-f44.google.com with SMTP id d10so2521785lfg.6 for ; Thu, 16 Dec 2021 22:26:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4JB3LrSFklxZUw3BwLMb46O/JsstqLDxIBD/kU4iR/4=; b=XTs9f/K3eLYUKUFyt2MdV+caWjPANm++ajrgyb6sCpVUMpDrrMIuF1Gt6uF7Kea4U+ fxYIK5J8VUqKXuoAulEJH7+Z27Lfo03aMDY8h24xI/d4T32CWB5hnpTa8untHX5hQZz4 g9yAgLWELCwpbBRqDjy5K8JIgHeWzQ5GVL/QAmRWOu/TP0FvVB6mASvsihiZ5hx3+o89 OLrpxAh+gf+31W2ya0wH2WzLTCFDS3mTkffmkKZS5NrV/ZlgX1Qieeiqb3Tb9IakbI6U FnFTwHLnDDqZBGs9in1feu6tM7rkpUs1lzjduDJlQK8AgDv3PU2nt/7+JrcHt6iHYI0M 5VJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4JB3LrSFklxZUw3BwLMb46O/JsstqLDxIBD/kU4iR/4=; b=k3w3VICb3YLdaE32HQcPvnLPWkt76lpVKXTvOPsCj6dzm5RDnLPDBfNJwKAUsbaw0n peEQNvGMuT05jmimdrBK+rcylywyYebXgNX7bl7WMhYkpvxdbaLGko1MgX7ocHeA2OwE Ai2Z7ry3OJ7GtFd226OD7yxWUAAxfHYLIB9TGG3TuiHN0+80CGPrvpLakL0zRSnu4Jdb c52dJ5LSpt5yNXKzi7chyrhQHKj6xlCmco+p+tdv3zKfydQb4eEHaHAHE+k9yz64Gxm5 lWmplSuouBVk/TSFrO1d2xQGY0CShOW3D4rS/S/d2EzWRGV3vSu7LXIuCCctg8GVbmi4 iemA== X-Gm-Message-State: AOAM531woUQUxwyX8uq0wsiwUPHSY/U2VmrVSc+y6+PAOfw4dO/zDii0 r7arkvs+mD203gvOnnWLgeZSGsh6p3I= X-Google-Smtp-Source: ABdhPJwtVpy+zFOTYm0VzvkYFHXJcCmEwQGODk7Tl7aQuR//Se5OWcr3uI1jchaCEHG7XMQ9/dYp/A== X-Received: by 2002:a05:6512:3f04:: with SMTP id y4mr1708686lfa.227.1639722368381; Thu, 16 Dec 2021 22:26:08 -0800 (PST) Received: from localhost.localdomain ([131.228.2.21]) by smtp.gmail.com with ESMTPSA id j21sm1550872lji.88.2021.12.16.22.26.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 22:26:08 -0800 (PST) From: Li Xinhai To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, Zi Yan , "Huang, Ying" , "Kirill A. Shutemov" Subject: [PATCH V4] mm/gup.c: stricter check on THP migration entry during follow_pmd_mask Date: Fri, 17 Dec 2021 14:25:59 +0800 Message-Id: <20211217062559.737063-1-lixinhai.lxh@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: DEE774001C X-Stat-Signature: p577b5r9ewq6t396fym6p7ny8onbwuh4 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="XTs9f/K3"; spf=pass (imf11.hostedemail.com: domain of lixinhai.lxh@gmail.com designates 209.85.167.44 as permitted sender) smtp.mailfrom=lixinhai.lxh@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1639722367-511332 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 BUG_ON check for THP migration entry, the existing code only check thp_migration_supported case, but not for !thp_migration_supported case. If !thp_migration_supported() and !pmd_present(), the original code may dead loop in theory. To make the BUG_ON check consistent, we need catch both cases. Move the BUG_ON check one step earlier, because if the bug happen we should know it instead of depend on FOLL_MIGRATION been used by caller. Because pmdval instead of *pmd is read by the is_pmd_migration_entry() check, the existing code don't help to avoid useless locking within pmd_migration_entry_wait(), so remove that check. Signed-off-by: Li Xinhai Cc: Zi Yan Cc: "Huang, Ying" Cc: "Kirill A. Shutemov" Reviewed-by: "Huang, Ying" Reviewed-by: Miaohe Lin --- V3->V4: Fix typos V2->V3: mention about the dead loop in commit message. V1->V2: Move the BUG_ON() check before if(!(flags & FOLL_MIGRATION)); and add comments for it. mm/gup.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 2c51e9748a6a..1b500ca2a1b8 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -642,12 +642,17 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma, } retry: if (!pmd_present(pmdval)) { + /* + * Should never reach here, if thp migration is not supported; + * Otherwise, it must be a thp migration entry. + */ + VM_BUG_ON(!thp_migration_supported() || + !is_pmd_migration_entry(pmdval)); + if (likely(!(flags & FOLL_MIGRATION))) return no_page_table(vma, flags); - VM_BUG_ON(thp_migration_supported() && - !is_pmd_migration_entry(pmdval)); - if (is_pmd_migration_entry(pmdval)) - pmd_migration_entry_wait(mm, pmd); + + pmd_migration_entry_wait(mm, pmd); pmdval = READ_ONCE(*pmd); /* * MADV_DONTNEED may convert the pmd to null because