Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp95868iof; Sun, 5 Jun 2022 22:13:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwPvact1Ql4/ogfxFD7GfgjpANwjZsDTUNuL1AOD5aCxbICefRiauqvpVXTbTk2/oc8FXv4 X-Received: by 2002:a05:6a00:1902:b0:4fa:fa9e:42e6 with SMTP id y2-20020a056a00190200b004fafa9e42e6mr50710268pfi.1.1654492424299; Sun, 05 Jun 2022 22:13:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654492424; cv=none; d=google.com; s=arc-20160816; b=ArPFFZ4bLjLbEeRih0dEWQvRaNIdcPG91S/+J9rP8FW7tDzKwxJhsNbbpukBOLM8ex hnX/4NiKSuu4K7KdAV4gu2sdOij9ivQuNNwoc/3Yda/xlKly5XYTLdOktY7LYgcpHBQc Z2g2lJ3VEWXvl7HOaaL9ySg60hkiu1EnkLqY/9iT/Co529FRySzT8whdJujraExkKjth kX36NnNKje+HT+cotVDEDtEAH/Nc2NGwO22GiSWZzfiwbtMomu8cybz0nSGbwSOOXIgn lI/x++T7Gh+9wnsQcfC0HH3tGHuZu3pDWh7bxJD0rfcZGXa8I1jjjsnF2PxcrWLP5PUs 8rCA== 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=dqSdHWwqsAI/jJXKHSY48do/Dbebg7hG7MExVC0ema4=; b=ZFOWq+6IsZ5bcCV2Y7+y9nF2ulUvvYadfNpjhFyXY010JDxm7Z3nCovCMVLv9V7mpn khL8sDXaCQKhF0SwPHMGW8Tb/xOTCWXwC6xKjRdbl7/SpyVUsUIMM9ffDRyg4ndrtuXe PWNJzuy8A9h/fxymwoAEdmfbv80G3OAjfyowb5KTIAMUOtYvMP2ulwFTfr0WCJ6CK0xb w8aUqxvO0R/v380YY9+m8fUYdyiJJ+kyr2gszLHZv2Da58qU0/0b+n10C/BaDkO1F6U1 mr9KbtgTmk4urmYtNxd3oSHFiJsqSflPSqHficmgbuoBTtpNtBAwNxQyFqokmBMw2x/E o9vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CI+ZXyKW; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id u13-20020a170903124d00b00158d6f07b2esi19889730plh.85.2022.06.05.22.13.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 22:13:44 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CI+ZXyKW; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id ABA7015A75C; Sun, 5 Jun 2022 21:19:30 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345834AbiFCR4X (ORCPT + 99 others); Fri, 3 Jun 2022 13:56:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346325AbiFCRvE (ORCPT ); Fri, 3 Jun 2022 13:51:04 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C218D54020; Fri, 3 Jun 2022 10:47:32 -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 7DFD3B823B0; Fri, 3 Jun 2022 17:47:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C78AC3411C; Fri, 3 Jun 2022 17:47:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654278450; bh=V/yGTt7Ocp4ajWdl5FHnoH8c4RKdCJlTJGqL+no4af0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CI+ZXyKWUfXJaEliAU14xeyRbAkYLjq8qibbJ/E/j2NrdPSH3cLawwhnafVlhBeiJ RN5FYfyacyPY3Ho7KjBivf9IKihRNPCirA73GufBelTjS2+QVlrmYyoTSFrJ1gyLBH dZmfkP1Gb85cSenX5daB44m8oJ+BxHhBZRw+qR6s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sultan Alsawaf , Minchan Kim , Nitin Gupta , Sergey Senozhatsky , Andrew Morton Subject: [PATCH 5.10 37/53] zsmalloc: fix races between asynchronous zspage free and page migration Date: Fri, 3 Jun 2022 19:43:22 +0200 Message-Id: <20220603173819.801075996@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173818.716010877@linuxfoundation.org> References: <20220603173818.716010877@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Sultan Alsawaf commit 2505a981114dcb715f8977b8433f7540854851d8 upstream. The asynchronous zspage free worker tries to lock a zspage's entire page list without defending against page migration. Since pages which haven't yet been locked can concurrently migrate off the zspage page list while lock_zspage() churns away, lock_zspage() can suffer from a few different lethal races. It can lock a page which no longer belongs to the zspage and unsafely dereference page_private(), it can unsafely dereference a torn pointer to the next page (since there's a data race), and it can observe a spurious NULL pointer to the next page and thus not lock all of the zspage's pages (since a single page migration will reconstruct the entire page list, and create_page_chain() unconditionally zeroes out each list pointer in the process). Fix the races by using migrate_read_lock() in lock_zspage() to synchronize with page migration. Link: https://lkml.kernel.org/r/20220509024703.243847-1-sultan@kerneltoast.com Fixes: 77ff465799c602 ("zsmalloc: zs_page_migrate: skip unnecessary loops but not return -EBUSY if zspage is not inuse") Signed-off-by: Sultan Alsawaf Acked-by: Minchan Kim Cc: Nitin Gupta Cc: Sergey Senozhatsky Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/zsmalloc.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1748,11 +1748,40 @@ static enum fullness_group putback_zspag */ static void lock_zspage(struct zspage *zspage) { - struct page *page = get_first_page(zspage); + struct page *curr_page, *page; - do { - lock_page(page); - } while ((page = get_next_page(page)) != NULL); + /* + * Pages we haven't locked yet can be migrated off the list while we're + * trying to lock them, so we need to be careful and only attempt to + * lock each page under migrate_read_lock(). Otherwise, the page we lock + * may no longer belong to the zspage. This means that we may wait for + * the wrong page to unlock, so we must take a reference to the page + * prior to waiting for it to unlock outside migrate_read_lock(). + */ + while (1) { + migrate_read_lock(zspage); + page = get_first_page(zspage); + if (trylock_page(page)) + break; + get_page(page); + migrate_read_unlock(zspage); + wait_on_page_locked(page); + put_page(page); + } + + curr_page = page; + while ((page = get_next_page(curr_page))) { + if (trylock_page(page)) { + curr_page = page; + } else { + get_page(page); + migrate_read_unlock(zspage); + wait_on_page_locked(page); + put_page(page); + migrate_read_lock(zspage); + } + } + migrate_read_unlock(zspage); } static int zs_init_fs_context(struct fs_context *fc)