Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1004812iob; Fri, 13 May 2022 19:20:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTyxlSPkFuA+cDlr9M1zqrDZJ/Uz+SFDAwSaBJYRpX51fkB+F2nWBwok2eXPPBLRnQgq8G X-Received: by 2002:adf:f2cc:0:b0:20a:cfbb:ac29 with SMTP id d12-20020adff2cc000000b0020acfbbac29mr5978689wrp.263.1652494814489; Fri, 13 May 2022 19:20:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652494814; cv=none; d=google.com; s=arc-20160816; b=bzrP/pcC56DJhgUvjyonP2nsXhMT0vShYL2TRHbQhFNRNc/JqcsfIx7KK2SfvBfQ51 Ksq9D8fVIQlGmVXAtwc4QJxqMCIpiU6xZ6fE71ckzjfh6FrLm7VRTI/+yyLEF1q6lc/b Pl3ejn8ys3WHdVLll+6/TBJvsLch96xbmAxLX24TEyo6857nKOWvELymOnEiAGP3rmem 6MXQyvjc8thH+40qxIfLjMBXGMFkzgpJR6cjqDxbedH54EnykkrAZ/Uve+HMam5egLgY bgcrmWP7kPN5Af1+picG6XHx5fu/LBXMnCHFrvlKYd+nyQJ1wRPo91E8oXZcpGN5OgIz KmSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:sender:dkim-signature; bh=b6r4iHXphce3qNrnt+WRxrpdsuktB8YXEoXsfGUyHyI=; b=is8IvYjJFCSjf1muhXO9eLagBivOyDmKeBLDLL3QTsjARYui6bi0+2h7XHwXGmLo8L rFozBr2jJmQZMp3cVYFPmaxyZCEeipLAMe1SukVh7VBb3r33nGRqh5besChiSqwc7T/p I8FmbdBbgHrFsnuLtioIwOyN98SZfe6GiwmOYrggeNIK9OGeI5WGKNTDwXxd+GIx/auw wvLiIaPkmVFgVcntyetgKY44AMtJcm1G8ZdGJLX3T5ICbL+AsebCoWfFSX4qphHtnicc CP2/ECb96ClaIJ481mYfdfZtkl26ru49aHz5cvrRUe/h8Sq8PfvYeS2ErudtlBtaGDHw 5Yog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=d4IUXwD6; 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=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id bg11-20020a05600c3c8b00b0039426511f8dsi3722039wmb.43.2022.05.13.19.20.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 19:20:14 -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=@gmail.com header.s=20210112 header.b=d4IUXwD6; 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=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C7F794B8222; Fri, 13 May 2022 17:36:49 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347987AbiEKVHa (ORCPT + 99 others); Wed, 11 May 2022 17:07:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347976AbiEKVHY (ORCPT ); Wed, 11 May 2022 17:07:24 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40A3769734; Wed, 11 May 2022 14:07:22 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id l7-20020a17090aaa8700b001dd1a5b9965so3139608pjq.2; 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=yG4sJACYvMFxOzgf7HuEIz/VwsmQcl1lv6gdVrI2c8xxjxORtdiPRk/T7fQ9tteS2z AJ8TaXCzBClhfUHRzZOoCSTk8HofbVp2sFVSJghQHSY5lQlIXFNBa3Noe2p10EuOm3wA SUzqFs2tAjSHp17EXA8DHYkiFSPTjHmCUbKytD30k0lQy4lrDFegO+LnyFUitR4mDair EwnKZOkmUqUjowU2YFptSobksTpb4P+22OAg4IZZEkJYXKnk5nyo9DzM8l7CWDyAqBoz +c8m9Z61XaKM2HmsFu4m3Y9LZ1zI0qFffnf3BaYT+82v/CFWwmU3tCcugvCtYAvkPHYS ycWw== X-Gm-Message-State: AOAM533TfmZpJwK9gKN/UoVdrO74Bv3ZLEwg7MoLDXKqwW+BUWxx2UuS Af8ispytLjvrMm5PhJ36OOk= 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) Sender: Minchan Kim 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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=no 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 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);