From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42588C433EF for ; Wed, 11 May 2022 21:07:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B10206B0074; Wed, 11 May 2022 17:07:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A97BF6B0075; Wed, 11 May 2022 17:07:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EAEC6B0078; Wed, 11 May 2022 17:07:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 77A036B0074 for ; Wed, 11 May 2022 17:07:23 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 431C761E41 for ; Wed, 11 May 2022 21:07:23 +0000 (UTC) X-FDA: 79454697966.10.A16FBDF Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf29.hostedemail.com (Postfix) with ESMTP id D5056120003 for ; Wed, 11 May 2022 21:07:15 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id d22so3037411plr.9 for ; Wed, 11 May 2022 14:07:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=b6r4iHXphce3qNrnt+WRxrpdsuktB8YXEoXsfGUyHyI=; b=d4IUXwD6gNj+uMj33TgTBflRj0rCzQNCBSP2h/Dsw1Iic+6tmFza6E33WjBeogUxoZ AXIYPxoDF965KU6BDZsGazc/wZ4G/Xp0tjVwjWHxKvKOSk8krMWdCH3KZmJ0zX1z8lJf kqh/21TsYPw21f4ZKwa9Cl3uxGSMGvlmNBquUpHI64w58P2SZ8BEnXTSeBmJbBuVziKM 8VnV7k8BAdJUdtCI7BNzD4gGGjiid1w5DE14hFWu4z/nipkb4+MkQPaWeZ78lOp/nNgF Pl4/leS3mZ0ZiMOHxGhjI8LgV2DbiD6+rptjSoAfZiNdAKlq0J5CHsInL7e7fkXIGZmj 0v4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=b6r4iHXphce3qNrnt+WRxrpdsuktB8YXEoXsfGUyHyI=; b=szyARny8kMFbD5C3SCI1I3ORhCWOo6WeFyPQ9HoHA4GQ+F2WsDt5ohc7QOj5BnIGAd +XAHSnnId93o0MuP67oSt/1dABhaxqL1hK4cqXcpD8Eu8xHDgamFG7WIRHPJ6q+r5doi e1xx/PSTI+NJpUOSlgrOgp3Q2f1L5OfBl0L64q+Dqib4ZuX7+U2NMpWZg5itzNpvIHfz T2dTTLIagQBHPXAI/5beLSlWt8kc/DEZGPDJzLWuc54Ke0pmq5NwZ9Bq5nLFO+EFyoE/ aJP5PY2n/tJTRss6MoQ8PXlsOw9p1kRuZauJz38wIxjTPfOZEZtqb3TBvetnyBcppztY KAOg== X-Gm-Message-State: AOAM531EVBiGP0ts9TW7FIHsvZiaCt+CFDTSXLevn8S7fYwERGIxT0TL A2uUituxay/eeB4piJ+BKoI= X-Google-Smtp-Source: ABdhPJw/H8QUlaG/k/sM5buMEEA7It/aboEPNCK6XBNNFa2imfElrbbgBDOeD4Oqn5yTRTrjuxAgLA== X-Received: by 2002:a17:90a:d497:b0:1dc:9cbe:6a47 with SMTP id s23-20020a17090ad49700b001dc9cbe6a47mr7218865pju.108.1652303241630; Wed, 11 May 2022 14:07:21 -0700 (PDT) Received: from google.com ([2620:15c:211:201:69ef:9c87:7816:4f74]) by smtp.gmail.com with ESMTPSA id p5-20020a170902b08500b0015e8d4eb1d6sm2361601plr.32.2022.05.11.14.07.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 14:07:21 -0700 (PDT) Date: Wed, 11 May 2022 14:07:19 -0700 From: Minchan Kim To: Sultan Alsawaf Cc: stable@vger.kernel.org, Nitin Gupta , Sergey Senozhatsky , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] zsmalloc: Fix races between asynchronous zspage free and page migration Message-ID: References: <20220509024703.243847-1-sultan@kerneltoast.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D5056120003 X-Stat-Signature: u5rx7speopmne6kfo1jiwohfy5kquh8e X-Rspam-User: Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=d4IUXwD6; spf=pass (imf29.hostedemail.com: domain of minchan.kim@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=minchan.kim@gmail.com; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=kernel.org (policy=none) X-HE-Tag: 1652303235-902180 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, May 11, 2022 at 12:50:20PM -0700, Sultan Alsawaf wrote: > On Wed, May 11, 2022 at 11:01:01AM -0700, Minchan Kim wrote: > > On Sun, May 08, 2022 at 07:47:02PM -0700, Sultan Alsawaf wrote: > > > Cc: stable@vger.kernel.org > > > Fixes: 48b4800a1c6a ("zsmalloc: page migration support") > > > > Shouldn't the fix be Fixes: 77ff465799c6 ("zsmalloc: zs_page_migrate: skip > > unnecessary loops but not return -EBUSY if zspage is not inuse)? > > Because we didn't migrate ZS_EMPTY pages before. > > Hi, > > Yeah, 77ff465799c6 indeed seems like the commit that introduced the bug. > > > I couldn't get the point here. Why couldn't we simple lock zspage migration? > > > > diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c > > index 9152fbde33b5..05ff2315b7b1 100644 > > --- a/mm/zsmalloc.c > > +++ b/mm/zsmalloc.c > > @@ -1987,7 +1987,10 @@ static void async_free_zspage(struct work_struct *work) > > > > list_for_each_entry_safe(zspage, tmp, &free_pages, list) { > > list_del(&zspage->list); > > + > > + migrate_read_lock(zspage); > > lock_zspage(zspage); > > + migrate_read_unlock(zspage); > > > > get_zspage_mapping(zspage, &class_idx, &fullness); > > VM_BUG_ON(fullness != ZS_EMPTY); > > There are two problems with this: > 1. migrate_read_lock() is a rwlock and lock_page() can sleep. > 2. This will cause a deadlock because it violates the lock ordering in > zs_page_migrate(), since zs_page_migrate() takes migrate_write_lock() under > the page lock. > That's true. Thanks! Then, how about this? diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 9152fbde33b5..2f205c18aee4 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1716,12 +1716,31 @@ static enum fullness_group putback_zspage(struct size_class *class, * To prevent zspage destroy during migration, zspage freeing should * hold locks of all pages in the zspage. */ -static void lock_zspage(struct zspage *zspage) +static void lock_zspage(struct zs_pool *pool, struct zspage *zspage) { - struct page *page = get_first_page(zspage); - + struct page *page; + int nr_locked; + struct page *locked_pages[ZS_MAX_PAGES_PER_ZSPAGE]; + struct address_space *mapping; +retry: + nr_locked = 0; + memset(locked_pages, 0, sizeof(struct page) * ARRAY_SIZE(locked_pages)); + page = get_first_page(zspage); do { lock_page(page); + locked_pages[nr_locked++] = page; + /* + * subpage in the zspage could be migrated under us so + * verify it. Once it happens, retry the lock sequence. + */ + mapping = page_mapping(page) + if (mapping != pool->inode->i_mapping || + page_private(page) != (unsigned long)zspage) { + do { + unlock_page(locked_pages[--nr_locked]); + } while (nr_locked > 0) + goto retry; + } } while ((page = get_next_page(page)) != NULL); } @@ -1987,7 +2006,7 @@ static void async_free_zspage(struct work_struct *work) list_for_each_entry_safe(zspage, tmp, &free_pages, list) { list_del(&zspage->list); - lock_zspage(zspage); + lock_zspage(pool, zspage); get_zspage_mapping(zspage, &class_idx, &fullness); VM_BUG_ON(fullness != ZS_EMPTY);