All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org,
	cgroups@vger.kernel.org, linux-sh@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org,
	David Hildenbrand <david@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	"Matthew Wilcox (Oracle)" <willy@infradead.org>,
	Peter Xu <peterx@redhat.com>, Ryan Roberts <ryan.roberts@arm.com>,
	Yin Fengwei <fengwei.yin@intel.com>,
	Yang Shi <shy828301@gmail.com>, Zi Yan <ziy@nvidia.com>,
	Jonathan Corbet <corbet@lwn.net>, Hugh Dickins <hughd@google.com>,
	Yoshinori Sato <ysato@users.sourceforge.jp>,
	Rich Felker <dalias@libc.org>,
	John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>,
	Chris Zankel <chris@zankel.net>,
	Max Filippov <jcmvbkbc@gmail.com>,
	Muchun Song <muchun.song@linux.dev>,
	Miaohe Lin <linmiaohe@huawei.com>,
	Naoya Horiguchi <naoya.horiguchi@nec.com>,
	Richard Chang <richardycc@google.com>
Subject: [PATCH v1 03/18] mm/rmap: add fast-path for small folios when adding/removing/duplicating
Date: Tue,  9 Apr 2024 21:22:46 +0200	[thread overview]
Message-ID: <20240409192301.907377-4-david@redhat.com> (raw)
In-Reply-To: <20240409192301.907377-1-david@redhat.com>

Let's add a fast-path for small folios to all relevant rmap functions.
Note that only RMAP_LEVEL_PTE applies.

This is a preparation for tracking the mapcount of large folios in a
single value.

