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 E15D1EB64DA for ; Wed, 12 Jul 2023 18:50:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3E5598D0006; Wed, 12 Jul 2023 14:50:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 36E818D0002; Wed, 12 Jul 2023 14:50:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E7C28D0006; Wed, 12 Jul 2023 14:50:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 082EE8D0002 for ; Wed, 12 Jul 2023 14:50:04 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AA705B0399 for ; Wed, 12 Jul 2023 18:50:03 +0000 (UTC) X-FDA: 81003849486.07.8F0D829 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf27.hostedemail.com (Postfix) with ESMTP id 50B6640016 for ; Wed, 12 Jul 2023 18:50:00 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=Cmysr7Uz; dmarc=none; spf=none (imf27.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689187801; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LPJq7msReVGM7XD8PLFEQXBzJhFOFc1muivhRaapYBk=; b=OdK+e3pub48SZmC0CIdbUWl5r3n62mq3U5uRdlRKKMDtnMVUhtBbyUMz3qS9/vTCWEyz1U Me4NVpCKCkQpZQNSqVUDK0uWVO7WfxLvbnQ/V3nW6oCP0bW0d0phCsL3Do+qnJz7twDLWW x3+9fA91d4OR48EmhDWPD5vYCJ4qlvM= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=Cmysr7Uz; dmarc=none; spf=none (imf27.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689187801; a=rsa-sha256; cv=none; b=FXgn8pVI2l7Cu48lwurhAgMmLvlFN7JXOix6OBy/msw/lR3Gytff3AJ+03UzmYlwm6onjN 6P+cbqA9lm3mWvJ/zauJrsCBh2Ym81y8kg/FT1s4DLGyHW/iAKonoshhVbFMWyR2P9yKBW gGuj70ePSElUtm8jtGfwte5kiRpFu7k= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=LPJq7msReVGM7XD8PLFEQXBzJhFOFc1muivhRaapYBk=; b=Cmysr7UzDFGp5KznYUFwAk6AKu VCyECjVAgg7CdMgKP2gIXT9LqIN1W2UJyUMzKOHG52PP6DYkjf8K62Vk3/2CuB20Ta8e7l7BVF8mN 2KJBuMRm9yHtaSwiXKiVhkXfC9/YtHErgp1gJ6toCgVLvdZoJypDn9OfB3X/kWeNyO3vvcaXbaoZE +gnmH2UQDzHZoomp1vqHrp5kEbDch1hlZNV3W5YRlEpxFulzxsiC6qhG4hriSP9v/REST1tRfru41 vIY0aphe5sVPGsQsA3RrNX6lNn9UV35JUWtXdzw3GGJtBD+Imx0sntuBI6+ayUHgRlMCS7JUky48R 3wfn/HAg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qJeuP-00GzFa-VL; Wed, 12 Jul 2023 18:49:54 +0000 Date: Wed, 12 Jul 2023 19:49:53 +0100 From: Matthew Wilcox To: David Hildenbrand Cc: Suren Baghdasaryan , Dan Carpenter , linux-mm@kvack.org, Andrew Morton , "Liam R. Howlett" , Laurent Dufour , Michel Lespinasse , Jerome Glisse , Michal Hocko , Vlastimil Babka , Johannes Weiner , Peter Xu , Dimitri Sivanich , Mike Travis , Steve Wahl Subject: Re: [bug report] mm: replace vma->vm_flags direct modifications with modifier calls Message-ID: References: <9704a138-60e6-4ede-91f0-844e1df2ad84@moroto.mountain> <331201b2-5f13-8e81-b5d4-b17f8784d498@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <331201b2-5f13-8e81-b5d4-b17f8784d498@redhat.com> X-Rspamd-Queue-Id: 50B6640016 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 5usqg55q65kuqwexgrz8e4douaofwz93 X-HE-Tag: 1689187800-379551 X-HE-Meta: U2FsdGVkX18NQKAIpZo9jxq6m8Nk4+a2XFscZrAWTnCYyhuUWqIYeoPg+duCFR5V+xm28oPy/X7NIhG4pNhIKRXLZ8VRgsiHeBsds6//NoRvRPwNftGL9pOk6fZatRFQwiQPsG5u/nNyPh5HyNr29ea/D8QxI2Ra3daO7pjx9onvNLl5OvP1gjjaW5pK+Jpl2Et/vqQ39itIeJYGMrVei2k2HtnsdgUUq/Eiz7nATH75YN7t5ZMz/asNo0+ZOH1Mn2V/aF9JHvTF3Cwx21qSC98W7vJ28Rln9rRy3SHcERaR18mipFzg7XEv3PIFouOWC1IVX/i9w/2tW18jTQxqjTeWKRbnI/o+HZ9AjseKJBj4BYMCdOhSzNsH0qkF5afk4E9kr0R7sMInLhWEjD+tfaqpedgzxUZ0MkwbrviZVx0/eSTvCabnGUGqb3h70jZPD2evs3NAPdmjMWkVv5l3bPAYbVMCjBuLfkHWQcY2emjogLknkDgbPkwp9KwN78xHhFx1w1wpEa59N21E0u1PDtTVwo68CsbYYlgR8CvDDRJGrqXegZ7xOb4ovx/JWKYTV/T/Jiia07Cfx4AcQdNE4Yp5R+ElWuKhthqHdHM0smk9ckISTjLkw8FlhSiMadr0AjIJmPdd04n3HgI1gHJNB+uUjfkZkJNlgR542VEgo+uYFJTonpoB6JWCtgTamCH98jRupDhuwflL1mpv3iZuHYsVTADfM3NPu0BGCptZYgQiEvg6CSDWoDybHIKucfMurO+28O8tQJEDMkKhe7Wz+jHXxhEq6n52upXyuVw+1EkomCbhYG9WUB7+Tj18M/UfEfEcR7azZFoTAKBoR45z7I29EF5Yc0J/DJhnDvUem0L4yBQkZlR6aTiftz6vxR+io53aRr4MBv8Wi42z4I7Uk2BfgD+Xk+1GJo4ReHwFWCxm5bFg4i/pvyYN/kQjYaKd3sbPFuyIynSWxHUi47q O9j+ovp0 eG39pTqK/LYQGadSP+KYhoAJA1ZZPijbTtVHPAkEXcvO7frho7LqHwKt5gMfstPQl87nUi5fvCWmCqn2TbEdaqFsT7305creXP5hlSYfL/o7yX/p+WQvH7IGE3g710Fz8FSt+B+7mgVjocfOCHDU9c5s49/8InG8TKz6klW5B7y+Cp1PKQIx6HUZPQoFxSVnnxrYz 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, Jul 12, 2023 at 05:55:47PM +0200, David Hildenbrand wrote: > On 12.07.23 17:52, Matthew Wilcox wrote: > > On Wed, Jul 12, 2023 at 08:01:18AM -0700, Suren Baghdasaryan wrote: > > > Are you suggesting to break remap_pfn_range() into two stages > > > (remap_pfn_range_prepare() then remap_pfn_range())? > > > If so, there are many places remap_pfn_range() is called and IIUC all > > > of them would need to use that 2-stage approach (lots of code churn). > > > In addition, this is an exported function, so many more drivers might > > > expect the current behavior. > > > > You do not understand correctly. > > > > When somebody calls mmap, there are two reasonable implementations. > > Here's one: > > > > .mmap = snd_dma_iram_mmap, > > > > static int snd_dma_iram_mmap(struct snd_dma_buffer *dmab, > > struct vm_area_struct *area) > > { > > area->vm_page_prot = pgprot_writecombine(area->vm_page_prot); > > return remap_pfn_range(area, area->vm_start, > > dmab->addr >> PAGE_SHIFT, > > area->vm_end - area->vm_start, > > area->vm_page_prot); > > } > > > > This is _fine_. It is not called from the fault path, it is called in > > process context. Few locks are held (which ones aren't even > > documented!) > > > > The other way is to set vma->vm_ops. The fault handler in vm_ops > > should not be calling remap_pfn_range(). It should be calling > > set_ptes(). I almost have this driver fixed up, but I have another > > meeting to go to now. > > Just a note that we still have to make sure that the VMA flags will be set > properly -- I guess at mmap time is the right time as I suggested above. It actually does that already: static int gru_file_mmap(struct file *file, struct vm_area_struct *vma) { if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) != (VM_SHARED | VM_WRITE)) return -EPERM; if (vma->vm_start & (GRU_GSEG_PAGESIZE - 1) || vma->vm_end & (GRU_GSEG_PAGESIZE - 1)) return -EINVAL; vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_LOCKED | VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP); This compiles, but obviously I don't have a spare HP supercomputer lying around for me to test whether it works. Also set_ptes() was only just introduced to the mm tree, so doing something that needs backporting would take more effort (maybe having a private set_ptes() in the driver would be a good backport option that calls set_pte_at() in a loop). diff --git a/drivers/misc/sgi-gru/grumain.c b/drivers/misc/sgi-gru/grumain.c index 4eb4b9455139..c21bcb528f12 100644 --- a/drivers/misc/sgi-gru/grumain.c +++ b/drivers/misc/sgi-gru/grumain.c @@ -951,6 +951,8 @@ vm_fault_t gru_fault(struct vm_fault *vmf) } if (!gts->ts_gru) { + pte_t *ptep, pte; + STAT(load_user_context); if (!gru_assign_gru_context(gts)) { preempt_enable(); @@ -964,9 +966,12 @@ vm_fault_t gru_fault(struct vm_fault *vmf) } gru_load_context(gts); paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum); - remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1), - paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE, - vma->vm_page_prot); + + pte = pfn_pte(paddr / PAGE_SIZE, vma->vm_page_prot); + ptep = vmf->pte - (vaddr % GRU_GSEG_PAGESIZE) / PAGE_SIZE; + set_ptes(vma->vm_mm, vaddr & ~(GRU_GSEG_PAGESIZE - 1), + ptep, pte_mkspecial(pte), + GRU_GSEG_PAGESIZE / PAGE_SIZE); } preempt_enable();