Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp144265ybi; Tue, 2 Jul 2019 17:56:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqy+Cs46i+tCelkmRheXuq8wXWMsbVQTooOSvdcyItKG7PhEMJqdV0Dhe/yuYlrxw1rMMdgA X-Received: by 2002:a17:902:7894:: with SMTP id q20mr37163694pll.339.1562115371259; Tue, 02 Jul 2019 17:56:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562115371; cv=none; d=google.com; s=arc-20160816; b=dFdKCGXIwLYH8Z2Pa0Mutn9Lf9mVmXfNzPej1nCLhljitChPwxqyI8rOC6A6nNKFMz +KwG5lzWMuZZSCrKFHaqugasHA99zk5ebo/4fSC03De20l9rJesWyy2GUCNdpX3QJiOU zLIZQQCp3enUr1XqxULsYeG9dXUeQt+5LPZzrZqHNuRH/GoR0pmuHyRIZbXfbEPKBz5o wZG/o0wFdORIY1hX1PxVKnB1ctqI8gYdsmHk+g/cE8PRkcP1yZtLAaLaBk4XH9gadi7H uA8kGxDIFFjmwSRKXb/bkq2jltx/j9aVRWL8RuMvE53YUx3o7emEOxOwjmiKQ3nRZ+T1 xC2w== 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=ytW0OWDzeMr1hdoz0EslmLVkCowQp/51ALwoyHUI2GI=; b=SnVFts1ggrT2G5IKHMDgT+0KKOtQgt1VpjnTeZU3iyEivZKNhWuJmGz666izRJmvXT 3mnb+sPVpeTQgzkBZjJMCK/L8qp8tvqC7V1kXLzMO25XLOi/1FJU7rP5iF3D3rfSQ0tl 00t3BO9F/C+cLSjlZbCKnSG9JjvojhbXc6Wm/6ObRlhewQUXMe1NDKuiXZfyk2WrT6oB UR/59aWsLv9oAvTpF4FM8zqtxeG3+i2+E7ZDuzg3TKK/yrlaveiLIsQFuLpyFEePzuPf cBcwDKV6loKf7ooOhHW+fEA+5fFB32kTjXOzmJ0XZ8cZmtOaLnyfQ967/RwdYs8BjJKf z5cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=nBSEOeFp; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r41si244343pjb.11.2019.07.02.17.55.55; Tue, 02 Jul 2019 17:56:11 -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=@google.com header.s=20161025 header.b=nBSEOeFp; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727158AbfGCAyR (ORCPT + 99 others); Tue, 2 Jul 2019 20:54:17 -0400 Received: from mail-io1-f66.google.com ([209.85.166.66]:34076 "EHLO mail-io1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727049AbfGCAyR (ORCPT ); Tue, 2 Jul 2019 20:54:17 -0400 Received: by mail-io1-f66.google.com with SMTP id k8so817113iot.1 for ; Tue, 02 Jul 2019 17:54:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ytW0OWDzeMr1hdoz0EslmLVkCowQp/51ALwoyHUI2GI=; b=nBSEOeFp5wxsOc5nTyqZmSBLDUrnq1nUlE9LRL00bMxxQ1nC9cUrMJLQwOr63l7q1w o9YaaR5F4rYdFz2pUixfzkhj1Ip79oCTaPKe4HDDH0qjCAseTf5T7zRHqKjKVe4JBP52 Nplnas64U+BD2r13HOt+d293Y7bvus/A0EwtMJ7gGxdYEIT2HQybaKmQ179ennlf2XSZ P6EnWzAwK0ddqKUQA6tNNS7uAB8KzxJBrrkpcaJmxWRhWrpG32OQ0nZ8w3cWEqUEqfgU pvwbgy6SkYP+RNRrBBdfM5woWRI2kb/sMj2nIfV0U8gXMGhqsq8dX9zL6WqQBWSM/+X8 SLSQ== 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=ytW0OWDzeMr1hdoz0EslmLVkCowQp/51ALwoyHUI2GI=; b=MwNFK1b4tCLy5GHejkOWNmSBqKrS5TvwpTOBqTV0/WVTzAB1qd1jAs6WjsoKR1OXRQ 3Ae4+T82gDZU6sVYVcWR3Y/GQHIUCnEpZOZPoaFELIsg+mpUH7rKugLeKRwdfyhOZmjk 3J5kheAucZRCm0zlHSY4eOp8sI874VIcozzB7WbAqZeOX64mRgVsezeAkMUcLV8xjDLz clTXSLLcTrRi8GUbnltQ0a6VSHV8zlnD/Qy6A6WqYL/ldW2V4qm9KUsAH4ieKdWp7e5w LoOOiTKodQ6ND7wX614AfT2x1EXBvQ7GYiQr0noh1ddkURR1KFO7UUgyOPURNJuD2MJ7 rtSQ== X-Gm-Message-State: APjAAAWmvIzEiH0YcdiDF8BAvA+0rOwEBLPXbbmJ+8qIdnVQx5yQIxwC wc45NtCzYDMXnYn9wXl0DlbiUIQYlRujERR8hEhDLampEYE= X-Received: by 2002:a02:aa8f:: with SMTP id u15mr37908229jai.39.1562105903431; Tue, 02 Jul 2019 15:18:23 -0700 (PDT) MIME-Version: 1.0 References: <20190702005122.41036-1-henryburns@google.com> <20190702141930.e31bf1c07a77514d976ef6e2@linux-foundation.org> In-Reply-To: <20190702141930.e31bf1c07a77514d976ef6e2@linux-foundation.org> From: Henry Burns Date: Tue, 2 Jul 2019 15:17:47 -0700 Message-ID: Subject: Re: [PATCH v2] mm/z3fold.c: Lock z3fold page before __SetPageMovable() To: Andrew Morton Cc: Shakeel Butt , Vitaly Wool , Vitaly Vul , Mike Rapoport , Xidong Wang , 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 On Tue, Jul 2, 2019 at 2:19 PM Andrew Morton wrote: > > On Mon, 1 Jul 2019 18:16:30 -0700 Henry Burns wrote: > > > Cc: Vitaly Wool , Vitaly Vul > > Are these the same person? I Think it's the same person, but i wasn't sure which email to include because one was in the list of maintainers and I had contacted the other earlier. > > > Subject: Re: [PATCH v2] mm/z3fold.c: Lock z3fold page before __SetPageMovable() > > Date: Mon, 1 Jul 2019 18:16:30 -0700 > > > > On Mon, Jul 1, 2019 at 6:00 PM Shakeel Butt wrote: > > > > > > On Mon, Jul 1, 2019 at 5:51 PM Henry Burns wrote: > > > > > > > > __SetPageMovable() expects it's page to be locked, but z3fold.c doesn't > > > > lock the page. Following zsmalloc.c's example we call trylock_page() and > > > > unlock_page(). Also makes z3fold_page_migrate() assert that newpage is > > > > passed in locked, as documentation. > > The changelog still doesn't mention that this bug triggers a > VM_BUG_ON_PAGE(). It should do so. I did this: > > : __SetPageMovable() expects its page to be locked, but z3fold.c doesn't > : lock the page. This triggers the VM_BUG_ON_PAGE(!PageLocked(page), page) > : in __SetPageMovable(). > : > : Following zsmalloc.c's example we call trylock_page() and unlock_page(). > : Also make z3fold_page_migrate() assert that newpage is passed in locked, > : as per the documentation. > > I'll add a cc:stable to this fix. > > > > > Signed-off-by: Henry Burns > > > > Suggested-by: Vitaly Wool > > > > --- > > > > Changelog since v1: > > > > - Added an if statement around WARN_ON(trylock_page(page)) to avoid > > > > unlocking a page locked by a someone else. > > > > > > > > mm/z3fold.c | 6 +++++- > > > > 1 file changed, 5 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/mm/z3fold.c b/mm/z3fold.c > > > > index e174d1549734..6341435b9610 100644 > > > > --- a/mm/z3fold.c > > > > +++ b/mm/z3fold.c > > > > @@ -918,7 +918,10 @@ static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp, > > > > set_bit(PAGE_HEADLESS, &page->private); > > > > goto headless; > > > > } > > > > - __SetPageMovable(page, pool->inode->i_mapping); > > > > + if (!WARN_ON(!trylock_page(page))) { > > > > + __SetPageMovable(page, pool->inode->i_mapping); > > > > + unlock_page(page); > > > > + } > > > > > > Can you please comment why lock_page() is not used here? > > Shakeel asked "please comment" (ie, please add a code comment), not > "please comment on". Subtle ;) > > > Since z3fold_alloc can be called in atomic or non atomic context, > > calling lock_page() could trigger a number of > > warnings about might_sleep() being called in atomic context. WARN_ON > > should avoid the problem described > > above as well, and in any weird condition where someone else has the > > page lock, we can avoid calling > > __SetPageMovable(). > > I think this will suffice: > > --- a/mm/z3fold.c~mm-z3foldc-lock-z3fold-page-before-__setpagemovable-fix > +++ a/mm/z3fold.c > @@ -919,6 +919,9 @@ retry: > set_bit(PAGE_HEADLESS, &page->private); > goto headless; > } > + /* > + * z3fold_alloc() can be called from atomic contexts, hence the trylock > + */ > if (!WARN_ON(!trylock_page(page))) { > __SetPageMovable(page, pool->inode->i_mapping); > unlock_page(page); > > However this code would be more effective if z3fold_alloc() were to be > told when it is running in non-atomic context so it can perform a > sleeping lock_page() in that case. That's an improvement to consider > for later, please. > z3fold_alloc() can tell when its called in atomic context, new patch incoming! I'm thinking something like this: > > > > + if (can_sleep) { > > > > + lock_page(page); > > > > + __SetPageMovable(page, pool->inode->i_mapping); > > > > + unlock_page(page); > > > > + } else { > > > > + if (!WARN_ON(!trylock_page(page))) { > > > > + __SetPageMovable(page, pool->inode->i_mapping); > > > > + unlock_page(page); > > > > + } else { > > > > + pr_err("Newly allocated z3fold page is locked\n"); > > > > + WARN_ON(1); > > > > + } > > > > + }