Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp327317imu; Wed, 2 Jan 2019 21:17:37 -0800 (PST) X-Google-Smtp-Source: ALg8bN4RDs0IdHOn1WK2wdcqBgR2i0tYTJYRFBCSt2oUX1NLhh8V4dvVGPZthZWLyEWHZANc+D3S X-Received: by 2002:a63:8f45:: with SMTP id r5mr15657250pgn.222.1546492657294; Wed, 02 Jan 2019 21:17:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546492657; cv=none; d=google.com; s=arc-20160816; b=S7haWCtRM58F4kB+xcG/Fi+IX7R3hCkDNxAWML8OVh8xTA8wW51M3NAWLCLR56QLXQ FWWpVvomNXdL/6LTltw/vMjdw+XzkfoiLKpOqe0mqlTf+XoIzEptajOTNm4cy3Ee/cqd +E20wEgvYSR4uOOXqSMjTGI3IRVLhUplNqEF/VgoewOHSq5CfcByctJa1vleXJppVPae Eefa7Gpced1ELJ7z67Aykc1iNFa+B4zD52rRh6epOFx5unkut4rFe1aq9CnjKdFctb2z L7CEx8jXNgOzGwWp2iehJJPC6FtrJumr7xko+bWp7t2zbBF55H7pI68yGH5LYhFh9Psb HlUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=cQJXKKW1cwl5y9scV6x9SwXrJt7HhkHji7cmHswF870=; b=pbP7g7hBZeU02ONqnasqOACXY1kVafBl/A8QkHF9sPjwkYrfML1EUPM8IaWdedEjiR 9YMBVqiKI4alXfg4SZ8f9nf6OcNYZNDqqirlVNrnv14+iCk+SMz1IyMvAAVPnwwQ3w9Q CzcywoBGf2BRMG8kayRwucJ8Cojv3m5sQdEZv8ccJ+DCP5M8TCRQi2tnksvUWGyfwpQp EkYT/TOVQpgVAh2Os1BfmdNdOubdaFae36+IlsDTJ5EEZGzlNEW71xswyb/AK//aqg8d Pt++UUkS+UKHuWLos4moGQG/spfYadD913bfEzYEECgu9RAYgRMAg/EgEz9WACxHzycK G7zQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v2si2450216pgn.451.2019.01.02.21.17.22; Wed, 02 Jan 2019 21:17:37 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726875AbfACBzl (ORCPT + 99 others); Wed, 2 Jan 2019 20:55:41 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52974 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726146AbfACBzl (ORCPT ); Wed, 2 Jan 2019 20:55:41 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EBAAE2DACC3; Thu, 3 Jan 2019 01:55:39 +0000 (UTC) Received: from redhat.com (ovpn-123-62.rdu2.redhat.com [10.10.123.62]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 24A825D9C9; Thu, 3 Jan 2019 01:55:35 +0000 (UTC) Date: Wed, 2 Jan 2019 20:55:33 -0500 From: Jerome Glisse To: Jan Kara Cc: John Hubbard , Matthew Wilcox , Dave Chinner , Dan Williams , John Hubbard , Andrew Morton , Linux MM , tom@talpey.com, Al Viro , benve@cisco.com, Christoph Hellwig , Christopher Lameter , "Dalessandro, Dennis" , Doug Ledford , Jason Gunthorpe , Michal Hocko , mike.marciniszyn@intel.com, rcampbell@nvidia.com, Linux Kernel Mailing List , linux-fsdevel Subject: Re: [PATCH 1/2] mm: introduce put_user_page*(), placeholder versions Message-ID: <20190103015533.GA15619@redhat.com> References: <20181212150319.GA3432@redhat.com> <20181212214641.GB29416@dastard> <20181214154321.GF8896@quack2.suse.cz> <20181216215819.GC10644@dastard> <20181217181148.GA3341@redhat.com> <20181217183443.GO10600@bombadil.infradead.org> <20181218093017.GB18032@quack2.suse.cz> <9f43d124-2386-7bfd-d90b-4d0417f51ccd@nvidia.com> <20181219020723.GD4347@redhat.com> <20181219110856.GA18345@quack2.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20181219110856.GA18345@quack2.suse.cz> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 03 Jan 2019 01:55:40 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 19, 2018 at 12:08:56PM +0100, Jan Kara wrote: > On Tue 18-12-18 21:07:24, Jerome Glisse wrote: > > On Tue, Dec 18, 2018 at 03:29:34PM -0800, John Hubbard wrote: > > > OK, so let's take another look at Jerome's _mapcount idea all by itself (using > > > *only* the tracking pinned pages aspect), given that it is the lightest weight > > > solution for that. > > > > > > So as I understand it, this would use page->_mapcount to store both the real > > > mapcount, and the dma pinned count (simply added together), but only do so for > > > file-backed (non-anonymous) pages: > > > > > > > > > __get_user_pages() > > > { > > > ... > > > get_page(page); > > > > > > if (!PageAnon) > > > atomic_inc(page->_mapcount); > > > ... > > > } > > > > > > put_user_page(struct page *page) > > > { > > > ... > > > if (!PageAnon) > > > atomic_dec(&page->_mapcount); > > > > > > put_page(page); > > > ... > > > } > > > > > > ...and then in the various consumers of the DMA pinned count, we use page_mapped(page) > > > to see if any mapcount remains, and if so, we treat it as DMA pinned. Is that what you > > > had in mind? > > > > Mostly, with the extra two observations: > > [1] We only need to know the pin count when a write back kicks in > > [2] We need to protect GUP code with wait_for_write_back() in case > > GUP is racing with a write back that might not the see the > > elevated mapcount in time. > > > > So for [2] > > > > __get_user_pages() > > { > > get_page(page); > > > > if (!PageAnon) { > > atomic_inc(page->_mapcount); > > + if (PageWriteback(page)) { > > + // Assume we are racing and curent write back will not see > > + // the elevated mapcount so wait for current write back and > > + // force page fault > > + wait_on_page_writeback(page); > > + // force slow path that will fault again > > + } > > } > > } > > This is not needed AFAICT. __get_user_pages() gets page reference (and it > should also increment page->_mapcount) under PTE lock. So at that point we > are sure we have writeable PTE nobody can change. So page_mkclean() has to > block on PTE lock to make PTE read-only and only after going through all > PTEs like this, it can check page->_mapcount. So the PTE lock provides > enough synchronization. > > > For [1] only needing pin count during write back turns page_mkclean into > > the perfect spot to check for that so: > > > > int page_mkclean(struct page *page) > > { > > int cleaned = 0; > > + int real_mapcount = 0; > > struct address_space *mapping; > > struct rmap_walk_control rwc = { > > .arg = (void *)&cleaned, > > .rmap_one = page_mkclean_one, > > .invalid_vma = invalid_mkclean_vma, > > + .mapcount = &real_mapcount, > > }; > > > > BUG_ON(!PageLocked(page)); > > > > if (!page_mapped(page)) > > return 0; > > > > mapping = page_mapping(page); > > if (!mapping) > > return 0; > > > > // rmap_walk need to change to count mapping and return value > > // in .mapcount easy one > > rmap_walk(page, &rwc); > > > > // Big fat comment to explain what is going on > > + if ((page_mapcount(page) - real_mapcount) > 0) { > > + SetPageDMAPined(page); > > + } else { > > + ClearPageDMAPined(page); > > + } > > This is the detail I'm not sure about: Why cannot rmap_walk_file() race > with e.g. zap_pte_range() which decrements page->_mapcount and thus the > check we do in page_mkclean() is wrong? > Ok so i found a solution for that. First GUP must wait for racing write back. If GUP see a valid write-able PTE and the page has write back flag set then it must back of as if the PTE was not valid to force fault. It is just a race with page_mkclean and we want ordering between the two. Note this is not strictly needed so we can relax that but i believe this ordering is better to do in GUP rather then having each single user of GUP test for this to avoid the race. GUP increase mapcount only after checking that it is not racing with writeback it also set a page flag (SetPageDMAPined(page)). When clearing a write-able pte we set a special entry inside the page table (might need a new special swap type for this) and change page_mkclean_one() to clear to 0 those special entry. Now page_mkclean: int page_mkclean(struct page *page) { int cleaned = 0; + int real_mapcount = 0; struct address_space *mapping; struct rmap_walk_control rwc = { .arg = (void *)&cleaned, .rmap_one = page_mkclean_one, .invalid_vma = invalid_mkclean_vma, + .mapcount = &real_mapcount, }; + int mapcount1, mapcount2; BUG_ON(!PageLocked(page)); if (!page_mapped(page)) return 0; mapping = page_mapping(page); if (!mapping) return 0; + mapcount1 = page_mapcount(page); // rmap_walk need to change to count mapping and return value // in .mapcount easy one rmap_walk(page, &rwc); + if (PageDMAPined(page)) { + int rc2; + + if (mapcount1 == real_count) { + /* Page is no longer pin, no zap pte race */ + ClearPageDMAPined(page); + goto out; + } + /* No new mapping of the page so mp1 < rc is illegal. */ + VM_BUG_ON(mapcount1 < real_count); + /* Page might be pin. */ + mapcount2 = page_mapcount(page); + if (mapcount2 > real_count) { + /* Page is pin for sure. */ + goto out; + } + /* We had a race with zap pte we need to rewalk again. */ + rc2 = real_mapcount; + real_mapcount = 0; + rwc.rmap_one = page_pin_one; + rmap_walk(page, &rwc); + if (mapcount2 <= (real_count + rc2)) { + /* Page is no longer pin */ + ClearPageDMAPined(page); + } + /* At this point the page pin flag reflect pin status of the page */ + } + +out: ... } The page_pin_one() function count the number of special PTE entry so which match the count of pte that have been zapped since the first reverse map walk. So worst case a page that was pin by a GUP would need 2 reverse map walk during page_mkclean(). Moreover this is only needed if we race with something that clear pte. I believe this is an acceptable worst case. I will work on some RFC patchset next week (once i am down with email catch up). I do not think i made mistake here, i have been torturing my mind trying to think of any race scenario and i believe it holds to any racing zap and page_mkclean() Cheers, J?r?me