From patchwork Thu Jun 9 12:13:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12875468 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 06F50C43334 for ; Thu, 9 Jun 2022 12:13:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41B2F8D0008; Thu, 9 Jun 2022 08:13:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A3818D0006; Thu, 9 Jun 2022 08:13:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2451F8D0008; Thu, 9 Jun 2022 08:13:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0F2A08D0006 for ; Thu, 9 Jun 2022 08:13:04 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D35B660EBC for ; Thu, 9 Jun 2022 12:13:03 +0000 (UTC) X-FDA: 79558586646.17.C95F07B Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf08.hostedemail.com (Postfix) with ESMTP id 23618160068 for ; Thu, 9 Jun 2022 12:13:02 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LJjcD5BvbzgYbW; Thu, 9 Jun 2022 20:11:08 +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.2375.24; Thu, 9 Jun 2022 20:12:58 +0800 From: Miaohe Lin To: CC: , , , Subject: [PATCH] mm/memremap: fix memunmap_pages() race with get_dev_pagemap() Date: Thu, 9 Jun 2022 20:13:05 +0800 Message-ID: <20220609121305.2508-1-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654776783; a=rsa-sha256; cv=none; b=pBSc+FDKgzbph55jqc3vzPsG1dxl9RvOWEJFlK0qeW2E8xAR9cYmCrVqv3U1GP6FbqoZuy NyIDsmPTVg8gyUHeKk/rSv/Tn469u2DuqYnA9M1pcvCleTqQM/WTKFRh9AtuWlMwjQrmY4 bnE7W9P89jtoxWwrfizWgy4aOlthlYM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1654776783; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=ztPdJx8teJnBPjBiqhu0kJMVYQJ2939kkvyW2wLz504=; b=ByUuuyexlWMJKt8yohdDJo7yU7K8qnQCwH1U0uMj3jcZyQXZYDRrGVgJqccldZvuMcBuyw 1RuaIvMm4gVW6HwDNQY2TD4XnMun/X5pHPL2m+rtExhKbTMM7q1q2Nkp1UwTAxDSUYBVqv OU1BQvU1kFI7csYjwQOKsbJI1hYAXxE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf08.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf08.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Stat-Signature: rmi1j911h4hrrxkhaas3tzewwfa87zgm X-Rspamd-Queue-Id: 23618160068 X-Rspamd-Server: rspam12 X-Rspam-User: X-HE-Tag: 1654776782-467174 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: Think about the below scene: CPU1 CPU2 memunmap_pages percpu_ref_exit __percpu_ref_exit free_percpu(percpu_count); /* percpu_count is freed here! */ get_dev_pagemap xa_load(&pgmap_array, PHYS_PFN(phys)) /* pgmap still in the pgmap_array */ percpu_ref_tryget_live(&pgmap->ref) if __ref_is_percpu /* __PERCPU_REF_ATOMIC_DEAD not set yet */ this_cpu_inc(*percpu_count) /* access freed percpu_count here! */ ref->percpu_count_ptr = __PERCPU_REF_ATOMIC_DEAD; /* too late... */ pageunmap_range To fix the issue, do percpu_ref_exit() after pgmap_array is emptied. So we won't do percpu_ref_tryget_live() against a being freed percpu_ref. Fixes: b7b3c01b1915 ("mm/memremap_pages: support multiple ranges per invocation") Signed-off-by: Miaohe Lin --- mm/memremap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/memremap.c b/mm/memremap.c index 8a432cdfd9d4..f0955785150f 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -141,10 +141,10 @@ void memunmap_pages(struct dev_pagemap *pgmap) for (i = 0; i < pgmap->nr_range; i++) percpu_ref_put_many(&pgmap->ref, pfn_len(pgmap, i)); wait_for_completion(&pgmap->done); - percpu_ref_exit(&pgmap->ref); for (i = 0; i < pgmap->nr_range; i++) pageunmap_range(pgmap, i); + percpu_ref_exit(&pgmap->ref); WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n"); devmap_managed_enable_put(pgmap);