Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp357445imm; Thu, 11 Oct 2018 22:35:21 -0700 (PDT) X-Google-Smtp-Source: ACcGV63Y8HmXlqtfdVL7zKq3PFXa0NfiVXo3KwKbSL4cyBmOBLYi/B44/WW9VlLrVuEce/FqrsjA X-Received: by 2002:a17:902:7142:: with SMTP id u2-v6mr4518907plm.154.1539322521364; Thu, 11 Oct 2018 22:35:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539322521; cv=none; d=google.com; s=arc-20160816; b=h1OKyAOYVaSDl7uBGLJXR7FmnEWfzBCNyt8gfwU5VscX9cwTM75gDZo6mmmeygjCUR vM71UBF/CmDZVqEPUPSRIbqROwsj0IM2lQDyPSsEIUSPL1HAOENwMrufVQ0ccGLihuoq oTRGNscfspJIx13iJ0mIwjeA8V69VoPpOeRUeAR5sbMXciAp3vjLHpjurav1KYTwI36b IeDldvFL6PFVIzR+0qX/024/JXd+WYlnT4vDlrxxwxuvv1wgt+0rb4hbBRIy1Gr7B1co U6/nQwGoavkokTwEDBVj1aE9waRRA1WA8YwA/CRSOn5/jxhLdeA+e97UKGwhkn+CT5j8 wI3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=i3DEy2LhGTMU9lfPm4HI6S/P1WXfxtGZCDnRboWhqW0=; b=Ty9ReaRUrf17XkLC94kUtIkDONt466z0VbQK2Gl2LEENjxrI8DeTvLc6RvbW8Fadak cGimPYiaWOob1jO9DANl+LmV/QOwI1mYLEnG8aVk2FqUvg434HHGTnul5eW6wO8Fi6iK m1Qv3i/7+ZV8VUpnoA5o2PWcGWjIOHo7p3fo74SPJWSM22YGAMPurSgZgQk2O8jgiq2e ItUTx1S6515CPffMNZnMiGyNNBhO/fgfqBw621VzBI5THJKn+9SAROa623xXcUh4ilNn rBAiFkV+eNV5eroGEIE3eAv7EL7NJfxbvkG3GspCrDX7O+PfyTo6YO8Y1LXUOdC6e9D7 P7YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=GN3vBNv3; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w15-v6si134364pgg.529.2018.10.11.22.35.05; Thu, 11 Oct 2018 22:35:21 -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=@google.com header.s=20161025 header.b=GN3vBNv3; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727443AbeJLNFR (ORCPT + 99 others); Fri, 12 Oct 2018 09:05:17 -0400 Received: from mail-oi1-f194.google.com ([209.85.167.194]:38170 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727056AbeJLNFQ (ORCPT ); Fri, 12 Oct 2018 09:05:16 -0400 Received: by mail-oi1-f194.google.com with SMTP id u197-v6so8971103oif.5 for ; Thu, 11 Oct 2018 22:34:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=i3DEy2LhGTMU9lfPm4HI6S/P1WXfxtGZCDnRboWhqW0=; b=GN3vBNv3QnOlijn1AH3C7KvP+imDJFP2OuRAzc8q6aeMzhMidHc+x5fznzsQvxMd9b LYN/If6IrPONWAzhDxJR4/k051gGVHGO8Ciib19RjRvVNlqqafohYtz9A3+VM5M7zWxO 6SOSTmfqZHP5fWWINbWnvrRIuoAAn3Pta43gbWOatBiFdya/TYijGQENCdqnDpa0pUTg 0NCjgpy590mDS9HOt0cBIf2A1hDI6tkit3aTP3DnozuftegOLa0FAzt4RRLGADttx4lm aBZox6Wfj1r5ro5unOZi+ANwxwwJQoGSrYUHGQ1TE0EUsQLl4PHoArIzdGb6dfdVWKxs jOyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=i3DEy2LhGTMU9lfPm4HI6S/P1WXfxtGZCDnRboWhqW0=; b=ezS1exxfXTOXDPei8TJVQtSjHWS50WiAR7iVvzHba1Di/s9Na185CRtwXKaU6qJs2N udgYjA4Xn327fICgaNApaxKlSZptU+jp1+caK56FcizL3rbAZPxd67B7BPKyhu87OfWq ekDS039d2HFkK20AffEkk597VvdjmsWRz/tq9a7aAVCugDFjPBPwaXw7QpVERkpmn7DU d0Klx9VS8UO6uoeJ7SOwLwGUbtl2Z1KFgPyttTH0ubql9uFU2n6XqTDTiI0SKFS1SxY8 zw/annWvv8SuXP5cskSg0fPWd+RmyYrXcOk4mTjrwYxx0K7x6XxT8nhdeOGLIasFVQXs sDOQ== X-Gm-Message-State: ABuFfogaQfeMPOOvcWcrtI5IHnOEJ76MpTTw+zP4jZAIeGUXSsjyWSMJ PbpfYemvbQFCE0Y3AAlO77gB/mMCm+dmLZa2FOubYw== X-Received: by 2002:aca:490b:: with SMTP id w11-v6mr2428226oia.126.1539322472858; Thu, 11 Oct 2018 22:34:32 -0700 (PDT) MIME-Version: 1.0 References: <20181009201400.168705-1-joel@joelfernandes.org> <42b81ac4-35de-754e-545b-d57b3bab3b7a@suse.com> In-Reply-To: <42b81ac4-35de-754e-545b-d57b3bab3b7a@suse.com> From: Jann Horn Date: Fri, 12 Oct 2018 07:34:05 +0200 Message-ID: Subject: Re: [PATCH] mm: Speed up mremap on large regions To: Juergen Gross Cc: joel@joelfernandes.org, kernel list , Linux-MM , kernel-team@android.com, Minchan Kim , Hugh Dickins , lokeshgidra@google.com, Andrew Morton , Greg Kroah-Hartman , Kate Stewart , pombredanne@nexb.com, Thomas Gleixner , Boris Ostrovsky , Paolo Bonzini , =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 12, 2018 at 7:29 AM Juergen Gross wrote: > On 12/10/2018 05:21, Jann Horn wrote: > > +cc xen maintainers and kvm folks > > > > On Fri, Oct 12, 2018 at 4:40 AM Joel Fernandes (Google) > > wrote: > >> Android needs to mremap large regions of memory during memory management > >> related operations. The mremap system call can be really slow if THP is > >> not enabled. The bottleneck is move_page_tables, which is copying each > >> pte at a time, and can be really slow across a large map. Turning on THP > >> may not be a viable option, and is not for us. This patch speeds up the > >> performance for non-THP system by copying at the PMD level when possible. > > [...] > >> +bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr, > >> + unsigned long new_addr, unsigned long old_end, > >> + pmd_t *old_pmd, pmd_t *new_pmd, bool *need_flush) > >> +{ > > [...] > >> + /* > >> + * We don't have to worry about the ordering of src and dst > >> + * ptlocks because exclusive mmap_sem prevents deadlock. > >> + */ > >> + old_ptl = pmd_lock(vma->vm_mm, old_pmd); > >> + if (old_ptl) { > >> + pmd_t pmd; > >> + > >> + new_ptl = pmd_lockptr(mm, new_pmd); > >> + if (new_ptl != old_ptl) > >> + spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); > >> + > >> + /* Clear the pmd */ > >> + pmd = *old_pmd; > >> + pmd_clear(old_pmd); > >> + > >> + VM_BUG_ON(!pmd_none(*new_pmd)); > >> + > >> + /* Set the new pmd */ > >> + set_pmd_at(mm, new_addr, new_pmd, pmd); > >> + if (new_ptl != old_ptl) > >> + spin_unlock(new_ptl); > >> + spin_unlock(old_ptl); > > > > How does this interact with Xen PV? From a quick look at the Xen PV > > integration code in xen_alloc_ptpage(), it looks to me as if, in a > > config that doesn't use split ptlocks, this is going to temporarily > > drop Xen's type count for the page to zero, causing Xen to de-validate > > and then re-validate the L1 pagetable; if you first set the new pmd > > before clearing the old one, that wouldn't happen. I don't know how > > this interacts with shadow paging implementations. > > No, this isn't an issue. As the L1 pagetable isn't being released it > will stay pinned, so there will be no need to revalidate it. Where exactly is the L1 pagetable pinned? xen_alloc_ptpage() does: if (static_branch_likely(&xen_struct_pages_ready)) SetPagePinned(page); if (!PageHighMem(page)) { xen_mc_batch(); __set_pfn_prot(pfn, PAGE_KERNEL_RO); if (level == PT_PTE && USE_SPLIT_PTE_PTLOCKS) __pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn); xen_mc_issue(PARAVIRT_LAZY_MMU); } else { /* make sure there are no stray mappings of this page */ kmap_flush_unused(); } which means that if USE_SPLIT_PTE_PTLOCKS is false, the table doesn't get pinned and only stays typed as long as it is referenced by an L2 table, right?