Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3021170rwb; Mon, 15 Aug 2022 16:18:23 -0700 (PDT) X-Google-Smtp-Source: AA6agR499k6O/f39CcMQAuByoHK9qBqYv1t3hjVip/ehew3IJ/oLgrGxpwMSaQqbXF1gHE860GGW X-Received: by 2002:a17:902:8683:b0:171:3114:7678 with SMTP id g3-20020a170902868300b0017131147678mr19144537plo.172.1660605503551; Mon, 15 Aug 2022 16:18:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605503; cv=none; d=google.com; s=arc-20160816; b=xgI+51n6CtBaLVPsbF/ttP00dQywnMZw3mk8xuKhH/nTLjTVVBxWsnykPCzOir7Qv9 ZAFDEq6Flf1vZqFJbrTUKgsdXTvUstfvD05lrM3ca7eLpRTg+AQsQpQbmHzml6ENhVkq 6vPyvg6bu9xxM7qXVlygsQqcGJwIAkbwlEuiORU+DUpDqkPKzxxmSJx5UNRa5uLbSFnG +rJgWHuUOCpnjtLS9niKjgLueXGiR7ER+IA564wdObFnKTx/3wHuDzPAhQK2vEV+wDLS J2KEOBxmBB4CQ1toGh8cxL6GfiKGjEUUqOjCoB2WSjPRN9zrjQ6Wvs6JvqcuVhEmc6e4 vG6A== 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=GLrJSjmO0K0BGwSDoPwuD4MTB0HMBP6AyNejF4Q6jX8=; b=c9Mr6rD2TOiVViihVMbDDvbwKAg37gQX8pxov8FBJl65DJO9PHt6SaMe2YI+nqFhND yJmTuV6Rzw11ck1YNToulXTDOLVHM5sVCDk+ruwY6DEcof9FfSao7swzdjqZvmKQf/bu PJ7Uzfa5jdsS3qKmI4y/Ob/YIL4uy9S2UsndlUDhU0vLA+/h7/sEr8YxyKorQNI0Puz+ 0oNfcOLRNy6ZPrsjIYX3ep8lvimYQ1+7ltAID7oHjZg7JtpkwJBWI9nLMuagNTlUw69R wzJys2j4a1nxP+h34SoP7QeUTmTXnzQpbAz1jCcb5QCcClhTkb5eDtxBy1Jnk2FySPBi 4NEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=QmunClIV; 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 h20-20020a056a001a5400b0052b7233120esi13313913pfv.56.2022.08.15.16.18.11; Mon, 15 Aug 2022 16:18:23 -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=QmunClIV; 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 S1349821AbiHOVsn (ORCPT + 99 others); Mon, 15 Aug 2022 17:48:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349290AbiHOVpJ (ORCPT ); Mon, 15 Aug 2022 17:45:09 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FCE8C9E91; Mon, 15 Aug 2022 12:30:02 -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 ams.source.kernel.org (Postfix) with ESMTPS id D7ED1B81062; Mon, 15 Aug 2022 19:30:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17FC2C433C1; Mon, 15 Aug 2022 19:29:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660591799; bh=kQSvD+paUr+cZU1TDKWmkofASbgYNIP6QBT9xrztMWE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QmunClIVxO2c+TZJ7bv9Aim/mwwqUoXZEMD9wIFki4qqoLXxiXHqb6y/2nSRX4VK8 0IUxH/peA1Ye93K5s02AUXiw1UbTjWL6EaA7yWOEbuj+lMuCq1AfPLxUlG23Vbdx+r GdTHDj0tkfzll2evXvGQfH8k1zwUpavf30V4KLrg= 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.18 0683/1095] mm/memremap: fix memunmap_pages() race with get_dev_pagemap() Date: Mon, 15 Aug 2022 20:01:22 +0200 Message-Id: <20220815180457.629161015@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@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 e11653fd348c..2c1130486d28 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