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=-12.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 F33F7C35257 for ; Mon, 21 Sep 2020 21:17:54 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7548C23A61 for ; Mon, 21 Sep 2020 21:17:54 +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="cAHVbzeZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7548C23A61 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 979A88E0003; Mon, 21 Sep 2020 17:17:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 907168E0001; Mon, 21 Sep 2020 17:17:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70BE18E0003; Mon, 21 Sep 2020 17:17:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0182.hostedemail.com [216.40.44.182]) by kanga.kvack.org (Postfix) with ESMTP id 542A88E0001 for ; Mon, 21 Sep 2020 17:17:53 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 10A968249980 for ; Mon, 21 Sep 2020 21:17:53 +0000 (UTC) X-FDA: 77288330826.06.twig32_221866827148 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id E2EB1100B8340 for ; Mon, 21 Sep 2020 21:17:52 +0000 (UTC) X-HE-Tag: twig32_221866827148 X-Filterd-Recvd-Size: 6822 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf20.hostedemail.com (Postfix) with ESMTP for ; Mon, 21 Sep 2020 21:17:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600723071; 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=PpHRYhcxU0Jm84kdlicXVMnaAbcYEgTsJ/Su9ANodSU=; b=cAHVbzeZtJ7DuC6rHK7wU2jnA5LZWtKHqId1Y4Wrro6tZQ0ugPhqNxLcYqZTS/mj4J16PX qYUY1o05zLKa69FcVwLyhDrj44cWrdIaj1ZbxfpX3lmzu6RIGPVxQ1kNV7O0qXvLZtFrRk uDt35i91X0vxD8zMq0qE5mseFz8xqKo= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-565-14REDEzaMU6xaqI_xFxYEA-1; Mon, 21 Sep 2020 17:17:50 -0400 X-MC-Unique: 14REDEzaMU6xaqI_xFxYEA-1 Received: by mail-qk1-f198.google.com with SMTP id r128so12043383qkc.9 for ; Mon, 21 Sep 2020 14:17:50 -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=PpHRYhcxU0Jm84kdlicXVMnaAbcYEgTsJ/Su9ANodSU=; b=lSu4qcdhq0ny6TY9EHisJfslJgMFn1ezhYbdmEPgrkAEfH3Pxgv9CVUu42yuutiiXF f3XdeFxas4gBWZdok7wb49+GVT/+NSfO1RwplDuueK0aNGrXVw2J6AawKDtrbOcvavVd OGcJqHqHr1O5hrM4kJZtZcqJQ9aJd7VCC5+x2xp68X4dol3UbLMy78eRA5XkAcsBX4P4 8gBAHVmT5vd2wcmzruCvTtDHZQfYiBEJSLZPR/NduiufVQW12Xv35DgJKEcnu8AW+l57 x2aMvLwlunCwckMiFgbfHSweOrxEXeORB8T28yph2p8cBUfrCJY0ytUvC//Uf6ETj3xZ kD5Q== X-Gm-Message-State: AOAM530A0dE/T9qF+LtSymcMCed2OVsGGmVO4+drxM3DMsEpOJxTmyk2 qKnY8kYxXgx/t2VTOaRptLrEMUvvyaGze1+kZUxphUx22KRGmiDgXZn2WJjlk4ZGUxiNLMy/6Kv +kSuRvSySRY0= X-Received: by 2002:aed:2767:: with SMTP id n94mr1564506qtd.237.1600723069544; Mon, 21 Sep 2020 14:17:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAG8txO9WFH1rMkSLxGPM+sc0589D4XINUeX9PKEy7c+VL4Izqm5WI4T67amcxlFTx2ENd2g== X-Received: by 2002:aed:2767:: with SMTP id n94mr1564475qtd.237.1600723069285; Mon, 21 Sep 2020 14:17:49 -0700 (PDT) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-11-70-53-122-15.dsl.bell.ca. [70.53.122.15]) by smtp.gmail.com with ESMTPSA id h68sm10225108qkf.30.2020.09.21.14.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Sep 2020 14:17:48 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Jason Gunthorpe , Andrew Morton , Jan Kara , Michal Hocko , Kirill Tkhai , Kirill Shutemov , Hugh Dickins , Peter Xu , Christoph Hellwig , Andrea Arcangeli , John Hubbard , Oleg Nesterov , Leon Romanovsky , Linus Torvalds , Jann Horn Subject: [PATCH 1/5] mm: Introduce mm_struct.has_pinned Date: Mon, 21 Sep 2020 17:17:40 -0400 Message-Id: <20200921211744.24758-2-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200921211744.24758-1-peterx@redhat.com> References: <20200921211744.24758-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 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable 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 collected from Jason Gunthorpe) Reduce the chance of false positive from page_maybe_dma_pinned() by keepi= ng track if the mm_struct has ever been used with pin_user_pages(). mm_struc= ts that have never been passed to pin_user_pages() cannot have a positive page_maybe_dma_pinned() by definition. This allows cases that might drive= up the page ref_count to avoid any penalty from handling dma_pinned pages. Due to complexities with unpining this trivial version is a permanent sti= cky bit, future work will be needed to make this a counter. 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..6f291f8b74c6 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -441,6 +441,16 @@ struct mm_struct { #endif int map_count; /* number of VMAs */ =20 + /** + * @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. + */ + int has_pinned; + spinlock_t page_table_lock; /* Protects page tables and some * counters */ diff --git a/kernel/fork.c b/kernel/fork.c index 49677d668de4..7237d418e7b5 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 =3D 0; mm->locked_vm =3D 0; + mm->has_pinned =3D 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..2d9019bf1773 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 !=3D 1); } =20 + if (flags & FOLL_PIN) + WRITE_ONCE(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 lo= ng start, int nr_pages, FOLL_FAST_ONLY))) return -EINVAL; =20 + if (gup_flags & FOLL_PIN) + WRITE_ONCE(current->mm->has_pinned, 1); + if (!(gup_flags & FOLL_FAST_ONLY)) might_lock_read(¤t->mm->mmap_lock); =20 --=20 2.26.2