Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp146093ybi; Tue, 2 Jul 2019 17:58:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqy/bUYHEu1ZxLdfLjYGvswIRJbbgcho8F5V9n+YC0mMvyhF4FUoAGqNA3YxwPYIDvbpQzIH X-Received: by 2002:a17:902:aa0a:: with SMTP id be10mr38998613plb.27.1562115528625; Tue, 02 Jul 2019 17:58:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562115528; cv=none; d=google.com; s=arc-20160816; b=wT9GApdVfZ3amrye5mp+ZpXdSMcsYHSu+TyGvUvEVWQ0CFI65G6F/yRl1D/I9qrcMF yniiQW/ly+RjgV/J1oMZWbyQgUxlPJi7zHwx3mqd5bTPGUD/qWXosR0CpQGxXtfjxyVb TDcgzxH5nqZlQE6gQMwJ79KCOjP+xbFGZZIKYMJ2oeVvLPUxgA2Pb1VAoEwmVWALNTqz vBuG1A4XztO4yRLuqFGVAgwGSilviIVBs6g3l4WBm+PUH+6Ej78aFWOiUbicfxg57ZZy WFjA2AeOJsBgfWfpBkgXzgzD359Om7l4gLC4Z+9rnPwHs+FsoI/3iHdRQK5bUUpKEpnN 4Kww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=fFnP/4ko1bTRM65ZZce3YMyBsH4542HegdeVnSUHtDs=; b=ROAfCEtyjMNTHoB95Otf7x8TV5+JRq3XjRdhFlD/7fZ0ow0wilYDvIw83tiBtGXvKK sMKeEfv56m007bVhnHIbOmpEGKmFdOdzRF20yU+B9PvQER6z6epzUjF9KtnQs+adqWFC naHcOxyvY0EfIu0qnvlS/lI4muDw3mo8bFGaL3R77R5+sJJdgIMSaQCtI5FKWq7Vj15o IJMFF65BUY0Of3bMBBGfUlG0JH4fR4YD/ZYo7ig4mD841hOxCMBPgv/bsHeNBp/LpiNY p/De0iYAl/im8psg+KaHRAMfYDFIiqtI6pAfhoKv7bH5a8UuCpFFVMTFSMB9oKxqU2A9 z8Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mTmZs1yo; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m14si345751pgj.377.2019.07.02.17.58.33; Tue, 02 Jul 2019 17:58:48 -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=@kernel.org header.s=default header.b=mTmZs1yo; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727171AbfGCA6H (ORCPT + 99 others); Tue, 2 Jul 2019 20:58:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:49334 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727090AbfGCA6F (ORCPT ); Tue, 2 Jul 2019 20:58:05 -0400 Received: from akpm3.svl.corp.google.com (unknown [104.133.8.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5E86B218BA; Tue, 2 Jul 2019 21:19:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562102371; bh=Tlj2RrGNaOqOZ+7sngsROh+dPsr/HURv8KYaqPdX2Tc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=mTmZs1yoM8R9sJY0ieO8eIQJr3f8HYOWvQ3AMF5RJaDSv2pSaZaihzF+/IR3zRzS1 c1Lo86AgdvNT8xO+Z6NMAKkcwx8Bv9Q2MIOWKOkhIYBQdrBruYLu3rZABrPfWQySrt 4Gaz5KQ1vRA5wuFWuTaH2vEnRIcEeE+JF2BKSBmI= Date: Tue, 2 Jul 2019 14:19:30 -0700 From: Andrew Morton To: Henry Burns Cc: Shakeel Butt , Vitaly Wool , Vitaly Vul , Mike Rapoport , Xidong Wang , Jonathan Adams , Linux MM , LKML Subject: Re: [PATCH v2] mm/z3fold.c: Lock z3fold page before __SetPageMovable() Message-Id: <20190702141930.e31bf1c07a77514d976ef6e2@linux-foundation.org> In-Reply-To: References: <20190702005122.41036-1-henryburns@google.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 1 Jul 2019 18:16:30 -0700 Henry Burns wrote: > Cc: Vitaly Wool , Vitaly Vul Are these the same person? > 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.