Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2885868rwb; Mon, 15 Aug 2022 13:18:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR7DvD8SMlCz7hqy7jzvjOl5vr2QJezKpiPpa5qSypD6YLGcjTy8bVdqqmC7lnMHmP9HRo4C X-Received: by 2002:a05:6402:329b:b0:43d:bd4b:2f08 with SMTP id f27-20020a056402329b00b0043dbd4b2f08mr16019769eda.244.1660594709617; Mon, 15 Aug 2022 13:18:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660594709; cv=none; d=google.com; s=arc-20160816; b=uhGk+Liyfnp9F2R43EG4t2lyHXnZsBlmQjS5xrvIKttynn+Fo6iwFI0PuJY1kLwwNV O3p2qXB3k3DiG6Qoasyvbgt8q1wUEQV9RlI+EdC7d2cyX+eDhWjk6bggDj25b7M/+CYw H9cHdACH6Pw3ZrENjv0WzGLIh8gFVfRKRmlTVrg88VDgLHxYD1BSRw58/FIYcXaMExYs 1n4Sl4XE2q4LFoy35a430oEzu3k+4I5AFn8Krdrhz3mkD8whxe9LK6ZhGFlwW3KeU4eM dhJMz4JSN2lpv7rkD3zaPWbEtAZyvogjeUrLSTabIiZx46iXbKlD7idvhbYumqPybwXN flCQ== 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=mC3kJaA7C1dqKbxOG/ttQX1y9FqWu1O9C7zYYVMPiGM=; b=Jed8vJwHXB39l7EuJnBrwyvrYB0NXZPW3bJIPi3AUZoP3bmIxcs8qkwY6xsvd93RRU b+zE+a6rJiS+aJtwZfr0hKzoyFKLKEbHqHkNsRx0gFZBFrZ+hjiatt66CHfpoNGvBKQF 1Jz9gn0ueFmjb6e9RplHAj5nm1G/tztoYskG3Sn/RazsVqzWPaEzJrXU5YiUd5VUt22p YD68FEywFR9C/toIpfXM9xVip2U2ub5LSD/tFBtFkGGLHe1f/OVnxwfpjjpqZ0MUsVDS SxDv+4Repxc/evb3txjJbQcyndgCIUU1Z8C5uycvH4IrJ32200J0ZXKLlfOmCQth5/YQ nfGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uiBZJgsS; 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 x3-20020aa7d6c3000000b0043c141aaddfsi7841301edr.146.2022.08.15.13.18.04; Mon, 15 Aug 2022 13:18:29 -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=uiBZJgsS; 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 S1343943AbiHOTOe (ORCPT + 99 others); Mon, 15 Aug 2022 15:14:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343947AbiHOTLY (ORCPT ); Mon, 15 Aug 2022 15:11:24 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD33C4F1BE; Mon, 15 Aug 2022 11:37:00 -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 DA16861120; Mon, 15 Aug 2022 18:36:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4FCAC433C1; Mon, 15 Aug 2022 18:36:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660588619; bh=ok122iydLCawAKzOmxzxOc64Y/ddZl/jPVv7UsppXHY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uiBZJgsSClJ1ehsujimquuyrTAJFGfa3nPZUwbNcnM8+E7k6HgDLgicnfKsIXbmPf SkAp41tE1ep02eCnAMubjUAXkrq3ccqQSYLlNSW+l4ZyoAPqsPcnoG4jyNLy9cy9JL T9GBTJTVQpRY8YlSRJRXaFVtgk7BK7zWRJH3HBqQ= 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.15 431/779] mm/memremap: fix memunmap_pages() race with get_dev_pagemap() Date: Mon, 15 Aug 2022 20:01:15 +0200 Message-Id: <20220815180355.705093723@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@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 a638a27d89f5..8d743cbc2964 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -148,10 +148,10 @@ void memunmap_pages(struct dev_pagemap *pgmap) for_each_device_pfn(pfn, pgmap, i) put_page(pfn_to_page(pfn)); 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