Signed-off-by: David Hildenbrand <david@redhat.com>
---
 include/linux/rmap.h | 13 +++++++++++++
 mm/rmap.c            | 26 ++++++++++++++++----------
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/include/linux/rmap.h b/include/linux/rmap.h
index 9549d78928bb..327f1ca5a487 100644
--- a/include/linux/rmap.h
+++ b/include/linux/rmap.h
@@ -322,6 +322,11 @@ static __always_inline void __folio_dup_file_rmap(struct folio *folio,
 
 	switch (level) {
 	case RMAP_LEVEL_PTE:
+		if (!folio_test_large(folio)) {
+			atomic_inc(&page->_mapcount);
+			break;
+		}
+
 		do {
 			atomic_inc(&page->_mapcount);
 		} while (page++, --nr_pages > 0);
@@ -405,6 +410,14 @@ static __always_inline int __folio_try_dup_anon_rmap(struct folio *folio,
 				if (PageAnonExclusive(page + i))
 					return -EBUSY;
 		}
+
+		if (!folio_test_large(folio)) {
+			if (PageAnonExclusive(page))
+				ClearPageAnonExclusive(page);
+			atomic_inc(&page->_mapcount);
+			break;
+		}
+
 		do {
 			if (PageAnonExclusive(page))
 				ClearPageAnonExclusive(page);
diff --git a/mm/rmap.c b/mm/rmap.c
index 56b313aa2ebf..4bde6d60db6c 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1172,15 +1172,18 @@ static __always_inline unsigned int __folio_add_rmap(struct folio *folio,
 
 	switch (level) {
 	case RMAP_LEVEL_PTE:
+		if (!folio_test_large(folio)) {
+			nr = atomic_inc_and_test(&page->_mapcount);
+			break;
+		}
+
 		do {
 			first = atomic_inc_and_test(&page->_mapcount);
-			if (first && folio_test_large(folio)) {
+			if (first) {
 				first = atomic_inc_return_relaxed(mapped);
-				first = (first < ENTIRELY_MAPPED);
+				if (first < ENTIRELY_MAPPED)
+					nr++;
 			}
-
-			if (first)
-				nr++;
 		} while (page++, --nr_pages > 0);
 		break;
 	case RMAP_LEVEL_PMD:
@@ -1514,15 +1517,18 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
 
 	switch (level) {
 	case RMAP_LEVEL_PTE:
+		if (!folio_test_large(folio)) {
+			nr = atomic_add_negative(-1, &page->_mapcount);
+			break;
+		}
+
 		do {
 			last = atomic_add_negative(-1, &page->_mapcount);
-			if (last && folio_test_large(folio)) {
+			if (last) {
 				last = atomic_dec_return_relaxed(mapped);
-				last = (last < ENTIRELY_MAPPED);
+				if (last < ENTIRELY_MAPPED)
+					nr++;
 			}
-
-			if (last)
-				nr++;
 		} while (page++, --nr_pages > 0);
 		break;
 	case RMAP_LEVEL_PMD:
-- 
2.44.0


  parent reply	other threads:[~2024-04-09 19:24 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-09 19:22 [PATCH v1 00/18] mm: mapcount for large folios + page_mapcount() cleanups David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 01/18] mm: allow for detecting underflows with page_mapcount() again David Hildenbrand
2024-04-09 20:06   ` Zi Yan
2024-04-09 21:42   ` Matthew Wilcox
2024-04-10  8:10     ` David Hildenbrand
2024-04-24  9:38   ` David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 02/18] mm/rmap: always inline anon/file rmap duplication of a single PTE David Hildenbrand
2024-04-19  2:25   ` Yin, Fengwei
2024-04-19  9:14     ` David Hildenbrand
2024-04-19 14:01   ` Yin, Fengwei
2024-04-09 19:22 ` David Hildenbrand [this message]
2024-04-19 14:02   ` [PATCH v1 03/18] mm/rmap: add fast-path for small folios when adding/removing/duplicating Yin, Fengwei
2024-04-09 19:22 ` [PATCH v1 04/18] mm: track mapcount of large folios in single value David Hildenbrand
2024-04-09 20:13   ` Zi Yan
2024-04-10  8:20     ` David Hildenbrand
2024-04-18 14:50   ` Lance Yang
2024-04-18 15:09     ` David Hildenbrand
2024-04-19  0:31       ` Lance Yang
2024-04-19 14:02   ` Yin, Fengwei
2024-04-09 19:22 ` [PATCH v1 05/18] mm: improve folio_likely_mapped_shared() using the mapcount of large folios David Hildenbrand
2024-04-16 10:40   ` Lance Yang
2024-04-16 10:47     ` David Hildenbrand
2024-04-16 10:52       ` Lance Yang
2024-04-16 10:53         ` David Hildenbrand
2024-04-19  2:29   ` Yin, Fengwei
2024-04-19  9:19     ` David Hildenbrand
2024-04-19 13:47       ` Yin, Fengwei
2024-04-19 13:48         ` David Hildenbrand
2024-04-19 14:03   ` Yin, Fengwei
2024-04-09 19:22 ` [PATCH v1 06/18] mm: make folio_mapcount() return 0 for small typed folios David Hildenbrand
2024-04-24  9:40   ` David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 07/18] mm/memory: use folio_mapcount() in zap_present_folio_ptes() David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 08/18] mm/huge_memory: use folio_mapcount() in zap_huge_pmd() sanity check David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 09/18] mm/memory-failure: use folio_mapcount() in hwpoison_user_mappings() David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 10/18] mm/page_alloc: use folio_mapped() in __alloc_contig_migrate_range() David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 11/18] mm/migrate: use folio_likely_mapped_shared() in add_page_for_migration() David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 12/18] sh/mm/cache: use folio_mapped() in copy_from_user_page() David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 13/18] mm/filemap: use folio_mapcount() in filemap_unaccount_folio() David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 14/18] mm/migrate_device: use folio_mapcount() in migrate_vma_check_page() David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 15/18] trace/events/page_ref: trace the raw page mapcount value David Hildenbrand
2024-04-09 19:22 ` [PATCH v1 16/18] xtensa/mm: convert check_tlb_entry() to sanity check folios David Hildenbrand
2024-04-09 19:23 ` [PATCH v1 17/18] mm/debug: print only page mapcount (excluding folio entire mapcount) in __dump_folio() David Hildenbrand
2024-04-09 19:23 ` [PATCH v1 18/18] Documentation/admin-guide/cgroup-v1/memory.rst: don't reference page_mapcount() David Hildenbrand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240409192301.907377-4-david@redhat.com \
    --to=david@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=chris@zankel.net \
    --cc=corbet@lwn.net \
    --cc=dalias@libc.org \
    --cc=fengwei.yin@intel.com \
    --cc=glaubitz@physik.fu-berlin.de \
    --cc=hughd@google.com \
    --cc=jcmvbkbc@gmail.com \
    --cc=linmiaohe@huawei.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=naoya.horiguchi@nec.com \
    --cc=peterx@redhat.com \
    --cc=richardycc@google.com \
    --cc=ryan.roberts@arm.com \
    --cc=shy828301@gmail.com \
    --cc=willy@infradead.org \
    --cc=ysato@users.sourceforge.jp \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.