From patchwork Fri Sep 25 22:25:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 11800933 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CC88E6CB for ; Fri, 25 Sep 2020 22:26:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 566F421775 for ; Fri, 25 Sep 2020 22:26:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="IV1w8iXx" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 566F421775 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 668216B005C; Fri, 25 Sep 2020 18:26:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 619C48E0001; Fri, 25 Sep 2020 18:26:02 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5079B6B0068; Fri, 25 Sep 2020 18:26:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0190.hostedemail.com [216.40.44.190]) by kanga.kvack.org (Postfix) with ESMTP id 365816B005C for ; Fri, 25 Sep 2020 18:26:02 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id EF5578249980 for ; Fri, 25 Sep 2020 22:26:01 +0000 (UTC) X-FDA: 77303017722.15.salt66_1907c372716b Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id C8B461814B0C7 for ; Fri, 25 Sep 2020 22:26:01 +0000 (UTC) X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,peterx@redhat.com,,RULES_HIT:30054:30070:30090,0,RBL:216.205.24.124:@redhat.com:.lbl8.mailshell.net-62.18.0.100 64.10.201.10;04yr8eq8r6gaatcw5f7kicdn7hpkkycenf5rxys16o9i3dezhco5o9kmka39qfa.jjadostqmin1gsg6d7fr7f6gkgotf8ibg4zo3jzu79ofs4cfb7jjpcz8hddxf4r.q-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:ft,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: salt66_1907c372716b X-Filterd-Recvd-Size: 6771 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Fri, 25 Sep 2020 22:26:01 +0000 (UTC) Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601072760; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d+o6VUGgCEedkH4qsZJWlfLcQyoccvndv43VtGbLQGg=; b=IV1w8iXxCstLg+0k7Y+sZMdCNA5fmFfqKBo4SgFfSjNZOyt5IjFoH3+1MKkcOS/TDNGQ73 zdb2gWRQA+D0bMD8+o3pw7pRLcBukgXng5hUEzfRYSuqGE5fyDGXYwxOCQNkxaXBgZbGyO HqxbYzNOiG5BFASb1ly+NpXh5Gsmk9M= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-397-r5LYy334Pu-TBqVgJH64Ig-1; Fri, 25 Sep 2020 18:25:59 -0400 X-MC-Unique: r5LYy334Pu-TBqVgJH64Ig-1 Received: by mail-qk1-f200.google.com with SMTP id 139so3165266qkl.11 for ; Fri, 25 Sep 2020 15:25:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=d+o6VUGgCEedkH4qsZJWlfLcQyoccvndv43VtGbLQGg=; b=YdkyRcNlISiIt7L2izWt7lN0ry806SR4u6nCqvtelmQIMjcQIZXZuRiQEb3lEUsWZM dlG/LOf7USTOLCJ/AxyBcQWARzWPuX8m+ecd1PSWD1MUR7e6ORbdG6o49O5WcreGGQCN TteUcXEN7Ye2Yy1qK4obtDgafeX/wtIzgg2h4k87ExjLl81AJO+QpqplZ3vSMl5f6oUF LmTinNKgeYbccBM2m4dUHh2hvGk7ntUj1LVssEqlYDW0UEbbk/qoVj3c6mIEWxQK/ddk 0j2EwqOshpuzYFJm4tfxa4bg+gXlh45Lr15Z7IHv4VqJR2cZGHkQQVldZx/aicQq+pS2 RstA== X-Gm-Message-State: AOAM533NrsyM9aWl5Rd22HA+RuGQZ1ArvcoeIq0UyBA+JSrjvw+j6cPc Q3gERDPaG2bWOj4ve8JdytQni+9SZkbv2f18HUrgocdS58au2WbNZZH1LsKWvgui5Z42C8gm9ca /aY50glBjDB0= X-Received: by 2002:a37:9ed3:: with SMTP id h202mr2282052qke.66.1601072758428; Fri, 25 Sep 2020 15:25:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRrlc9138emjhNCE1DdjfK+f5739ImDYiquqqyoPqiCh+4PuUBO3ufEAh8Ba/zMvHD/ji8FQ== X-Received: by 2002:a37:9ed3:: with SMTP id h202mr2282025qke.66.1601072758162; Fri, 25 Sep 2020 15:25:58 -0700 (PDT) Received: from localhost.localdomain (bras-vprn-toroon474qw-lp130-11-70-53-122-15.dsl.bell.ca. [70.53.122.15]) by smtp.gmail.com with ESMTPSA id w44sm3051471qth.9.2020.09.25.15.25.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Sep 2020 15:25:57 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: peterx@redhat.com, Jason Gunthorpe , John Hubbard , Andrew Morton , Christoph Hellwig , Yang Shi , Oleg Nesterov , Kirill Tkhai , Kirill Shutemov , Hugh Dickins , Jann Horn , Linus Torvalds , Michal Hocko , Jan Kara , Andrea Arcangeli , Leon Romanovsky Subject: [PATCH v2 1/4] mm: Introduce mm_struct.has_pinned Date: Fri, 25 Sep 2020 18:25:57 -0400 Message-Id: <20200925222600.6832-2-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200925222600.6832-1-peterx@redhat.com> References: <20200925222600.6832-1-peterx@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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: (Commit message majorly collected from Jason Gunthorpe) Reduce the chance of false positive from page_maybe_dma_pinned() by keeping track if the mm_struct has ever been used with pin_user_pages(). This allows cases that might drive up the page ref_count to avoid any penalty from handling dma_pinned pages. Future work is planned, to provide a more sophisticated solution, likely to turn it into a real counter. For now, make it atomic_t but use it as a boolean for simplicity. Suggested-by: Jason Gunthorpe Signed-off-by: Peter Xu --- include/linux/mm_types.h | 10 ++++++++++ kernel/fork.c | 1 + mm/gup.c | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 496c3ff97cce..ed028af3cb19 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -436,6 +436,16 @@ struct mm_struct { */ atomic_t mm_count; + /** + * @has_pinned: Whether this mm has pinned any pages. This can + * be either replaced in the future by @pinned_vm when it + * becomes stable, or grow into a counter on its own. We're + * aggresive on this bit now - even if the pinned pages were + * unpinned later on, we'll still keep this bit set for the + * lifecycle of this mm just for simplicity. + */ + atomic_t has_pinned; + #ifdef CONFIG_MMU atomic_long_t pgtables_bytes; /* PTE page table pages */ #endif diff --git a/kernel/fork.c b/kernel/fork.c index 49677d668de4..e65d8192d080 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1011,6 +1011,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, mm_pgtables_bytes_init(mm); mm->map_count = 0; mm->locked_vm = 0; + atomic_set(&mm->has_pinned, 0); atomic64_set(&mm->pinned_vm, 0); memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); spin_lock_init(&mm->page_table_lock); diff --git a/mm/gup.c b/mm/gup.c index e5739a1974d5..238667445337 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1255,6 +1255,9 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm, BUG_ON(*locked != 1); } + if (flags & FOLL_PIN) + atomic_set(¤t->mm->has_pinned, 1); + /* * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior * is to set FOLL_GET if the caller wants pages[] filled in (but has @@ -2660,6 +2663,9 @@ static int internal_get_user_pages_fast(unsigned long start, int nr_pages, FOLL_FAST_ONLY))) return -EINVAL; + if (gup_flags & FOLL_PIN) + atomic_set(¤t->mm->has_pinned, 1); + if (!(gup_flags & FOLL_FAST_ONLY)) might_lock_read(¤t->mm->mmap_lock);