Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1360286ybl; Sat, 10 Aug 2019 02:08:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqzzResB3JNpxFyRupP2Wv2JRaNE8sWg4Fsz6UcOz/idriXBZLjskRreF3aSCIs0g6ZA59b4 X-Received: by 2002:a17:90a:e2d6:: with SMTP id fr22mr8309376pjb.105.1565428085240; Sat, 10 Aug 2019 02:08:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565428085; cv=none; d=google.com; s=arc-20160816; b=bRuM66iPX5NN6cq75xqmrMwzU3s5YtkzUO2BJDwOnSKaTBcZKby6H4HiP6LhbCes5o gRf1rw1FyRwL+TqUxy/92bKxppAOlwcvF0g/Hy2Rr8AKuIR2geEmE+f0j8jjrjrn0oX2 7B6nNyWgIN3avxrgjMnXK7cj8jCBnq3Q9MTbNvMS017yx3Q3oLqKIA0OTuBvF5LwPDmV SVo7u5PdLTOjOZufD1pbVplrf/wWhz9E5A15t+/Dt1R2XwasbDgyZJ4JH8KWgZszZmJU pJgMaX+OqBG4oEhPiedLXfZkW6NE8so+eTeOFenr2qM2/4T6yE8+b/iAmSXAT2gx1O06 gizA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=x/iVkWacMKBMI8tUTq90sb+qNE12YPEmDRdfDkGujEg=; b=wdeRAagnN/EO0EjF0XxV6mWMLXp4Z9YMuW+Ri6EOWD1mScNLHv7bOgE4knCDC/TitF 8fvC7254jWEhZUKH0IDVEZ/h+SRqzwWrDl1XIXaFxw5IVi+MDKehCVSin+vcoamMe4w3 6U0vy7hwBTEN5ZWGIZ9Ez65rWZ3+Z0QrJX14NCJw87oaartZs/+uTvmdUS3gf7fjdfz3 LeufQNfC/A5Guzk7Y8yz+iC+O+AyOv1zqK7Wcal4kJHK0gtbKCO+67w9NQ8J1URR4zYh 6HuyAwgsg/R+WJ+Cm0BDhwNrBB/+AnSgrwaD4FaYFETsT9XCpkLUe/+8gb2TW01S8DKL XuKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="WvlE/0bj"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i10si62228211pfr.130.2019.08.10.02.07.49; Sat, 10 Aug 2019 02:08:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="WvlE/0bj"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726057AbfHJJFk (ORCPT + 99 others); Sat, 10 Aug 2019 05:05:40 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:32851 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725468AbfHJJFj (ORCPT ); Sat, 10 Aug 2019 05:05:39 -0400 Received: by mail-lj1-f193.google.com with SMTP id z17so5751430ljz.0 for ; Sat, 10 Aug 2019 02:05:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=x/iVkWacMKBMI8tUTq90sb+qNE12YPEmDRdfDkGujEg=; b=WvlE/0bjif9cFAUGbIm/f0nu8+dOOpAZMPm9E+uI8zYm4MQUaeFnTN0/mLAbdeOfCI x3IN08Godktf7mrpJ4w+H6zi9+7XgTOBEnva1iHxVbFc3hU+4fq/w5vvz7mw0oYfAeuf eRNRgpMPMdbEH6h9IYz+5voskY5PFplpNp3SZQAe/tSwhdW2g8V3wR0i/fE2r7HXSxvt zmphBIxc1HKujLmnct8w+4coO6/9/COYW9sMx37l/pGHOVNi7vxizxw122bkUMEwiYkQ 9B0b/5ViG93yQn/yadNuscw4Pd3eK/lthDz87lcR42UJ5iz/3m2qR+/GciYvZdbRmU9/ 4sjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=x/iVkWacMKBMI8tUTq90sb+qNE12YPEmDRdfDkGujEg=; b=rv2gXdTI/3aYCpnxpRbA6dXaco7zD0CDDm9crhMGOLoYqG0G/CCdW9Z3ysAvliZPGt 0dKWRMD8F+/gUszUUpjPME9CQa4Ah8dgPSFeRIZtjyOxhYt1qvQxdh3Cyv05zCMsUbhT te3EtdIiQ+8pf7tehCXV39QXmrKjrifr4dCfk0uXZxmqq1JI7/whyELxubVh02Zccixs 0B6G4AGC4kXVfmbOv9VGi48UhtJO556PP2dJhfFOpR82UpqSDts2nCq5I4a+gVzjpDXO LD928swqJLlug4IFQtMnnD8XTjSjSj6EZJmnOb/KPmEbeBjvmbRBvxgZDS+NyufSy+iN zFLA== X-Gm-Message-State: APjAAAWSHahKp1GlSKwQ7SH8jFDoMkhz+MQnq8uRE5E7O/O6ffDYmSbr y8AEJhPr1xwB0Tcv8P4xoD/xvWL74nh6EkzeRS4= X-Received: by 2002:a2e:720b:: with SMTP id n11mr4092017ljc.213.1565427936877; Sat, 10 Aug 2019 02:05:36 -0700 (PDT) MIME-Version: 1.0 References: <20190809164643.5978-1-henryburns@google.com> In-Reply-To: <20190809164643.5978-1-henryburns@google.com> From: Vitaly Wool Date: Sat, 10 Aug 2019 12:05:23 +0300 Message-ID: Subject: Re: [PATCH] mm/z3fold.c: Fix race between migration and destruction To: Henry Burns Cc: Andrew Morton , Vitaly Vul , Shakeel Butt , Jonathan Adams , Linux-MM , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Henry, Den fre 9 aug. 2019 6:46 emHenry Burns skrev: > > In z3fold_destroy_pool() we call destroy_workqueue(&pool->compact_wq). > However, we have no guarantee that migration isn't happening in the > background at that time. > > Migration directly calls queue_work_on(pool->compact_wq), if destruction > wins that race we are using a destroyed workqueue. Thanks for the fix. Would you please comment why adding flush_workqueue() isn't enough? ~Vitaly > > > Signed-off-by: Henry Burns > --- > mm/z3fold.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 51 insertions(+) > > diff --git a/mm/z3fold.c b/mm/z3fold.c > index 78447cecfffa..e136d97ce56e 100644 > --- a/mm/z3fold.c > +++ b/mm/z3fold.c > @@ -40,6 +40,7 @@ > #include > #include > #include > +#include > #include > > /* > @@ -161,8 +162,10 @@ struct z3fold_pool { > const struct zpool_ops *zpool_ops; > struct workqueue_struct *compact_wq; > struct workqueue_struct *release_wq; > + struct wait_queue_head isolate_wait; > struct work_struct work; > struct inode *inode; > + int isolated_pages; > }; > > /* > @@ -772,6 +775,7 @@ static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp, > goto out_c; > spin_lock_init(&pool->lock); > spin_lock_init(&pool->stale_lock); > + init_waitqueue_head(&pool->isolate_wait); > pool->unbuddied = __alloc_percpu(sizeof(struct list_head)*NCHUNKS, 2); > if (!pool->unbuddied) > goto out_pool; > @@ -811,6 +815,15 @@ static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp, > return NULL; > } > > +static bool pool_isolated_are_drained(struct z3fold_pool *pool) > +{ > + bool ret; > + > + spin_lock(&pool->lock); > + ret = pool->isolated_pages == 0; > + spin_unlock(&pool->lock); > + return ret; > +} > /** > * z3fold_destroy_pool() - destroys an existing z3fold pool > * @pool: the z3fold pool to be destroyed > @@ -821,6 +834,13 @@ static void z3fold_destroy_pool(struct z3fold_pool *pool) > { > kmem_cache_destroy(pool->c_handle); > > + /* > + * We need to ensure that no pages are being migrated while we destroy > + * these workqueues, as migration can queue work on either of the > + * workqueues. > + */ > + wait_event(pool->isolate_wait, !pool_isolated_are_drained(pool)); > + > /* > * We need to destroy pool->compact_wq before pool->release_wq, > * as any pending work on pool->compact_wq will call > @@ -1317,6 +1337,28 @@ static u64 z3fold_get_pool_size(struct z3fold_pool *pool) > return atomic64_read(&pool->pages_nr); > } > > +/* > + * z3fold_dec_isolated() expects to be called while pool->lock is held. > + */ > +static void z3fold_dec_isolated(struct z3fold_pool *pool) > +{ > + assert_spin_locked(&pool->lock); > + VM_BUG_ON(pool->isolated_pages <= 0); > + pool->isolated_pages--; > + > + /* > + * If we have no more isolated pages, we have to see if > + * z3fold_destroy_pool() is waiting for a signal. > + */ > + if (pool->isolated_pages == 0 && waitqueue_active(&pool->isolate_wait)) > + wake_up_all(&pool->isolate_wait); > +} > + > +static void z3fold_inc_isolated(struct z3fold_pool *pool) > +{ > + pool->isolated_pages++; > +} > + > static bool z3fold_page_isolate(struct page *page, isolate_mode_t mode) > { > struct z3fold_header *zhdr; > @@ -1343,6 +1385,7 @@ static bool z3fold_page_isolate(struct page *page, isolate_mode_t mode) > spin_lock(&pool->lock); > if (!list_empty(&page->lru)) > list_del(&page->lru); > + z3fold_inc_isolated(pool); > spin_unlock(&pool->lock); > z3fold_page_unlock(zhdr); > return true; > @@ -1417,6 +1460,10 @@ static int z3fold_page_migrate(struct address_space *mapping, struct page *newpa > > queue_work_on(new_zhdr->cpu, pool->compact_wq, &new_zhdr->work); > > + spin_lock(&pool->lock); > + z3fold_dec_isolated(pool); > + spin_unlock(&pool->lock); > + > page_mapcount_reset(page); > put_page(page); > return 0; > @@ -1436,10 +1483,14 @@ static void z3fold_page_putback(struct page *page) > INIT_LIST_HEAD(&page->lru); > if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) { > atomic64_dec(&pool->pages_nr); > + spin_lock(&pool->lock); > + z3fold_dec_isolated(pool); > + spin_unlock(&pool->lock); > return; > } > spin_lock(&pool->lock); > list_add(&page->lru, &pool->lru); > + z3fold_dec_isolated(pool); > spin_unlock(&pool->lock); > z3fold_page_unlock(zhdr); > } > -- > 2.22.0.770.g0f2c4a37fd-goog >