Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp5862325iob; Tue, 10 May 2022 05:34:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1LZ4COqYHlGttM1smNdVcrdh/AyWd1NS4naHOQaRiEhVArsqlTD+bOZ3BwpdIsIcCcwh5 X-Received: by 2002:a17:907:1b05:b0:6f0:18d8:7be0 with SMTP id mp5-20020a1709071b0500b006f018d87be0mr19348049ejc.561.1652186081122; Tue, 10 May 2022 05:34:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652186081; cv=none; d=google.com; s=arc-20160816; b=x0G/TZjgiPk2eNiijPcNoNaS3y1CWl14pQAbLynT88rizCfI/iFUtiWL2+leRlv/Gu rDS6K2apaZ/tROH1vCcDnHo/EA0gsujkSi6ZV8Uqj0dt5YYWZhyKjPCxI0lMRblb47Hp z91vA6MQ55w6JmxcGE7pVVG1mYrj0E4My6pk56V6c5KS3XhxKjlFYqF1vM7v+W1QscYa gmB8nn4eI+8akV3gyYQrgaDhgpZD9ts3yzHhKHfQq+RjngNdw/HtOv1qvqntyiltJcXP HB5pGXK5IXQVkZqLKlBETeSlPqVK1i/SmPsqUw9XZzr0bxJg9BNWPR5zXSDW3a/dtnfP OYaA== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=0XoYNphikwLrOKci2Ff2y65NedCUkLd0Z6G17leMlxI=; b=QAFAReFDlI3f/Xyua25dOdNNmidd8QQTGuotkWm5VQunPFXaKikt9pE2e8KlEUTelI Fr1PK82UIa6IODfHcoITaaVkLMN/35LX1BbyM/bQXF4fG9PV+/Op3q1xPH28e0n0XirW RuZHJSRzyZQjTUPMuL5Je4rdpOm2klkyMpHvBia1qEmDbKuObTOkLpT+f937RNUbNbRC 24qQQvVDlvPwqguLqlNGAW+a4HecgV6js6Oct7NXlKUGCnGBOMUPzxrM1zpUm7kkLXWR 8QbWpyVaMwbVqAxl0MMEDl+Dcr1wRCEM2iynUSyd5Czs0YYrkUsw4wEKfEERHm3i1Yxz H+1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=BPrOIvDl; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k9-20020a170906158900b006f44d992450si15592718ejd.11.2022.05.10.05.34.12; Tue, 10 May 2022 05:34: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=@linux-foundation.org header.s=korg header.b=BPrOIvDl; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233335AbiEJAKd (ORCPT + 99 others); Mon, 9 May 2022 20:10:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232139AbiEJAKc (ORCPT ); Mon, 9 May 2022 20:10:32 -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 4DF419FDF; Mon, 9 May 2022 17:06:36 -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 E5D1AB819B0; Tue, 10 May 2022 00:06:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5DD17C385C5; Tue, 10 May 2022 00:06:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1652141193; bh=DgAVSJugWswR9uGU+ofLa7fkVHUE/3d5Oo1amCmtuNs=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=BPrOIvDlxOMKUg1sOv2e2PoJnlDv3eWLxx0sGSl0HsM6FQEouAPObXr1iCN5PWqP1 xlwCdWAvnwhnBHmbuDq0MYeyEHoryRYw9h4oCjbAPN3GbIYIrhZTIs8+9eMuRi1XSm gLuN3VXFFEVIHvkw4926kwehsfY2sHummU76ogwc= Date: Mon, 9 May 2022 17:06:32 -0700 From: Andrew Morton To: Sultan Alsawaf Cc: stable@vger.kernel.org, Minchan Kim , Nitin Gupta , Sergey Senozhatsky , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] zsmalloc: Fix races between asynchronous zspage free and page migration Message-Id: <20220509170632.fec2f56ad9f640329330b9de@linux-foundation.org> In-Reply-To: <20220509024703.243847-1-sultan@kerneltoast.com> References: <20220509024703.243847-1-sultan@kerneltoast.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 On Sun, 8 May 2022 19:47:02 -0700 Sultan Alsawaf wrote: > From: Sultan Alsawaf > > 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. > > --- a/mm/zsmalloc.c > +++ b/mm/zsmalloc.c > @@ -1718,11 +1718,40 @@ static enum fullness_group putback_zspage(struct size_class *class, > */ > 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); Why not simply lock_page() here? The get_page() alone won't protect from all the dire consequences which you have identified? > + 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); ditto. > + put_page(page); > + migrate_read_lock(zspage); > + } > + } > + migrate_read_unlock(zspage); > } > > static int zs_init_fs_context(struct fs_context *fc)