Received: by 10.192.165.148 with SMTP id m20csp4104087imm; Mon, 30 Apr 2018 11:48:41 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo74tGoxdISFHu3ADpDtf/tCWkS1yrKZeaxZFEguve5oP5IIGms6fRT0kObXJvoR4t0dKZU X-Received: by 2002:a17:902:b60a:: with SMTP id b10-v6mr13192509pls.221.1525114121673; Mon, 30 Apr 2018 11:48:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525114121; cv=none; d=google.com; s=arc-20160816; b=F3o9jVClDjWaLDj9u+JmPNgttZAqJjr60Wh/SRKbXFgCkeXLj1PwrEioQgKpN220sc mBrEDLVbHvoWnouZ+dQP++MyOIi6iWslS3LcDnkwW1ARUrClWg/P/rneWMBADZ66VNXv Uyc5X2meeIz1iKFyEOO72s1q8DbrdENYLIasjoAJboCBx/S0kjUXNdK8JMXlf4iRQoHd E4QzNlmzOzn2daYgRuxP1Sw7YY+SaXV1DOC49k03czrTXYKyte0/nH/LnmHQPoFkjEBf c5wJnVluKgh458aGGFd45EpTxs96ilb3bL6syjeP9OD5gV9CF8urxSWQRysqNhjf0c+R 514g== 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 :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=okTDKtkfq8hxYUoRibB7U965sWne3nPRVRxxlDMDAK8=; b=WlnwcK9ijmNspj1MBcsQko3b6ESbyuTW+RrJfRKPxcpKPGo/83WS2UvaqDPwqntTaK vyjcKXhQgLdfZmi/tKzwLYLpAJylLAzsYc025U57+cdrzN09gh6Q5FeK4oEWHn2LoJfv Z7o7/Zo0B1GguIcQKGNweopkJzxL7dGpHujc+JiWjX2CNhS4pCUS5jrzoeeD43Kf60dK Nd950yRR1OppX4FQ+jZY84rFboOJGF2krmZSLohNTj1l/rG+xChDO7jITOTQh9KvauhP HXsCkzaF4ygY1sh2iRkuy2ZDvxGmcxrumQZ7xHNl50C8KKFOCVE4PDhyUTQuat8tXasG 0fDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Nco4Q4Sj; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x20-v6si6339546pln.235.2018.04.30.11.48.27; Mon, 30 Apr 2018 11:48:41 -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=@gmail.com header.s=20161025 header.b=Nco4Q4Sj; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754398AbeD3SsQ (ORCPT + 99 others); Mon, 30 Apr 2018 14:48:16 -0400 Received: from mail-qt0-f195.google.com ([209.85.216.195]:44033 "EHLO mail-qt0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753397AbeD3SsO (ORCPT ); Mon, 30 Apr 2018 14:48:14 -0400 Received: by mail-qt0-f195.google.com with SMTP id d3-v6so12089379qtp.11 for ; Mon, 30 Apr 2018 11:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=okTDKtkfq8hxYUoRibB7U965sWne3nPRVRxxlDMDAK8=; b=Nco4Q4SjZMm1rZjmgiK7yP7QIk/1Rn17AsqHk6TBCWxlWgFk6DIMXVncuxKK7A6cBj J+UWvhUds8XZnwCcu42liDMiQo2LTXWJIc1P4qvghAXLypr0GsaxRYQFcAHE3cnlCJzg DfRYBeKuOz4SSjz78sN5XwEXxSbiwml4DM0V9znph6BNCyhm9TYBYAPHuo3xBxV3yd7k EUEGip9R03vbdRSm9L97GIScolm6Bqc8kZXDjL/eD4odNn326y7QEW3M/UKHo10MjMFZ c2ynjdXT23Lfyuug7/L2iRtjEYVqf7AcSex/uzmUH2C8bQbROGuzld0swxg/SKoJAHcq SjgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=okTDKtkfq8hxYUoRibB7U965sWne3nPRVRxxlDMDAK8=; b=b0oUVKg/lwD9c1/KVhP+CRbF7ZzwlaTVsO2qhpXljUf0ZhmHWgTDCiLuJATpxjDMRh 6ppf2NBfmezryTIQGkiOl5giAS6t2SQYHCs1l56gxs1GDJvwDh+12fiThhsv6T6kFepc D8WFZwY13KKWSzpjPTF5CUANn/8u47JzRR0qEhzZCvd5csrbuhIkieaowGWLUbiyCeq0 lKZ8VZYRJFhIDJ7ud3BISX/3iKGsb2zKtBJ6kZlkome4+szH+gkvLXsdmNrqJpdVzJ3f b4a1+K9RrivmBttklQwPKb4WKJRuHsBD5K9nfv1M7i8S2CTn7aAvYNVlFMxnTg9ETY6b RD/w== X-Gm-Message-State: ALQs6tAtY71DqWIzHhkx9cn+jfcF3W2z+jp/0tDelPuvO/CFDOfS533w ftdgeGRxAAkihac4nVhm6S/Rr/gP5LpOBF08Rt0= X-Received: by 2002:a0c:8464:: with SMTP id l91-v6mr11670758qva.15.1525114093940; Mon, 30 Apr 2018 11:48:13 -0700 (PDT) MIME-Version: 1.0 Received: by 10.200.53.19 with HTTP; Mon, 30 Apr 2018 11:47:53 -0700 (PDT) In-Reply-To: <1523975611-15978-18-git-send-email-ldufour@linux.vnet.ibm.com> References: <1523975611-15978-1-git-send-email-ldufour@linux.vnet.ibm.com> <1523975611-15978-18-git-send-email-ldufour@linux.vnet.ibm.com> From: Punit Agrawal Date: Mon, 30 Apr 2018 19:47:53 +0100 Message-ID: Subject: Re: [PATCH v10 17/25] mm: protect mm_rb tree with a rwlock To: Laurent Dufour Cc: akpm@linux-foundation.org, mhocko@kernel.org, peterz@infradead.org, kirill@shutemov.name, ak@linux.intel.com, dave@stgolabs.net, jack@suse.cz, Matthew Wilcox , benh@kernel.crashing.org, mpe@ellerman.id.au, paulus@samba.org, Thomas Gleixner , Ingo Molnar , hpa@zytor.com, Will Deacon , Sergey Senozhatsky , Andrea Arcangeli , Alexei Starovoitov , kemi.wang@intel.com, sergey.senozhatsky.work@gmail.com, Daniel Jordan , David Rientjes , Jerome Glisse , Ganesh Mahendran , linux-kernel@vger.kernel.org, linux-mm@kvack.org, haren@linux.vnet.ibm.com, khandual@linux.vnet.ibm.com, npiggin@gmail.com, bsingharora@gmail.com, paulmck@linux.vnet.ibm.com, Tim Chen , linuxppc-dev@lists.ozlabs.org, x86@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 Hi Laurent, One nitpick below. On Tue, Apr 17, 2018 at 3:33 PM, Laurent Dufour wrote: > This change is inspired by the Peter's proposal patch [1] which was > protecting the VMA using SRCU. Unfortunately, SRCU is not scaling well in > that particular case, and it is introducing major performance degradation > due to excessive scheduling operations. > > To allow access to the mm_rb tree without grabbing the mmap_sem, this patch > is protecting it access using a rwlock. As the mm_rb tree is a O(log n) > search it is safe to protect it using such a lock. The VMA cache is not > protected by the new rwlock and it should not be used without holding the > mmap_sem. > > To allow the picked VMA structure to be used once the rwlock is released, a > use count is added to the VMA structure. When the VMA is allocated it is > set to 1. Each time the VMA is picked with the rwlock held its use count > is incremented. Each time the VMA is released it is decremented. When the > use count hits zero, this means that the VMA is no more used and should be > freed. > > This patch is preparing for 2 kind of VMA access : > - as usual, under the control of the mmap_sem, > - without holding the mmap_sem for the speculative page fault handler. > > Access done under the control the mmap_sem doesn't require to grab the > rwlock to protect read access to the mm_rb tree, but access in write must > be done under the protection of the rwlock too. This affects inserting and > removing of elements in the RB tree. > > The patch is introducing 2 new functions: > - vma_get() to find a VMA based on an address by holding the new rwlock. > - vma_put() to release the VMA when its no more used. > These services are designed to be used when access are made to the RB tree > without holding the mmap_sem. > > When a VMA is removed from the RB tree, its vma->vm_rb field is cleared and > we rely on the WMB done when releasing the rwlock to serialize the write > with the RMB done in a later patch to check for the VMA's validity. > > When free_vma is called, the file associated with the VMA is closed > immediately, but the policy and the file structure remained in used until > the VMA's use count reach 0, which may happens later when exiting an > in progress speculative page fault. > > [1] https://patchwork.kernel.org/patch/5108281/ > > Cc: Peter Zijlstra (Intel) > Cc: Matthew Wilcox > Signed-off-by: Laurent Dufour > --- > include/linux/mm.h | 1 + > include/linux/mm_types.h | 4 ++ > kernel/fork.c | 3 ++ > mm/init-mm.c | 3 ++ > mm/internal.h | 6 +++ > mm/mmap.c | 115 +++++++++++++++++++++++++++++++++++------------ > 6 files changed, 104 insertions(+), 28 deletions(-) > [...] > diff --git a/mm/mmap.c b/mm/mmap.c > index 5601f1ef8bb9..a82950960f2e 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -160,6 +160,27 @@ void unlink_file_vma(struct vm_area_struct *vma) > } > } > > +static void __free_vma(struct vm_area_struct *vma) > +{ > + if (vma->vm_file) > + fput(vma->vm_file); > + mpol_put(vma_policy(vma)); > + kmem_cache_free(vm_area_cachep, vma); > +} > + > +#ifdef CONFIG_SPECULATIVE_PAGE_FAULT > +void put_vma(struct vm_area_struct *vma) > +{ > + if (atomic_dec_and_test(&vma->vm_ref_count)) > + __free_vma(vma); > +} > +#else > +static inline void put_vma(struct vm_area_struct *vma) > +{ > + return __free_vma(vma); Please drop the "return". Thanks, Punit [...]