Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3091120rwb; Mon, 15 Aug 2022 17:54:42 -0700 (PDT) X-Google-Smtp-Source: AA6agR6YFnXii8lQLXH9mlAeWrJfOq6wUiK5Ej84mUGWwyFO0UeIMvsph1wz+h9spuPjPWOkL/dq X-Received: by 2002:a17:907:2c47:b0:730:8bbb:69a8 with SMTP id hf7-20020a1709072c4700b007308bbb69a8mr12319652ejc.38.1660611281914; Mon, 15 Aug 2022 17:54:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660611281; cv=none; d=google.com; s=arc-20160816; b=O8i81a4e92OdmS/Kv0l1Dnx7yH1jcDVq4G7l6PaO/4CZrMChfGLR/Czr0PsvLJPrMh rjyapGwtgLjoGfWnviguwBLbwHo8t35kuTHAJq2a0YyMlGiAUu1k3sO2+p4qsJdnPtbK yqhs6yPdz2DHevMajXZrFwAULJX+XxfjaimbaBz9eIwim4iYPL3BBqBH4Xi7sWIhJj5k xyqxUM+tPlSoZPZNSnvbQ4gf3oF3hKKiRYlw3bdxo92l54+NZZc1+ILypyf0LeSR8j6Q tDq4hEC7BxIzM1xpHJTVgm7GksZqAiGMF0Kd8h2xeeGhVLvfOKSjM9t3ZY3S5TXqkJ+i RCMg== 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=N+9oma4ooC25svrg7dCUXJFMs/PYL16fLqCCeLtdRnc=; b=F/FbRSztvCJb1q5l+2DlYbGVHiAdFg9bCdEBobLCy4bnlm9Js1jD/4v3wYCq0B9ORT Pu9ciWN39B8gg22eLE5VF/NrHP7YSQC85ojVTwzLg0VvuvIe6Xa8hqHC9wPxaC9JNgeq mWUd7E38mZkmSgup+Ih0bvoZ23KOWB1VNdn7gQ7f0vV+aCTWGOEc/AAss0PpOHn4m2yF WcNjaibGfre4A91NcwfTwIDkQi+UaUlM/zdOrNAdwxd5lU20GUyDcHNtQ0+2aCSxEP08 nJ4e5EOhgsPFS62Qtr5L6HFeXl/SNwcs/r5PtFmdhUolzvkYMcRcf2wEWqwwjt8rYUu2 wXuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=v1sQlsaM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l11-20020a170906794b00b00730f07ead67si9911262ejo.929.2022.08.15.17.54.16; Mon, 15 Aug 2022 17:54:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=v1sQlsaM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344945AbiHPAWs (ORCPT + 99 others); Mon, 15 Aug 2022 20:22:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353110AbiHPAQC (ORCPT ); Mon, 15 Aug 2022 20:16:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6651178C18; Mon, 15 Aug 2022 13:30:28 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9634261057; Mon, 15 Aug 2022 20:30:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8468BC433C1; Mon, 15 Aug 2022 20:30:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660595427; bh=BbXz+ox2h5kTAn0erYbR5JWQLTX37Bno+Z1CFv8e5lc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=v1sQlsaMnqG8ZsaL7hFK7pZGqQ/YXc+os0l8QSTtBwEheOwrZyzbj4UIPbUUmYrRD N9CrUzL0zuqYFXOYzBH3wkvgdAjV6a+8nJWnodBPGykfh/EI65ASdp/dgELajq4Pux pSVi4U0khSYmE37FycnBmywhNI+TRrJYRclQnX0U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Miaohe Lin , Dan Williams , Andrew Morton , Sasha Levin Subject: [PATCH 5.19 0728/1157] mm/memremap: fix memunmap_pages() race with get_dev_pagemap() Date: Mon, 15 Aug 2022 20:01:24 +0200 Message-Id: <20220815180508.590799365@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Miaohe Lin [ Upstream commit 1e57ffb6e3fd9583268c6462c4e3853575b21701 ] 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. Link: https://lkml.kernel.org/r/20220609121305.2508-1-linmiaohe@huawei.com Fixes: b7b3c01b1915 ("mm/memremap_pages: support multiple ranges per invocation") Signed-off-by: Miaohe Lin Cc: Dan Williams Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- mm/memremap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/memremap.c b/mm/memremap.c index 745eea0f99c3..2bdb66854832 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); -- 2.35.1