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 X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96E2BC433E0 for ; Mon, 15 Mar 2021 07:42:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1320764E33 for ; Mon, 15 Mar 2021 07:42:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1320764E33 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9C2BA6B0070; Mon, 15 Mar 2021 03:42:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9729F6B0071; Mon, 15 Mar 2021 03:42:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 815C86B0072; Mon, 15 Mar 2021 03:42:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0091.hostedemail.com [216.40.44.91]) by kanga.kvack.org (Postfix) with ESMTP id 656786B0070 for ; Mon, 15 Mar 2021 03:42:53 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 1D75A181AF5CC for ; Mon, 15 Mar 2021 07:42:53 +0000 (UTC) X-FDA: 77921317026.20.3122FC9 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf16.hostedemail.com (Postfix) with ESMTP id AC8EB801914F for ; Mon, 15 Mar 2021 07:42:52 +0000 (UTC) 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=a0crwDimWuDDFbQSyz3+xFwWCr0UuEjinSqp5FgMyJQ=; b=GonZpIh8oQ35/lgbuyAazr754g ymArFIK48vHRADKrI7FoPunJP2FDYfPjcCFFX8sZOOXbc5S0+Tv0oLBvzXawhHXhrDIxKE2vDdWGc 0ZFmYCNt1B0CpKBLvCNl+zazYzJnc05ohUmEkHMLdEde6ONgnLxWkSQkghMm73TA7msRuL53MreVd AHAltoj4GcwxUbanRdRiGgz9xDB8SKQ7lpeR2RZ6HcMqRqRk1kwyW+b9j8UPDCRdVTTn5Vwpcac+x i0gb0GbEuyZejWop1J3hTZ0QbU8pjf43J2fzmYlAPV68tQYl/0aoNYn8jAGzrDPnnby+8dB0IbjDU weKd2ByQ==; Received: from hch by casper.infradead.org with local (Exim 4.94 #2 (Red Hat Linux)) id 1lLhsD-00HNdD-P4; Mon, 15 Mar 2021 07:42:46 +0000 Date: Mon, 15 Mar 2021 07:42:45 +0000 From: Christoph Hellwig To: Alistair Popple Cc: linux-mm@kvack.org, nouveau@lists.freedesktop.org, bskeggs@redhat.com, akpm@linux-foundation.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kvm-ppc@vger.kernel.org, dri-devel@lists.freedesktop.org, jhubbard@nvidia.com, rcampbell@nvidia.com, jglisse@redhat.com, jgg@nvidia.com, hch@infradead.org, daniel@ffwll.ch, willy@infradead.org Subject: Re: [PATCH v6 5/8] mm: Device exclusive memory access Message-ID: <20210315074245.GC4136862@infradead.org> References: <20210312083851.15981-1-apopple@nvidia.com> <20210312083851.15981-6-apopple@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210312083851.15981-6-apopple@nvidia.com> X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html X-Stat-Signature: g7gn5uew1pff4ed5kifw4gmxpsqjjgo7 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: AC8EB801914F Received-SPF: none (casper.srs.infradead.org>: No applicable sender policy available) receiver=imf16; identity=mailfrom; envelope-from=""; helo=casper.infradead.org; client-ip=90.155.50.34 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1615794172-298268 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: > +Not all devices support atomic access to system memory. To support atomic > +operations to a shared virtual memory page such a device needs access to that > +page which is exclusive of any userspace access from the CPU. The > +``make_device_exclusive_range()`` function can be used to make a memory range > +inaccessible from userspace. s/Not all devices/Some devices/ ? > static inline int mm_has_notifiers(struct mm_struct *mm) > @@ -528,7 +534,17 @@ static inline void mmu_notifier_range_init_migrate( > { > mmu_notifier_range_init(range, MMU_NOTIFY_MIGRATE, flags, vma, mm, > start, end); > - range->migrate_pgmap_owner = pgmap; > + range->owner = pgmap; > +} > + > +static inline void mmu_notifier_range_init_exclusive( > + struct mmu_notifier_range *range, unsigned int flags, > + struct vm_area_struct *vma, struct mm_struct *mm, > + unsigned long start, unsigned long end, void *owner) > +{ > + mmu_notifier_range_init(range, MMU_NOTIFY_EXCLUSIVE, flags, vma, mm, > + start, end); > + range->owner = owner; Maybe just replace mmu_notifier_range_init_migrate with a mmu_notifier_range_init_owner helper that takes the owner but does not hard code a type? > } > + } else if (is_device_exclusive_entry(entry)) { > + page = pfn_swap_entry_to_page(entry); > + > + get_page(page); > + rss[mm_counter(page)]++; > + > + if (is_writable_device_exclusive_entry(entry) && > + is_cow_mapping(vm_flags)) { > + /* > + * COW mappings require pages in both > + * parent and child to be set to read. > + */ > + entry = make_readable_device_exclusive_entry( > + swp_offset(entry)); > + pte = swp_entry_to_pte(entry); > + if (pte_swp_soft_dirty(*src_pte)) > + pte = pte_swp_mksoft_dirty(pte); > + if (pte_swp_uffd_wp(*src_pte)) > + pte = pte_swp_mkuffd_wp(pte); > + set_pte_at(src_mm, addr, src_pte, pte); > + } Just cosmetic, but I wonder if should factor this code block into a little helper. > + > +static bool try_to_protect_one(struct page *page, struct vm_area_struct *vma, > + unsigned long address, void *arg) > +{ > + struct mm_struct *mm = vma->vm_mm; > + struct page_vma_mapped_walk pvmw = { > + .page = page, > + .vma = vma, > + .address = address, > + }; > + struct ttp_args *ttp = (struct ttp_args *) arg; This cast should not be needed. > + return ttp.valid && (!page_mapcount(page) ? true : false); This can be simplified to: return ttp.valid && !page_mapcount(page); > + npages = get_user_pages_remote(mm, start, npages, > + FOLL_GET | FOLL_WRITE | FOLL_SPLIT_PMD, > + pages, NULL, NULL); > + for (i = 0; i < npages; i++, start += PAGE_SIZE) { > + if (!trylock_page(pages[i])) { > + put_page(pages[i]); > + pages[i] = NULL; > + continue; > + } > + > + if (!try_to_protect(pages[i], mm, start, arg)) { > + unlock_page(pages[i]); > + put_page(pages[i]); > + pages[i] = NULL; > + } Should the trylock_page go into try_to_protect to simplify the loop a little? Also I wonder if we need make_device_exclusive_range or should just open code the get_user_pages_remote + try_to_protect loop in the callers, as that might allow them to also deduct other information about the found pages. Otherwise looks good: Reviewed-by: Christoph Hellwig