Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1628294pxp; Thu, 17 Mar 2022 13:05:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyizCjnfFal2ARhcPe43lcmypot+AXrza0vZcESFaQkTuTezqdqeUDu9Mo3D5E+dAd5TpU3 X-Received: by 2002:a63:465b:0:b0:374:642c:ab62 with SMTP id v27-20020a63465b000000b00374642cab62mr5197536pgk.187.1647547506237; Thu, 17 Mar 2022 13:05:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647547506; cv=none; d=google.com; s=arc-20160816; b=flBL0RxJcIym0I1XuUygYfXYfA7t0nYfE9f1wuUpmgtRAd2Yq8jPFPzmc6BIrgxKXK Avl0zfUiiGlpObFShoXB+A9HFJKqKzB4HP61x2SGZOEGlmbF3+8AEcCfkgXiyokjAQ5y 6xZB374RzxawYOhLEmtrN2LASQE6xtrHgCRse4m4v3zX5Rpd205dWChVcniehfJL2SoM YOB9bJB9z97gwjDk3+bC/Z7qRsvY3F5sK8ZJfIncQx+OJh0rrGS6mjq/HCuEJQ025Ozd PGiSQANyhlyhZBlYUyQSLWxB0ibhKZzEidCyhJktwWDc1L3c7f+s7969/F4t7Bc7Ni/I FojQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=YTwJ2YuQgCH3eV9lJNZVeLKrO36JZsp1doSyE1xi1uo=; b=Xg7JD88V7+hUU5IsNn9pT1W/2j/Lt8Iz922iL7CI/bNnft3FlbI/cVoIM0Zx/wymvI pX0x3DkdJT4tvX4QyYd5VX8Dp9Scdbyl4jXFX7xl1vIaWmK5xqz60s7dOZl5HJB4bpVv JfMD+B97jDQHdhf2RVJR4mXRf6vpgmovVpKTogbP8xYOwex00pxzo5LLIMdB/DIUQpmr u9ewSJpexAvgP8oZZvfsB4IUUdBONwwziF5hoiFAfPSTdPk4jnb7JxcG4obo1ePfDXjz hkNbMnbSz61Z9OR4bLbaA6J6hCqvrQR5XGe9A+QlU8ePAfz/x+kGagz+fOex/U3I0BA1 Lt5Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id z2-20020a63b902000000b003816043f070si3105962pge.613.2022.03.17.13.05.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 13:05:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 490CB214F99; Thu, 17 Mar 2022 12:53:51 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234921AbiCQOnx (ORCPT + 99 others); Thu, 17 Mar 2022 10:43:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235423AbiCQOnM (ORCPT ); Thu, 17 Mar 2022 10:43:12 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE472200963 for ; Thu, 17 Mar 2022 07:41:55 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4KK8rX1f68z9shf; Thu, 17 Mar 2022 22:38:04 +0800 (CST) Received: from huawei.com (10.175.124.27) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Thu, 17 Mar 2022 22:41:53 +0800 From: Miaohe Lin To: CC: , , , , , , , Subject: [PATCH v2 11/11] mm/migration: fix possible do_pages_stat_array racing with memory offline Date: Fri, 18 Mar 2022 19:17:09 +0800 Message-ID: <20220318111709.60311-12-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20220318111709.60311-1-linmiaohe@huawei.com> References: <20220318111709.60311-1-linmiaohe@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Spam-Status: No, score=1.3 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_12_24, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Spam-Level: * When follow_page peeks a page, the page could be migrated and then be offlined while it's still being used by the do_pages_stat_array(). Use FOLL_GET to hold the page refcnt to fix this potential race. Signed-off-by: Miaohe Lin --- mm/migrate.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index dbd91fbdb127..cd85ba0ab592 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1807,13 +1807,18 @@ static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, goto set_status; /* FOLL_DUMP to ignore special (like zero) pages */ - page = follow_page(vma, addr, FOLL_DUMP); + page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); err = PTR_ERR(page); if (IS_ERR(page)) goto set_status; - err = page ? page_to_nid(page) : -ENOENT; + if (page) { + err = page_to_nid(page); + put_page(page); + } else { + err = -ENOENT; + } set_status: *status = err; -- 2.23.0