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 351EFC433F5 for ; Wed, 2 Feb 2022 21:53:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 597558D011B; Wed, 2 Feb 2022 16:53:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 544CE8D0114; Wed, 2 Feb 2022 16:53:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 433F88D011B; Wed, 2 Feb 2022 16:53:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0027.hostedemail.com [216.40.44.27]) by kanga.kvack.org (Postfix) with ESMTP id 34E658D0114 for ; Wed, 2 Feb 2022 16:53:28 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id DDF6D1828958F for ; Wed, 2 Feb 2022 21:53:27 +0000 (UTC) X-FDA: 79099191654.24.B114556 Received: from mail-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) by imf20.hostedemail.com (Postfix) with ESMTP id 6F1A21C0002 for ; Wed, 2 Feb 2022 21:53:27 +0000 (UTC) Received: by mail-io1-f54.google.com with SMTP id c188so824071iof.6 for ; Wed, 02 Feb 2022 13:53:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=cP6a8V+TG3up9uQ72M6jJ3rO0BN/B9RvaaZdDPQqQ3Q=; b=HlpSMbh619MeZwBiBmh/8oN+kByjM6EkLuOV/kxtbG4kevQkM2pWOdbaIfXk59LAhc O4YJU1x/KaPVGUpypUnIZA9wUL8EPrfwVvmQr1EwxDkFM4SR6GC1aQvOCdAQWzipwwAt nZC6wXITpf9bnUaNkrsqq8OCMN8UhhboI+aWFiSZPbWOAZ9iKd1J9KTQ+hY2BXDQh8XU yhgTqtevqQYgRcxGm4qtNWxvZkO4MTAF11tGMjua9G6jBt009AArS2YoN+8Trc5mRaxg ullXf/ISHjCrmk1UwNQB/Z14XFQkfQKXC4G0Lj1q9WT00ppMUA+iPqGqdQknTS/qsADv zJEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=cP6a8V+TG3up9uQ72M6jJ3rO0BN/B9RvaaZdDPQqQ3Q=; b=eUvExQXvDZ7kjPTejLvHOtM0ziRdvERmRc8JtiC+7FyORjlLGNxtGlLt7o+MOSxreZ 26HyrhVuorqacmU0Prfb0a9huErbCr4kCljdSmhK8jAA8Vn73Vhmm6gXaR/rfn9/S2Lj nWipMbL59SsTUMcvKHFjFmnx3byItmoEgP3FZZE354Utn7burOpTxsNIk0OMuJdfzQX4 MTG2utL+v0/iDDFR4rGPXQTx2pgegj16fzAM8LleAo2eFfMdxwDqZCoz8JTsAz7MULMz tESR+U+tpElrxzCTxNzzn9JOSweGv8SnXzS8TEwZlmjU77zANBFe/Qga99X/j+JhL1GT ZqMA== X-Gm-Message-State: AOAM532ksSMzWBY0CuE1dJemqUxRLWk6U/mpuKWKYdZg+ExEoysTl8Dd KouZ39IYwOj7rtcIsGxBElhDOw== X-Google-Smtp-Source: ABdhPJx7n33IlwAgOoXc9VJo9IWvTthJNyf9Bvol1fDrtnOq7fcdplTvGdVcnwWG4F/zWd8T6gah3Q== X-Received: by 2002:a02:29cd:: with SMTP id p196mr16314874jap.82.1643838806619; Wed, 02 Feb 2022 13:53:26 -0800 (PST) Received: from google.com ([2620:15c:183:200:a1e5:3cc4:4458:6ce8]) by smtp.gmail.com with ESMTPSA id h3sm21898094ild.11.2022.02.02.13.53.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 13:53:26 -0800 (PST) Date: Wed, 2 Feb 2022 14:53:22 -0700 From: Yu Zhao To: Mauricio Faria de Oliveira Cc: Minchan Kim , "Huang, Ying" , Andrew Morton , Yang Shi , Miaohe Lin , linux-mm@kvack.org, linux-block@vger.kernel.org Subject: Re: [PATCH v3] mm: fix race between MADV_FREE reclaim and blkdev direct IO read Message-ID: References: <20220131230255.789059-1-mfo@canonical.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspam-User: nil X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6F1A21C0002 X-Stat-Signature: c3mseceamfmdojxyj13z35pgssry1696 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=HlpSMbh6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of yuzhao@google.com designates 209.85.166.54 as permitted sender) smtp.mailfrom=yuzhao@google.com X-HE-Tag: 1643838807-745085 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, Feb 02, 2022 at 06:27:47PM -0300, Mauricio Faria de Oliveira wrote: > On Wed, Feb 2, 2022 at 4:56 PM Yu Zhao wrote: > > > > On Mon, Jan 31, 2022 at 08:02:55PM -0300, Mauricio Faria de Oliveira wrote: > > > Problem: > > > ======= > > > > Thanks for the update. A couple of quick questions: > > > > > Userspace might read the zero-page instead of actual data from a > > > direct IO read on a block device if the buffers have been called > > > madvise(MADV_FREE) on earlier (this is discussed below) due to a > > > race between page reclaim on MADV_FREE and blkdev direct IO read. > > > > 1) would page migration be affected as well? > > Could you please elaborate on the potential problem you considered? > > I checked migrate_pages() -> try_to_migrate() holds the page lock, > thus shouldn't race with shrink_page_list() -> with try_to_unmap() > (where the issue with MADV_FREE is), but maybe I didn't get you > correctly. Could the race exist between DIO and migration? While DIO is writing to a page, could migration unmap it and copy the data from this page to a new page? > > > @@ -1599,7 +1599,30 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma, > > > > > > /* MADV_FREE page check */ > > > if (!PageSwapBacked(page)) { > > > - if (!PageDirty(page)) { > > > + int ref_count, map_count; > > > + > > > + /* > > > + * Synchronize with gup_pte_range(): > > > + * - clear PTE; barrier; read refcount > > > + * - inc refcount; barrier; read PTE > > > + */ > > > + smp_mb(); > > > + > > > + ref_count = page_count(page); > > > + map_count = page_mapcount(page); > > > + > > > + /* > > > + * Order reads for page refcount and dirty flag; > > > + * see __remove_mapping(). > > > + */ > > > + smp_rmb(); > > > > 2) why does it need to order against __remove_mapping()? It seems to > > me that here (called from the reclaim path) it can't race with > > __remove_mapping() because both lock the page. > > I'll improve that comment in v4. The ordering isn't against __remove_mapping(), > but actually because of an issue described in __remove_mapping()'s comments > (something else that doesn't hold the page lock, just has a page reference, that > may clear the page dirty flag then drop the reference; thus check ref, > then dirty). Got it. IIRC, get_user_pages() doesn't imply a write barrier. If so, there should be a smp_wmb() on the other side: * get_user_pages(&page); smp_wmb() * SetPageDirty(page); * put_page(page); (__remove_mapping() doesn't need smp_[rw]mb() on either side because it relies on page refcnt freeze and retesting.) Thanks.