From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 59F7926159A for ; Mon, 24 Feb 2025 16:56:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416174; cv=none; b=q9UrgG7QC7776Tp3Jzhg8uxBf/JrtCE4UsWR3idP2DjljftNhRktgrNZxBzRDnA3mrzrdsUkB3gr8V6SvSuqh0rFSq+Au2Kwv5Wrc25tX+9CAz80v6mEwATMbMQDjWqhrZQKUBzNy814o5CzqZSuoMrTs0So3y3J96WmqaiK0/k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416174; c=relaxed/simple; bh=VmpdZSRRUc/hbRQ4rkKLLuRh8r6J0srT0ICzqEnE21Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R+tbuL7/DTDnNbn+KoFHFK/LKkudNPWIFH+Zm5g2uDDOIwsnxwtDfrMzW6BM9O9jBgPJVJCu2DAzto9ohHrzuntMRdcyDMCHPYto9fiKKZh3nlG+FUtpaA3fdlF2I6u095CrTwJIVPc1RTLF01x8iuBOiatUOGy4sS9U40DkWFA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=cha1gSw3; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cha1gSw3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416171; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8Yy4w/XZlsv8uxY4mojETBma8CfgELpSYNnd4DKCaPQ=; b=cha1gSw3Kiv8Yl+3JboraN1PwPJ2OlR6kRiZz4R8DoHXA8GuLUBzxxGnbEintqA7onK5zB y7jczqnG/Qhmqr8IQ0Vi1PpCqE1GWcJsRd7uBY5sRlu6rohkizUhGH8A72pfqfI0JihEw4 CuAnDbV0iRbFTEsTDzPGtC5/Q1oiK6c= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-606-lT5u7ZiNNJe_VyYC6djmTw-1; Mon, 24 Feb 2025 11:56:10 -0500 X-MC-Unique: lT5u7ZiNNJe_VyYC6djmTw-1 X-Mimecast-MFC-AGG-ID: lT5u7ZiNNJe_VyYC6djmTw_1740416168 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-43945f32e2dso40222405e9.2 for ; Mon, 24 Feb 2025 08:56:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416168; x=1741020968; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8Yy4w/XZlsv8uxY4mojETBma8CfgELpSYNnd4DKCaPQ=; b=YAcgd9CU138lnoxPKqBwgKoCeYenqxbPJtYpdqvkYS317FiHhEf/rXV524gC3MNxDp Of5puFS/XEzIkwzLRvaY/5mCvCHzcn5He1xoUWZ7/ayskypJWyqwyQFIYmf2/IiXbana WDQVFYWwTZurvAEq+lRZtzf8LJiPRIDvrOhuAtVckBopdmLZvh9uIO8+yIOCT2PlF6Lz zQHP2BzFBRFMGlqSkXb3wLVyXUNnAcpDL+NN6WH+Tnmv3vGOiTPa6Ci+Z7N990FCBoZL NB3Tyspn59CA5bYWJwRBfjwKgPrZ+XrDvOMbl4XgaIAlS8STlWUuURsk5aaBNSSNsz+T u53A== X-Gm-Message-State: AOJu0YwefqRfhuJF3VTl8HSv/oHwpysa8JbkTq0VvrsG167krbd5t57Y E3fPnW8r4gfVvBqfyfRpBczHCTjiNjMF5YwCPLby2mE2SXg58UzwXQ5/H0mdOwNBHmhYOGTRAH8 5wdRY6Ge+Fp1GD/gorfpQgvhH9R32LAyK4H4y9Q7+mb0UPkbteLzaqPaGmouubUZCkkfOLRbOcg rYayWPJu7AohctLhInCf7R6D5lFwUkH6lj7DvleClyjugF X-Gm-Gg: ASbGncsJ+j4FeypIUoq6oIeRcNXWW81spZpHmRBIuZBYOtsGvL3K6HShwL66/JUgers Eqvsfrs5HWuhzBPcP6r25UFhDrtB7wlzV91nl0RY5YNouMWuixWckf8Gl9qrHebMH5KcSvc879z rPX+biE10AOyl9I8B8631WFHCbvaiNyc5tfm/mEIMOPfy4gaHepG3+O2FNb4Fv9PMVEAgFetqI/ jkvK5owAYr4SNBYfoio2aBPyJH5aTTQeAqm0oIjCf20eqfzjuZpLYKNd0ftIUWmgbi/7LDCxE6q bpFmobVE2qoVS2apSYIv3Akb2olBp2DFdn3qLL96Bw== X-Received: by 2002:a05:6000:1ac7:b0:38f:2a99:b377 with SMTP id ffacd0b85a97d-38f6f0c4523mr13709717f8f.53.1740416167716; Mon, 24 Feb 2025 08:56:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHrYsCtuTnI9EW4wn4ZrbygGJqAwQI73Wf+OvRO5jakiYr702W8tJwU3BfJXILKECpWzIC3lQ== X-Received: by 2002:a05:6000:1ac7:b0:38f:2a99:b377 with SMTP id ffacd0b85a97d-38f6f0c4523mr13709656f8f.53.1740416167205; Mon, 24 Feb 2025 08:56:07 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f259d5e92sm32611493f8f.66.2025.02.24.08.56.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:06 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , Lance Yang , "Kirill A. Shutemov" Subject: [PATCH v2 01/20] mm: factor out large folio handling from folio_order() into folio_large_order() Date: Mon, 24 Feb 2025 17:55:43 +0100 Message-ID: <20250224165603.1434404-2-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's factor it out into a simple helper function. This helper will also come in handy when working with code where we know that our folio is large. Maybe in the future we'll have the order readily available for small and large folios; in that case, folio_large_order() would simply translate to folio_order(). Reviewed-by: Lance Yang Reviewed-by: Kirill A. Shutemov Signed-off-by: David Hildenbrand --- include/linux/mm.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d82feabbe44f8..d00214e22a174 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1194,6 +1194,11 @@ struct inode; =20 extern void prep_compound_page(struct page *page, unsigned int order); =20 +static inline unsigned int folio_large_order(const struct folio *folio) +{ + return folio->_flags_1 & 0xff; +} + /* * compound_order() can be called without holding a reference, which means * that niceties like page_folio() don't work. These callers should be @@ -1207,7 +1212,7 @@ static inline unsigned int compound_order(struct page= *page) =20 if (!test_bit(PG_head, &folio->flags)) return 0; - return folio->_flags_1 & 0xff; + return folio_large_order(folio); } =20 /** @@ -1223,7 +1228,7 @@ static inline unsigned int folio_order(const struct f= olio *folio) { if (!folio_test_large(folio)) return 0; - return folio->_flags_1 & 0xff; + return folio_large_order(folio); } =20 #include @@ -2139,7 +2144,7 @@ static inline long folio_nr_pages(const struct folio = *folio) #ifdef CONFIG_64BIT return folio->_folio_nr_pages; #else - return 1L << (folio->_flags_1 & 0xff); + return 1L << folio_large_order(folio); #endif } =20 @@ -2164,7 +2169,7 @@ static inline unsigned long compound_nr(struct page *= page) #ifdef CONFIG_64BIT return folio->_folio_nr_pages; #else - return 1L << (folio->_flags_1 & 0xff); + return 1L << folio_large_order(folio); #endif } =20 --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07E97264A60 for ; Mon, 24 Feb 2025 16:56:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416176; cv=none; b=lJ4RskcmvJXSxP1sm4yiAN8D94VzAVqBOxjGMxEAqKua5maWQGwMo+UTFm7WtXsParkApqM1/cG8OB7bM5T+15D7346hNkdEg9zJQCR+wWphLsswiE1EwR3R6bbOKqFFUzOLzrs1lIJXQJyQGtbVDcuCwcwY6svgOXRwh2HVtmA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416176; c=relaxed/simple; bh=+NZ/1EFzSZLxmiTEzceGSJVCP7MjSwgxhuOimjZWqPI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q9DDIFrtuzTmlb/B3789IO7x1QfJ25kxjj+E68qitQ9PqE3fTzmM7jb/z8cAE3n/LxQ4FPKZzUN12XnTYylJZrUSA0mLrhSlA9A26Q5rGHodWYKyOASaylofZl7+oJYr5GYwj3HAC/fgiZIAzbGh22XqGhbB77orvvm4q1OZ8Fo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZhH06oWl; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZhH06oWl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416173; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uW/SnbGZfnRd/ZrL3KWYJ9GZqWtDH7XgQk5JORWTALU=; b=ZhH06oWlzAdPP+fbULUpx1LGopAE7DTH79Dd4GNG9WqS817eeuxV7R038KP7atLUWjex4Z 9oE3HegwjSvKLTDYCgGg/uRLAu+w3rMk3tUwamK7jIhk5EAqUFNmiswQQdQIkotv52Vr1p 5VSZpH5yKgBeN86WU5rLOeXObM6V8Lc= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-208-yTBkzKCcMAGhdJqLDyE7Pg-1; Mon, 24 Feb 2025 11:56:11 -0500 X-MC-Unique: yTBkzKCcMAGhdJqLDyE7Pg-1 X-Mimecast-MFC-AGG-ID: yTBkzKCcMAGhdJqLDyE7Pg_1740416170 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-43947979ce8so19945505e9.0 for ; Mon, 24 Feb 2025 08:56:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416170; x=1741020970; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uW/SnbGZfnRd/ZrL3KWYJ9GZqWtDH7XgQk5JORWTALU=; b=royg0q780Hz04YctlDghqcTcMyed4e75x2LJCLCvV7PzU5Or1PBNFWPes8WgZjdk/J HrfKWoocd987iV5B7CVBa1YzPFoEXVRjR/I3UWf3tXK3dLxW7T9ExQGcUn8pRS75Gzc6 dyuC+D0VEXuEwDQnGVV2ymM62FIYbureYArNPhFokEdtei8jbwoirTMSS3P9oeInbE0G F8fZ0LU449OS0DbmBoAyWzojEAkXSs6d831Mcouw1djry720h9h4iI23wDvdqM6w513f 3X+rtcUe3tY7hSMnuB7Wqj7Oo71fm8Bv7OI4CFnLPhylP9Es5Kn1rY6waFRtr5DV7/Z4 7RvQ== X-Gm-Message-State: AOJu0YwtZWeeGqJ0PAZnbk9z28cmNfhMUFNJOeZeUqV7ZsLiISOkj0xT ydpFBCRIH+cljN4hM7U0K6k6FudTWMrgzXYAUxZJirwvXI37bx7T1MnjeMTWF//gE7S9ZiWVNvE JjrivvcLCzZb47MJdA65xia2+9UjqqVUSUsFJDZkoPBTHuSPYKRhhr2THFXp1ERM7S/vOAfIBiG rxfBWX/oY3RlnCQXu2us52wLLvt3HFvpYyCfcoihU7VV69 X-Gm-Gg: ASbGncvxb2LndeUB8NpgFAVIdLgTF3GZ9csbaOKMuqnfl5pwX+iixBTP2iM348jMXWE lRdiZ8Cx7hP4AbMPWp4pqP4KS/lDqRhIMadX8VyzOZ5Ypu/OSy/D2mRWo6d5ngJc4kfuS+saDyZ WqYhKtU6tMLe+a56yErxx2riYS/AjdfcPz8zfnxnHKlaslVSd8SyNEmAIQm/Fmcg3K13N/4RLAd MxBIiXFaVsKxfa0K/+E/rzU8Cm9DuMUkoNYInco7q8FJWcQvI52BoTOtEr/BdEdVrLUVYnnCgx9 YquDtfjDv+w6+E0/sQ9y6UbQDuDhz302e/xjhLc+hA== X-Received: by 2002:a05:600c:34c2:b0:439:9828:c447 with SMTP id 5b1f17b1804b1-439aecf1426mr121409485e9.17.1740416169924; Mon, 24 Feb 2025 08:56:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IFMOxvVak9cDaBV2o8+q98fYh3JQOIg7zVYXNoNjPQAK1ZeuqEa4rOOZBLfaDjBIQaJ00l68A== X-Received: by 2002:a05:600c:34c2:b0:439:9828:c447 with SMTP id 5b1f17b1804b1-439aecf1426mr121408975e9.17.1740416169419; Mon, 24 Feb 2025 08:56:09 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b02f3eaesm111237045e9.24.2025.02.24.08.56.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:08 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , "Kirill A. Shutemov" Subject: [PATCH v2 02/20] mm: factor out large folio handling from folio_nr_pages() into folio_large_nr_pages() Date: Mon, 24 Feb 2025 17:55:44 +0100 Message-ID: <20250224165603.1434404-3-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's factor it out into a simple helper function. This helper will also come in handy when working with code where we know that our folio is large. While at it, let's consistently return a "long" value from all these similar functions. Note that we cannot use "unsigned int" (even though _folio_nr_pages is of that type), because it would break some callers that do stuff like "-folio_nr_pages()". Both "int" or "unsigned long" would work as well. Maybe in the future we'll have the nr_pages readily available for all large folios, maybe even for small folios, or maybe for none. Reviewed-by: Kirill A. Shutemov Signed-off-by: David Hildenbrand --- include/linux/mm.h | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index d00214e22a174..7c5a8fd29cfcd 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1199,6 +1199,18 @@ static inline unsigned int folio_large_order(const s= truct folio *folio) return folio->_flags_1 & 0xff; } =20 +#ifdef CONFIG_64BIT +static inline long folio_large_nr_pages(const struct folio *folio) +{ + return folio->_folio_nr_pages; +} +#else +static inline long folio_large_nr_pages(const struct folio *folio) +{ + return 1L << folio_large_order(folio); +} +#endif + /* * compound_order() can be called without holding a reference, which means * that niceties like page_folio() don't work. These callers should be @@ -2141,11 +2153,7 @@ static inline long folio_nr_pages(const struct folio= *folio) { if (!folio_test_large(folio)) return 1; -#ifdef CONFIG_64BIT - return folio->_folio_nr_pages; -#else - return 1L << folio_large_order(folio); -#endif + return folio_large_nr_pages(folio); } =20 /* Only hugetlbfs can allocate folios larger than MAX_ORDER */ @@ -2160,24 +2168,20 @@ static inline long folio_nr_pages(const struct foli= o *folio) * page. compound_nr() can be called on a tail page, and is defined to * return 1 in that case. */ -static inline unsigned long compound_nr(struct page *page) +static inline long compound_nr(struct page *page) { struct folio *folio =3D (struct folio *)page; =20 if (!test_bit(PG_head, &folio->flags)) return 1; -#ifdef CONFIG_64BIT - return folio->_folio_nr_pages; -#else - return 1L << folio_large_order(folio); -#endif + return folio_large_nr_pages(folio); } =20 /** * thp_nr_pages - The number of regular pages in this huge page. * @page: The head page of a huge page. */ -static inline int thp_nr_pages(struct page *page) +static inline long thp_nr_pages(struct page *page) { return folio_nr_pages((struct folio *)page); } --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B42AD263F27 for ; Mon, 24 Feb 2025 16:56:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416177; cv=none; b=M1BgzpquiP6BoZFgzOHa1lQK28+alkYOGIVMWfC837VrfncyKheGSm/zcZPsDlNPx7OkiViRF4lcc+xqCvErjkIoRFs08B3JLBkBRpvFDWZ4hjZz3tCLpMDQU32cnjkNsoNc/WvaBGDaBNmBWQBRnCgZbndKccKifg0wjs8tLow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416177; c=relaxed/simple; bh=5WP1H6l1SRHf+A6AVEl6PPcHjhpjv5kvsrxnZBwuLrM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nOuYCGEbMjd2UbYPBltNafA0lcTRobU/6QaG+gDdriA57bbEx+MmyqGtBIV6QkSBT7Op+zpAZTfKk/wJz3hdRX1iCZKcKF8oqmb5mjDJK8LkEHVCvJwyHEjwGoEHZs+lZO7Kq6wHxc37SrA/kyBFq+bgvF+3qCeDA0oH0gYq+pw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JjBqgzZm; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JjBqgzZm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416174; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8Z/i7SxHkRJm72w2k3iTqc1vo3DynQo4UiIchyZFPhI=; b=JjBqgzZmd+rGbSFEW7fxe2AVXSbjZp+IKWsU0WbINjUkksGhgys+hmcVVI63CcXZzClCJi H14NO9QakbsPdKmB9xiD5pl/qwgnElckqnAsf+rdcEwPCERkAsPEIAH0xc/molvJMG7Wh3 3df4ypRKEU6dSdwqz/YVabE2qWIFP5o= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-493-Nu7zpWJwPfSUXPE9ln4GaA-1; Mon, 24 Feb 2025 11:56:13 -0500 X-MC-Unique: Nu7zpWJwPfSUXPE9ln4GaA-1 X-Mimecast-MFC-AGG-ID: Nu7zpWJwPfSUXPE9ln4GaA_1740416172 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-4399d2a1331so22851135e9.1 for ; Mon, 24 Feb 2025 08:56:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416172; x=1741020972; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8Z/i7SxHkRJm72w2k3iTqc1vo3DynQo4UiIchyZFPhI=; b=gy9T9TnRQEUyO2tMo93TvyAkETexqRtQ2trNo/Lb3J8HFM67XMc9pp1m71SRdhsuAl Q8B4Hhn2JLmSrMb7AFameaaBQZc8cGDhpOYLkftM9LCjlAo4ZFKLFZl8dng6sLihxgac TWDdZDqvRmp/xzH452LPwrs7Wgy2uNtpOYGZEe3c85e5EDsFRTV9SR3TPXQAjpUzr0YC s5AlO2wVkcv+ufGC8jJ63/oomfyWkYa6qAYgULnQFBCRWTVeANyXHX7kH/aiCDAHhp3z GxvWkepQMxBFsC/C8Bu/6Nc7Y6oStzI5i+m2TG5S81UIixM6Mr59L3swtgYEROXado7v iYeQ== X-Gm-Message-State: AOJu0YyzM2HlYectAEys9pxG6wnaulTM3I4Xjg589pnmkzCab5i/U9rd 4aAVk8gYbhtPxm/3TdCE1k1iT3fFlnbjcgEEsmyNare+Ax9h1JCYBvuxgsNiU++kDQPn+eoHv5L NG9rypc0oGxETWwKOf+6v6vA4ARiSy7IMMRHb8c7ZAQj6lJk8u1O5DkMT1HyOZGmtTdO7HnHhSF j4EY4kBsEUzqbbmE9l6zv2wr2AGPiC9pp881+eZkoywcEH X-Gm-Gg: ASbGncsumnkWo0KtXs/8ZhWEt8hFz222Abpddc49h6nZu1vWFT5HelWh/bkSMmtCnje dw/uf8rzZbhd9Wv+EucfXLDJlX3ENYJZYnEtaMEL8iAuGP4Q41CHfO1XF3WWjKk1sk1nb5yfviL I9FCt/iob9WMBxCjyk3LOavm5xqpyaIP9xhZGuATi6WbH4pLlMZszfPxtoC5CHbdcMcJlDF8OZ1 EDWdNX53X4pfHEoXxMBe2W0/s1nr/7jTs6NEQ0AjMywX+7kZYtr4n9aY3Jug0Pq0xZiZM/rQWeS z4ZVCFJhzo5heZShEcMkifB6HWeIZqj5xqXOwvEt2g== X-Received: by 2002:a05:600c:1c85:b0:439:968b:6669 with SMTP id 5b1f17b1804b1-439ae2d254bmr112029265e9.1.1740416171972; Mon, 24 Feb 2025 08:56:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IGPiVGKd002kgHq/C+YhiMRT6ZEt5tkRc6eny1qWEWThiWAlrLvHc+EEKxBr1Q0DjH+goSmcg== X-Received: by 2002:a05:600c:1c85:b0:439:968b:6669 with SMTP id 5b1f17b1804b1-439ae2d254bmr112028725e9.1.1740416171409; Mon, 24 Feb 2025 08:56:11 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b0371b7bsm112170735e9.33.2025.02.24.08.56.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:10 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , "Kirill A. Shutemov" Subject: [PATCH v2 03/20] mm: let _folio_nr_pages overlay memcg_data in first tail page Date: Mon, 24 Feb 2025 17:55:45 +0100 Message-ID: <20250224165603.1434404-4-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's free up some more of the "unconditionally available on 64BIT" space in order-1 folios by letting _folio_nr_pages overlay memcg_data in the first tail page (second folio page). Consequently, we have the optimization now whenever we have CONFIG_MEMCG, independent of 64BIT. We have to make sure that page->memcg on tail pages does not return "surprises". page_memcg_check() already properly refuses PageTail(). Let's do that earlier in print_page_owner_memcg() to avoid printing wrong "Slab cache page" information. No other code should touch that field on tail pages of compound pages. Reset the "_nr_pages" to 0 when splitting folios, or when freeing them back to the buddy (to avoid false page->memcg_data "bad page" reports). Note that in __split_huge_page(), folio_nr_pages() would stop working already as soon as we start messing with the subpages. Most kernel configs should have at least CONFIG_MEMCG enabled, even if disabled at runtime. 64byte "struct memmap" is what we usually have on 64BIT. While at it, rename "_folio_nr_pages" to "_nr_pages". Hopefully memdescs / dynamically allocating "strut folio" in the future will further clean this up, e.g., making _nr_pages available in all configs and maybe even in small folios. Doing that should be fairly easy on top of this change. Reviewed-by: Kirill A. Shutemov Signed-off-by: David Hildenbrand --- include/linux/mm.h | 4 ++-- include/linux/mm_types.h | 30 ++++++++++++++++++++++-------- mm/huge_memory.c | 16 +++++++++++++--- mm/internal.h | 4 ++-- mm/page_alloc.c | 6 +++++- mm/page_owner.c | 2 +- 6 files changed, 45 insertions(+), 17 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 7c5a8fd29cfcd..f6b6373a864dd 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1199,10 +1199,10 @@ static inline unsigned int folio_large_order(const = struct folio *folio) return folio->_flags_1 & 0xff; } =20 -#ifdef CONFIG_64BIT +#ifdef NR_PAGES_IN_LARGE_FOLIO static inline long folio_large_nr_pages(const struct folio *folio) { - return folio->_folio_nr_pages; + return folio->_nr_pages; } #else static inline long folio_large_nr_pages(const struct folio *folio) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 689b2a7461892..e81be20bbabc6 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -287,6 +287,11 @@ typedef struct { unsigned long val; } swp_entry_t; =20 +#if defined(CONFIG_MEMCG) || defined(CONFIG_SLAB_OBJ_EXT) +/* We have some extra room after the refcount in tail pages. */ +#define NR_PAGES_IN_LARGE_FOLIO +#endif + /** * struct folio - Represents a contiguous set of bytes. * @flags: Identical to the page flags. @@ -312,7 +317,7 @@ typedef struct { * @_large_mapcount: Do not use directly, call folio_mapcount(). * @_nr_pages_mapped: Do not use outside of rmap and debug code. * @_pincount: Do not use directly, call folio_maybe_dma_pinned(). - * @_folio_nr_pages: Do not use directly, call folio_nr_pages(). + * @_nr_pages: Do not use directly, call folio_nr_pages(). * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h. * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h. * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgr= oup.h. @@ -377,13 +382,20 @@ struct folio { unsigned long _flags_1; unsigned long _head_1; /* public: */ - atomic_t _large_mapcount; - atomic_t _entire_mapcount; - atomic_t _nr_pages_mapped; - atomic_t _pincount; -#ifdef CONFIG_64BIT - unsigned int _folio_nr_pages; -#endif + union { + struct { + atomic_t _large_mapcount; + atomic_t _entire_mapcount; + atomic_t _nr_pages_mapped; + atomic_t _pincount; + }; + unsigned long _usable_1[4]; + }; + atomic_t _mapcount_1; + atomic_t _refcount_1; +#ifdef NR_PAGES_IN_LARGE_FOLIO + unsigned int _nr_pages; +#endif /* NR_PAGES_IN_LARGE_FOLIO */ /* private: the union with struct page is transitional */ }; struct page __page_1; @@ -435,6 +447,8 @@ FOLIO_MATCH(_last_cpupid, _last_cpupid); offsetof(struct page, pg) + sizeof(struct page)) FOLIO_MATCH(flags, _flags_1); FOLIO_MATCH(compound_head, _head_1); +FOLIO_MATCH(_mapcount, _mapcount_1); +FOLIO_MATCH(_refcount, _refcount_1); #undef FOLIO_MATCH #define FOLIO_MATCH(pg, fl) \ static_assert(offsetof(struct folio, fl) =3D=3D \ diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 1e45064046a0f..13152313e77ce 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3307,9 +3307,10 @@ bool can_split_folio(struct folio *folio, int caller= _pins, int *pextra_pins) * It splits @folio into @new_order folios and copies the @folio metadata = to * all the resulting folios. */ -static void __split_folio_to_order(struct folio *folio, int new_order) +static void __split_folio_to_order(struct folio *folio, int old_order, + int new_order) { - long nr_pages =3D folio_nr_pages(folio); + long nr_pages =3D 1 << old_order; long new_nr_pages =3D 1 << new_order; long index; =20 @@ -3529,12 +3530,21 @@ static int __split_unmapped_folio(struct folio *fol= io, int new_order, } } =20 + /* + * Reset any memcg data overlay in the tail pages. + * folio_nr_pages() is unreliable until prep_compound_page() + * was called again. + */ +#ifdef NR_PAGES_IN_LARGE_FOLIO + folio->_nr_pages =3D 0; +#endif + /* complete memcg works before add pages to LRU */ split_page_memcg(&folio->page, old_order, split_order); split_page_owner(&folio->page, old_order, split_order); pgalloc_tag_split(folio, old_order, split_order); =20 - __split_folio_to_order(folio, split_order); + __split_folio_to_order(folio, old_order, split_order); =20 after_split: /* diff --git a/mm/internal.h b/mm/internal.h index b07550db2bfd1..7f6d5def00fa0 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -684,8 +684,8 @@ static inline void folio_set_order(struct folio *folio,= unsigned int order) return; =20 folio->_flags_1 =3D (folio->_flags_1 & ~0xffUL) | order; -#ifdef CONFIG_64BIT - folio->_folio_nr_pages =3D 1U << order; +#ifdef NR_PAGES_IN_LARGE_FOLIO + folio->_nr_pages =3D 1U << order; #endif } =20 diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 16dfcf7ade74a..bdfc954dab9aa 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1174,8 +1174,12 @@ __always_inline bool free_pages_prepare(struct page = *page, if (unlikely(order)) { int i; =20 - if (compound) + if (compound) { page[1].flags &=3D ~PAGE_FLAGS_SECOND; +#ifdef NR_PAGES_IN_LARGE_FOLIO + folio->_nr_pages =3D 0; +#endif + } for (i =3D 1; i < (1 << order); i++) { if (compound) bad +=3D free_tail_page_prepare(page, page + i); diff --git a/mm/page_owner.c b/mm/page_owner.c index 2d6360eaccbb6..a409e2561a8fd 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -507,7 +507,7 @@ static inline int print_page_owner_memcg(char *kbuf, si= ze_t count, int ret, =20 rcu_read_lock(); memcg_data =3D READ_ONCE(page->memcg_data); - if (!memcg_data) + if (!memcg_data || PageTail(page)) goto out_unlock; =20 if (memcg_data & MEMCG_DATA_OBJEXTS) --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34739264A97 for ; Mon, 24 Feb 2025 16:56:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416179; cv=none; b=DjRUILqbYmTsaDaMKpk60nXoRSPZ7oXUIi5jun0rkWigqryAG7Qe93p0GrKVvmaFRQ5Hj1IlsAKoZffsbhnFEGQB7Zahs/FesPPlvsT7QmUpCJP1JlqDLPwqKPuYeNIlYyMm0ZdsgGaTVDOhtMuZVJZjSSTaGZnkRBUxkmjOBVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416179; c=relaxed/simple; bh=2+hWsKxtAvrobFrzRh+8Ehcyr57/khonaGBhd7DELH4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TN6VKqK45urxUJzku5dbXg4MP4vdq1RjpYH/o3ucAGQpPkvD7CxYgStYWK+nBpq7oxzZW8Bfq12veSKnoUQBqYBDjkEZRRNcglehIGdNAk6i72i0tdRnayLWnC9PbsCpx6+5Oe7zjxUZPufF//4LGW3M6qg6qGZxdkWv3gKp8Gc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=bWW06DQS; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bWW06DQS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416176; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2J0EAZe5fI1EPFno0gWSdFeb6AH1J2rAdhMuBREbwsI=; b=bWW06DQSBiPOIrnTrNo/ebNbaVO+xH0tuVbWOCC3M5qyYoMjtfPD53vxvYYvDT4rDTjLQb L6aJgk10TA1hT2Q1tUF8+86gDYEF92Y3BV7QOMen9d539FiK7YA0nZhM3ePvgPXLbFe9HL yYxErvSsdhat/Zk9DRA08UGi49kLFys= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-283-gHY4568mM3uvRhMgi6OYEQ-1; Mon, 24 Feb 2025 11:56:15 -0500 X-MC-Unique: gHY4568mM3uvRhMgi6OYEQ-1 X-Mimecast-MFC-AGG-ID: gHY4568mM3uvRhMgi6OYEQ_1740416174 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38f34b3f9f1so3474681f8f.0 for ; Mon, 24 Feb 2025 08:56:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416174; x=1741020974; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2J0EAZe5fI1EPFno0gWSdFeb6AH1J2rAdhMuBREbwsI=; b=D1ZkANucvCnefmyDndiqlS/cGDsVojxPAkhK1iBgp4gPdNgGidcQfmBsWuVt74T/Dl gHIe8H0D3SwjHzPkVXNeyfG/1biFl8ObN5kjaIHi7ZdDhoxLTAikhigGwA5bXdNhmw5E SfSiRI3ZeP3/XiVbUTPYqznr+bNJzTeP1Z57/5nU5qsaTnCw9JZHnavG1w4y3QOE+R4Z 58eBkh+u54uPGybn0QOodNqfC8v/fhgmCtZW+tsyHZDh+8sgURrl6uxwselmfiA4JgfK CQShh9heMTSPw+MKHwALEv/L3kgBPsMwy/uvXZautA1qr4vmxDP/Ouj4m+exRfym77x6 k6Ww== X-Gm-Message-State: AOJu0YwQH5JtUCsSaKmc/kJ+RTGsVFc/5GyXcaf/UlBFVgqimC6AoeVm j1Yl9p0ePOoJHouIlwQ5F8R6/7pgzTTSCRwQnplTv21hGYcruLSIYM1O1zlfqkj6IyHpz0nZ8/V xrchUFl3btZPMylHSDtCEHnyQLIIfp20I3plxSPSP6nBrfaZmbhMVE0G4k8q4qNkqlPrp0B2ERY yRSXyVwje6JLKiAxYShWyBU+T6RuHC6jeoOChNGuAWY9Uf X-Gm-Gg: ASbGncsIEzZJtHwFwOBEEvwTJy4scgMcAEgciPbBTWnzIfPTfgRl6nhVjLK7CdG1NKR 3D7P2baQLd/J0Tqw7qrMXxCnc6mCCRdurjCpqfiyyJ+uthjz50MZvlmotuBXK7sGzix18RXc8bd zX9LdHifQU51GkDjCrVV4yluJ+Hc9MaGYHYXlKpd/c/dDbEEnkucaiAQugXHlvN1+59leztjBvt JhtQoWCqaFTCK4tStKKs2VsLm5ueWdvewDoyJjUl1ufa9roMPSPTAhBcgp2yd9U7R+Y7AclwymR eTZ7vdIzoaK6UcMrIdB9eweI40oguSqlP29bqNDwAA== X-Received: by 2002:a05:6000:178c:b0:38f:32ac:7e55 with SMTP id ffacd0b85a97d-38f6f0bd36dmr13581433f8f.48.1740416173987; Mon, 24 Feb 2025 08:56:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IHEK4+EskiCQe6b4Tt/1yywMxIN3N8u+em5znQyoHp6oO/UQ3gXRmnxMS/Oum/lKFSKXiOoHw== X-Received: by 2002:a05:6000:178c:b0:38f:32ac:7e55 with SMTP id ffacd0b85a97d-38f6f0bd36dmr13581391f8f.48.1740416173570; Mon, 24 Feb 2025 08:56:13 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f258ddbe0sm32403003f8f.39.2025.02.24.08.56.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:13 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 04/20] mm: move hugetlb specific things in folio to page[3] Date: Mon, 24 Feb 2025 17:55:46 +0100 Message-ID: <20250224165603.1434404-5-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's just move the hugetlb specific stuff to a separate page, and stop letting it overlay other fields for now. This frees up some space in page[2], which we will use on 32bit to free up some space in page[1]. While we could move these things to page[3] instead, it's cleaner to just move the hugetlb specific things out of the way and pack the core-folio stuff as tight as possible. ... and we can minimize the work required in dump_folio. We can now avoid re-initializing &folio->_deferred_list in hugetlb code. Hopefully dynamically allocating "strut folio" in the future will further clean this up. Signed-off-by: David Hildenbrand --- include/linux/mm_types.h | 27 +++++++++++++++++---------- mm/hugetlb.c | 1 - mm/page_alloc.c | 5 +++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index e81be20bbabc6..1d9c68c551d42 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -405,20 +405,23 @@ struct folio { unsigned long _flags_2; unsigned long _head_2; /* public: */ - void *_hugetlb_subpool; - void *_hugetlb_cgroup; - void *_hugetlb_cgroup_rsvd; - void *_hugetlb_hwpoison; + struct list_head _deferred_list; /* private: the union with struct page is transitional */ }; + struct page __page_2; + }; + union { struct { - unsigned long _flags_2a; - unsigned long _head_2a; + unsigned long _flags_3; + unsigned long _head_3; /* public: */ - struct list_head _deferred_list; + void *_hugetlb_subpool; + void *_hugetlb_cgroup; + void *_hugetlb_cgroup_rsvd; + void *_hugetlb_hwpoison; /* private: the union with struct page is transitional */ }; - struct page __page_2; + struct page __page_3; }; }; =20 @@ -455,8 +458,12 @@ FOLIO_MATCH(_refcount, _refcount_1); offsetof(struct page, pg) + 2 * sizeof(struct page)) FOLIO_MATCH(flags, _flags_2); FOLIO_MATCH(compound_head, _head_2); -FOLIO_MATCH(flags, _flags_2a); -FOLIO_MATCH(compound_head, _head_2a); +#undef FOLIO_MATCH +#define FOLIO_MATCH(pg, fl) \ + static_assert(offsetof(struct folio, fl) =3D=3D \ + offsetof(struct page, pg) + 3 * sizeof(struct page)) +FOLIO_MATCH(flags, _flags_3); +FOLIO_MATCH(compound_head, _head_3); #undef FOLIO_MATCH =20 /** diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9faa1034704ff..2ad5c292568ab 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1646,7 +1646,6 @@ static void __update_and_free_hugetlb_folio(struct hs= tate *h, =20 folio_ref_unfreeze(folio, 1); =20 - INIT_LIST_HEAD(&folio->_deferred_list); hugetlb_free_folio(folio); } =20 diff --git a/mm/page_alloc.c b/mm/page_alloc.c index bdfc954dab9aa..05a2a9492cdb0 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -971,6 +971,11 @@ static int free_tail_page_prepare(struct page *head_pa= ge, struct page *page) goto out; } break; + case 3: + /* the third tail page: hugetlb specifics overlap ->mappings */ + if (IS_ENABLED(CONFIG_HUGETLB_PAGE)) + break; + fallthrough; default: if (page->mapping !=3D TAIL_MAPPING) { bad_page(page, "corrupted mapping in tail page"); --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D830026560B for ; Mon, 24 Feb 2025 16:56:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416181; cv=none; b=s/AgVbLihzKFvHaOeUWcC41wwdnO6tC9C7pZg5n8FVzLzgAsIhWcpVVKCwkLnOtCm+Y96a5q4Cj3hYJoPTXYE0iNZso+I9eA8XE4qoLhC2uFkSUM2VLIxzOIKXbWNwmtODY4ZOue5QiHZqN4VrMcQ+nhotjPyv2tbrHa1i/nMk0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416181; c=relaxed/simple; bh=jCFgdCkbqHJ9MbrS7XN0TCcs0JBnjj+yrJRy9PI9wEw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NjZDV1pVKTbdBB5DALzorrtMslYbOcy0dS6Y5PWRYD386913GMO2Gm8Wy91hD81PNNcyguIsM0oRvQuOHpmDTHLDoBGA6FdNs4p033ndSy/ue+6KDYHSc2JRpkMX+FFVoSD4U8uDPld+o3h4syW6JSl3AIWDv42LytDGeaXMvWA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=GUFrNSqU; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GUFrNSqU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416178; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v9Kk0sDLIOZEo9TGeBWuftLWUgUisx0QoodxZudqT8w=; b=GUFrNSqUsiT69fUQf+fECNE0yCzGRR4/XN9TUZQUpwOxawNz/Q8uF60Rq9jyd0bCqwjc43 xUzgyJFGFBkt2JtIfN5QOw8RRdG/ukgR43gir+d8GvIsjQ0sWvOj28eRbMJkE4ZshNHnmI PzWh57NOjCNTclvYQig+EdYOHbVV+wM= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-94-EzExgMH7Pea0QzY3mBLSwA-1; Mon, 24 Feb 2025 11:56:17 -0500 X-MC-Unique: EzExgMH7Pea0QzY3mBLSwA-1 X-Mimecast-MFC-AGG-ID: EzExgMH7Pea0QzY3mBLSwA_1740416176 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4399c32efb4so23537705e9.1 for ; Mon, 24 Feb 2025 08:56:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416176; x=1741020976; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v9Kk0sDLIOZEo9TGeBWuftLWUgUisx0QoodxZudqT8w=; b=hR6f4wqHRvQNv/Vy85osM3vnU1NjJpgKnhGL8cpQCfKsEK0SNzJFxft08y6CAGysD5 GURRi1YafZk4DdQ2J//3bgHyjGOzEClw/v5xwO1cD0Dwb0I83AXTw7YS9KmY2iYf8g7n ja3THJCHSfU2Ax/QSD+WUuh1HUH2eg1QLH0HCqshQ4aM5F194g7JB3wT7vGH+BhV5fWB ImUFata4vgZz6hhNRxnCP367TXHqcKi1yoyBDl4EldMlUPGiAQG4K0tf3ZHhQ+L1lKsj PbQdCNEujlObB5TXuEen9Lj0X0WetEMP1vt0tZ8KimJTv+0eAy6NwPdw42O0YD0y5IDo Y8iA== X-Gm-Message-State: AOJu0YzXJmggBcAxOdJHaSAkTgLfVoAFgaNnC/UyJvp79aLbf5O7k9H/ WG0fDzA/KxJQOVmKQk+sWLH+HQp052OsHkV/4655YhAESK3TJH9nJOoGa/J7wRcGhZNbZRMvtTt 1dE1hNACa/bMp2KD1nImVQNVs4EXpd1DCJ6FbuG9lMzZWXU8CM8WFUcVoZid8zvDtVphsfQkiow Hh4/Z3WDbSLzLEtk9OC7cKb1MerhqE/njKAjSQhTMjDtxr X-Gm-Gg: ASbGncsTNmWdO7Axf79jowqxvBYev68FCHyd2+Dvc2GPrqT3Piq9jEE/AW9P8PmzM+1 QAxxKt7IWIP/bC8q0xTdHQ4l2b2TgKrVLYPqid38HGvOn/yTCpKTP4oBL9IX3QDpR/qxDO6QsLo 3GcDiPhySNg1RzBrMArqNYVxv+KJE9sxwZOay2OhiBXEPj1lxPtxBvnfNSvoDHtc7AQ53FESU6m 8Qqhyoomco8nPmiZyO7+5aV48PPd7jxPx4kjCqVBpb9fn/lEF7NXL5doBF9rZXZm+kuBrSK9GzY dD++Iix11Ze3T8x1POXw9P1ehKDO56b1HjZWJ2ZWhg== X-Received: by 2002:a05:600c:5949:b0:439:8345:17a6 with SMTP id 5b1f17b1804b1-439a30d38e9mr130585695e9.12.1740416176191; Mon, 24 Feb 2025 08:56:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IGy0+PSAOTGdTFyYGF1ZzYS11kOEi6LhvuqHcevrUwSSRUG4LJeLXU4Rq4oj/c975sX5HkQ4A== X-Received: by 2002:a05:600c:5949:b0:439:8345:17a6 with SMTP id 5b1f17b1804b1-439a30d38e9mr130585275e9.12.1740416175731; Mon, 24 Feb 2025 08:56:15 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b02ce404sm113163905e9.7.2025.02.24.08.56.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:15 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 05/20] mm: move _pincount in folio to page[2] on 32bit Date: Mon, 24 Feb 2025 17:55:47 +0100 Message-ID: <20250224165603.1434404-6-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's free up some space on 32bit in page[1] by moving the _pincount to page[2]. For order-1 folios (never anon folios!) on 32bit, we will now also use the GUP_PIN_COUNTING_BIAS approach. A fully-mapped order-1 folio requires 2 references. With GUP_PIN_COUNTING_BIAS being 1024, we'd detect such folios as "maybe pinned" with 512 full mappings, instead of 1024 for order-0. As anon folios are out of the picture (which are the most relevant users of checking for pinnings on *mapped* pages) and we are talking about 32bit, this is not expected to cause any trouble. In __dump_page(), copy one additional folio page if we detect a folio with an order > 1, so we can dump the pincount on order > 1 folios reliably. Note that THPs on 32bit are not particularly common (and we don't care too much about performance), but we want to keep it working reliably, because likely we want to use large folios there as well in the future, independent of PMD leaf support. Once we dynamically allocate "struct folio", fortunately the 32bit specifics will likely go away again; even small folios could then have a pincount and folio_has_pincount() would essentially always return "true". Signed-off-by: David Hildenbrand --- include/linux/mm.h | 11 +++++++++-- include/linux/mm_types.h | 5 +++++ mm/debug.c | 10 +++++++++- mm/gup.c | 8 ++++---- mm/internal.h | 3 ++- mm/page_alloc.c | 14 +++++++++++--- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index f6b6373a864dd..1a4ee028a851e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2004,6 +2004,13 @@ static inline struct folio *pfn_folio(unsigned long = pfn) return page_folio(pfn_to_page(pfn)); } =20 +static inline bool folio_has_pincount(const struct folio *folio) +{ + if (IS_ENABLED(CONFIG_64BIT)) + return folio_test_large(folio); + return folio_order(folio) > 1; +} + /** * folio_maybe_dma_pinned - Report if a folio may be pinned for DMA. * @folio: The folio. @@ -2020,7 +2027,7 @@ static inline struct folio *pfn_folio(unsigned long p= fn) * get that many refcounts, and b) all the callers of this routine are * expected to be able to deal gracefully with a false positive. * - * For large folios, the result will be exactly correct. That's because + * For most large folios, the result will be exactly correct. That's becau= se * we have more tracking data available: the _pincount field is used * instead of the GUP_PIN_COUNTING_BIAS scheme. * @@ -2031,7 +2038,7 @@ static inline struct folio *pfn_folio(unsigned long p= fn) */ static inline bool folio_maybe_dma_pinned(struct folio *folio) { - if (folio_test_large(folio)) + if (folio_has_pincount(folio)) return atomic_read(&folio->_pincount) > 0; =20 /* diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 1d9c68c551d42..31f466d8485bc 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -387,7 +387,9 @@ struct folio { atomic_t _large_mapcount; atomic_t _entire_mapcount; atomic_t _nr_pages_mapped; +#ifdef CONFIG_64BIT atomic_t _pincount; +#endif /* CONFIG_64BIT */ }; unsigned long _usable_1[4]; }; @@ -406,6 +408,9 @@ struct folio { unsigned long _head_2; /* public: */ struct list_head _deferred_list; +#ifndef CONFIG_64BIT + atomic_t _pincount; +#endif /* !CONFIG_64BIT */ /* private: the union with struct page is transitional */ }; struct page __page_2; diff --git a/mm/debug.c b/mm/debug.c index 2d1bd67d957bc..83ef3bd0ccd32 100644 --- a/mm/debug.c +++ b/mm/debug.c @@ -79,12 +79,17 @@ static void __dump_folio(struct folio *folio, struct pa= ge *page, folio_ref_count(folio), mapcount, mapping, folio->index + idx, pfn); if (folio_test_large(folio)) { + int pincount =3D 0; + + if (folio_has_pincount(folio)) + pincount =3D atomic_read(&folio->_pincount); + pr_warn("head: order:%u mapcount:%d entire_mapcount:%d nr_pages_mapped:%= d pincount:%d\n", folio_order(folio), folio_mapcount(folio), folio_entire_mapcount(folio), folio_nr_pages_mapped(folio), - atomic_read(&folio->_pincount)); + pincount); } =20 #ifdef CONFIG_MEMCG @@ -146,6 +151,9 @@ static void __dump_page(const struct page *page) if (idx < MAX_FOLIO_NR_PAGES) { memcpy(&folio, foliop, 2 * sizeof(struct page)); nr_pages =3D folio_nr_pages(&folio); + if (nr_pages > 1) + memcpy(&folio.__page_2, &foliop->__page_2, + sizeof(struct page)); foliop =3D &folio; } =20 diff --git a/mm/gup.c b/mm/gup.c index e5040657870ea..2944fe8cf3174 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -109,7 +109,7 @@ static void gup_put_folio(struct folio *folio, int refs= , unsigned int flags) if (is_zero_folio(folio)) return; node_stat_mod_folio(folio, NR_FOLL_PIN_RELEASED, refs); - if (folio_test_large(folio)) + if (folio_has_pincount(folio)) atomic_sub(refs, &folio->_pincount); else refs *=3D GUP_PIN_COUNTING_BIAS; @@ -164,7 +164,7 @@ int __must_check try_grab_folio(struct folio *folio, in= t refs, * Increment the normal page refcount field at least once, * so that the page really is pinned. */ - if (folio_test_large(folio)) { + if (folio_has_pincount(folio)) { folio_ref_add(folio, refs); atomic_add(refs, &folio->_pincount); } else { @@ -223,7 +223,7 @@ void folio_add_pin(struct folio *folio) * page refcount field at least once, so that the page really is * pinned. */ - if (folio_test_large(folio)) { + if (folio_has_pincount(folio)) { WARN_ON_ONCE(atomic_read(&folio->_pincount) < 1); folio_ref_inc(folio); atomic_inc(&folio->_pincount); @@ -575,7 +575,7 @@ static struct folio *try_grab_folio_fast(struct page *p= age, int refs, * is pinned. That's why the refcount from the earlier * try_get_folio() is left intact. */ - if (folio_test_large(folio)) + if (folio_has_pincount(folio)) atomic_add(refs, &folio->_pincount); else folio_ref_add(folio, diff --git a/mm/internal.h b/mm/internal.h index 7f6d5def00fa0..d33db24c8b17b 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -723,7 +723,8 @@ static inline void prep_compound_head(struct page *page= , unsigned int order) atomic_set(&folio->_large_mapcount, -1); atomic_set(&folio->_entire_mapcount, -1); atomic_set(&folio->_nr_pages_mapped, 0); - atomic_set(&folio->_pincount, 0); + if (IS_ENABLED(CONFIG_64BIT) || order > 1) + atomic_set(&folio->_pincount, 0); if (order > 1) INIT_LIST_HEAD(&folio->_deferred_list); } diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 05a2a9492cdb0..3dff99cc54161 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -959,9 +959,11 @@ static int free_tail_page_prepare(struct page *head_pa= ge, struct page *page) bad_page(page, "nonzero nr_pages_mapped"); goto out; } - if (unlikely(atomic_read(&folio->_pincount))) { - bad_page(page, "nonzero pincount"); - goto out; + if (IS_ENABLED(CONFIG_64BIT)) { + if (unlikely(atomic_read(&folio->_pincount))) { + bad_page(page, "nonzero pincount"); + goto out; + } } break; case 2: @@ -970,6 +972,12 @@ static int free_tail_page_prepare(struct page *head_pa= ge, struct page *page) bad_page(page, "on deferred list"); goto out; } + if (!IS_ENABLED(CONFIG_64BIT)) { + if (unlikely(atomic_read(&folio->_pincount))) { + bad_page(page, "nonzero pincount"); + goto out; + } + } break; case 3: /* the third tail page: hugetlb specifics overlap ->mappings */ --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B60E264FBC for ; Mon, 24 Feb 2025 16:56:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416183; cv=none; b=CoFheQw+Iz0eQtLYV8LxGzngyMmaltnPyaWkPfqM/4AYw/S4Rz4EzmLJFV46LWwlcwy6/01OTR5+6ESm+I7E4KaHKZM6UBU/+TBfajlBpIi1gBKFSvqHWvJk9AXKcjjVttYcw+GyDqGmB5Vwy2HOIqpcm0QEI+OppHSWrfbOhuA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416183; c=relaxed/simple; bh=bXPvxNVE3XeaBKg/JoFKv1juEo8KZy99yrPhS4Bp1Z8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dsuBGfdbTxbyQZoWaF2V7ORGRzP632h5yZ1rPuk5OqZ/3Z8Z5OQX0S0kqEoybPMyLO7m4Xw1WrxZM5hKen8/RfEersRpEF3R7yYi09IZCymMtg7FKL7wBSBTNOl/f7bJe2SEu1wGuz3q6qEgU47njwC7bObx+UNG0ty4ITyU/UQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=J3Aa4IoS; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="J3Aa4IoS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416180; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JowchFKNsu8Ay0dQlyPFoFvu2rxlXy5D20/ejxoZdUQ=; b=J3Aa4IoSZzYkuctLYARUtsuacmOWp72z/ykZSxcdELHGG+MGY4IHEty8fcdPJ88/sY87iW Vg2/cC+kUov7PtBFRchGJ/t8FDFdssvsttu+sG2iQHq9TGd8et6PfC8rud4DcR1RIbIU4j jordv74/naJRxo5JJE8jvymFLnJ+iAQ= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-261-mAVDyivfOAi7xRDR3MvRXA-1; Mon, 24 Feb 2025 11:56:19 -0500 X-MC-Unique: mAVDyivfOAi7xRDR3MvRXA-1 X-Mimecast-MFC-AGG-ID: mAVDyivfOAi7xRDR3MvRXA_1740416178 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38f4bf0d5faso2472196f8f.0 for ; Mon, 24 Feb 2025 08:56:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416178; x=1741020978; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JowchFKNsu8Ay0dQlyPFoFvu2rxlXy5D20/ejxoZdUQ=; b=I2XJ/pjvCwV2y18Z6IYdlVnwcoHsKIA8J31eMnD285G3/NA0mfE/lxQZLHgUnP/Cgp pymFnvfyCDoRE6/Sosi+UbuGB51dODXadWlPzie7Oh5oWPEKxFl+rGARomwhRxwpGK2j wbIcPeRNuhRADUz9u02u7FUGufg7uEEAewaztQrCj4vAh18uKnNXBaQPDfzACfHEp9ij cwWqmY3mcnk+h7nSG4tWlyE2jeG66b7KSlim1nWPU/9ywIJZ4LHho6dhtY/Ibi71Kct/ UiWeeW9NDdh2BvdnIe2pOxP1c8E3jx7agAqX3sOoKtrHkB4Ni5qPXobg3kik9CmVLDr+ BDrA== X-Gm-Message-State: AOJu0YxIuUKbWfBtw4sCTsv00KdIyp8WuAByKDHalh7gX8tZVwDIcJj5 Z2PaznAuA9akJyUgf7qwPpl6sEzmhDh0LqKXYBj2j5gQGZya1aklkDNZJ2zibu9PKKH/CO4z5BM AchIk7nTOrrpfaOtsU9GrVYlD86bq6fGvXRlNt+qpv5zuVvX7Rbr4Vm1HiBkJlkHTW9HbLx3jaA nDACHU1mOBNKKf2XTxP2ekY0c0l+SqhaCUHgDltCMhdLqx X-Gm-Gg: ASbGnctseaf1BnBSif7L9Idh3U2UvVn4qKHxtAhj9xv3FvXsacRu0+YL4knn6Q5CrkP nIbPV3NZvYx/JKmvGhxIaBAIDoHSzBDFkMTR4UuyvuW0+dUlkOiB7HfnMWO6s+Iry179tksu066 KXDL4+oen0A+ao0PKhHPZzsDzg3sfQAxvUQ7YRh2f4tlBlFyeJXyUJPRhXAy58p1icKuwK0siLW j+lO62oQNQYER0q4c6I2Ne3FbZLk7XtuUv9x76sEnPo7H91toGhlkVwlQBjf/zQGeEikdkwSST6 LhKcrWjGd3sKmJpNIhrOhji5nHcGOpPArDUIsBNA2g== X-Received: by 2002:a5d:47a3:0:b0:38d:d8fb:e90f with SMTP id ffacd0b85a97d-38f6e975ca7mr11045691f8f.24.1740416178162; Mon, 24 Feb 2025 08:56:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IEppyEZ/CiRop1NWtbK9zceuiQ/u5I/zpQhUebwrOc1yf1KZs6E/F1s5c1KapHNpyvpSRQcvA== X-Received: by 2002:a5d:47a3:0:b0:38d:d8fb:e90f with SMTP id ffacd0b85a97d-38f6e975ca7mr11045649f8f.24.1740416177739; Mon, 24 Feb 2025 08:56:17 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f258f5fb6sm31629683f8f.44.2025.02.24.08.56.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:17 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 06/20] mm: move _entire_mapcount in folio to page[2] on 32bit Date: Mon, 24 Feb 2025 17:55:48 +0100 Message-ID: <20250224165603.1434404-7-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's free up some space on 32bit in page[1] by moving the _pincount to page[2]. Ordinary folios only use the entire mapcount with PMD mappings, so order-1 folios don't apply. Similarly, hugetlb folios are always larger than order-1, turning the entire mapcount essentially unused for all order-1 folios. Moving it to order-1 folios will not change anything. On 32bit, simply check in folio_entire_mapcount() whether we have an order-1 folio, and return 0 in that case. Note that THPs on 32bit are not particularly common (and we don't care too much about performance), but we want to keep it working reliably, because likely we want to use large folios there as well in the future, independent of PMD leaf support. Once we dynamically allocate "struct folio", the 32bit specifics will go away again; even small folios could then have a pincount. Signed-off-by: David Hildenbrand --- include/linux/mm.h | 2 ++ include/linux/mm_types.h | 3 ++- mm/internal.h | 5 +++-- mm/page_alloc.c | 12 ++++++++---- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 1a4ee028a851e..9c1290588a11e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1333,6 +1333,8 @@ static inline int is_vmalloc_or_module_addr(const voi= d *x) static inline int folio_entire_mapcount(const struct folio *folio) { VM_BUG_ON_FOLIO(!folio_test_large(folio), folio); + if (!IS_ENABLED(CONFIG_64BIT) && unlikely(folio_large_order(folio) =3D=3D= 1)) + return 0; return atomic_read(&folio->_entire_mapcount) + 1; } =20 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 31f466d8485bc..c83dd2f1ee25e 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -385,9 +385,9 @@ struct folio { union { struct { atomic_t _large_mapcount; - atomic_t _entire_mapcount; atomic_t _nr_pages_mapped; #ifdef CONFIG_64BIT + atomic_t _entire_mapcount; atomic_t _pincount; #endif /* CONFIG_64BIT */ }; @@ -409,6 +409,7 @@ struct folio { /* public: */ struct list_head _deferred_list; #ifndef CONFIG_64BIT + atomic_t _entire_mapcount; atomic_t _pincount; #endif /* !CONFIG_64BIT */ /* private: the union with struct page is transitional */ diff --git a/mm/internal.h b/mm/internal.h index d33db24c8b17b..ffdc91b19322e 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -721,10 +721,11 @@ static inline void prep_compound_head(struct page *pa= ge, unsigned int order) =20 folio_set_order(folio, order); atomic_set(&folio->_large_mapcount, -1); - atomic_set(&folio->_entire_mapcount, -1); atomic_set(&folio->_nr_pages_mapped, 0); - if (IS_ENABLED(CONFIG_64BIT) || order > 1) + if (IS_ENABLED(CONFIG_64BIT) || order > 1) { atomic_set(&folio->_pincount, 0); + atomic_set(&folio->_entire_mapcount, -1); + } if (order > 1) INIT_LIST_HEAD(&folio->_deferred_list); } diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 3dff99cc54161..7036530bd1bca 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -947,10 +947,6 @@ static int free_tail_page_prepare(struct page *head_pa= ge, struct page *page) switch (page - head_page) { case 1: /* the first tail page: these may be in place of ->mapping */ - if (unlikely(folio_entire_mapcount(folio))) { - bad_page(page, "nonzero entire_mapcount"); - goto out; - } if (unlikely(folio_large_mapcount(folio))) { bad_page(page, "nonzero large_mapcount"); goto out; @@ -960,6 +956,10 @@ static int free_tail_page_prepare(struct page *head_pa= ge, struct page *page) goto out; } if (IS_ENABLED(CONFIG_64BIT)) { + if (unlikely(atomic_read(&folio->_entire_mapcount) + 1)) { + bad_page(page, "nonzero entire_mapcount"); + goto out; + } if (unlikely(atomic_read(&folio->_pincount))) { bad_page(page, "nonzero pincount"); goto out; @@ -973,6 +973,10 @@ static int free_tail_page_prepare(struct page *head_pa= ge, struct page *page) goto out; } if (!IS_ENABLED(CONFIG_64BIT)) { + if (unlikely(atomic_read(&folio->_entire_mapcount) + 1)) { + bad_page(page, "nonzero entire_mapcount"); + goto out; + } if (unlikely(atomic_read(&folio->_pincount))) { bad_page(page, "nonzero pincount"); goto out; --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 874B7265CDF for ; Mon, 24 Feb 2025 16:56:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416185; cv=none; b=eMrmIrE6sqRRHvokMduiI8HyxdZ5bxEt7iK1WUFCn3FgEipyhUq0v1EbdbFFqcu95AxCpAYZbRdxQ2AhP2wiLAtXcjInTVSWiKUS7w2tGtYHzZoI6YxZPHExXRerzmKxe6hXqAcnnrIjjhadRqZsF+vWOR9AUBJXf48H3EanZ/0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416185; c=relaxed/simple; bh=s7pp9IdgM2n/jUNyLSIifV8ruKPYElCOLPo+/ycD/IY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ErZgM0mxkzAhBYMnheI+eFgvPRcU6UrFBxjajWiMcC9trf/0DPXRfnrnTiFGMCrV23skjzWiu6yp8D9Jrlo5pzzKN4+NXCFP6ln0N0jbeXSPP/XmrQldImTBhsza5BRygS/gm2RzaO0vNUTVVOz3aRcT3g0UbsTO9eLIhzh19H4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JYa7MR4Q; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JYa7MR4Q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416182; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=evcOi+TBEtbvV15MXszgYeJTQO+N+ZLjDDY2W9tEmL0=; b=JYa7MR4QrNOY7VqutCaD0Mpxe2JCDKfj8UASUcHCjtUEOS3bd1To2g4jdw2cTeF8PxkBfg qksulTjgC+vIB6j1i++eqFMP3HfwBVTKqwwRhHwQj3NeiZ6JGTX10+2KRX50m8GCNqADRD 86Dfy0NXEggeNw6qLzITfCaCd7R0ECI= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-313-sukDf9F_NqazAfd26-L8Cw-1; Mon, 24 Feb 2025 11:56:21 -0500 X-MC-Unique: sukDf9F_NqazAfd26-L8Cw-1 X-Mimecast-MFC-AGG-ID: sukDf9F_NqazAfd26-L8Cw_1740416180 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-43995bff469so39046345e9.2 for ; Mon, 24 Feb 2025 08:56:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416180; x=1741020980; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=evcOi+TBEtbvV15MXszgYeJTQO+N+ZLjDDY2W9tEmL0=; b=Q+7kA7M6DuzavUXuLjODbF710IDXGzNmr6PXYHh9NcKDl4AgAEpwsf8i1Vx9Ss6Cq6 8W9EzdVSqBJOy5dBjyGUKcJO4/peaP0EkAIrQbmkKCIFgQ0Vg+ENpPa6JvUX/CG2RQ4M 7mdm8jNl3wGyDRFMjHWT4YKT23PLfDVv8zrKW+dsspoQtlLOPPTrtGmgCRa1VA951I3U VGxVWhhixy4ZG14zIwCRCl75a5NH4wGqziK3st655yFnRWsCOPlfiZcCSagcy/xwWsvS CXzzE/i+/wJze5r70iDTcN9dgVBNprZl/4ndAVr9WT95aiPjZ/zjL/5kd6n9OP9sZ5A/ KzNA== X-Gm-Message-State: AOJu0YwG3WH3B7vSCCWC0tDRorSxnCDvY6m+xmQZWJ45jl8GH9c6lclF +ofKHH5FZ2JuQHt82mupaUPSMZDpQ0pslArmqRpJ7UeYUEllqZFmmUPJm8AHON7YChjKoFwZxfF ku0pi4H49L17CO8HCx8br6GqnE8D4qKy7O6ku2utaQQweaLaeRqWShPSC5Pmud86/U+3MiXzV1+ Q1A1wdZAbGSZYrRG4ZcqU8HQMUV06rg9uqAK1cp8tjs+Gt X-Gm-Gg: ASbGnctcv7ZEbE6CxkdSoZ+uehb6r6yN+r43T5gDswyduccPW4dAd1DCf6rpq7a7WhV PUAsEUo3zS/gAXxviT1AmcjE011bAgSkcYgcBBzM7YnzX0VVyEn1Smk1AOgEEtm2l3J6L6Odo8D NUpa4M71ZKRePDBbk4sn7K6rrMUV9kuz/cyIA+2+0z9RY6lR0gSwC6ffHzQdts5WZPriq5rH4Yo Eqq9TarjjJTl6dJ7WGaAi+dshQbjH+Kv0f2LOZ2O7wDTmi75Iuym35GCYMGSDuOw92FP3H0jF2I lsD6b5h+NfCqUbqTBl+jgB/WkzqYxWmp5/7yWN/mxA== X-Received: by 2002:a05:6000:18a2:b0:38f:50bd:ad11 with SMTP id ffacd0b85a97d-38f707840afmr10692090f8f.5.1740416180258; Mon, 24 Feb 2025 08:56:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJ0CTZOs7oip6JQ6V5mLo1AyAa7BC2bCEfy4T3EJKtPxndTNVUXGmx+vtnsEhrmmF+DjMIVg== X-Received: by 2002:a05:6000:18a2:b0:38f:50bd:ad11 with SMTP id ffacd0b85a97d-38f707840afmr10692042f8f.5.1740416179763; Mon, 24 Feb 2025 08:56:19 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b02e489esm111463335e9.21.2025.02.24.08.56.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:19 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 07/20] mm/rmap: pass dst_vma to folio_dup_file_rmap_pte() and friends Date: Mon, 24 Feb 2025 17:55:49 +0100 Message-ID: <20250224165603.1434404-8-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We'll need access to the destination MM when modifying the large mapcount of a non-hugetlb large folios next. So pass in the destination VMA. Signed-off-by: David Hildenbrand --- include/linux/rmap.h | 42 +++++++++++++++++++++++++----------------- mm/huge_memory.c | 2 +- mm/memory.c | 10 +++++----- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 6abf7960077aa..e795610bade80 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -335,7 +335,8 @@ static inline void hugetlb_remove_rmap(struct folio *fo= lio) } =20 static __always_inline void __folio_dup_file_rmap(struct folio *folio, - struct page *page, int nr_pages, enum rmap_level level) + struct page *page, int nr_pages, struct vm_area_struct *dst_vma, + enum rmap_level level) { const int orig_nr_pages =3D nr_pages; =20 @@ -366,45 +367,47 @@ static __always_inline void __folio_dup_file_rmap(str= uct folio *folio, * @folio: The folio to duplicate the mappings of * @page: The first page to duplicate the mappings of * @nr_pages: The number of pages of which the mapping will be duplicated + * @dst_vma: The destination vm area * * The page range of the folio is defined by [page, page + nr_pages) * * The caller needs to hold the page table lock. */ static inline void folio_dup_file_rmap_ptes(struct folio *folio, - struct page *page, int nr_pages) + struct page *page, int nr_pages, struct vm_area_struct *dst_vma) { - __folio_dup_file_rmap(folio, page, nr_pages, RMAP_LEVEL_PTE); + __folio_dup_file_rmap(folio, page, nr_pages, dst_vma, RMAP_LEVEL_PTE); } =20 static __always_inline void folio_dup_file_rmap_pte(struct folio *folio, - struct page *page) + struct page *page, struct vm_area_struct *dst_vma) { - __folio_dup_file_rmap(folio, page, 1, RMAP_LEVEL_PTE); + __folio_dup_file_rmap(folio, page, 1, dst_vma, RMAP_LEVEL_PTE); } =20 /** * folio_dup_file_rmap_pmd - duplicate a PMD mapping of a page range of a = folio * @folio: The folio to duplicate the mapping of * @page: The first page to duplicate the mapping of + * @dst_vma: The destination vm area * * The page range of the folio is defined by [page, page + HPAGE_PMD_NR) * * The caller needs to hold the page table lock. */ static inline void folio_dup_file_rmap_pmd(struct folio *folio, - struct page *page) + struct page *page, struct vm_area_struct *dst_vma) { #ifdef CONFIG_TRANSPARENT_HUGEPAGE - __folio_dup_file_rmap(folio, page, HPAGE_PMD_NR, RMAP_LEVEL_PTE); + __folio_dup_file_rmap(folio, page, HPAGE_PMD_NR, dst_vma, RMAP_LEVEL_PTE); #else WARN_ON_ONCE(true); #endif } =20 static __always_inline int __folio_try_dup_anon_rmap(struct folio *folio, - struct page *page, int nr_pages, struct vm_area_struct *src_vma, - enum rmap_level level) + struct page *page, int nr_pages, struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma, enum rmap_level level) { const int orig_nr_pages =3D nr_pages; bool maybe_pinned; @@ -470,6 +473,7 @@ static __always_inline int __folio_try_dup_anon_rmap(st= ruct folio *folio, * @folio: The folio to duplicate the mappings of * @page: The first page to duplicate the mappings of * @nr_pages: The number of pages of which the mapping will be duplicated + * @dst_vma: The destination vm area * @src_vma: The vm area from which the mappings are duplicated * * The page range of the folio is defined by [page, page + nr_pages) @@ -488,16 +492,18 @@ static __always_inline int __folio_try_dup_anon_rmap(= struct folio *folio, * Returns 0 if duplicating the mappings succeeded. Returns -EBUSY otherwi= se. */ static inline int folio_try_dup_anon_rmap_ptes(struct folio *folio, - struct page *page, int nr_pages, struct vm_area_struct *src_vma) + struct page *page, int nr_pages, struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma) { - return __folio_try_dup_anon_rmap(folio, page, nr_pages, src_vma, - RMAP_LEVEL_PTE); + return __folio_try_dup_anon_rmap(folio, page, nr_pages, dst_vma, + src_vma, RMAP_LEVEL_PTE); } =20 static __always_inline int folio_try_dup_anon_rmap_pte(struct folio *folio, - struct page *page, struct vm_area_struct *src_vma) + struct page *page, struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma) { - return __folio_try_dup_anon_rmap(folio, page, 1, src_vma, + return __folio_try_dup_anon_rmap(folio, page, 1, dst_vma, src_vma, RMAP_LEVEL_PTE); } =20 @@ -506,6 +512,7 @@ static __always_inline int folio_try_dup_anon_rmap_pte(= struct folio *folio, * of a folio * @folio: The folio to duplicate the mapping of * @page: The first page to duplicate the mapping of + * @dst_vma: The destination vm area * @src_vma: The vm area from which the mapping is duplicated * * The page range of the folio is defined by [page, page + HPAGE_PMD_NR) @@ -524,11 +531,12 @@ static __always_inline int folio_try_dup_anon_rmap_pt= e(struct folio *folio, * Returns 0 if duplicating the mapping succeeded. Returns -EBUSY otherwis= e. */ static inline int folio_try_dup_anon_rmap_pmd(struct folio *folio, - struct page *page, struct vm_area_struct *src_vma) + struct page *page, struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma) { #ifdef CONFIG_TRANSPARENT_HUGEPAGE - return __folio_try_dup_anon_rmap(folio, page, HPAGE_PMD_NR, src_vma, - RMAP_LEVEL_PMD); + return __folio_try_dup_anon_rmap(folio, page, HPAGE_PMD_NR, dst_vma, + src_vma, RMAP_LEVEL_PMD); #else WARN_ON_ONCE(true); return -EBUSY; diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 13152313e77ce..a3264d88d4b49 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1782,7 +1782,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm= _struct *src_mm, src_folio =3D page_folio(src_page); =20 folio_get(src_folio); - if (unlikely(folio_try_dup_anon_rmap_pmd(src_folio, src_page, src_vma))) { + if (unlikely(folio_try_dup_anon_rmap_pmd(src_folio, src_page, dst_vma, sr= c_vma))) { /* Page maybe pinned: split and retry the fault on PTEs. */ folio_put(src_folio); pte_free(dst_mm, pgtable); diff --git a/mm/memory.c b/mm/memory.c index cacf6d53bdf32..ef74c4f009f84 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -845,7 +845,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm= _struct *src_mm, folio_get(folio); rss[mm_counter(folio)]++; /* Cannot fail as these pages cannot get pinned. */ - folio_try_dup_anon_rmap_pte(folio, page, src_vma); + folio_try_dup_anon_rmap_pte(folio, page, dst_vma, src_vma); =20 /* * We do not preserve soft-dirty information, because so @@ -999,14 +999,14 @@ copy_present_ptes(struct vm_area_struct *dst_vma, str= uct vm_area_struct *src_vma folio_ref_add(folio, nr); if (folio_test_anon(folio)) { if (unlikely(folio_try_dup_anon_rmap_ptes(folio, page, - nr, src_vma))) { + nr, dst_vma, src_vma))) { folio_ref_sub(folio, nr); return -EAGAIN; } rss[MM_ANONPAGES] +=3D nr; VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio); } else { - folio_dup_file_rmap_ptes(folio, page, nr); + folio_dup_file_rmap_ptes(folio, page, nr, dst_vma); rss[mm_counter_file(folio)] +=3D nr; } if (any_writable) @@ -1024,7 +1024,7 @@ copy_present_ptes(struct vm_area_struct *dst_vma, str= uct vm_area_struct *src_vma * guarantee the pinned page won't be randomly replaced in the * future. */ - if (unlikely(folio_try_dup_anon_rmap_pte(folio, page, src_vma))) { + if (unlikely(folio_try_dup_anon_rmap_pte(folio, page, dst_vma, src_vma))= ) { /* Page may be pinned, we have to copy. */ folio_put(folio); err =3D copy_present_page(dst_vma, src_vma, dst_pte, src_pte, @@ -1034,7 +1034,7 @@ copy_present_ptes(struct vm_area_struct *dst_vma, str= uct vm_area_struct *src_vma rss[MM_ANONPAGES]++; VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio); } else { - folio_dup_file_rmap_pte(folio, page); + folio_dup_file_rmap_pte(folio, page, dst_vma); rss[mm_counter_file(folio)]++; } =20 --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90AA0266199 for ; Mon, 24 Feb 2025 16:56:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416187; cv=none; b=rpGVco4Y0RDBzZf5iWT6phpnLE159ZFMpW3zrcp5MLo+cSd8CDtkysTAhqDF14r3Kv94vHx1UhA1ZRcJEkL1+2/OroickfZARYYMxgAY0kjePNvNOV3d3aEsEBxt8iQDLaZ8gFpsmvaMhoUEKNKm161msk/LaITclV0I+sAGj48= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416187; c=relaxed/simple; bh=jBMc/6KfiUuhsqx8U3JCMjpv5FKb+tGdfD4lU0RajKI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U1DcJ6Q3vIB0cZ30rDjCqYS7FcthiTPHtMuelUzt4Po/h8hTvDaOYeIAq/85BNlfpMGGSsXSrAHc6/f6bzRFUF4z/qU5P6IxgAkv7UYuLoLJaIn7c7b+ar0QzaHrsjN4pbUVSt0yGPB0biPKUIBQE4AxFBpp5YVS4SqfHrf7HWw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=jKgbasYB; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="jKgbasYB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416184; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lWnUBbJbNal4OtQP6KylSC1HMhJkWgV+LTGJMK3tR3E=; b=jKgbasYB02yaCJ73nPuVCeeVAphtL8YpC5H4XOWwKtenVzDrvTUTyFqKUe9uXJhFjT5WBX TqdsBWlqI++5GO8x5frdfGpYkxKrOKjO3uoimWASuYTwvOv4OYsKMnveTyxybBo58ZMsz4 vi5jdc/VBOGS1bxCvpKQwbh9KrLUacw= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-298-bYNy1XJ6Npqc49XBDLE_Og-1; Mon, 24 Feb 2025 11:56:23 -0500 X-MC-Unique: bYNy1XJ6Npqc49XBDLE_Og-1 X-Mimecast-MFC-AGG-ID: bYNy1XJ6Npqc49XBDLE_Og_1740416182 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-38f37b4a72fso2695782f8f.1 for ; Mon, 24 Feb 2025 08:56:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416182; x=1741020982; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lWnUBbJbNal4OtQP6KylSC1HMhJkWgV+LTGJMK3tR3E=; b=Cqmqbj1eFKROrCmL+Tr9SlceABSkrdiI6rKndJpjtSzrdAdRIeqzj2+iFIPfpAa35A 7e1Vu42jT3uvkxYmjudVtq3VsdrwYrzqiT8MCkOpXpzXbmbTPrTElUc3++fiTyf09DYr iEf3JT7o2A3tBRuWl1VwqV5re2r0AaBO80gISKJe6lN0acGR1b1lSQWEpzz/2Rl9aX3P y0fUSx8qon9qkc/WHTgiCwBDAdK8tje29yZ3x75BysQ2kCgQhkaAKS0IYLkZabA/L4AP aMJLeKaULoaAF3hF62NQ2cA4TvyrwpAAKKe9F+BckAczsJ/PlzFgpfXE2Z8hLAGWuwH3 PYfg== X-Gm-Message-State: AOJu0YwOuU2E1gE65XBaJ8HN1uGXsIZ3+VahodozhQUVOVKuNJx2oB25 G0kNFuMxlfSeOwt7U4frWwMgvf9PdjHkLau9MJ7diDnN4XaY8MULKPF/5tBEAuG7tcOLnDv56ZR YWi09SBx/oO/EBc1oyMXVvqlFCf4PDwHZ42sn4YLAVNMD2FF1XMSUbYTJ6kE4T02XSIMHc2Rnpa 6gwCLevNcTtuRODljoehckds/sHT23G6lPRj1o2sKa+fcf X-Gm-Gg: ASbGncuulcnCbgN8VGPkeglTaV0Vx/jZRcfiGPx7bZvpGRml2YayEnKfvL8EUaoiyIX U6ai7L/F2A4CMKIJXvrtzQ2+5TtrO6fmBnZB+HeJ1R6Na1TJf/6JmXfBE/9q4gM9NEtDdypOOtc hP1g7Xf57JfD1cLfX0KetkyDCLVrM+Vw/Sm6W3fVDYDjMSDiITPqK1ceBxXSPhLwioROfgclWXb C7VN5Qs/wNgYlrUMcE0M4RBN+6dCgyuTjr0TbUYvxQcJPvNjMDXldeScJ6OmYQHZO2+JcnmXHjX s+/Mv6irdZ0blrsN3N6lUf26sC0I48cHe7WhSN2m/g== X-Received: by 2002:a5d:6d82:0:b0:38a:4184:14ec with SMTP id ffacd0b85a97d-38f6f3c507amr11123625f8f.1.1740416182394; Mon, 24 Feb 2025 08:56:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHG35xVnH9LhIY4oSSBr3n8xfuxml7Goul51SSXyqtQOXZX4W9oDib+9PjJY2pvFXqp8djOQA== X-Received: by 2002:a5d:6d82:0:b0:38a:4184:14ec with SMTP id ffacd0b85a97d-38f6f3c507amr11123573f8f.1.1740416181836; Mon, 24 Feb 2025 08:56:21 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f259f8115sm32604557f8f.92.2025.02.24.08.56.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:21 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 08/20] mm/rmap: pass vma to __folio_add_rmap() Date: Mon, 24 Feb 2025 17:55:50 +0100 Message-ID: <20250224165603.1434404-9-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We'll need access to the destination MM when modifying the mapcount large folios next. So pass in the VMA. Signed-off-by: David Hildenbrand --- mm/rmap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index bcec8677f68df..8a7d023b02e0c 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1242,8 +1242,8 @@ int pfn_mkclean_range(unsigned long pfn, unsigned lon= g nr_pages, pgoff_t pgoff, } =20 static __always_inline unsigned int __folio_add_rmap(struct folio *folio, - struct page *page, int nr_pages, enum rmap_level level, - int *nr_pmdmapped) + struct page *page, int nr_pages, struct vm_area_struct *vma, + enum rmap_level level, int *nr_pmdmapped) { atomic_t *mapped =3D &folio->_nr_pages_mapped; const int orig_nr_pages =3D nr_pages; @@ -1411,7 +1411,7 @@ static __always_inline void __folio_add_anon_rmap(str= uct folio *folio, =20 VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); =20 - nr =3D __folio_add_rmap(folio, page, nr_pages, level, &nr_pmdmapped); + nr =3D __folio_add_rmap(folio, page, nr_pages, vma, level, &nr_pmdmapped); =20 if (likely(!folio_test_ksm(folio))) __page_check_anon_rmap(folio, page, vma, address); @@ -1582,7 +1582,7 @@ static __always_inline void __folio_add_file_rmap(str= uct folio *folio, =20 VM_WARN_ON_FOLIO(folio_test_anon(folio), folio); =20 - nr =3D __folio_add_rmap(folio, page, nr_pages, level, &nr_pmdmapped); + nr =3D __folio_add_rmap(folio, page, nr_pages, vma, level, &nr_pmdmapped); __folio_mod_stat(folio, nr, nr_pmdmapped); =20 /* See comments in folio_add_anon_rmap_*() */ --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0299266599 for ; Mon, 24 Feb 2025 16:56:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416191; cv=none; b=oaJ6fDOFLpBZqGRwbDhgajUDWns3s0HDjBquZHO6QJnq/MTq59W4x4TWlh2FWGajbEh3CCefadCvfIxCPVF5pOclrAeokpm0BDAfjfXa1Y0bdNmlYvX8btrPjvaRy7EahwdCu+naNwyVOi0vJR+/wdMhCQfrUtEwkl9YBPhzSBs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416191; c=relaxed/simple; bh=Ycu3g93agHZ5+lzaLOnuhINjqTNBvJJTMooJF9ouEus=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NjJVMf7hp1DWB6suY2ZtQXFWaya/OVBfF9rELaeaJ86pPZJSC+Ps/QmHEUbKvQ4R8cv9SwF4MZcRt7NmDm0rj1U23hWbERo6OJaSo0ND6kjlKhwZy/x/h4YqGPbb2M5Bwk2PoboeeAixFzBqnOS8HubOgXVEnSK7bqZOBgqmsEI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=TGOYcCRk; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TGOYcCRk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416188; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Cb8Ah8lJoOkHJddlEV7mqidKdKB3PYdRohUjXTfR2AA=; b=TGOYcCRk+oScdqIk+XREUk+dL5pqXUK8hLiCLIw05ba26RLYymupLksUFiuT4lotew3cSg r6FoLfP+nS7UZA7dvZ49O0sULwD09Hb4LFj88PXy4tRRCO7J2zyulfj9tjPp4dHHR2ja5p a5lcaoigxY2Xek6iOw0Vnkx1boRf6Jk= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-235-SXR-YjzYOJqB09zPWAM6hA-1; Mon, 24 Feb 2025 11:56:25 -0500 X-MC-Unique: SXR-YjzYOJqB09zPWAM6hA-1 X-Mimecast-MFC-AGG-ID: SXR-YjzYOJqB09zPWAM6hA_1740416184 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4393e8738b3so32900995e9.0 for ; Mon, 24 Feb 2025 08:56:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416184; x=1741020984; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Cb8Ah8lJoOkHJddlEV7mqidKdKB3PYdRohUjXTfR2AA=; b=SwS7q9/j7gd+F9Yk3vWEm9DT0Isa1jd4kvyy+m4Hf98HRJKlxvR3Qv2lnB3yLvKM5K YvMbfVNSQt7CN5Q4E3cuQ2LfzGcHAVCy09i56cHy8HDv91OdMsLnXWPhOd6lCIAQfBHK ostQo721jaZ/vtWjIWXcMXPTn1yGb/pI/ZNLfm4wzEhUQvd6RUp0LT8U7zc63X0W2k3v Ppv7TIfOpDw7u1/HLeRQs64MjD+6Me0oJMsiEQepkcSMB4fShjs39R8NlxGvFJNL6tJB yqm2hyYMKx/hP7qRLrkbV6C0LWzio+XQ1rFH7jCnlxkllWUGqyseZ5ReIIyjZ4m3CEHj wqwg== X-Gm-Message-State: AOJu0Yy/UxcCJLj6NT3uswcYCr/pBc4guABdMK7PkiUfBaKrQ485G8nU T4QngEcgGi3E5EqkCwcBnr709Krxnm4WEAbddM0dhzUNnK7Ddd/5CkP9/zVrcuTKXFmZwZcWyuH upeUlNDGibAmD2ink1jY74FQSlRoRZWsUj/kObucFweenoH/S2QD+mJ7NE0SdXiV19S57yisuaF CvtgWj2JG2hNn92qvg7mixHpxtts0p/K3ROtqefZPvOXie X-Gm-Gg: ASbGnct/YmTGiyzyohqM0zHD4eXKOS1dVcYntJWAjmf2nG3mIXfD8nLk6T869wLAIBm 0ORipSMhB3U5NxkTc2ov9qMIk4LfQALtXbDn26U3jiuct7EVk67HgixSbX7x3D1F5oXrSQw9YyH cn7S3w9W4jSKz6Hiz8ucu9CRQNN+wxUIgnQbb+k++muJjnFylMatku/ysm2rcFbSJkfsWG/6Eb0 QtlH6EEqpxjzNHDehbBOnapY3RzjKF6pBFPq/KHJvVzvjyk4uMp6PFm+cj97+VD+Vdkqkbmzhnq 41t/CQIpQ+QD8XG5RYNJPp5M6IcAjGOQ0QB+tZ3Paw== X-Received: by 2002:a05:600c:4e8c:b0:439:6712:643d with SMTP id 5b1f17b1804b1-439ae1e6c15mr101103615e9.9.1740416184393; Mon, 24 Feb 2025 08:56:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IELVR15SfmZbX6a5/LWsD6f0rg6Y8hiw7+oMACeve7JZ9CA6HteUq/ZR57fRSUerpUCoh+bqg== X-Received: by 2002:a05:600c:4e8c:b0:439:6712:643d with SMTP id 5b1f17b1804b1-439ae1e6c15mr101103135e9.9.1740416183869; Mon, 24 Feb 2025 08:56:23 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b031b613sm111082365e9.33.2025.02.24.08.56.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:23 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 09/20] mm/rmap: abstract large mapcount operations for large folios (!hugetlb) Date: Mon, 24 Feb 2025 17:55:51 +0100 Message-ID: <20250224165603.1434404-10-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's abstract the operations so we can extend these operations easily. Signed-off-by: David Hildenbrand --- include/linux/rmap.h | 32 ++++++++++++++++++++++++++++---- mm/rmap.c | 14 ++++++-------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index e795610bade80..d1e888cc97a58 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -173,6 +173,30 @@ static inline void anon_vma_merge(struct vm_area_struc= t *vma, =20 struct anon_vma *folio_get_anon_vma(const struct folio *folio); =20 +static inline void folio_set_large_mapcount(struct folio *folio, int mapco= unt, + struct vm_area_struct *vma) +{ + /* Note: mapcounts start at -1. */ + atomic_set(&folio->_large_mapcount, mapcount - 1); +} + +static inline void folio_add_large_mapcount(struct folio *folio, + int diff, struct vm_area_struct *vma) +{ + atomic_add(diff, &folio->_large_mapcount); +} + +static inline void folio_sub_large_mapcount(struct folio *folio, + int diff, struct vm_area_struct *vma) +{ + atomic_sub(diff, &folio->_large_mapcount); +} + +#define folio_inc_large_mapcount(folio, vma) \ + folio_add_large_mapcount(folio, 1, vma) +#define folio_dec_large_mapcount(folio, vma) \ + folio_sub_large_mapcount(folio, 1, vma) + /* RMAP flags, currently only relevant for some anon rmap operations. */ typedef int __bitwise rmap_t; =20 @@ -352,12 +376,12 @@ static __always_inline void __folio_dup_file_rmap(str= uct folio *folio, do { atomic_inc(&page->_mapcount); } while (page++, --nr_pages > 0); - atomic_add(orig_nr_pages, &folio->_large_mapcount); + folio_add_large_mapcount(folio, orig_nr_pages, dst_vma); break; case RMAP_LEVEL_PMD: case RMAP_LEVEL_PUD: atomic_inc(&folio->_entire_mapcount); - atomic_inc(&folio->_large_mapcount); + folio_inc_large_mapcount(folio, dst_vma); break; } } @@ -451,7 +475,7 @@ static __always_inline int __folio_try_dup_anon_rmap(st= ruct folio *folio, ClearPageAnonExclusive(page); atomic_inc(&page->_mapcount); } while (page++, --nr_pages > 0); - atomic_add(orig_nr_pages, &folio->_large_mapcount); + folio_add_large_mapcount(folio, orig_nr_pages, dst_vma); break; case RMAP_LEVEL_PMD: case RMAP_LEVEL_PUD: @@ -461,7 +485,7 @@ static __always_inline int __folio_try_dup_anon_rmap(st= ruct folio *folio, ClearPageAnonExclusive(page); } atomic_inc(&folio->_entire_mapcount); - atomic_inc(&folio->_large_mapcount); + folio_inc_large_mapcount(folio, dst_vma); break; } return 0; diff --git a/mm/rmap.c b/mm/rmap.c index 8a7d023b02e0c..08846b7eced60 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1266,7 +1266,7 @@ static __always_inline unsigned int __folio_add_rmap(= struct folio *folio, atomic_add_return_relaxed(first, mapped) < ENTIRELY_MAPPED) nr =3D first; =20 - atomic_add(orig_nr_pages, &folio->_large_mapcount); + folio_add_large_mapcount(folio, orig_nr_pages, vma); break; case RMAP_LEVEL_PMD: case RMAP_LEVEL_PUD: @@ -1290,7 +1290,7 @@ static __always_inline unsigned int __folio_add_rmap(= struct folio *folio, nr =3D 0; } } - atomic_inc(&folio->_large_mapcount); + folio_inc_large_mapcount(folio, vma); break; } return nr; @@ -1556,14 +1556,12 @@ void folio_add_new_anon_rmap(struct folio *folio, s= truct vm_area_struct *vma, SetPageAnonExclusive(page); } =20 - /* increment count (starts at -1) */ - atomic_set(&folio->_large_mapcount, nr - 1); + folio_set_large_mapcount(folio, nr, vma); atomic_set(&folio->_nr_pages_mapped, nr); } else { /* increment count (starts at -1) */ atomic_set(&folio->_entire_mapcount, 0); - /* increment count (starts at -1) */ - atomic_set(&folio->_large_mapcount, 0); + folio_set_large_mapcount(folio, 1, vma); atomic_set(&folio->_nr_pages_mapped, ENTIRELY_MAPPED); if (exclusive) SetPageAnonExclusive(&folio->page); @@ -1665,7 +1663,7 @@ static __always_inline void __folio_remove_rmap(struc= t folio *folio, break; } =20 - atomic_sub(nr_pages, &folio->_large_mapcount); + folio_sub_large_mapcount(folio, nr_pages, vma); do { last +=3D atomic_add_negative(-1, &page->_mapcount); } while (page++, --nr_pages > 0); @@ -1678,7 +1676,7 @@ static __always_inline void __folio_remove_rmap(struc= t folio *folio, break; case RMAP_LEVEL_PMD: case RMAP_LEVEL_PUD: - atomic_dec(&folio->_large_mapcount); + folio_dec_large_mapcount(folio, vma); last =3D atomic_add_negative(-1, &folio->_entire_mapcount); if (last) { nr =3D atomic_sub_return_relaxed(ENTIRELY_MAPPED, mapped); --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA6FA266B41 for ; Mon, 24 Feb 2025 16:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416191; cv=none; b=SnNFhfSYGiYBIc/Y+fYSQSzcoY+uZuZMbuHZBQq/7tAtS43x+vu348yVZ1BgpSJNVYWe62vsXUwFFaEHRQA7+k6y+IlXf6Z8eIeDxze3WpBy9iky7EAkVX3KrY+QnYNIt6Z1LuZNbms4k2sVlpC7WoM8T6Kkapiz3929Gxyg4IY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416191; c=relaxed/simple; bh=OBXpWDsR7o4dKf8NkhRON4PwL/H6iQiwplPc25GckEo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UWwQUGFwMKr153T5C00B5CEEwqVu6Mqa865m9RKBHSWHPgyHs6xWyBOeLTbGCZK9gEeNdcWMAAeCP206AW98hYQV/7jClromZzCO8+KgtfkyaQZlau/FlUCrvxbUFennXtSDh5VJ/kz56QeVDLD8BDE8kf7ddTfR3Mn/DhzWCHw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XYqazAkG; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XYqazAkG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416188; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VusPlBbKEOGpL4wS1BqoWY6CYb2f4HQ+/wIDh8p+vAo=; b=XYqazAkG+H8MsZ1DsDZBLAuDIk8qE2QVwewqvdVt3+9wnFjoRfUHy9ZGePLPfFwxzgYokt YIObUxzfFuVrQt+6ngONj9KBCDtWCAPVg3DDSobzOBoVcTUI5sGZqLV68vQOhDzxRMZPkn y8h2nK/2vDEBtX24uaEGJegRiFXR588= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-407-5j3xAji8PFGKr_GPwX6ECw-1; Mon, 24 Feb 2025 11:56:27 -0500 X-MC-Unique: 5j3xAji8PFGKr_GPwX6ECw-1 X-Mimecast-MFC-AGG-ID: 5j3xAji8PFGKr_GPwX6ECw_1740416186 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-438e180821aso23678695e9.1 for ; Mon, 24 Feb 2025 08:56:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416186; x=1741020986; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VusPlBbKEOGpL4wS1BqoWY6CYb2f4HQ+/wIDh8p+vAo=; b=ba60Cb1TIAt2rK7I65I0dk05XIV1J471D8AW2DElOeKfo7OynoWoWVqI/NRkfGXBsK +M3cNf2PfHY0+ap0n7PZvdisC1URyLKULzdlAUKk4Pfer8r7xEJ391LNW0EUVRDg5UdN lsGDm26U8IuXdzJ2oSYw2k6Iw6JtBewgWib/kcAAVz4OwNJqddYQ3hDzx3Wmepmy43g9 +H0tMtvynAcOWVzoEl++ytl6KwznhFFeJY4hXp7lDNmONmcGtiqKaR+C5ArjBuMpotkO G9x6BJl8Quz6h8G1etaKrrAoM2PVvZYWAU5Tgyaz+OnMzKUX4MsbCcoOFKMccRCFp8Eg NgBQ== X-Gm-Message-State: AOJu0YzbknefouMP+8QgNwfr8LopDznTHPARWjdBBBYh+xINtf0VHXJH s1gWmj4NAm9AbV5LsNQt/mGRQqO38bZntUToNAczeJHyPllyl40cMCe0jtrO/LJXvtBf/oW5ulz AcCeAIB2ILbikCMhVximl6Ev2Qp8T6B+g/N0jEFbO0G+pw8NWIb2kXeb6w92E3stz4rYq9JlvaN PVSQ3myrGKEnbSfOrchVWBmCP9wbjJY9JeIMuiGWKRgw2+ X-Gm-Gg: ASbGncsir+S843OzRfzZ2UVRRREHtokrLZn0UqiWP+sKqLPUZDbhmhiaUap5tQEsIAe dhaadCTXgVYyI+qrRQqIyQKxWpwzdxAcMuAcZvNxwU/gDr2hnmhANrcltb+tGrzHBXIrY5Mz8cp cV+QUc2N/bMsjk9hiqotHM/l6ndOPvQHt7bBtnXC+QwImpWcInXA19f5UO3H//Z8FBF9mGcAmtM 1S9ux1EWtXmGjZllAFomXJouQLLe1og9yDl8F4RwOA3T4z9g9g4hrTfxn7leaOOA0f5EQw2BU8p f0NtXE6PLh9OcGOhvNS8K2TENud6akUvzhwsoOmXew== X-Received: by 2002:a05:600c:4f43:b0:439:9828:c425 with SMTP id 5b1f17b1804b1-43ab0f2db61mr336885e9.7.1740416186429; Mon, 24 Feb 2025 08:56:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHWfaIC9jLsYbcLXi3Fdeg1qCi0/sJNUC8bLA+3qaJ5mWtkIjQvv3YtNjkQQj/btQBGxQxPlw== X-Received: by 2002:a05:600c:4f43:b0:439:9828:c425 with SMTP id 5b1f17b1804b1-43ab0f2db61mr336445e9.7.1740416185910; Mon, 24 Feb 2025 08:56:25 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b030b347sm111391345e9.26.2025.02.24.08.56.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:25 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 10/20] bit_spinlock: __always_inline (un)lock functions Date: Mon, 24 Feb 2025 17:55:52 +0100 Message-ID: <20250224165603.1434404-11-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The compiler might decide that it is a smart idea to not inline bit_spin_lock(), primarily when a couple of functions in the same file end up calling it. Especially when used in RMAP map/unmap code next, the compiler sometimes decides to not inline, which is then observable in some micro-benchmarks. Let's simply flag all lock/unlock functions as __always_inline; arch_test_and_set_bit_lock() and friends are already tagged like that (but not test_and_set_bit_lock() for some reason). If ever a problem, we could split it into a fast and a slow path, and only force the fast path to be inlined. But there is nothing particularly "big" here. Signed-off-by: David Hildenbrand --- include/linux/bit_spinlock.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/bit_spinlock.h b/include/linux/bit_spinlock.h index bbc4730a6505c..c0989b5b0407f 100644 --- a/include/linux/bit_spinlock.h +++ b/include/linux/bit_spinlock.h @@ -13,7 +13,7 @@ * Don't use this unless you really need to: spin_lock() and spin_unlock() * are significantly faster. */ -static inline void bit_spin_lock(int bitnum, unsigned long *addr) +static __always_inline void bit_spin_lock(int bitnum, unsigned long *addr) { /* * Assuming the lock is uncontended, this never enters @@ -38,7 +38,7 @@ static inline void bit_spin_lock(int bitnum, unsigned lon= g *addr) /* * Return true if it was acquired */ -static inline int bit_spin_trylock(int bitnum, unsigned long *addr) +static __always_inline int bit_spin_trylock(int bitnum, unsigned long *add= r) { preempt_disable(); #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) @@ -54,7 +54,7 @@ static inline int bit_spin_trylock(int bitnum, unsigned l= ong *addr) /* * bit-based spin_unlock() */ -static inline void bit_spin_unlock(int bitnum, unsigned long *addr) +static __always_inline void bit_spin_unlock(int bitnum, unsigned long *add= r) { #ifdef CONFIG_DEBUG_SPINLOCK BUG_ON(!test_bit(bitnum, addr)); @@ -71,7 +71,7 @@ static inline void bit_spin_unlock(int bitnum, unsigned l= ong *addr) * non-atomic version, which can be used eg. if the bit lock itself is * protecting the rest of the flags in the word. */ -static inline void __bit_spin_unlock(int bitnum, unsigned long *addr) +static __always_inline void __bit_spin_unlock(int bitnum, unsigned long *a= ddr) { #ifdef CONFIG_DEBUG_SPINLOCK BUG_ON(!test_bit(bitnum, addr)); --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EB03266B5C for ; Mon, 24 Feb 2025 16:56:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416193; cv=none; b=SlM/EYi3vKV8SHqYHrvRVKJaDhYAHmnQWhPdyIsAbYPTs5TQyzaXKIsppTlFcxx72F0QWVGL846LEZYkH0GW+RGaZ+NL89Jef06JOiOaKrd0gvcNtS7B3b8tfVTMdU4MnIuR3eT01T4I76e78hGN2zKfi/8yGpHWeUZ5N3GtBWE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416193; c=relaxed/simple; bh=RlWl3fpiF9O9sdm/iMb9TRR2Zh30yDuqbmqk7QeHHkM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iKLDDn8SRng/znswASc1lmALSjTrb/TUUPONDMPCP2+JKSE0uAW2/BY7S+0s72olVdzbBFNLPSI/Z7NRleZmTRnv+MQRkocy+cryWLN12qpxuIM+EnYV8hnm470Rvu9RM1Vde1OBTZduR9dXQfd8OCMTDjCe5jQP3UWDrgiwBSM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=N6x230+x; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="N6x230+x" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416190; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TqnqolcXMjdbgOoHxDkquZtgq0jreZJuwmbRkc4obqU=; b=N6x230+xCXv2O/9pqIOzFsCrDX97wuOwVG+FD5/TWPf1Hsm/zyAKzWNLWmUvJqsS/xrBjI +7J8c7lZz9cqW+ZSuCq6WeXd7OC2kfE2KLnzmQpsQ8kEcEUWNFwBaEmVYkX8UFRmgpbpVJ w5C4i7lnNFwMLhijh1Qy3sIqtKP19bM= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-596-dSqbwZGdNx-GLPnDpo_MfA-1; Mon, 24 Feb 2025 11:56:29 -0500 X-MC-Unique: dSqbwZGdNx-GLPnDpo_MfA-1 X-Mimecast-MFC-AGG-ID: dSqbwZGdNx-GLPnDpo_MfA_1740416188 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38f44be93a8so2474431f8f.1 for ; Mon, 24 Feb 2025 08:56:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416188; x=1741020988; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TqnqolcXMjdbgOoHxDkquZtgq0jreZJuwmbRkc4obqU=; b=mwIJaTlQDF+6yexjXO1nBMcjo61t16WJwo098SQWagttN5Hw7TdfrMO0Wzd9SAuLa3 p7wVpkSMkQBj7wDETFGM3l8Vh/CxzL/NtlrOza/KWhwUWXJS+7Bxzt0H2oIUDjCn9yaG 1kAmiUIPEDOWv7jIUsclbeAXHLqUDZ1cnmGjG9ex8lqNJYFoqKbGYTZVZFg6NQCbgz2v CYy0UhO+x6qSGzYHGGpu8I2b3Hic/JYdgzl9J1jml+s4ehOM3/L9hCKSKm55/SZbvvYu Gpt7B4FYrKSZSvlTcWDAlvl+it4mZ+ag5HB1ITgAiZz3btf7pw62qJiJD3Z7pI1OK7/J vEqg== X-Gm-Message-State: AOJu0Yw+oP06ovrrF4X2PmQHrrHcX85rYiX70r8sjmFGxEwvIyqt/LOA xlU3+zE8cxLFxKQMb1v1MmTixiaLE1UPkWNkfz2DU5LWUzDO9GEzm4sQSBjl7ikiOambbUPNRyr SJxoptcTTMjIypb41JgYN1v8WtY1RBB7bi9jQFbP80+OJoIq60ALiHGqIyzDi0n5XItyTJ1m/cR 4Z8WVimdygBJIg/cAYGh5f/NJAFA4IYBta+ld+rSwArOCb X-Gm-Gg: ASbGnctNFoAwzixYUYCja5kdV/EBzzY6Y8sg6rTGs007STWXO9tUGncpoTluolPO+Ur YZds+jtHG3vjRdGLjOsAdYb5sFqWzpJQCRl1/UU9llGQ8gBVvMq4yZcmYRoH8b+FVbNxgQSXdcH 5e8ZtzgnUwnin48JS1aSKFFN6jfkkwiLcREIIN4VXGGdqSRQSM2U1AyAPs+ere7MMl3sFrx2rKE RB+b6hiAXMGE7mn3onmhPs0wo12wBGZPiGwBOYr6bOu1u2StRgEVVYb9Ni779OpjFizQLSj+6Qi fYIXVsQzBtR/v92yOpLjCJ5++eJSt9jBSzGetV7wHg== X-Received: by 2002:adf:e50d:0:b0:38f:2f9f:5249 with SMTP id ffacd0b85a97d-38f6e95b1acmr9525768f8f.19.1740416188414; Mon, 24 Feb 2025 08:56:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEFpYW1I6Rdgq9wh83UDsQAXL1XjDcZxKn17wlosPtiSthbm9EkXmkPxjrqhOMZ4wG7mJ1LrA== X-Received: by 2002:adf:e50d:0:b0:38f:2f9f:5249 with SMTP id ffacd0b85a97d-38f6e95b1acmr9525729f8f.19.1740416187961; Mon, 24 Feb 2025 08:56:27 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f259f8121sm31621063f8f.88.2025.02.24.08.56.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:27 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn , "Kirill A. Shutemov" Subject: [PATCH v2 11/20] mm/rmap: use folio_large_nr_pages() in add/remove functions Date: Mon, 24 Feb 2025 17:55:53 +0100 Message-ID: <20250224165603.1434404-12-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's just use the "large" variant in code where we are sure that we have a large folio in our hands: this way we are sure that we don't perform any unnecessary "large" checks. While at it, convert the VM_BUG_ON_VMA to a VM_WARN_ON_ONCE. Maybe in the future there will not be a difference in that regard between large and small folios; in that case, unifying the handling again will be easy. E.g., folio_large_nr_pages() will simply translate to folio_nr_pages() until we replace all instances. Reviewed-by: Kirill A. Shutemov Signed-off-by: David Hildenbrand --- mm/rmap.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 08846b7eced60..c9922928616ee 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1274,7 +1274,7 @@ static __always_inline unsigned int __folio_add_rmap(= struct folio *folio, if (first) { nr =3D atomic_add_return_relaxed(ENTIRELY_MAPPED, mapped); if (likely(nr < ENTIRELY_MAPPED + ENTIRELY_MAPPED)) { - nr_pages =3D folio_nr_pages(folio); + nr_pages =3D folio_large_nr_pages(folio); /* * We only track PMD mappings of PMD-sized * folios separately. @@ -1522,14 +1522,11 @@ void folio_add_anon_rmap_pmd(struct folio *folio, s= truct page *page, void folio_add_new_anon_rmap(struct folio *folio, struct vm_area_struct *v= ma, unsigned long address, rmap_t flags) { - const int nr =3D folio_nr_pages(folio); const bool exclusive =3D flags & RMAP_EXCLUSIVE; - int nr_pmdmapped =3D 0; + int nr =3D 1, nr_pmdmapped =3D 0; =20 VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio); VM_WARN_ON_FOLIO(!exclusive && !folio_test_locked(folio), folio); - VM_BUG_ON_VMA(address < vma->vm_start || - address + (nr << PAGE_SHIFT) > vma->vm_end, vma); =20 /* * VM_DROPPABLE mappings don't swap; instead they're just dropped when @@ -1547,6 +1544,7 @@ void folio_add_new_anon_rmap(struct folio *folio, str= uct vm_area_struct *vma, } else if (!folio_test_pmd_mappable(folio)) { int i; =20 + nr =3D folio_large_nr_pages(folio); for (i =3D 0; i < nr; i++) { struct page *page =3D folio_page(folio, i); =20 @@ -1559,6 +1557,7 @@ void folio_add_new_anon_rmap(struct folio *folio, str= uct vm_area_struct *vma, folio_set_large_mapcount(folio, nr, vma); atomic_set(&folio->_nr_pages_mapped, nr); } else { + nr =3D folio_large_nr_pages(folio); /* increment count (starts at -1) */ atomic_set(&folio->_entire_mapcount, 0); folio_set_large_mapcount(folio, 1, vma); @@ -1568,6 +1567,9 @@ void folio_add_new_anon_rmap(struct folio *folio, str= uct vm_area_struct *vma, nr_pmdmapped =3D nr; } =20 + VM_WARN_ON_ONCE(address < vma->vm_start || + address + (nr << PAGE_SHIFT) > vma->vm_end); + __folio_mod_stat(folio, nr, nr_pmdmapped); mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON, 1); } @@ -1681,7 +1683,7 @@ static __always_inline void __folio_remove_rmap(struc= t folio *folio, if (last) { nr =3D atomic_sub_return_relaxed(ENTIRELY_MAPPED, mapped); if (likely(nr < ENTIRELY_MAPPED)) { - nr_pages =3D folio_nr_pages(folio); + nr_pages =3D folio_large_nr_pages(folio); if (level =3D=3D RMAP_LEVEL_PMD) nr_pmdmapped =3D nr_pages; nr =3D nr_pages - (nr & FOLIO_PAGES_MAPPED); --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8F7C266F14 for ; Mon, 24 Feb 2025 16:56:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416197; cv=none; b=sl4i4o1ZMT+W0CSqauQcWhnUgYkVzUuLjW53zUwpSl2NrLrl8sJVuPOPkDJN2P77kV4EKagpE6I277wVkNmqa+959eCmPBQED1jaCHICB6zJVoOU7PT16JnUFN3bz7SgXUV38NiNoP9ZrbMb07bX/evJAtrEud93pMy/NdtRLOA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416197; c=relaxed/simple; bh=vWrUcOhFvYtcC/ATMuhKYsfsNDDwitsHYlUbAUpMZb8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IjUMK/uwQCopoFJKsTlmlodemOZrYMjJ5i6its4NQXfptHuL67FLMODu5gddFrYwxItC0twdQTSofnHccCOKmZwpjg3aSP8nt1XopipCRsxPkvo1nsA3eIIPs6MiyRRqDpHtbgYZx6yePqktpus0E5pASKkoplx7Afqci4s25Kc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=b/j4iBn3; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="b/j4iBn3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416194; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4nktJgh7Km5OllUbbPT14AR851zLdnVlyxLYdX13mgM=; b=b/j4iBn3AkwdaYOR1YwpgScmumBJYIv8NRsAkcyTzLoHNc9LDAI4QN1ru20AjSevGvESHg XrHVcqqo4KOoAk0pLsf2Gr3R4uhJW75ir8JRs0jd8nrfBrs7V3dFgTFq/fHCigYS6AXclp 3WqOdLk5/MOHAROl2l8KwOtCUWXzFas= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-244-L1jZuyYjM-eY95WYKL71rg-1; Mon, 24 Feb 2025 11:56:32 -0500 X-MC-Unique: L1jZuyYjM-eY95WYKL71rg-1 X-Mimecast-MFC-AGG-ID: L1jZuyYjM-eY95WYKL71rg_1740416191 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-38f39352f1dso1755570f8f.0 for ; Mon, 24 Feb 2025 08:56:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416191; x=1741020991; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4nktJgh7Km5OllUbbPT14AR851zLdnVlyxLYdX13mgM=; b=wOiFvbRUWtWKeGFLf+paD8pkFY9ne1rOAPTmlVP75OffWUmOS8N2LoDe54MPsf8w+M m54Yxg/Opeh1tyPwCMkNIPZ2pnSCY1hELw9tzvE7EH7qDnB/fshMMW+GKYWuZZnkEfun bCYcpUJjh7j4IlRJzuVoTozXXV8lDRj9FT3cPe7LdKVnkcQ8d43yQiOIvaQAjAdC3Xgo UyZwkzUePT9+mSRqe/ukomMKSmpxfRLbsagCFKh99mpH2URi1QhG1bkxh4kLQCVQUGlF 0n87FFWv69hUirQEW+/PMCwX9y3XmDpuw+1caNl8BJZKcvFJzmPsaodpVyB4FMkM5EbK aKgw== X-Gm-Message-State: AOJu0YwXRuLQkV4Pw/Z1XYE3/J0ZGrmSVNrmKw5G+Yotp33AEXui367t 6aBhN48VlPRtFDg+H34mKAts1Q4TUQFQh6oWm508ir5ieEN8HYtaSA2DIrDlIyMSThphblcYIpN D2Aw7P+/x+fiiCySOGEPbXFBJ9cYZ6AaHcVyrenjtlE+eyxYZpihK3Lqctetkyjq0L3iJ4MMOsG EdLFsdN/7tYLCfaw2AyWFbqQZKnn2dtIfC3VJV368hyL9t X-Gm-Gg: ASbGncv0vK0chrnXKyJS77p5poWFd5wD23QSEtA/covT2UZ+/wL40dX+l98jy767oLC tV9hDtfpB8J6x/yoKwPQgjXDer3bGk8JH+Vx5HCA97t6dSI4uK31I321WhPVDJRsTHhcBc9Eacl R2nTRQvAAiMGMLo8tzZ2umWG//Pj7CfouOTtMoT/KIJUE54sN7yiQo9S9UTe9vmPNYMtruNNtVi YZR6dnrcNE/0/jIGdd2RMhHiPpbefkLFnMFBRm23PCF3MZGI4jyBGVSeQkjfhnsYJ38eoEwHyXA lTlEFBOtb6QGVvpTJfXM/CbBKKpiYfhBTjtAkgHKCA== X-Received: by 2002:a05:6000:188d:b0:38d:df83:7142 with SMTP id ffacd0b85a97d-38f7078c0a9mr10960298f8f.22.1740416190799; Mon, 24 Feb 2025 08:56:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IHolbkh2QWz49qXbuMSOIH32yACqqeS6botLOgN7rWbjv1cvtvSFiQIUCwh9g31Rr/N7UNb4A== X-Received: by 2002:a05:6000:188d:b0:38d:df83:7142 with SMTP id ffacd0b85a97d-38f7078c0a9mr10960239f8f.22.1740416190066; Mon, 24 Feb 2025 08:56:30 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f258fc7ecsm31759439f8f.49.2025.02.24.08.56.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:29 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 12/20] mm/rmap: basic MM owner tracking for large folios (!hugetlb) Date: Mon, 24 Feb 2025 17:55:54 +0100 Message-ID: <20250224165603.1434404-13-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For small folios, we traditionally use the mapcount to decide whether it was "certainly mapped exclusively" by a single MM (mapcount =3D=3D 1) or whether it "maybe mapped shared" by multiple MMs (mapcount > 1). For PMD-sized folios that were PMD-mapped, we were able to use a similar mechanism (single PMD mapping), but for PTE-mapped folios and in the future folios that span multiple PMDs, this does not work. So we need a different mechanism to handle large folios. Let's add a new mechanism to detect whether a large folio is "certainly mapped exclusively", or whether it is "maybe mapped shared". We'll use this information next to optimize CoW reuse for PTE-mapped anonymous THP, and to convert folio_likely_mapped_shared() to folio_maybe_mapped_shared(), independent of per-page mapcounts. For each large folio, we'll have two slots, whereby a slot stores: (1) an MM id: unique id assigned to each MM (2) a per-MM mapcount If a slot is unoccupied, it can be taken by the next MM that maps folio page. In addition, we'll remember the current state -- "mapped exclusively" vs. "maybe mapped shared" -- and use a bit spinlock to sync on updates and to reduce the total number of atomic accesses on updates. In the future, it might be possible to squeeze a proper spinlock into "struct folio". For now, keep it simple, as we require the whole thing with THP only, that is incompatible with RT. As we have to squeeze this information into the "struct folio" of even folios of order-1 (2 pages), and we generally want to reduce the required metadata, we'll assign each MM a unique ID that can fit into an int. In total, we can squeeze everything into 4x int (2x long) on 64bit. 32bit support is a bit challenging, because we only have 2x long =3D=3D 2x int in order-1 folios. But we can make it work for now, because we neither expect many MMs nor very large folios on 32bit. We will reliably detect folios as "mapped exclusively" vs. "mapped shared" as long as only two MMs map pages of a folio at one point in time -- for example with fork() and short-lived child processes, or with apps that hand over state from one instance to another. As soon as three MMs are involved at the same time, we might detect "maybe mapped shared" although the folio is "mapped exclusively". Example 1: (1) App1 faults in a (shmem/file-backed) folio page -> Tracked as MM0 (2) App2 faults in a folio page -> Tracked as MM1 (4) App1 unmaps all folio pages -> We will detect "mapped exclusively". Example 2: (1) App1 faults in a (shmem/file-backed) folio page -> Tracked as MM0 (2) App2 faults in a folio page -> Tracked as MM1 (3) App3 faults in a folio page -> No slot available, tracked as "unknown" (4) App1 and App2 unmap all folio pages -> We will detect "maybe mapped shared". Make use of __always_inline to keep possible performance degradation when (un)mapping large folios to a minimum. Note: by squeezing the two flags into the "unsigned long" that stores the MM ids, we can use non-atomic __bit_spin_unlock() and non-atomic setting/clearing of the "maybe mapped shared" bit, effectively not adding any new atomics on the hot path when updating the large mapcount + new metadata, which further helps reduce the runtime overhead in micro-benchmarks. Signed-off-by: David Hildenbrand --- Documentation/mm/transhuge.rst | 8 ++ include/linux/mm_types.h | 49 ++++++++++ include/linux/page-flags.h | 4 + include/linux/rmap.h | 165 +++++++++++++++++++++++++++++++++ kernel/fork.c | 36 +++++++ mm/Kconfig | 4 + mm/internal.h | 5 + mm/page_alloc.c | 10 ++ 8 files changed, 281 insertions(+) diff --git a/Documentation/mm/transhuge.rst b/Documentation/mm/transhuge.rst index a2cd8800d5279..baa17d718a762 100644 --- a/Documentation/mm/transhuge.rst +++ b/Documentation/mm/transhuge.rst @@ -120,11 +120,19 @@ pages: and also increment/decrement folio->_nr_pages_mapped by ENTIRELY_MAPPED when _entire_mapcount goes from -1 to 0 or 0 to -1. =20 + We also maintain the two slots for tracking MM owners (MM ID and + corresponding mapcount), and the current status ("maybe mapped shared"= vs. + "mapped exclusively"). + - map/unmap of individual pages with PTE entry increment/decrement page->_mapcount, increment/decrement folio->_large_mapcount and also increment/decrement folio->_nr_pages_mapped when page->_mapcount goes from -1 to 0 or 0 to -1 as this counts the number of pages mapped by P= TE. =20 + We also maintain the two slots for tracking MM owners (MM ID and + corresponding mapcount), and the current status ("maybe mapped shared"= vs. + "mapped exclusively"). + split_huge_page internally has to distribute the refcounts in the head page to the tail pages before clearing all PG_head/tail bits from the page structures. It can be done easily for refcounts taken by page table diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index c83dd2f1ee25e..2d657ac8e9b0c 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -292,6 +292,44 @@ typedef struct { #define NR_PAGES_IN_LARGE_FOLIO #endif =20 +/* + * On 32bit, we can cut the required metadata in half, because: + * (a) PID_MAX_LIMIT implicitly limits the number of MMs we could ever hav= e, + * so we can limit MM IDs to 15 bit (32767). + * (b) We don't expect folios where even a single complete PTE mapping by + * one MM would exceed 15 bits (order-15). + */ +#ifdef CONFIG_64BIT +typedef int mm_id_mapcount_t; +#define MM_ID_MAPCOUNT_MAX INT_MAX +typedef unsigned int mm_id_t; +#else /* !CONFIG_64BIT */ +typedef short mm_id_mapcount_t; +#define MM_ID_MAPCOUNT_MAX SHRT_MAX +typedef unsigned short mm_id_t; +#endif /* CONFIG_64BIT */ + +/* We implicitly use the dummy ID for init-mm etc. where we never rmap pag= es. */ +#define MM_ID_DUMMY 0 +#define MM_ID_MIN (MM_ID_DUMMY + 1) + +/* + * We leave the highest bit of each MM id unused, so we can store a flag + * in the highest bit of each folio->_mm_id[]. + */ +#define MM_ID_BITS ((sizeof(mm_id_t) * BITS_PER_BYTE) - 1) +#define MM_ID_MASK ((1U << MM_ID_BITS) - 1) +#define MM_ID_MAX MM_ID_MASK + +/* + * In order to use bit_spin_lock(), which requires an unsigned long, we + * operate on folio->_mm_ids when working on flags. + */ +#define FOLIO_MM_IDS_LOCK_BITNUM MM_ID_BITS +#define FOLIO_MM_IDS_LOCK_BIT BIT(FOLIO_MM_IDS_LOCK_BITNUM) +#define FOLIO_MM_IDS_SHARED_BITNUM (2 * MM_ID_BITS + 1) +#define FOLIO_MM_IDS_SHARED_BIT BIT(FOLIO_MM_IDS_SHARED_BITNUM) + /** * struct folio - Represents a contiguous set of bytes. * @flags: Identical to the page flags. @@ -318,6 +356,9 @@ typedef struct { * @_nr_pages_mapped: Do not use outside of rmap and debug code. * @_pincount: Do not use directly, call folio_maybe_dma_pinned(). * @_nr_pages: Do not use directly, call folio_nr_pages(). + * @_mm_id: Do not use outside of rmap code. + * @_mm_ids: Do not use outside of rmap code. + * @_mm_id_mapcount: Do not use outside of rmap code. * @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h. * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h. * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgr= oup.h. @@ -390,6 +431,11 @@ struct folio { atomic_t _entire_mapcount; atomic_t _pincount; #endif /* CONFIG_64BIT */ + mm_id_mapcount_t _mm_id_mapcount[2]; + union { + mm_id_t _mm_id[2]; + unsigned long _mm_ids; + }; }; unsigned long _usable_1[4]; }; @@ -1111,6 +1157,9 @@ struct mm_struct { #endif } lru_gen; #endif /* CONFIG_LRU_GEN_WALKS_MMU */ +#ifdef CONFIG_MM_ID + mm_id_t mm_id; +#endif /* CONFIG_MM_ID */ } __randomize_layout; =20 /* diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 30fe3eb62b90c..01716710066df 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -1222,6 +1222,10 @@ static inline int folio_has_private(const struct fol= io *folio) return !!(folio->flags & PAGE_FLAGS_PRIVATE); } =20 +static inline bool folio_test_large_maybe_mapped_shared(const struct folio= *folio) +{ + return test_bit(FOLIO_MM_IDS_SHARED_BITNUM, &folio->_mm_ids); +} #undef PF_ANY #undef PF_HEAD #undef PF_NO_TAIL diff --git a/include/linux/rmap.h b/include/linux/rmap.h index d1e888cc97a58..c131b0efff0fa 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -13,6 +13,7 @@ #include #include #include +#include =20 /* * The anon_vma heads a list of private "related" vmas, to scan if @@ -173,6 +174,169 @@ static inline void anon_vma_merge(struct vm_area_stru= ct *vma, =20 struct anon_vma *folio_get_anon_vma(const struct folio *folio); =20 +#ifdef CONFIG_MM_ID +static __always_inline void folio_lock_large_mapcount(struct folio *folio) +{ + bit_spin_lock(FOLIO_MM_IDS_LOCK_BITNUM, &folio->_mm_ids); +} + +static __always_inline void folio_unlock_large_mapcount(struct folio *foli= o) +{ + __bit_spin_unlock(FOLIO_MM_IDS_LOCK_BITNUM, &folio->_mm_ids); +} + +static inline unsigned int folio_mm_id(const struct folio *folio, int idx) +{ + VM_WARN_ON_ONCE(idx !=3D 0 && idx !=3D 1); + return folio->_mm_id[idx] & MM_ID_MASK; +} + +static inline void folio_set_mm_id(struct folio *folio, int idx, mm_id_t i= d) +{ + VM_WARN_ON_ONCE(idx !=3D 0 && idx !=3D 1); + folio->_mm_id[idx] &=3D ~MM_ID_MASK; + folio->_mm_id[idx] |=3D id; +} + +static inline void __folio_large_mapcount_sanity_checks(const struct folio= *folio, + int diff, mm_id_t mm_id) +{ + VM_WARN_ON_ONCE(!folio_test_large(folio) || folio_test_hugetlb(folio)); + VM_WARN_ON_ONCE(diff <=3D 0); + VM_WARN_ON_ONCE(mm_id < MM_ID_MIN || mm_id > MM_ID_MAX); + + /* + * Make sure we can detect at least one complete PTE mapping of the + * folio in a single MM as "exclusively mapped". This is primarily + * a check on 32bit, where we currently reduce the size of the per-MM + * mapcount to a short. + */ + VM_WARN_ON_ONCE(diff > folio_large_nr_pages(folio)); + VM_WARN_ON_ONCE(folio_large_nr_pages(folio) - 1 > MM_ID_MAPCOUNT_MAX); + + VM_WARN_ON_ONCE(folio_mm_id(folio, 0) =3D=3D MM_ID_DUMMY && + folio->_mm_id_mapcount[0] !=3D -1); + VM_WARN_ON_ONCE(folio_mm_id(folio, 0) !=3D MM_ID_DUMMY && + folio->_mm_id_mapcount[0] < 0); + VM_WARN_ON_ONCE(folio_mm_id(folio, 1) =3D=3D MM_ID_DUMMY && + folio->_mm_id_mapcount[1] !=3D -1); + VM_WARN_ON_ONCE(folio_mm_id(folio, 1) !=3D MM_ID_DUMMY && + folio->_mm_id_mapcount[1] < 0); + VM_WARN_ON_ONCE(!folio_mapped(folio) && + folio_test_large_maybe_mapped_shared(folio)); +} + +static __always_inline void folio_set_large_mapcount(struct folio *folio, + int mapcount, struct vm_area_struct *vma) +{ + __folio_large_mapcount_sanity_checks(folio, mapcount, vma->vm_mm->mm_id); + + VM_WARN_ON_ONCE(folio_mm_id(folio, 0) !=3D MM_ID_DUMMY); + VM_WARN_ON_ONCE(folio_mm_id(folio, 1) !=3D MM_ID_DUMMY); + + /* Note: mapcounts start at -1. */ + atomic_set(&folio->_large_mapcount, mapcount - 1); + folio->_mm_id_mapcount[0] =3D mapcount - 1; + folio_set_mm_id(folio, 0, vma->vm_mm->mm_id); +} + +static __always_inline void folio_add_large_mapcount(struct folio *folio, + int diff, struct vm_area_struct *vma) +{ + const mm_id_t mm_id =3D vma->vm_mm->mm_id; + int new_mapcount_val; + + folio_lock_large_mapcount(folio); + __folio_large_mapcount_sanity_checks(folio, diff, mm_id); + + new_mapcount_val =3D atomic_read(&folio->_large_mapcount) + diff; + atomic_set(&folio->_large_mapcount, new_mapcount_val); + + /* + * If a folio is mapped more than once into an MM on 32bit, we + * can in theory overflow the per-MM mapcount (although only for + * fairly large folios), turning it negative. In that case, just + * free up the slot and mark the folio "mapped shared", otherwise + * we might be in trouble when unmapping pages later. + */ + if (folio_mm_id(folio, 0) =3D=3D mm_id) { + folio->_mm_id_mapcount[0] +=3D diff; + if (!IS_ENABLED(CONFIG_64BIT) && unlikely(folio->_mm_id_mapcount[0] < 0)= ) { + folio->_mm_id_mapcount[0] =3D -1; + folio_set_mm_id(folio, 0, MM_ID_DUMMY); + folio->_mm_ids |=3D FOLIO_MM_IDS_SHARED_BIT; + } + } else if (folio_mm_id(folio, 1) =3D=3D mm_id) { + folio->_mm_id_mapcount[1] +=3D diff; + if (!IS_ENABLED(CONFIG_64BIT) && unlikely(folio->_mm_id_mapcount[1] < 0)= ) { + folio->_mm_id_mapcount[1] =3D -1; + folio_set_mm_id(folio, 1, MM_ID_DUMMY); + folio->_mm_ids |=3D FOLIO_MM_IDS_SHARED_BIT; + } + } else if (folio_mm_id(folio, 0) =3D=3D MM_ID_DUMMY) { + folio_set_mm_id(folio, 0, mm_id); + folio->_mm_id_mapcount[0] =3D diff - 1; + /* We might have other mappings already. */ + if (new_mapcount_val !=3D diff - 1) + folio->_mm_ids |=3D FOLIO_MM_IDS_SHARED_BIT; + } else if (folio_mm_id(folio, 1) =3D=3D MM_ID_DUMMY) { + folio_set_mm_id(folio, 1, mm_id); + folio->_mm_id_mapcount[1] =3D diff - 1; + /* Slot 0 certainly has mappings as well. */ + folio->_mm_ids |=3D FOLIO_MM_IDS_SHARED_BIT; + } + folio_unlock_large_mapcount(folio); +} + +static __always_inline void folio_sub_large_mapcount(struct folio *folio, + int diff, struct vm_area_struct *vma) +{ + const mm_id_t mm_id =3D vma->vm_mm->mm_id; + int new_mapcount_val; + + folio_lock_large_mapcount(folio); + __folio_large_mapcount_sanity_checks(folio, diff, mm_id); + + new_mapcount_val =3D atomic_read(&folio->_large_mapcount) - diff; + atomic_set(&folio->_large_mapcount, new_mapcount_val); + + /* + * There are valid corner cases where we might underflow a per-MM + * mapcount (some mappings added when no slot was free, some mappings + * added once a slot was free), so we always set it to -1 once we go + * negative. + */ + if (folio_mm_id(folio, 0) =3D=3D mm_id) { + folio->_mm_id_mapcount[0] -=3D diff; + if (folio->_mm_id_mapcount[0] >=3D 0) + goto out; + folio->_mm_id_mapcount[0] =3D -1; + folio_set_mm_id(folio, 0, MM_ID_DUMMY); + } else if (folio_mm_id(folio, 1) =3D=3D mm_id) { + folio->_mm_id_mapcount[1] -=3D diff; + if (folio->_mm_id_mapcount[1] >=3D 0) + goto out; + folio->_mm_id_mapcount[1] =3D -1; + folio_set_mm_id(folio, 1, MM_ID_DUMMY); + } + + /* + * If one MM slot owns all mappings, the folio is mapped exclusively. + * Note that if the folio is now unmapped (new_mapcount_val =3D=3D -1), b= oth + * slots must be free (mapcount =3D=3D -1), and we'll also mark it as + * exclusive. + */ + if (folio->_mm_id_mapcount[0] =3D=3D new_mapcount_val || + folio->_mm_id_mapcount[1] =3D=3D new_mapcount_val) + folio->_mm_ids &=3D ~FOLIO_MM_IDS_SHARED_BIT; +out: + folio_unlock_large_mapcount(folio); +} +#else /* !CONFIG_MM_ID */ +/* + * See __folio_rmap_sanity_checks(), we might map large folios even without + * CONFIG_TRANSPARENT_HUGEPAGE. We'll keep that working for now. + */ static inline void folio_set_large_mapcount(struct folio *folio, int mapco= unt, struct vm_area_struct *vma) { @@ -191,6 +355,7 @@ static inline void folio_sub_large_mapcount(struct foli= o *folio, { atomic_sub(diff, &folio->_large_mapcount); } +#endif /* CONFIG_MM_ID */ =20 #define folio_inc_large_mapcount(folio, vma) \ folio_add_large_mapcount(folio, 1, vma) diff --git a/kernel/fork.c b/kernel/fork.c index 364b2d4fd3efa..f9cf0f056eb6f 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -802,6 +802,36 @@ static int dup_mmap(struct mm_struct *mm, struct mm_st= ruct *oldmm) #define mm_free_pgd(mm) #endif /* CONFIG_MMU */ =20 +#ifdef CONFIG_MM_ID +static DEFINE_IDA(mm_ida); + +static inline int mm_alloc_id(struct mm_struct *mm) +{ + int ret; + + ret =3D ida_alloc_range(&mm_ida, MM_ID_MIN, MM_ID_MAX, GFP_KERNEL); + if (ret < 0) + return ret; + mm->mm_id =3D ret; + return 0; +} + +static inline void mm_free_id(struct mm_struct *mm) +{ + const mm_id_t id =3D mm->mm_id; + + mm->mm_id =3D MM_ID_DUMMY; + if (id =3D=3D MM_ID_DUMMY) + return; + if (WARN_ON_ONCE(id < MM_ID_MIN || id > MM_ID_MAX)) + return; + ida_free(&mm_ida, id); +} +#else /* !CONFIG_MM_ID */ +static inline int mm_alloc_id(struct mm_struct *mm) { return 0; } +static inline void mm_free_id(struct mm_struct *mm) {} +#endif /* CONFIG_MM_ID */ + static void check_mm(struct mm_struct *mm) { int i; @@ -905,6 +935,7 @@ void __mmdrop(struct mm_struct *mm) =20 WARN_ON_ONCE(mm =3D=3D current->active_mm); mm_free_pgd(mm); + mm_free_id(mm); destroy_context(mm); mmu_notifier_subscriptions_destroy(mm); check_mm(mm); @@ -1289,6 +1320,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm= , struct task_struct *p, if (mm_alloc_pgd(mm)) goto fail_nopgd; =20 + if (mm_alloc_id(mm)) + goto fail_noid; + if (init_new_context(p, mm)) goto fail_nocontext; =20 @@ -1308,6 +1342,8 @@ static struct mm_struct *mm_init(struct mm_struct *mm= , struct task_struct *p, fail_cid: destroy_context(mm); fail_nocontext: + mm_free_id(mm); +fail_noid: mm_free_pgd(mm); fail_nopgd: free_mm(mm); diff --git a/mm/Kconfig b/mm/Kconfig index 2761098dbc1a2..af72fe92f96b0 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -817,11 +817,15 @@ config ARCH_WANT_GENERAL_HUGETLB config ARCH_WANTS_THP_SWAP def_bool n =20 +config MM_ID + def_bool n + menuconfig TRANSPARENT_HUGEPAGE bool "Transparent Hugepage Support" depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT select COMPACTION select XARRAY_MULTI + select MM_ID help Transparent Hugepages allows the kernel to use huge pages and huge tlb transparently to the applications whenever possible. diff --git a/mm/internal.h b/mm/internal.h index ffdc91b19322e..7303ddd9dac04 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -722,6 +722,11 @@ static inline void prep_compound_head(struct page *pag= e, unsigned int order) folio_set_order(folio, order); atomic_set(&folio->_large_mapcount, -1); atomic_set(&folio->_nr_pages_mapped, 0); + if (IS_ENABLED(CONFIG_MM_ID)) { + folio->_mm_ids =3D 0; + folio->_mm_id_mapcount[0] =3D -1; + folio->_mm_id_mapcount[1] =3D -1; + } if (IS_ENABLED(CONFIG_64BIT) || order > 1) { atomic_set(&folio->_pincount, 0); atomic_set(&folio->_entire_mapcount, -1); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 7036530bd1bca..08caa92553998 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -955,6 +955,16 @@ static int free_tail_page_prepare(struct page *head_pa= ge, struct page *page) bad_page(page, "nonzero nr_pages_mapped"); goto out; } + if (IS_ENABLED(CONFIG_MM_ID)) { + if (unlikely(folio->_mm_id_mapcount[0] !=3D -1)) { + bad_page(page, "nonzero mm mapcount 0"); + goto out; + } + if (unlikely(folio->_mm_id_mapcount[1] !=3D -1)) { + bad_page(page, "nonzero mm mapcount 1"); + goto out; + } + } if (IS_ENABLED(CONFIG_64BIT)) { if (unlikely(atomic_read(&folio->_entire_mapcount) + 1)) { bad_page(page, "nonzero entire_mapcount"); --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAF22266599 for ; Mon, 24 Feb 2025 16:56:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416198; cv=none; b=QDiiCuel8avbfzvTD7PHwu5ga+AMMldhz0UGUXXjP43vxFQlaBHh65Io1OqaC/OSj3IudstycBnWxtgBr/DQeOmFNk+R95KGEhr4R9grgIJHfrq9037J78nh4/qoodgyDayVE/w72eTD7DPyhBIBqBxraeOfhbUStHwG5n+s3fc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416198; c=relaxed/simple; bh=+oO0YOFo2loLfbEqLOJlR2w0XdWVO+uOgMtYkv7+0wA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vFfBJgF2oGqeo3u/2TLfnDQcjacLJqisHO+2TaE+oLs22ai2bC/IjQCfzhtj7GVEugtY6dBhRmKsaqJE3KDSKUElLInxcBnqoY/kU6M+T4E8Sl12kv3B4KjNuBH/XVLs/RezpMoGWX1HfDkhStG+nZOk5BK8emT5AGhwgnuBv9A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Bxb+WrKb; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Bxb+WrKb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416195; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pg2vY9vqWz7IHDYregwabj5LRZT1HxPxFKsGucVLMU4=; b=Bxb+WrKbyvXSXgGJjpMwICQUQNQhB00d5hchNzMIJ5WfPw0G3AB6DL/KUXwQ47VlE6GLFN n+O2U97CeIkDmFm0ZjRFSFnsrnOWjO2FgIaKIuGfM09QchtwoXnsEgLlXjGEpbXhowPNoS yqWe9AdTdOf00hsexXRAqA/koEejg38= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-686-D2C0LbqBMgmet-OFDEqZCg-1; Mon, 24 Feb 2025 11:56:34 -0500 X-MC-Unique: D2C0LbqBMgmet-OFDEqZCg-1 X-Mimecast-MFC-AGG-ID: D2C0LbqBMgmet-OFDEqZCg_1740416193 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-38f4cce15c8so2207832f8f.2 for ; Mon, 24 Feb 2025 08:56:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416193; x=1741020993; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pg2vY9vqWz7IHDYregwabj5LRZT1HxPxFKsGucVLMU4=; b=Ap5y6v7CxSRqG0ZTvlPMkZITXJucgaqJhjI9vZZHIalocjLnBg01LOnaBBSF3LMzgK gdZpboDgoEFsWXhDdGtKdlPOxplvXuaVFBvx1UQ4TnrIEHhZdK7svDORTueaI3bUr/PX t0ncZ20SDPJEwUuKp5Sz/rP+wxkbhJNhJCy/5Zj5zBr/cKzyiHSzVfweKwmEvhH0ht4e zdPDnhUrzxc4f8a8GtBRMATxM9buSMI3z1l0xhOywRd9+WmCbsXF/b6ZgUkybNKLwZQs 20huFTulqGz/x3Y3WgQqNA0ZUBLX/F2psrYy1Q0sxhXAHwl3rpEUkEQd3S3q39wT8aOd A4uw== X-Gm-Message-State: AOJu0YxEtc50LAIRZW8pcOWRT67OGaDSrjZBmTPOqr6wdESAafNSflKO yn34nf/R1X3a+p1DmlBYMDrti6ANB+q8AHSeukAtZuFMK2DlJ2tGhDtCc4whUcQ/yMx44TBp7Kj bqAjGl4JNX4cQbgkfbUb7+LhLoHxt5enIqTrJYZOAt76u15ROuorng5qqvQreJcoCszeLYMxcZ/ KfGzPfY4X+lXJWjnK1zv7ptJgFQY4IpoXBvcvolFhlKs/l X-Gm-Gg: ASbGncsrpKpx7tPSFpoA7Z5C0Pb81Oxo5jSTyNM9kN3C0F54fFG3Cakjcpd/YcnK0iK Tp3RQc/dIrAdZwiUqJ6IB69Mwq16n+aNX/g3gjuWo3UaIvaoQgnUCuv+DSLnR0GJvt7+uxCS/5A 5J/M6vH9gk3TF0Txb4XJIa/fGnJuQeM+e7h+V2wV9RBJ1CQmPcVYObjK6auiyvnBVM9sux5FeMM g1hGEmhXxdits6f8gogCay2z8u7HFCU8pKZXgJzaHJ2k6RbFdEDE5qCVMDBRdqzx6XJkq5AILC7 XrPPu5vN1RRtMWsJnLiZjUGTHZLu6Eq6ArHVXs0Sgw== X-Received: by 2002:a5d:648a:0:b0:38d:db7b:5d90 with SMTP id ffacd0b85a97d-38f6f0951bbmr11757109f8f.41.1740416192565; Mon, 24 Feb 2025 08:56:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IHaIBsZiKHsVYYcLtCIPoeZB3FHIPU3YJhCLPdlvukEQ+/K5hEIgCDRy8AjGPWVYvGgArScYA== X-Received: by 2002:a5d:648a:0:b0:38d:db7b:5d90 with SMTP id ffacd0b85a97d-38f6f0951bbmr11757061f8f.41.1740416192060; Mon, 24 Feb 2025 08:56:32 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f258b4163sm32531566f8f.19.2025.02.24.08.56.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:31 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 13/20] mm: Copy-on-Write (COW) reuse support for PTE-mapped THP Date: Mon, 24 Feb 2025 17:55:55 +0100 Message-ID: <20250224165603.1434404-14-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, we never end up reusing PTE-mapped THPs after fork. This wasn't really a problem with PMD-sized THPs, because they would have to be PTE-mapped first, but it's getting a problem with smaller THP sizes that are effectively always PTE-mapped. With our new "mapped exclusively" vs "maybe mapped shared" logic for large folios, implementing CoW reuse for PTE-mapped THPs is straight forward: if exclusively mapped, make sure that all references are from these (our) mappings. Add some helpful comments to explain the details. CONFIG_TRANSPARENT_HUGEPAGE selects CONFIG_MM_ID. If we spot an anon large folio without CONFIG_TRANSPARENT_HUGEPAGE in that code, something is seriously messed up. There are plenty of things we can optimize in the future: For example, we could remember that the folio is fully exclusive so we could speedup the next fault further. Also, we could try "faulting around", turning surrounding PTEs that map the same folio writable. But especially the latter might increase COW latency, so it would need further investigation. Signed-off-by: David Hildenbrand --- mm/memory.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 8 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index ef74c4f009f84..8dc241961b684 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3706,19 +3706,87 @@ static vm_fault_t wp_page_shared(struct vm_fault *v= mf, struct folio *folio) return ret; } =20 -static bool wp_can_reuse_anon_folio(struct folio *folio, - struct vm_area_struct *vma) +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static bool __wp_can_reuse_large_anon_folio(struct folio *folio, + struct vm_area_struct *vma) { + bool exclusive =3D false; + + /* Let's just free up a large folio if only a single page is mapped. */ + if (folio_large_mapcount(folio) <=3D 1) + return false; + /* - * We could currently only reuse a subpage of a large folio if no - * other subpages of the large folios are still mapped. However, - * let's just consistently not reuse subpages even if we could - * reuse in that scenario, and give back a large folio a bit - * sooner. + * The assumption for anonymous folios is that each page can only get + * mapped once into each MM. The only exception are KSM folios, which + * are always small. + * + * Each taken mapcount must be paired with exactly one taken reference, + * whereby the refcount must be incremented before the mapcount when + * mapping a page, and the refcount must be decremented after the + * mapcount when unmapping a page. + * + * If all folio references are from mappings, and all mappings are in + * the page tables of this MM, then this folio is exclusive to this MM. */ - if (folio_test_large(folio)) + if (folio_test_large_maybe_mapped_shared(folio)) + return false; + + VM_WARN_ON_ONCE(folio_test_ksm(folio)); + VM_WARN_ON_ONCE(folio_mapcount(folio) > folio_nr_pages(folio)); + VM_WARN_ON_ONCE(folio_entire_mapcount(folio)); + + if (unlikely(folio_test_swapcache(folio))) { + /* + * Note: freeing up the swapcache will fail if some PTEs are + * still swap entries. + */ + if (!folio_trylock(folio)) + return false; + folio_free_swap(folio); + folio_unlock(folio); + } + + if (folio_large_mapcount(folio) !=3D folio_ref_count(folio)) return false; =20 + /* Stabilize the mapcount vs. refcount and recheck. */ + folio_lock_large_mapcount(folio); + VM_WARN_ON_ONCE(folio_large_mapcount(folio) < folio_ref_count(folio)); + + if (folio_test_large_maybe_mapped_shared(folio)) + goto unlock; + if (folio_large_mapcount(folio) !=3D folio_ref_count(folio)) + goto unlock; + + VM_WARN_ON_ONCE(folio_mm_id(folio, 0) !=3D vma->vm_mm->mm_id && + folio_mm_id(folio, 1) !=3D vma->vm_mm->mm_id); + + /* + * Do we need the folio lock? Likely not. If there would have been + * references from page migration/swapout, we would have detected + * an additional folio reference and never ended up here. + */ + exclusive =3D true; +unlock: + folio_unlock_large_mapcount(folio); + return exclusive; +} +#else /* !CONFIG_TRANSPARENT_HUGEPAGE */ +static bool __wp_can_reuse_large_anon_folio(struct folio *folio, + struct vm_area_struct *vma) +{ + BUILD_BUG(); +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +static bool wp_can_reuse_anon_folio(struct folio *folio, + struct vm_area_struct *vma) +{ + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && folio_test_large(folio)) + return __wp_can_reuse_large_anon_folio(folio, vma); + VM_WARN_ON_ONCE(folio_test_large(folio)); + /* * We have to verify under folio lock: these early checks are * just an optimization to avoid locking the folio and freeing --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3AB7326463B for ; Mon, 24 Feb 2025 16:56:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416200; cv=none; b=QXmC9kXg3/LCJfwrm1BxloR44XgFRkLkDtzqcZKjzp2iqIMgjff3PDfDmb2lS4SEl1f0SAnVk+B7U/n03BpgS+kHRMM2fXQ6dKsdBWuRMYOomMOnJHAseDKUjnWlEck8GxHqQYAnaMp7hW9vaHMzn9OLu4CRiCoKK4ultTO1AGI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416200; c=relaxed/simple; bh=wUIitlkO9QXoubU6qeJRXGrVXqKQzYY862luOXGYRCk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aKiO8yCjYIEjofwNys41E1eZHZCEkJryXlwwEGOc5/KudRJgLb0g8/0YpWI896XgiHfctRFuPA7sOPEnjcEdgY/WOou95YhOs7e991xJaPGDywQmC1yBbekUbWDpwdK+LNiYZ72aZY+s0oady3S8b0NI8NrHZWa8qgXYSxDyXXs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Jt3hT+yG; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Jt3hT+yG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416197; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SzcTE+N2KyOzlARv+w+SRDPyrebeHo8NXdkT+2IHowQ=; b=Jt3hT+yGBebpsRGboC+YIRf16cUctWcj262KVl2P8vXKdp8RNemsUvRdDi0tfzEWvQz22n 5iSOeUgaokS2LKRRLaBvSsvozLwyFM8YF9t9AV+JTgzV0oOD6zMrUvxatm/L3GrANo2YPB PMX62xV1M0ChQHQB55KC9JXCAGGYWnw= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-295-y_kRha33O060YUTHm2qulQ-1; Mon, 24 Feb 2025 11:56:36 -0500 X-MC-Unique: y_kRha33O060YUTHm2qulQ-1 X-Mimecast-MFC-AGG-ID: y_kRha33O060YUTHm2qulQ_1740416195 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38f36fcf4b3so2856480f8f.1 for ; Mon, 24 Feb 2025 08:56:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416195; x=1741020995; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SzcTE+N2KyOzlARv+w+SRDPyrebeHo8NXdkT+2IHowQ=; b=tm1mV5Zhd5yXhL544ZXkRsIcNHWufgJLA4PAdaXjfh8p4DwPbnQk2o6x7DuubiidXo q/h5zAtVveFImofF2ECCrosNezDoCZGFQYG02rnA6mnTd2Yie5GfqbZQlcdePd+WXVPp i+Cgxkdjh8DZGju21qAr1J0h8ScjlRs8MWlY+8Zn7DovS4SBIKo2WOV7zyzm+behF7hY dUSYNydUv8AXgeummPGdmnGb9/j8XkkiYcvFfVHGF+Xd8KkDhiSS/UGrgGyzprBzy9iN epOtY5/85e0FjqQwKFfNxTG+tTumafEflw524BzSEFXen9xXaQxj4vQNQ+vmCkIRpxld 9zWA== X-Gm-Message-State: AOJu0YzHZmjPivR8I115iFsYNBQOYSt7R/mbrmbk+jwEPfbFQCkd7SRL eE0KAtbencOLMhdbJiAwncJV2l4MQ69f5FzADpPVVOK9tY5tjVh9QFzFo7MYINj/0PfP8FKprul jk+AiDLJOfzc8o+vt92aLzpVFuRySi6dJSxGlB/FOcprwFk+nth/QjbLXGUHecqtJHPfUoWzm/D Ew30cTHb9IU0rcza3pmi9dY8ISDxPrUmObj1Ugn/PG/zc6 X-Gm-Gg: ASbGncuw6VMGFNi4VaI9virmUbryqTrdP3MJLmj6m2xBrvIWhdSk2xlytwyMg55JvNw gROauYmZcdnWeNSWnX6ZANmwSFd6VxwCdAxQzjocFkfJYUKYI22RMEAlB2/fQKT3WKIA05MeYba yfKzR+Qa8wdehPWhA76j1vwTm+q5bAy+emWQMC3l4xx5/lBQTX4xr5ntdHyx4+dOzeN4QwtPszH hakxXKgeY5q7nE4kAtJfomNTudDTtCESczRERmcwtRSyxsEsSnWO9FHW9xXWuv6Py175GrTmIyS bfdqHBtr8vCMTN37dPt+scRS/5IU5VGIJQEAx4LZ8A== X-Received: by 2002:adf:f20f:0:b0:38f:2b34:5004 with SMTP id ffacd0b85a97d-38f6f0863c9mr12658040f8f.38.1740416194744; Mon, 24 Feb 2025 08:56:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IEXaXs4/LysHX+bj1gIx5L2JWtgUcH0vURwv8svqv3a7UoxYuTerlEfxa7PfRrFkuwf5fEjKw== X-Received: by 2002:adf:f20f:0:b0:38f:2b34:5004 with SMTP id ffacd0b85a97d-38f6f0863c9mr12657972f8f.38.1740416194154; Mon, 24 Feb 2025 08:56:34 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b0372124sm110600815e9.39.2025.02.24.08.56.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:33 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 14/20] mm: convert folio_likely_mapped_shared() to folio_maybe_mapped_shared() Date: Mon, 24 Feb 2025 17:55:56 +0100 Message-ID: <20250224165603.1434404-15-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's reuse our new MM ownership tracking infrastructure for large folios to make folio_likely_mapped_shared() never return false negatives -- never indicating "not mapped shared" although the folio *is* mapped shared. With that, we can rename it to folio_maybe_mapped_shared() and get rid of the dependency on the mapcount of the first folio page. The semantics are now arguably clearer: no mixture of "false negatives" and "false positives", only the remaining possibility for "false positives". Thoroughly document the new semantics. We might now detect that a large folio is "maybe mapped shared" although it *no longer* is -- but once was. Now, if more than two MMs mapped a folio at the same time, and the MM mapping the folio exclusively at the end is not one tracked in the two folio MM slots, we will detect the folio as "maybe mapped shared". For anonymous folios, usually (except weird corner cases) all PTEs that target a "maybe mapped shared" folio are R/O. As soon as a child process would write to them (iow, actively use them), we would CoW and effectively replace these PTEs. Most cases (below) are not expected to really matter with large anonymous folios for this reason. Most importantly, there will be no change at all for: * small folios * hugetlb folios * PMD-mapped PMD-sized THPs (single mapping) This change has the potential to affect existing callers of folio_likely_mapped_shared() -> folio_maybe_mapped_shared(): (1) fs/proc/task_mmu.c: no change (hugetlb) (2) khugepaged counts PTEs that target shared folios towards max_ptes_shared (default: HPAGE_PMD_NR / 2), meaning we could skip a collapse where we would have previously collapsed. This only applies to anonymous folios and is not expected to matter in practice. Worth noting that this change sorts out case (A) documented in commit 1bafe96e89f0 ("mm/khugepaged: replace page_mapcount() check by folio_likely_mapped_shared()") by removing the possibility for "false negatives". (3) MADV_COLD / MADV_PAGEOUT / MADV_FREE will not try splitting PTE-mapped THPs that are considered shared but not fully covered by the requested range, consequently not processing them. PMD-mapped PMD-sized THP are not affected, or when all PTEs are covered. These functions are usually only called on anon/file folios that are exclusively mapped most of the time (no other file mappings or no fork()), so the "false negatives" are not expected to matter in practice. (4) mbind() / migrate_pages() / move_pages() will refuse to migrate shared folios unless MPOL_MF_MOVE_ALL is effective (requires CAP_SYS_NICE). We will now reject some folios that could be migrated. Similar to (3), especially with MPOL_MF_MOVE_ALL, so this is not expected to matter in practice. Note that cpuset_migrate_mm_workfn() calls do_migrate_pages() with MPOL_MF_MOVE_ALL. (5) NUMA hinting mm/migrate.c:migrate_misplaced_folio_prepare() will skip file folios that are probably shared libraries (-> "mapped shared" and executable). This check would have detected it as a shared library at some point (at least 3 MMs mapping it), so detecting it afterwards does not sound wrong (still a shared library). Not expected to matter. mm/memory.c:numa_migrate_check() will indicate TNF_SHARED in MAP_SHARED file mappings when encountering a shared folio. Similar reasoning, not expected to matter. mm/mprotect.c:change_pte_range() will skip folios detected as shared in CoW mappings. Similarly, this is not expected to matter in practice, but if it would ever be a problem we could relax that check a bit (e.g., basing it on the average page-mapcount in a folio), because it was only an optimization when many (e.g., 288) processes were mapping the same folios -- see commit 859d4adc3415 ("mm: numa: do not trap faults on shared data section pages.") (6) mm/rmap.c:folio_referenced_one() will skip exclusive swapbacked folios in dying processes. Applies to anonymous folios only. Without "false negatives", we'll now skip all actually shared ones. Skipping ones that are actually exclusive won't really matter, it's a pure optimization, and is not expected to matter in practice. In theory, one can detect the problematic scenario: folio_mapcount() > 0 and no folio MM slot is occupied ("state unknown"). One could reset the MM slots while doing an rmap walk, which migration / folio split already do when setting everything up. Further, when batching PTEs we might naturally learn about a owner (e.g., folio_mapcount() =3D=3D nr_ptes) and could update the owner. However, we'll defer that until the scenarios where it would really matter are clear. Signed-off-by: David Hildenbrand --- fs/proc/task_mmu.c | 4 ++-- include/linux/mm.h | 43 ++++++++++++++++++++++--------------------- mm/huge_memory.c | 2 +- mm/khugepaged.c | 8 +++----- mm/madvise.c | 6 +++--- mm/memory.c | 2 +- mm/mempolicy.c | 8 ++++---- mm/migrate.c | 7 +++---- mm/mprotect.c | 2 +- mm/rmap.c | 2 +- 10 files changed, 41 insertions(+), 43 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index f02cd362309a0..2bddcea65cbf1 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1023,7 +1023,7 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned l= ong hmask, =20 if (folio) { /* We treat non-present entries as "maybe shared". */ - if (!present || folio_likely_mapped_shared(folio) || + if (!present || folio_maybe_mapped_shared(folio) || hugetlb_pmd_shared(pte)) mss->shared_hugetlb +=3D huge_page_size(hstate_vma(vma)); else @@ -1879,7 +1879,7 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigne= d long hmask, if (!folio_test_anon(folio)) flags |=3D PM_FILE; =20 - if (!folio_likely_mapped_shared(folio) && + if (!folio_maybe_mapped_shared(folio) && !hugetlb_pmd_shared(ptep)) flags |=3D PM_MMAP_EXCLUSIVE; =20 diff --git a/include/linux/mm.h b/include/linux/mm.h index 9c1290588a11e..98a67488b5fef 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2245,23 +2245,18 @@ static inline size_t folio_size(const struct folio = *folio) } =20 /** - * folio_likely_mapped_shared - Estimate if the folio is mapped into the p= age - * tables of more than one MM + * folio_maybe_mapped_shared - Whether the folio is mapped into the page + * tables of more than one MM * @folio: The folio. * - * This function checks if the folio is currently mapped into more than one - * MM ("mapped shared"), or if the folio is only mapped into a single MM - * ("mapped exclusively"). + * This function checks if the folio maybe currently mapped into more than= one + * MM ("maybe mapped shared"), or if the folio is certainly mapped into a = single + * MM ("mapped exclusively"). * * For KSM folios, this function also returns "mapped shared" when a folio= is * mapped multiple times into the same MM, because the individual page map= pings * are independent. * - * As precise information is not easily available for all folios, this fun= ction - * estimates the number of MMs ("sharers") that are currently mapping a fo= lio - * using the number of times the first page of the folio is currently mapp= ed - * into page tables. - * * For small anonymous folios and anonymous hugetlb folios, the return * value will be exactly correct: non-KSM folios can only be mapped at mos= t once * into an MM, and they cannot be partially mapped. KSM folios are @@ -2269,8 +2264,8 @@ static inline size_t folio_size(const struct folio *f= olio) * * For other folios, the result can be fuzzy: * #. For partially-mappable large folios (THP), the return value can w= rongly - * indicate "mapped exclusively" (false negative) when the folio is - * only partially mapped into at least one MM. + * indicate "mapped shared" (false positive) if a folio was mapped by + * more than two MMs at one point in time. * #. For pagecache folios (including hugetlb), the return value can wr= ongly * indicate "mapped shared" (false positive) when two VMAs in the sa= me MM * cover the same file range. @@ -2287,7 +2282,7 @@ static inline size_t folio_size(const struct folio *f= olio) * * Return: Whether the folio is estimated to be mapped into more than one = MM. */ -static inline bool folio_likely_mapped_shared(struct folio *folio) +static inline bool folio_maybe_mapped_shared(struct folio *folio) { int mapcount =3D folio_mapcount(folio); =20 @@ -2295,16 +2290,22 @@ static inline bool folio_likely_mapped_shared(struc= t folio *folio) if (!folio_test_large(folio) || unlikely(folio_test_hugetlb(folio))) return mapcount > 1; =20 - /* A single mapping implies "mapped exclusively". */ - if (mapcount <=3D 1) - return false; - - /* If any page is mapped more than once we treat it "mapped shared". */ - if (folio_entire_mapcount(folio) || mapcount > folio_nr_pages(folio)) + /* + * vm_insert_page() without CONFIG_TRANSPARENT_HUGEPAGE ... + * simply assume "mapped shared", nobody should really care + * about this for arbitrary kernel allocations. + */ + if (!IS_ENABLED(CONFIG_MM_ID)) return true; =20 - /* Let's guess based on the first subpage. */ - return atomic_read(&folio->_mapcount) > 0; + /* + * A single mapping implies "mapped exclusively", even if the + * folio flag says something different: it's easier to handle this + * case here instead of on the RMAP hot path. + */ + if (mapcount <=3D 1) + return false; + return folio_test_large_maybe_mapped_shared(folio); } =20 #ifndef HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE diff --git a/mm/huge_memory.c b/mm/huge_memory.c index a3264d88d4b49..d9a7614fe739a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2155,7 +2155,7 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, st= ruct vm_area_struct *vma, * If other processes are mapping this folio, we couldn't discard * the folio unless they all do MADV_FREE so let's skip the folio. */ - if (folio_likely_mapped_shared(folio)) + if (folio_maybe_mapped_shared(folio)) goto out; =20 if (!folio_trylock(folio)) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 5f0be134141e8..cc945c6ab3bdb 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -607,7 +607,7 @@ static int __collapse_huge_page_isolate(struct vm_area_= struct *vma, VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio); =20 /* See hpage_collapse_scan_pmd(). */ - if (folio_likely_mapped_shared(folio)) { + if (folio_maybe_mapped_shared(folio)) { ++shared; if (cc->is_khugepaged && shared > khugepaged_max_ptes_shared) { @@ -1359,11 +1359,9 @@ static int hpage_collapse_scan_pmd(struct mm_struct = *mm, =20 /* * We treat a single page as shared if any part of the THP - * is shared. "False negatives" from - * folio_likely_mapped_shared() are not expected to matter - * much in practice. + * is shared. */ - if (folio_likely_mapped_shared(folio)) { + if (folio_maybe_mapped_shared(folio)) { ++shared; if (cc->is_khugepaged && shared > khugepaged_max_ptes_shared) { diff --git a/mm/madvise.c b/mm/madvise.c index e01e93e179a8a..388dc289b5d12 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -387,7 +387,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, folio =3D pmd_folio(orig_pmd); =20 /* Do not interfere with other mappings of this folio */ - if (folio_likely_mapped_shared(folio)) + if (folio_maybe_mapped_shared(folio)) goto huge_unlock; =20 if (pageout_anon_only_filter && !folio_test_anon(folio)) @@ -486,7 +486,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, if (nr < folio_nr_pages(folio)) { int err; =20 - if (folio_likely_mapped_shared(folio)) + if (folio_maybe_mapped_shared(folio)) continue; if (pageout_anon_only_filter && !folio_test_anon(folio)) continue; @@ -721,7 +721,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned = long addr, if (nr < folio_nr_pages(folio)) { int err; =20 - if (folio_likely_mapped_shared(folio)) + if (folio_maybe_mapped_shared(folio)) continue; if (!folio_trylock(folio)) continue; diff --git a/mm/memory.c b/mm/memory.c index 8dc241961b684..2a1e7d9722866 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5672,7 +5672,7 @@ int numa_migrate_check(struct folio *folio, struct vm= _fault *vmf, * Flag if the folio is shared between multiple address spaces. This * is later used when determining whether to group tasks together */ - if (folio_likely_mapped_shared(folio) && (vma->vm_flags & VM_SHARED)) + if (folio_maybe_mapped_shared(folio) && (vma->vm_flags & VM_SHARED)) *flags |=3D TNF_SHARED; /* * For memory tiering mode, cpupid of slow memory page is used diff --git a/mm/mempolicy.c b/mm/mempolicy.c index bbaadbeeb2919..530e71fe91476 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -642,11 +642,11 @@ static int queue_folios_hugetlb(pte_t *pte, unsigned = long hmask, * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. * Choosing not to migrate a shared folio is not counted as a failure. * - * See folio_likely_mapped_shared() on possible imprecision when we + * See folio_maybe_mapped_shared() on possible imprecision when we * cannot easily detect if a folio is shared. */ if ((flags & MPOL_MF_MOVE_ALL) || - (!folio_likely_mapped_shared(folio) && !hugetlb_pmd_shared(pte))) + (!folio_maybe_mapped_shared(folio) && !hugetlb_pmd_shared(pte))) if (!folio_isolate_hugetlb(folio, qp->pagelist)) qp->nr_failed++; unlock: @@ -1033,10 +1033,10 @@ static bool migrate_folio_add(struct folio *folio, = struct list_head *foliolist, * Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. * Choosing not to migrate a shared folio is not counted as a failure. * - * See folio_likely_mapped_shared() on possible imprecision when we + * See folio_maybe_mapped_shared() on possible imprecision when we * cannot easily detect if a folio is shared. */ - if ((flags & MPOL_MF_MOVE_ALL) || !folio_likely_mapped_shared(folio)) { + if ((flags & MPOL_MF_MOVE_ALL) || !folio_maybe_mapped_shared(folio)) { if (folio_isolate_lru(folio)) { list_add_tail(&folio->lru, foliolist); node_stat_mod_folio(folio, diff --git a/mm/migrate.c b/mm/migrate.c index 365c6daa8d1b1..fb4afd31baf0c 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2228,7 +2228,7 @@ static int __add_folio_for_migration(struct folio *fo= lio, int node, if (folio_nid(folio) =3D=3D node) return 0; =20 - if (folio_likely_mapped_shared(folio) && !migrate_all) + if (folio_maybe_mapped_shared(folio) && !migrate_all) return -EACCES; =20 if (folio_test_hugetlb(folio)) { @@ -2653,11 +2653,10 @@ int migrate_misplaced_folio_prepare(struct folio *f= olio, * processes with execute permissions as they are probably * shared libraries. * - * See folio_likely_mapped_shared() on possible imprecision + * See folio_maybe_mapped_shared() on possible imprecision * when we cannot easily detect if a folio is shared. */ - if ((vma->vm_flags & VM_EXEC) && - folio_likely_mapped_shared(folio)) + if ((vma->vm_flags & VM_EXEC) && folio_maybe_mapped_shared(folio)) return -EACCES; =20 /* diff --git a/mm/mprotect.c b/mm/mprotect.c index 1444878f7aeb2..62c1f79457412 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -133,7 +133,7 @@ static long change_pte_range(struct mmu_gather *tlb, /* Also skip shared copy-on-write pages */ if (is_cow_mapping(vma->vm_flags) && (folio_maybe_dma_pinned(folio) || - folio_likely_mapped_shared(folio))) + folio_maybe_mapped_shared(folio))) continue; =20 /* diff --git a/mm/rmap.c b/mm/rmap.c index c9922928616ee..8de415157bc8d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -889,7 +889,7 @@ static bool folio_referenced_one(struct folio *folio, if ((!atomic_read(&vma->vm_mm->mm_users) || check_stable_address_space(vma->vm_mm)) && folio_test_anon(folio) && folio_test_swapbacked(folio) && - !folio_likely_mapped_shared(folio)) { + !folio_maybe_mapped_shared(folio)) { pra->referenced =3D -1; page_vma_mapped_walk_done(&pvmw); return false; --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70EA92676FA for ; Mon, 24 Feb 2025 16:56:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416203; cv=none; b=uVE/oVzbDxkegPvGHS6hBWdnMwai1pWWtD85lfJA26rGHExILSHfBH8fIYk3UY2SbIDix3GNrQOwS7VptrjgL1h3SubZR+koVi25Y+vDdLtDEzl1/uWXeccIM7GYWAcTZtd+5CcLCjIXRE8meseUlWVMkPDnwGUEfFU5LhoOO1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416203; c=relaxed/simple; bh=5WNM/6QDOH2Ize7Cdhhh3450CUV7Ph/XvN+jjna0q/k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZOLXh4yQAlsS6nkSagCgPfOZ2mcIW1Pt0oGt01/ELcYKg6+Zvx+MX/izJslxt9f727DXd3mc/+M8g4gJuGezZX1U1S3RV4nZsYSuMbkT/nofkZV6fBBFmaAsyDy5jpRUcZXPsdhV6mOmmMlsG+/Lp9PlcneZ4g/5TrPu6LQnYgg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YIUuoDs2; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YIUuoDs2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416200; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hjQ9M+/7gFJUkrRnlhRJAIZPqjntNNCFGRlUUDQiAps=; b=YIUuoDs24/lXrOIOjzD5uGKOQiyKE8m2aidpEOweRsSGBd6cJVzWzjJc6cEGJn+5RcYlgo GQxJmcsjEq0pJLTm2ohOtewRPjnIivIolhWKqhBm2U9FIc4/YjANYcX0YhhBYvJv7fzC6z mj1FdhcCZbgn7BLzU2G958SJywR3rWw= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-392-iCR5BSF8O9e-4QMf3a_SaQ-1; Mon, 24 Feb 2025 11:56:38 -0500 X-MC-Unique: iCR5BSF8O9e-4QMf3a_SaQ-1 X-Mimecast-MFC-AGG-ID: iCR5BSF8O9e-4QMf3a_SaQ_1740416198 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-38f29b4d4adso1932593f8f.3 for ; Mon, 24 Feb 2025 08:56:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416197; x=1741020997; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hjQ9M+/7gFJUkrRnlhRJAIZPqjntNNCFGRlUUDQiAps=; b=OJA5vMGLNjQqPOJYSOeiNkE4fgMKyYVutGJnPe7xg3gvgQhCu1x5IWaA2wN9OhY7df eeHBE/BVIxaokWHYdfBxcXlY/7ZbNZpmRLEhzbjA1gwoorOmHaBeaiaRmINvi/XhHo+2 5/dCicI4+ijiJj49KodTzoPkb0mwstGI80nmgCzqZsRrNyqHbub5eL5cV6NM0fMCTu30 ink01NjtlEhW6S5WL1vfxS/hxMwrm63Sp8pNktbJn4UC45KGnFvE6daGPHxsUoh3km8u wDN8fMTPD2Ezd7sOrlAXemBDOWOJfw47RFFjGt/W5xhqH9s3yjDbPHbkA+mSvw1POtUA +p6Q== X-Gm-Message-State: AOJu0Yw9IUPLK+Y6ciymYpakO42fbBVsHS6sB3sk4BwqKchQ7mEyExpP BLW324RODDj8yrN3sJgjRyxy3Aj0MzUXvLW9ExVJ36u0h65cJQS4RWM7M+hrKDGsGWGH6ppZojZ uXTeSnnRBwSAJL6nIr674GQlmiOQdnuD9n6XCjqbdq0pDrJp3KjaropCm7TA8WFYmbL3gWs0j3e ywwKGC7qP7YXNMU3BjMEjnE9ONb13GOez/uwcFFioX+Dq7 X-Gm-Gg: ASbGncvim9LXvU+7B+rOP9gASTdPxlXPUygexRePLvdU0Cs8069n2pS90HznTPXSkWX kx0BcUF45MrLH++ffsI/whb6MIaaSgzzx7vYgMDDGnCGquOaoRIX7JW8y/Vap1g/tCLIaP7/egR KgxWTP+lMy2ob3ujLMTG1TdZU+ENHR7V6A0elK5H0L0aO/8rylw2wyRnpRfZ1KMRZTIWH8NClRa WcRfGFREjAs8mrtOHwU3jplETyN11oUsz2ONAjwCCPxGOqOyoEMN9LhlwsMpeX4+Ug8h1PSCimN TsIwZq6IKVIo4+QVeLsbj6CfyLAMe4h+XAjoZ8SyVw== X-Received: by 2002:a05:600c:154b:b0:439:9f19:72ab with SMTP id 5b1f17b1804b1-43aa6d14bf5mr34158825e9.23.1740416197490; Mon, 24 Feb 2025 08:56:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJ9sKTQ3EBTX4dQr6LdeGl2B92vuZUpZDHC83nbKU72kdYMMOPYVrf72peoEztlZbl1dnGRA== X-Received: by 2002:a05:600c:154b:b0:439:9f19:72ab with SMTP id 5b1f17b1804b1-43aa6d14bf5mr34158365e9.23.1740416197028; Mon, 24 Feb 2025 08:56:37 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b02d519dsm109604025e9.11.2025.02.24.08.56.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:35 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 15/20] mm: CONFIG_NO_PAGE_MAPCOUNT to prepare for not maintain per-page mapcounts in large folios Date: Mon, 24 Feb 2025 17:55:57 +0100 Message-ID: <20250224165603.1434404-16-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We're close to the finishing line: let's introduce a new CONFIG_NO_PAGE_MAPCOUNT config option where we will incrementally remove any dependencies on per-page mapcounts in large folios. Once that's done, we'll stop maintaining the per-page mapcounts with this config option enabled. CONFIG_NO_PAGE_MAPCOUNT will be EXPERIMENTAL for now, as we'll have to learn about some of the real world impact of some of the implications. As writing "!CONFIG_NO_PAGE_MAPCOUNT" is really nasty, let's introduce a helper config option "CONFIG_PAGE_MAPCOUNT" that expresses the negation. Signed-off-by: David Hildenbrand --- mm/Kconfig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/mm/Kconfig b/mm/Kconfig index af72fe92f96b0..64bb80a281dd9 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -889,8 +889,26 @@ config READ_ONLY_THP_FOR_FS support of file THPs will be developed in the next few release cycles. =20 +config NO_PAGE_MAPCOUNT + bool "No per-page mapcount (EXPERIMENTAL)" + help + Do not maintain per-page mapcounts for pages part of larger + allocations, such as transparent huge pages. + + When this config option is enabled, some interfaces that relied on + this information will rely on less-precise per-allocation information + instead: for example, using the average per-page mapcount in such + a large allocation instead of the per-page mapcount. + + EXPERIMENTAL because the severity of some of the implications first + have to be understood properly. + endif # TRANSPARENT_HUGEPAGE =20 +# simple helper to make the code a bit easier to read +config PAGE_MAPCOUNT + def_bool !NO_PAGE_MAPCOUNT + # # The architecture supports pgtable leaves that is larger than PAGE_SIZE # --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CCC726772E for ; Mon, 24 Feb 2025 16:56:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416206; cv=none; b=YDPzqLUsdMaWTeKNjHf/FOEWYjUmi3Os02dWedJTW4a9CJny11ziQKiCJxS9AG/2cqrR86gyfzZpgKL8L52MN5obBvTa0B+9uCVnMWYWr+ZDHeJBhqw6pIiQvxcUK546cp60KWLcYeAke7OlF/xGDg2FkdkUhWIbvi8hjVco32o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416206; c=relaxed/simple; bh=COc9LSVS2gNAFQnh4IzvB8A8xKf1CalXjCi6/SHso4I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d7E3k1F4DQnrubLWGl4BVYldiT5fuqDt2zy2wd1JGNL0QXsZkd5UKxb0H5XSCmcH8bLAFGgq0hoQVxyAOSxYRj4OqNG6n1KQKle6W13O5WENKgesZvJvtbLr5qtv7t6yDgf4ilwjUrZxcTK39CkvIh5iVlXe9HP5pI3grVgodEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Cusc0PU9; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Cusc0PU9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=I7/ovFwFtUshRpZfEG8TPamCJVjib0AvdCdidGnTX/0=; b=Cusc0PU9QVMw5FaCdksXp6U1UHVonwpa5s7qbBt939LgyXKLiN00p0DNmezcuQuNfd7O+T i2ishTgS+Wk8sB93GCaThXTLMqk8g4JoDZv5MMlBwQrx0MSbCvziFVruDF7UeNmP3glTuH B6xYDm1CQW5RmDDLW5jeVaRoscr9RUg= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-647-1lNxZzPoMWGnC5ltj2nd-Q-1; Mon, 24 Feb 2025 11:56:41 -0500 X-MC-Unique: 1lNxZzPoMWGnC5ltj2nd-Q-1 X-Mimecast-MFC-AGG-ID: 1lNxZzPoMWGnC5ltj2nd-Q_1740416200 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-38f4e6e004fso3562242f8f.1 for ; Mon, 24 Feb 2025 08:56:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416200; x=1741021000; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I7/ovFwFtUshRpZfEG8TPamCJVjib0AvdCdidGnTX/0=; b=Rvlf0vtblrEBDJUVwhdXsJdPx8nf9diN/4LI2ibF6YiWi8bNPe7e4IaaaexjPNxG46 zLWAW/EnJLIF7acpPwmtmLsQLn68w1FFrDykHj8vMtueMduWU+mN/JkF28Rb8pUGp+TG Ba08DIWG3axhHreiXEc8qgjv/CwyETaetCXsxj/3r+GXw6ZJWUeBgj046/Uiwx8mBahW i4Okt+VM+ap8jc5qoAUqvyH9gCZ6XGc8v7jmIqzU6YMogURZ1S3oOnBSzWBikV1tn/zL F+dYj3Br+LtIDxvtFa+Bp2RC3QzSI3lWVxKUDG3lpPsq6OMMQ4YblAV3hd3A9Y1G10z+ qWig== X-Gm-Message-State: AOJu0YxV4mM7i3ezE+/S/MJZnsWz09kiBG5R3Z3qVEsVpeyWzrox9+m/ uyh02PABdXnkmTv4M01D6O0PAdPEBmPWBiBMSthbnntHCllOjyfZjXlwPZiP4fG/rWuEd7X6BDs VO4iAr0gPyC/GADWo5v6KLiRGkjAtuTtaKh+Hy6wsfIz5mN30uS/pX3j5LxxoBNwfVfCBz76bF4 B2iRohCu7pAH6TOBeIki35gggh5NPJcYViEABIXOT/9mMa X-Gm-Gg: ASbGncskthZ5ZVqgsMpKhrVACB+WZje0grmjINkqYvh4mMxcipPmTPSn3X37cNcDFBh VTw1auvhXRTRaKzkbMfCe83rOGuIxdlnhDUm0TNMZLSuPXSqrQ93TchbEUATOleDDDq2aP6YFH3 /p88ywN58GGPtr68Bj6Le6awCwfsl1Wt7/E620OF0+Wnhv4zMuribgcIc6QA50TL9a5fD3OQSti 0zrQPP+Q0V/yrE85h1CJqiLIp2aMlK2B/NsMDpANLgSRoCa59tRTKP5FLnJAUWmYTiZVW71GzkQ XW59zeRrNE4MyCBjulfJ2Jb3T1blk2EduSdQADJAgQ== X-Received: by 2002:a5d:588d:0:b0:38f:330a:acbc with SMTP id ffacd0b85a97d-38f6f0c743bmr12449970f8f.54.1740416200369; Mon, 24 Feb 2025 08:56:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IFfMgOxaCcP3QUPAwnlgJXjUNV21VqnGE//pcrHn7Js38XBMUq8lKiO2xorUlCAe/uWbu52qQ== X-Received: by 2002:a5d:588d:0:b0:38f:330a:acbc with SMTP id ffacd0b85a97d-38f6f0c743bmr12449918f8f.54.1740416199872; Mon, 24 Feb 2025 08:56:39 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f259f8121sm31621452f8f.88.2025.02.24.08.56.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:39 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 16/20] fs/proc/page: remove per-page mapcount dependency for /proc/kpagecount (CONFIG_NO_PAGE_MAPCOUNT) Date: Mon, 24 Feb 2025 17:55:58 +0100 Message-ID: <20250224165603.1434404-17-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's implement an alternative when per-page mapcounts in large folios are no longer maintained -- soon with CONFIG_NO_PAGE_MAPCOUNT. For large folios, we'll return the per-page average mapcount within the folio, except when the average is 0 but the folio is mapped: then we return 1. For hugetlb folios and for large folios that are fully mapped into all address spaces, there is no change. As an alternative, we could simply return 0 for non-hugetlb large folios, or disable this legacy interface with CONFIG_NO_PAGE_MAPCOUNT. But the information exposed by this interface can still be valuable, and frequently we deal with fully-mapped large folios where the average corresponds to the actual page mapcount. So we'll leave it like this for now and document the new behavior. Note: this interface is likely not very relevant for performance. If ever required, we could try doing a rather expensive rmap walk to collect precisely how often this folio page is mapped. Signed-off-by: David Hildenbrand --- Documentation/admin-guide/mm/pagemap.rst | 7 +++++- fs/proc/internal.h | 31 ++++++++++++++++++++++++ fs/proc/page.c | 19 ++++++++++++--- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Documentation/admin-guide/mm/pagemap.rst b/Documentation/admin= -guide/mm/pagemap.rst index caba0f52dd36c..49590306c61a0 100644 --- a/Documentation/admin-guide/mm/pagemap.rst +++ b/Documentation/admin-guide/mm/pagemap.rst @@ -42,7 +42,12 @@ There are four components to pagemap: skip over unmapped regions. =20 * ``/proc/kpagecount``. This file contains a 64-bit count of the number = of - times each page is mapped, indexed by PFN. + times each page is mapped, indexed by PFN. Some kernel configurations do + not track the precise number of times a page part of a larger allocation + (e.g., THP) is mapped. In these configurations, the average number of + mappings per page in this larger allocation is returned instead. Howeve= r, + if any page of the large allocation is mapped, the returned value will + be at least 1. =20 The page-types tool in the tools/mm directory can be used to query the number of times a page is mapped. diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 1695509370b88..16aa1fd260771 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -174,6 +174,37 @@ static inline int folio_precise_page_mapcount(struct f= olio *folio, return mapcount; } =20 +/** + * folio_average_page_mapcount() - Average number of mappings per page in = this + * folio + * @folio: The folio. + * + * The average number of present user page table entries that reference ea= ch + * page in this folio as tracked via the RMAP: either referenced directly + * (PTE) or as part of a larger area that covers this page (e.g., PMD). + * + * Returns: The average number of mappings per page in this folio. 0 for + * folios that are not mapped to user space or are not tracked via the RMAP + * (e.g., shared zeropage). + */ +static inline int folio_average_page_mapcount(struct folio *folio) +{ + int mapcount, entire_mapcount; + unsigned int adjust; + + if (!folio_test_large(folio)) + return atomic_read(&folio->_mapcount) + 1; + + mapcount =3D folio_large_mapcount(folio); + entire_mapcount =3D folio_entire_mapcount(folio); + if (mapcount <=3D entire_mapcount) + return entire_mapcount; + mapcount -=3D entire_mapcount; + + adjust =3D folio_large_nr_pages(folio) / 2; + return ((mapcount + adjust) >> folio_large_order(folio)) + + entire_mapcount; +} /* * array.c */ diff --git a/fs/proc/page.c b/fs/proc/page.c index a55f5acefa974..4d3290cc69667 100644 --- a/fs/proc/page.c +++ b/fs/proc/page.c @@ -67,9 +67,22 @@ static ssize_t kpagecount_read(struct file *file, char _= _user *buf, * memmaps that were actually initialized. */ page =3D pfn_to_online_page(pfn); - if (page) - mapcount =3D folio_precise_page_mapcount(page_folio(page), - page); + if (page) { + struct folio *folio =3D page_folio(page); + + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) { + mapcount =3D folio_precise_page_mapcount(folio, page); + } else { + /* + * Indicate the per-page average, but at least "1" for + * mapped folios. + */ + mapcount =3D folio_average_page_mapcount(folio); + if (!mapcount && folio_test_large(folio) && + folio_mapped(folio)) + mapcount =3D 1; + } + } =20 if (put_user(mapcount, out)) { ret =3D -EFAULT; --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 726E1267B78 for ; Mon, 24 Feb 2025 16:56:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416208; cv=none; b=YznPt2HD48vGugrAjuCqcC4qC0R2uem/tbvW/KYzjXULBPtzTCisHr5VTcV0Ku2K4UHkZkG8EzwkKv5VyMvUGNUVhcRzhDkWtOZ4CgfMyKZjQ9PkGX3aCgaYCVuh/0Mxjh0uFRZNn2o+tKRCS9oHJAwVW1MoDWH6YKUaq1530GA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416208; c=relaxed/simple; bh=y7rnQn4atWG4y4XI5Y3ZUk4Khub4cSbzFIEKIR0LMHc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XkqpscOJtM332W+rP0oci+Bq0bGaNQvs4yyPiQ485629fF7WBq0Zk4/gbgzHO/qmwNiCZGC4lfLK6IhcYMYVwVbfp6ey3b9KVUAa7bRbJHKVZVSjf7YXszoMJS7dsySjIjDP4iXC/9cH9rz50VloFzAnWekkf/g5fFn1Kp1t9IY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XdaJyw22; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XdaJyw22" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qrzeSAQepC6qjgZK5hS618fC1aWLshwTFA4Bvic/TRg=; b=XdaJyw22qn1YI1B67eVRlYv8jfBqTaRUSs5mAZw22xdTKf5jDI3Pj5Yy7Tme9K6NKvlleP OYZZRXvT+hB97Pb/rix9520a7IBVTcJLfv88Ty99dZLiBCgAAFldgih6BVa3d8WbxE/4HU ZDVlcNxTfCQ1ljj/MbMYxBjcTRS1FM8= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-621-lXb5IYKWPS2L1DyIpSNhbw-1; Mon, 24 Feb 2025 11:56:43 -0500 X-MC-Unique: lXb5IYKWPS2L1DyIpSNhbw-1 X-Mimecast-MFC-AGG-ID: lXb5IYKWPS2L1DyIpSNhbw_1740416202 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-38f34b3f9f1so3474982f8f.0 for ; Mon, 24 Feb 2025 08:56:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416202; x=1741021002; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qrzeSAQepC6qjgZK5hS618fC1aWLshwTFA4Bvic/TRg=; b=eD9boOddVgeFDBqGKwqvA4mfY0tpwB/z/SAsiNrbmRV8DH96vvTvWPqZFNrf7VaMr3 b6LhEY5VWCvf8M3s8WMuotamERUVpcQDHLrB7ULtaYyoLdDoxcnaVDy6WlU94KKVwekN e5gDCRY6oHTTn79iGCc1ts6Gm8sZ6fmsk0SogalCQUsm/Jxtag+N0d6pLNEw2X+1yg3n L4cVSJ0MkEoVjMxuPL7MvKj/6H8mhb2P9IiPQ7mvkxG7gS7I+9bNwA9+rUcYqboKJayH tKLt6zoqdeZK9ngOiOhM+lJi19zRoWsAZwYuB0zY3Fb5PSqweZ+rD2mJZ4n1Kyq8QKzH nOPA== X-Gm-Message-State: AOJu0YxogcOoemZNDuu0uu2pznwR96C6c67iGlyTubvy7r4qZng9T43X GbDgmccndE9LoVuADasJAxm+RejY6D47ngGhy/FSIuWtmIqvlJBPqY5Upyxj6jsc/vwTyxQOaKf qE2hFx3EklwJ+gX1ceSN52C3Q2u4yB57HTxWbqJ9Ot7ZJZA8tHAjXkDsbKW1fiQnU70l7WMsw19 Mi8wUc8FwPACYYdTTiMYVQj6TPcZskoUvM3mHPLAT5NZhV X-Gm-Gg: ASbGncvaR6mshtIiKK8pcy8Wd0QA8aGX9OFFz00+VhwGqA5lfwgIUKOjMwZDH4wsaTY 16NrI3RanO63FjAsTHlTwqaH0oFJm7/axyFDudpTMdYC3WPHKGji26bNBZWEni419GaqgJddyVm q1aagq/ofQDAcJ6qBBImkzgB/CpGZ8udt/snsk2D90VobU954RXTXa/3KJNousdyFoPh3nwin9K J12hoXet/CjJiZRM2jDh9GH+qXcowZ62+hsKhod/hXFG0MU9Z34t9+XO7GF8rTxO7bmaU2/1hsq 3ryzXam2nlTmMhr+scpnLpy0GZksoqP3pLKE4dK+Mw== X-Received: by 2002:a5d:588d:0:b0:38f:330a:acbc with SMTP id ffacd0b85a97d-38f6f0c743bmr12450090f8f.54.1740416202255; Mon, 24 Feb 2025 08:56:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IHB6f6br0E5FryNlZozgeR2JsRbAfPcg+xWwWdf/YpxK+ukwffc3aXIkqUDyRFtyhNU8GNoFQ== X-Received: by 2002:a5d:588d:0:b0:38f:330a:acbc with SMTP id ffacd0b85a97d-38f6f0c743bmr12450037f8f.54.1740416201773; Mon, 24 Feb 2025 08:56:41 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f259f8602sm32962279f8f.94.2025.02.24.08.56.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:41 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 17/20] fs/proc/task_mmu: remove per-page mapcount dependency for PM_MMAP_EXCLUSIVE (CONFIG_NO_PAGE_MAPCOUNT) Date: Mon, 24 Feb 2025 17:55:59 +0100 Message-ID: <20250224165603.1434404-18-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's implement an alternative when per-page mapcounts in large folios are no longer maintained -- soon with CONFIG_NO_PAGE_MAPCOUNT. PM_MMAP_EXCLUSIVE will now be set if folio_likely_mapped_shared() is true -- when the folio is considered "mapped shared", including when it once was "mapped shared" but no longer is, as documented. This might result in and under-indication of "exclusively mapped", which is considered better than over-indicating it: under-estimating the USS (Unique Set Size) is better than over-estimating it. As an alternative, we could simply remove that flag with CONFIG_NO_PAGE_MAPCOUNT completely, but there might be value to it. So, let's keep it like that and document the behavior. Signed-off-by: David Hildenbrand --- Documentation/admin-guide/mm/pagemap.rst | 9 +++++++++ fs/proc/task_mmu.c | 11 +++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/mm/pagemap.rst b/Documentation/admin= -guide/mm/pagemap.rst index 49590306c61a0..131c86574c39a 100644 --- a/Documentation/admin-guide/mm/pagemap.rst +++ b/Documentation/admin-guide/mm/pagemap.rst @@ -37,6 +37,15 @@ There are four components to pagemap: precisely which pages are mapped (or in swap) and comparing mapped pages between processes. =20 + Note that in some kernel configurations, all pages part of a larger + allocation (e.g., THP) might be considered "mapped shared" if the large + allocation is considered "mapped shared": if not all pages are exclusiv= e to + the same process. Further, some kernel configurations might consider la= rger + allocations "mapped shared", if they were at one point considered + "mapped shared", even if they would now be considered "exclusively mapp= ed". + Consequently, in these kernel configurations, bit 56 might be set altho= ugh + the page is actually "exclusively mapped" + Efficient users of this interface will use ``/proc/pid/maps`` to determine which areas of memory are actually mapped and llseek to skip over unmapped regions. diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 2bddcea65cbf1..80839bbf9657f 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1651,6 +1651,13 @@ static int add_to_pagemap(pagemap_entry_t *pme, stru= ct pagemapread *pm) return 0; } =20 +static bool __folio_page_mapped_exclusively(struct folio *folio, struct pa= ge *page) +{ + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) + return folio_precise_page_mapcount(folio, page) =3D=3D 1; + return !folio_maybe_mapped_shared(folio); +} + static int pagemap_pte_hole(unsigned long start, unsigned long end, __always_unused int depth, struct mm_walk *walk) { @@ -1739,7 +1746,7 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pa= gemapread *pm, if (!folio_test_anon(folio)) flags |=3D PM_FILE; if ((flags & PM_PRESENT) && - folio_precise_page_mapcount(folio, page) =3D=3D 1) + __folio_page_mapped_exclusively(folio, page)) flags |=3D PM_MMAP_EXCLUSIVE; } if (vma->vm_flags & VM_SOFTDIRTY) @@ -1814,7 +1821,7 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned lo= ng addr, unsigned long end, pagemap_entry_t pme; =20 if (folio && (flags & PM_PRESENT) && - folio_precise_page_mapcount(folio, page + idx) =3D=3D 1) + __folio_page_mapped_exclusively(folio, page)) cur_flags |=3D PM_MMAP_EXCLUSIVE; =20 pme =3D make_pme(frame, cur_flags); --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC290267F41 for ; Mon, 24 Feb 2025 16:56:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416211; cv=none; b=ke86jCSnd1o0L1RHaJPzhCSMem7kppwcjwjkzBfBfKS2OmldokKM+JXTmt5TzrqB1qz3esVh9pjo4oduTSHvBlzoCiU1KOQgm1e0g6yKTJK4EoND590/cGe4bHyFVgTMU/O6srM6np4HYfQUOPrEDvrTBpHOSWMt+I9n/Js8ywg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416211; c=relaxed/simple; bh=qpkZkgDQSCpDOrawHj8ee8It7p8kTmPkI0TP7miUP/A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ss9fl+C8G2VvOVT0Y6peGaPYyq3zvhZkUJPrN5odKWvA0A44p1vt6pBv8JkIqQOZq88nPmgGeFl1CD0zKLMmXWPLiWk5hkum9suyf40qtxIQ1gXMUGOPzD6fu0sOnXghIsxGayni6FEFfnfbK1Il9zZRfHXrD6fz5yx7eyI5wEk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=K51PjnkB; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="K51PjnkB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416206; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4IIiK38txs39RTm2yNSD9uzsg2Epj+W/QUBXdJN6a7s=; b=K51PjnkBDK9AvOSH4NkFDDdNw0hDO3FlKi9FH6vLn2UDFFhm+yADFC8S6KSB8dlEb1et6+ SqR+nLZBQKaP1hsvKtzBuZb34mWa94DiyoI4h5544KQp+tQLapv7hhbpQ3wRqsdL+bsLBN V8AV8O5G2q0UZzdxmHPLAvXMveUWPRM= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-84--N238KgcO0eWp7ExCw9R-g-1; Mon, 24 Feb 2025 11:56:45 -0500 X-MC-Unique: -N238KgcO0eWp7ExCw9R-g-1 X-Mimecast-MFC-AGG-ID: -N238KgcO0eWp7ExCw9R-g_1740416204 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-38f2f78aee1so1894295f8f.0 for ; Mon, 24 Feb 2025 08:56:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416204; x=1741021004; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4IIiK38txs39RTm2yNSD9uzsg2Epj+W/QUBXdJN6a7s=; b=RK3MwhempaLINMZ9xINFFyKfe4v97ex67co50cHBVcbFcUTchjHTTA59TetoYmtLmh 0tP1i3x/NgbgTKj6WG8OG3CNyybjNn83scolPgZek+PyjF1QFZfGdyxyIcQG/nSWYKVO 0syF8FI0npQlWoWVxU84FwUdFzzrP88VAPR6VrC0TTcJzzEQ8019BgXweKMKIEJr73MG 5lBtKYxieIteAllJznlNLnCl41L7xgiVhYjibsDqQFxNLzhnDvObgS1lHnnTYOMhopxL 8zKj4kKDsgjb8zW1tEmA0cETTQptqX//WKdZkqclvCEH6qyA0efLFJQFd4fnmLjx3p9G y7kw== X-Gm-Message-State: AOJu0YzZsOYC21eoZSJaN97D0B5IcKUweKyO4F7FAGJ1s2+y3n313cEt QsZDUvWXDFBFxohx4NBa6+cte6CBKIlQUeFkGpyJ1+x4uurDEMU3rIPugjEoPOUoEsH8LFPD0sl HqT/uN3OfbHSk9MJmmD1nfZlfN3Fsy6ByFjKoQ8dkwWaifi1r0mAXnt4WHVmva4eyehtjreyQMg SVJwUNZgnQloT5RTprTNLlom+Yn9pZ3lJC1k2ELoC1vmkv X-Gm-Gg: ASbGncvvy43i8j9GM0I/SvtcZbEXTfnpTQjryK40xc1cqErKywTCtNA7/f5X/hY+mfS BCxKiFYCUduPtoNmJj+CW+U8OXYJP+OxtaCDBiwE1qfXbbH65EQIBHxmaIFTy/x0KdZq2okLiaN Ltx3KjVicXfJ6omsE0xYfwI5Iq1Qj9SQ1Mv0+QT/vXPjk7YTjYzoIKALeTTXONhJrUinh/tnUyU GwDC8EPeUKqyPFvyklSsivOXAboJWGFuGnhYeVlCGgQzOjOdB916Z4l8UNEkjOs7H8klygmVpn2 UAC32u3ZUKtyO/Ov0vrOOInQMIjDpjt1jQHZ+lINGw== X-Received: by 2002:a5d:5f90:0:b0:38d:c6b8:9fe1 with SMTP id ffacd0b85a97d-38f6f51d885mr12699092f8f.24.1740416204207; Mon, 24 Feb 2025 08:56:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IHr48iDkbeT3BD6T31jawE7AoIz4aDQ8fc4EtI1CIkHNhBzvnREnXgHgZXA/JhJC2+PnfKZ2A== X-Received: by 2002:a5d:5f90:0:b0:38d:c6b8:9fe1 with SMTP id ffacd0b85a97d-38f6f51d885mr12699040f8f.24.1740416203760; Mon, 24 Feb 2025 08:56:43 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f25914171sm32572475f8f.53.2025.02.24.08.56.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:43 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 18/20] fs/proc/task_mmu: remove per-page mapcount dependency for "mapmax" (CONFIG_NO_PAGE_MAPCOUNT) Date: Mon, 24 Feb 2025 17:56:00 +0100 Message-ID: <20250224165603.1434404-19-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's implement an alternative when per-page mapcounts in large folios are no longer maintained -- soon with CONFIG_NO_PAGE_MAPCOUNT. For calculating "mapmax", we now use the average per-page mapcount in a large folio instead of the per-page mapcount. For hugetlb folios and folios that are not partially mapped into MMs, there is no change. Likely, this change will not matter much in practice, and an alternative might be to simple remove this stat with CONFIG_NO_PAGE_MAPCOUNT. However, there might be value to it, so let's keep it like that and document the behavior. Signed-off-by: David Hildenbrand --- Documentation/filesystems/proc.rst | 5 +++++ fs/proc/task_mmu.c | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems= /proc.rst index 09f0aed5a08ba..1aa190017f796 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -686,6 +686,11 @@ Where: node locality page counters (N0 =3D=3D node0, N1 =3D=3D node1, ...) and th= e kernel page size, in KB, that is backing the mapping up. =20 +Note that some kernel configurations do not track the precise number of ti= mes +a page part of a larger allocation (e.g., THP) is mapped. In these +configurations, "mapmax" might corresponds to the average number of mappin= gs +per page in such a larger allocation instead. + 1.2 Kernel data --------------- =20 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 80839bbf9657f..d7ee842367f0f 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -2862,7 +2862,12 @@ static void gather_stats(struct page *page, struct n= uma_maps *md, int pte_dirty, unsigned long nr_pages) { struct folio *folio =3D page_folio(page); - int count =3D folio_precise_page_mapcount(folio, page); + int count; + + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) + count =3D folio_precise_page_mapcount(folio, page); + else + count =3D min_t(int, folio_average_page_mapcount(folio), 1); =20 md->pages +=3D nr_pages; if (pte_dirty || folio_test_dirty(folio)) --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89E6D267F67 for ; Mon, 24 Feb 2025 16:56:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416211; cv=none; b=C96DhoFf6Hl9n6xGL1A6lx7hJ2vU3aISdiDJPnr9p0U/L8RU/AJh7ywW7zDR0+x19LKfaLbqVIlMYv9qS+QyflvFtADApxVXnHguREkhmglTPvYLhbViCWBd09EZBwln0Bo2OjV7L9xiXvala7ylPmBWNCHKaAvRMAQowwudDag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416211; c=relaxed/simple; bh=9KDOJkYIHHSmS8EWVgHad+sJWVwCwusKqKnGhETdnT4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j6V4Eyd9YnSoQvmHSrShBUisqp9mN4lJ0t0hHwRxslvZf0Zhia+dGcJgc3LnZ5OOhzb8tWvdJrISG2P6ITPwMDi8hucx6CEFcgzzpnUCT4vT8GGZ+aWgBhFrufnBU1GZMfmsmZ8l3TH7dFEV2hvHu0/FkB26CUkUb73PTXBjumc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=DPryXaiq; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DPryXaiq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416208; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YYLglXDyFMXUmuyPM7L4Wt5NI1TaS59XhalfSABwrEE=; b=DPryXaiqE2nXdM6aAX03qT/KRobjuXPgcdhA2sYFqMHeWgbDTq756lhjIi5c6DsS4/2Y94 Ndx2COVaTXlodCmlniCQIoa68ML6qt8+F3V170VCbV2SJDBgBh/xDBxbsTkOKN3LFvIdzq hypoBqiWGypXvOxp8cpp577T6IqzzOE= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-yZZyWuCiOL2nIECljcLeBg-1; Mon, 24 Feb 2025 11:56:47 -0500 X-MC-Unique: yZZyWuCiOL2nIECljcLeBg-1 X-Mimecast-MFC-AGG-ID: yZZyWuCiOL2nIECljcLeBg_1740416206 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-38f27acb979so4601944f8f.0 for ; Mon, 24 Feb 2025 08:56:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416206; x=1741021006; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YYLglXDyFMXUmuyPM7L4Wt5NI1TaS59XhalfSABwrEE=; b=D4Rayui6qUHO6YAKoSmAU6InacmjebxVT61x3duvgtf+lrRXAIZWxjl99ntJro1D0z ttY3FaWCanMmLSwd2v9Zmygmq4YA15GekqnkIYJe98yvautBC2Us4PSZo05t2VYSo11l MM7q43SaE0PYTF+6yDhSbl/uV28Lg7JCKgoI4kqZqN1bf4OOcDzegSwoE1ob6anu90Y7 rbFRSnK3quP3XxYYuFcy9I6xaq/jWttLdzwQc6a2GJpzoGPW8DBdaOqvS386cQIlSFAS jKjUcX+n9tZxjFL0yq7u6c4FwrlJsghiFFWIX9zAd8FNnTd0ISXCOxA+0B5gs346HDd2 b5wg== X-Gm-Message-State: AOJu0YwJPH6b/y0hzgE6KghB+XObkrtkGwJOkHNnYUry2pPSMtnEzJu1 rL2xMsQr/RqOla/4eCxcnnVH0h6kZnUOsjj1/PNsbAFdHq8V+95YdHi3VsZLVb7YOkaNjSJA4n+ 1dtvzanmZm/hdUBM5vOcdKJZDhAmmcMj2VHeBDg5tisE1G9dEf46eKtdEElgN2YoRcrHrGwyn85 lmsBzfD+8SC7kYSRAucGgpeQoZuPpdsNqL0FRmUeq7V9KJ X-Gm-Gg: ASbGncvPv6Z4vrcOT8HlFDozYrdF8ErFJJcq72CshbCElides/xVw1QOqbSL/862QaJ 9rvEm2aFnTEBabbpP9QQIpJXlto885AvV6L6uHmaqBaX7ui8KDAeug023a8KcZoaPIaR9+6e7i5 49OnlFN+LhGxnA/2WKjrfSBTr3ARIX2T5KlWikYoHqyKseks5NmEB8iMpBZbU5cKxZ6nwzy4nga fLuyc3rpY9sd5I0y90lAyx7nIZ4pgvxAmcUtn2f39oaRRgnmgSY6TeX22J0R4Ep1AAr6SyYti8o 55ze/+Zuj2w5IldvZcKqkGQCKNj+uQGDQPZCtR4PyA== X-Received: by 2002:a5d:6da5:0:b0:38d:cf33:31a1 with SMTP id ffacd0b85a97d-38f707afc79mr12824186f8f.23.1740416206066; Mon, 24 Feb 2025 08:56:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IH7oc6/f/Omy9ecKXhn6XnBr6bqzxs+GSFB5uGM8J4SWQqdkeD7RWsCxWskwEhPaUkzkpZm0Q== X-Received: by 2002:a5d:6da5:0:b0:38d:cf33:31a1 with SMTP id ffacd0b85a97d-38f707afc79mr12824140f8f.23.1740416205579; Mon, 24 Feb 2025 08:56:45 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-38f259f7998sm31659273f8f.82.2025.02.24.08.56.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:45 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 19/20] fs/proc/task_mmu: remove per-page mapcount dependency for smaps/smaps_rollup (CONFIG_NO_PAGE_MAPCOUNT) Date: Mon, 24 Feb 2025 17:56:01 +0100 Message-ID: <20250224165603.1434404-20-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Let's implement an alternative when per-page mapcounts in large folios are no longer maintained -- soon with CONFIG_NO_PAGE_MAPCOUNT. When computing the output for smaps / smaps_rollups, in particular when calculating the USS (Unique Set Size) and the PSS (Proportional Set Size), we still rely on per-page mapcounts. To determine private vs. shared, we'll use folio_likely_mapped_shared(), similar to how we handle PM_MMAP_EXCLUSIVE. Similarly, we might now under-estimate the USS and count pages towards "shared" that are actually "private" ("exclusively mapped"). When calculating the PSS, we'll now also use the average per-page mapcount for large folios: this can result in both, an over-estimation and an under-estimation of the PSS. The difference is not expected to matter much in practice, but we'll have to learn as we go. We can now provide folio_precise_page_mapcount() only with CONFIG_PAGE_MAPCOUNT, and remove one of the last users of per-page mapcounts when CONFIG_NO_PAGE_MAPCOUNT is enabled. Document the new behavior. Signed-off-by: David Hildenbrand --- Documentation/filesystems/proc.rst | 13 +++++++++++++ fs/proc/internal.h | 8 ++++++++ fs/proc/task_mmu.c | 17 +++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems= /proc.rst index 1aa190017f796..57d55274a1f42 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -506,6 +506,19 @@ Note that even a page which is part of a MAP_SHARED ma= pping, but has only a single pte mapped, i.e. is currently used by only one process, is accou= nted as private and not as shared. =20 +Note that in some kernel configurations, all pages part of a larger alloca= tion +(e.g., THP) might be considered "shared" if the large allocation is +considered "shared": if not all pages are exclusive to the same process. +Further, some kernel configurations might consider larger allocations "sha= red", +if they were at one point considered "shared", even if they would now be +considered "exclusive". + +Some kernel configurations do not track the precise number of times a page= part +of a larger allocation is mapped. In this case, when calculating the PSS, = the +average number of mappings per page in this larger allocation might be used +as an approximation for the number of mappings of a page. The PSS calculat= ion +will be imprecise in this case. + "Referenced" indicates the amount of memory currently marked as referenced= or accessed. =20 diff --git a/fs/proc/internal.h b/fs/proc/internal.h index 16aa1fd260771..70205425a2daa 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -143,6 +143,7 @@ unsigned name_to_int(const struct qstr *qstr); /* Worst case buffer size needed for holding an integer. */ #define PROC_NUMBUF 13 =20 +#ifdef CONFIG_PAGE_MAPCOUNT /** * folio_precise_page_mapcount() - Number of mappings of this folio page. * @folio: The folio. @@ -173,6 +174,13 @@ static inline int folio_precise_page_mapcount(struct f= olio *folio, =20 return mapcount; } +#else /* !CONFIG_PAGE_MAPCOUNT */ +static inline int folio_precise_page_mapcount(struct folio *folio, + struct page *page) +{ + BUILD_BUG(); +} +#endif /* CONFIG_PAGE_MAPCOUNT */ =20 /** * folio_average_page_mapcount() - Average number of mappings per page in = this diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index d7ee842367f0f..7ca0bc3bf417d 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -707,6 +707,8 @@ static void smaps_account(struct mem_size_stats *mss, s= truct page *page, struct folio *folio =3D page_folio(page); int i, nr =3D compound ? compound_nr(page) : 1; unsigned long size =3D nr * PAGE_SIZE; + bool exclusive; + int mapcount; =20 /* * First accumulate quantities that depend only on |size| and the type @@ -747,18 +749,29 @@ static void smaps_account(struct mem_size_stats *mss,= struct page *page, dirty, locked, present); return; } + + if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) { + mapcount =3D folio_average_page_mapcount(folio); + exclusive =3D !folio_maybe_mapped_shared(folio); + } + /* * We obtain a snapshot of the mapcount. Without holding the folio lock * this snapshot can be slightly wrong as we cannot always read the * mapcount atomically. */ for (i =3D 0; i < nr; i++, page++) { - int mapcount =3D folio_precise_page_mapcount(folio, page); unsigned long pss =3D PAGE_SIZE << PSS_SHIFT; + + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) { + mapcount =3D folio_precise_page_mapcount(folio, page); + exclusive =3D mapcount < 2; + } + if (mapcount >=3D 2) pss /=3D mapcount; smaps_page_accumulate(mss, folio, PAGE_SIZE, pss, - dirty, locked, mapcount < 2); + dirty, locked, exclusive); } } =20 --=20 2.48.1 From nobody Thu Dec 18 22:14:38 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EA57268C40 for ; Mon, 24 Feb 2025 16:56:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416215; cv=none; b=nafxBCLocbUOde6exU+pxiCggXZNCiPU76l52J8y32hoFcEAbF5t7GamWH9E3EsUqihPzspjxVyaLVokS5CsLwxVXswt0YzK6NM49xP4KvtlWA5ZHQIbb/KRhKf3ELgLFT6SCKs+QknOZCVGwUN0gvCKSU5cT1i1KddirJWEyO4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416215; c=relaxed/simple; bh=67pl9UOJ1GSYqL4a9JTWXCi0kA+lyx/GKrrnunY2cdA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RrNwffqbnv3GPtYr6ecmFSMcYgqiIl1sdMC3uc4YAZXriyH0+RL7VFPZu9ILNQhQYPy2MSxOXk8q2jMxRg/iYjUPGhCy12U9iwUlXsLXjglyP0IoNkioO1lrZMwg2bMojRPCS/gFcgT5PFehzQtnpKoZANyUvYbYj53I1ibtxeI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Y9rq7iQQ; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Y9rq7iQQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740416211; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MgdgCkfQT710L4EQa1Wd5rppib0ue2uPYRNKLBnKmzg=; b=Y9rq7iQQP70ohkPzt4jOUx+DGbi8OfTclsmk8p89QG3hEWJEDPEucTD5p/ZNLMyctJ3iw5 pPo0YRMn3rs5t3Bg9SPF19DMJ1HoQp9C+gGrpv3MLnw+FPSaDK8jrz7yBTyAy+Qd12fwYL 1mz/NzpomGayEZWDTyi98gGw42yeIMk= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-474-sdzbx-tFO9KtPUha_LSTUg-1; Mon, 24 Feb 2025 11:56:49 -0500 X-MC-Unique: sdzbx-tFO9KtPUha_LSTUg-1 X-Mimecast-MFC-AGG-ID: sdzbx-tFO9KtPUha_LSTUg_1740416209 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4393e8738b3so32903465e9.0 for ; Mon, 24 Feb 2025 08:56:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740416208; x=1741021008; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MgdgCkfQT710L4EQa1Wd5rppib0ue2uPYRNKLBnKmzg=; b=wO7dczfAKy8ZSfGSPwcHau6V7et/Y83D6nH5FwU+KsFgOiKe1ESo/IytdRvTLeOLvc tfW4IKTbcAo7zNVWGoPLBqRQ6aYWAV4v3Di1ALBYzvUB3PHxOO2O8wyMvJPJp2b2lxp0 I0yedTjKdEbumZjeqeshMrLbC57kaelRy3frNKa/b7S9D9L5QqRsLsC++tLao5d37Y5z efKx47PQtYnPEHcTLb7bUGgVY5W6MPCtH5e9aDx5AyRjaaq5QxEQdSAS70q2HJNLqEh7 IRG4sej4i8+Gri92CN13pnNGxgFEw90VahMXNWi3D5ovJj06JP/Yl44e9RvV6bDFgeUn WBxw== X-Gm-Message-State: AOJu0Yz8+7Lxy7ObI6kKiIoedPQFAzB2BykpqHoSEddq2SH8yXMcQWy4 3fruwDBwlm7fjN+wdbSw6Np93o4+b4ZC0bScgXajJoCsHihp4WhelfIsWUQIP9VaP/1WfDnTKrc ps8zJFsFknIBj5yNCM52mRnQFWyNIAXdc+h7KDXtCRMN6dpGVIy2SN9esNkNKBuAC7O98T9rV5y TaJfWY73UNSAoHPneTFuyEP6fD/ejNzB3pLvKDL7al4CSx X-Gm-Gg: ASbGncuY2S1LXOyDa7+0r1xzvKtUYjosTupY2IxFDpwNqr9PPGq+TIiwbwQgbhSTJ33 qrkrM/FMPLestd4Xtzwnck4iPfB12TLJjoN8hHcZmDvI5yZ31xR/MIMezH6cMjvTRw26pGJFa0R 3yiCuMT6mGUhLb4bTqzNImf9eyZZY2FP54xGS7o153/dvJXGzcAGWeasfX+bjyKe/8AA0TShmKp jsMglF8YhQ/4MgcpxJJUcnc6NK9mwZNUAZZNeZWYClAcDNjemyhwFqLSB3dYhY6cHHDdBeyrIru Z4wAaEESGHKmA8SK+rjKah1W01v7YL9VuWa6drSOSw== X-Received: by 2002:a05:600c:5112:b0:434:a781:f5d5 with SMTP id 5b1f17b1804b1-43aa4ee1f53mr49951605e9.30.1740416208294; Mon, 24 Feb 2025 08:56:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IEkH/GtVfiPSlmNTBjcB5VCGgkWNht+vToPEPB5qduC+IDFH5nKzmUcOLgdLr6vKVUyR6uQ9w== X-Received: by 2002:a05:600c:5112:b0:434:a781:f5d5 with SMTP id 5b1f17b1804b1-43aa4ee1f53mr49951055e9.30.1740416207692; Mon, 24 Feb 2025 08:56:47 -0800 (PST) Received: from localhost (p4ff234b6.dip0.t-ipconnect.de. [79.242.52.182]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-439b02ce41dsm115554595e9.1.2025.02.24.08.56.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 Feb 2025 08:56:47 -0800 (PST) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-doc@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Tejun Heo , Zefan Li , Johannes Weiner , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Jonathan Corbet , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Muchun Song , "Liam R. Howlett" , Lorenzo Stoakes , Vlastimil Babka , Jann Horn Subject: [PATCH v2 20/20] mm: stop maintaining the per-page mapcount of large folios (CONFIG_NO_PAGE_MAPCOUNT) Date: Mon, 24 Feb 2025 17:56:02 +0100 Message-ID: <20250224165603.1434404-21-david@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250224165603.1434404-1-david@redhat.com> References: <20250224165603.1434404-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Everything is in place to stop using the per-page mapcounts in large folios: the mapcount of tail pages will always be logically 0 (-1 value), just like it currently is for hugetlb folios already, and the page mapcount of the head page is either 0 (-1 value) or contains a page type (e.g., hugetlb). Maintaining _nr_pages_mapped without per-page mapcounts is impossible, so that one also has to go with CONFIG_NO_PAGE_MAPCOUNT. There are two remaining implications: (1) Per-node, per-cgroup and per-lruvec stats of "NR_ANON_MAPPED" ("mapped anonymous memory") and "NR_FILE_MAPPED" ("mapped file memory"): As soon as any page of the folio is mapped -- folio_mapped() -- we now account the complete folio as mapped. Once the last page is unmapped -- !folio_mapped() -- we account the complete folio as unmapped. This implies that ... * "AnonPages" and "Mapped" in /proc/meminfo and /sys/devices/system/node/*/meminfo * cgroup v2: "anon" and "file_mapped" in "memory.stat" and "memory.numa_stat" * cgroup v1: "rss" and "mapped_file" in "memory.stat" and "memory.numa_stat ... can now appear higher than before. But note that these folios do consume that memory, simply not all pages are actually currently mapped. It's worth nothing that other accounting in the kernel (esp. cgroup charging on allocation) is not affected by this change. [why oh why is "anon" called "rss" in cgroup v1] (2) Detecting partial mappings Detecting whether anon THPs are partially mapped gets a bit more unreliable. As long as a single MM maps such a large folio ("exclusively mapped"), we can reliably detect it. Especially before fork() / after a short-lived child process quit, we will detect partial mappings reliably, which is the common case. In essence, if the average per-page mapcount in an anon THP is < 1, we know for sure that we have a partial mapping. However, as soon as multiple MMs are involved, we might miss detecting partial mappings: this might be relevant with long-lived child processes. If we have a fully-mapped anon folio before fork(), once our child processes and our parent all unmap (zap/COW) the same pages (but not the complete folio), we might not detect the partial mapping. However, once the child processes quit we would detect the partial mapping. How relevant this case is in practice remains to be seen. Swapout/migration will likely mitigate this. In the future, RMAP walkers could check for that for that case (e.g., when collecting access bits during reclaim) and simply flag them for deferred-splitting. Signed-off-by: David Hildenbrand --- .../admin-guide/cgroup-v1/memory.rst | 4 + Documentation/admin-guide/cgroup-v2.rst | 10 ++- Documentation/filesystems/proc.rst | 10 ++- Documentation/mm/transhuge.rst | 31 +++++-- include/linux/rmap.h | 35 ++++++-- mm/internal.h | 5 +- mm/page_alloc.c | 3 +- mm/rmap.c | 80 +++++++++++++++++-- 8 files changed, 150 insertions(+), 28 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v1/memory.rst b/Documentation= /admin-guide/cgroup-v1/memory.rst index 286d16fc22ebb..53cf081b22e81 100644 --- a/Documentation/admin-guide/cgroup-v1/memory.rst +++ b/Documentation/admin-guide/cgroup-v1/memory.rst @@ -609,6 +609,10 @@ memory.stat file includes following statistics: =20 'rss + mapped_file" will give you resident set size of cgroup. =20 + Note that some kernel configurations might account complete larger + allocations (e.g., THP) towards 'rss' and 'mapped_file', even if + only some, but not all that memory is mapped. + (Note: file and shmem may be shared among other cgroups. In that case, mapped_file is accounted only when the memory cgroup is owner of page cache.) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-= guide/cgroup-v2.rst index 175e9435ad5c1..53ada5c2620a7 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1448,7 +1448,10 @@ The following nested keys are defined. =20 anon Amount of memory used in anonymous mappings such as - brk(), sbrk(), and mmap(MAP_ANONYMOUS) + brk(), sbrk(), and mmap(MAP_ANONYMOUS). Note that + some kernel configurations might account complete larger + allocations (e.g., THP) if only some, but not all the + memory of such an allocation is mapped anymore. =20 file Amount of memory used to cache filesystem data, @@ -1491,7 +1494,10 @@ The following nested keys are defined. Amount of application memory swapped out to zswap. =20 file_mapped - Amount of cached filesystem data mapped with mmap() + Amount of cached filesystem data mapped with mmap(). Note + that some kernel configurations might account complete + larger allocations (e.g., THP) if only some, but not + not all the memory of such an allocation is mapped. =20 file_dirty Amount of cached filesystem data that was modified but diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems= /proc.rst index 57d55274a1f42..c5052acfa0747 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -1150,9 +1150,15 @@ Dirty Writeback Memory which is actively being written back to the disk AnonPages - Non-file backed pages mapped into userspace page tables + Non-file backed pages mapped into userspace page tables. Not= e that + some kernel configurations might consider all pages part of a + larger allocation (e.g., THP) as "mapped", as soon as a sing= le + page is mapped. Mapped - files which have been mmapped, such as libraries + files which have been mmapped, such as libraries. Note that = some + kernel configurations might consider all pages part of a lar= ger + allocation (e.g., THP) as "mapped", as soon as a single page= is + mapped. Shmem Total memory used by shared memory (shmem) and tmpfs KReclaimable diff --git a/Documentation/mm/transhuge.rst b/Documentation/mm/transhuge.rst index baa17d718a762..0e7f8e4cd2e33 100644 --- a/Documentation/mm/transhuge.rst +++ b/Documentation/mm/transhuge.rst @@ -116,23 +116,28 @@ pages: succeeds on tail pages. =20 - map/unmap of a PMD entry for the whole THP increment/decrement - folio->_entire_mapcount, increment/decrement folio->_large_mapcount - and also increment/decrement folio->_nr_pages_mapped by ENTIRELY_MAPPED - when _entire_mapcount goes from -1 to 0 or 0 to -1. + folio->_entire_mapcount and folio->_large_mapcount. =20 We also maintain the two slots for tracking MM owners (MM ID and corresponding mapcount), and the current status ("maybe mapped shared"= vs. "mapped exclusively"). =20 + With CONFIG_PAGE_MAPCOUNT, we also increment/decrement + folio->_nr_pages_mapped by ENTIRELY_MAPPED when _entire_mapcount goes + from -1 to 0 or 0 to -1. + - map/unmap of individual pages with PTE entry increment/decrement - page->_mapcount, increment/decrement folio->_large_mapcount and also - increment/decrement folio->_nr_pages_mapped when page->_mapcount goes - from -1 to 0 or 0 to -1 as this counts the number of pages mapped by P= TE. + folio->_large_mapcount. =20 We also maintain the two slots for tracking MM owners (MM ID and corresponding mapcount), and the current status ("maybe mapped shared"= vs. "mapped exclusively"). =20 + With CONFIG_PAGE_MAPCOUNT, we also increment/decrement + page->_mapcount and increment/decrement folio->_nr_pages_mapped when + page->_mapcount goes from -1 to 0 or 0 to -1 as this counts the number + of pages mapped by PTE. + split_huge_page internally has to distribute the refcounts in the head page to the tail pages before clearing all PG_head/tail bits from the page structures. It can be done easily for refcounts taken by page table @@ -159,8 +164,8 @@ clear where references should go after split: it will s= tay on the head page. Note that split_huge_pmd() doesn't have any limitations on refcounting: pmd can be split at any point and never fails. =20 -Partial unmap and deferred_split_folio() -=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Partial unmap and deferred_split_folio() (anon THP only) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D =20 Unmapping part of THP (with munmap() or other way) is not going to free memory immediately. Instead, we detect that a subpage of THP is not in use @@ -175,3 +180,13 @@ a THP crosses a VMA boundary. The function deferred_split_folio() is used to queue a folio for splitting. The splitting itself will happen when we get memory pressure via shrinker interface. + +With CONFIG_PAGE_MAPCOUNT, we reliably detect partial mappings based on +folio->_nr_pages_mapped. + +With CONFIG_NO_PAGE_MAPCOUNT, we detect partial mappings based on the +average per-page mapcount in a THP: if the average is < 1, an anon THP is +certainly partially mapped. As long as only a single process maps a THP, +this detection is reliable. With long-running child processes, there can +be scenarios where partial mappings can currently not be detected, and +might need asynchronous detection during memory reclaim in the future. diff --git a/include/linux/rmap.h b/include/linux/rmap.h index c131b0efff0fa..6b82b618846ee 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -240,7 +240,7 @@ static __always_inline void folio_set_large_mapcount(st= ruct folio *folio, folio_set_mm_id(folio, 0, vma->vm_mm->mm_id); } =20 -static __always_inline void folio_add_large_mapcount(struct folio *folio, +static __always_inline int folio_add_return_large_mapcount(struct folio *f= olio, int diff, struct vm_area_struct *vma) { const mm_id_t mm_id =3D vma->vm_mm->mm_id; @@ -286,9 +286,11 @@ static __always_inline void folio_add_large_mapcount(s= truct folio *folio, folio->_mm_ids |=3D FOLIO_MM_IDS_SHARED_BIT; } folio_unlock_large_mapcount(folio); + return new_mapcount_val + 1; } +#define folio_add_large_mapcount folio_add_return_large_mapcount =20 -static __always_inline void folio_sub_large_mapcount(struct folio *folio, +static __always_inline int folio_sub_return_large_mapcount(struct folio *f= olio, int diff, struct vm_area_struct *vma) { const mm_id_t mm_id =3D vma->vm_mm->mm_id; @@ -331,7 +333,9 @@ static __always_inline void folio_sub_large_mapcount(st= ruct folio *folio, folio->_mm_ids &=3D ~FOLIO_MM_IDS_SHARED_BIT; out: folio_unlock_large_mapcount(folio); + return new_mapcount_val + 1; } +#define folio_sub_large_mapcount folio_sub_return_large_mapcount #else /* !CONFIG_MM_ID */ /* * See __folio_rmap_sanity_checks(), we might map large folios even without @@ -350,17 +354,33 @@ static inline void folio_add_large_mapcount(struct fo= lio *folio, atomic_add(diff, &folio->_large_mapcount); } =20 +static inline int folio_add_return_large_mapcount(struct folio *folio, + int diff, struct vm_area_struct *vma) +{ + BUILD_BUG(); +} + static inline void folio_sub_large_mapcount(struct folio *folio, int diff, struct vm_area_struct *vma) { atomic_sub(diff, &folio->_large_mapcount); } + +static inline int folio_sub_return_large_mapcount(struct folio *folio, + int diff, struct vm_area_struct *vma) +{ + BUILD_BUG(); +} #endif /* CONFIG_MM_ID */ =20 #define folio_inc_large_mapcount(folio, vma) \ folio_add_large_mapcount(folio, 1, vma) +#define folio_inc_return_large_mapcount(folio, vma) \ + folio_add_return_large_mapcount(folio, 1, vma) #define folio_dec_large_mapcount(folio, vma) \ folio_sub_large_mapcount(folio, 1, vma) +#define folio_dec_return_large_mapcount(folio, vma) \ + folio_sub_return_large_mapcount(folio, 1, vma) =20 /* RMAP flags, currently only relevant for some anon rmap operations. */ typedef int __bitwise rmap_t; @@ -538,9 +558,11 @@ static __always_inline void __folio_dup_file_rmap(stru= ct folio *folio, break; } =20 - do { - atomic_inc(&page->_mapcount); - } while (page++, --nr_pages > 0); + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) { + do { + atomic_inc(&page->_mapcount); + } while (page++, --nr_pages > 0); + } folio_add_large_mapcount(folio, orig_nr_pages, dst_vma); break; case RMAP_LEVEL_PMD: @@ -638,7 +660,8 @@ static __always_inline int __folio_try_dup_anon_rmap(st= ruct folio *folio, do { if (PageAnonExclusive(page)) ClearPageAnonExclusive(page); - atomic_inc(&page->_mapcount); + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) + atomic_inc(&page->_mapcount); } while (page++, --nr_pages > 0); folio_add_large_mapcount(folio, orig_nr_pages, dst_vma); break; diff --git a/mm/internal.h b/mm/internal.h index 7303ddd9dac04..7a4f81a6edd66 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -84,6 +84,8 @@ void page_writeback_init(void); */ static inline int folio_nr_pages_mapped(const struct folio *folio) { + if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) + return -1; return atomic_read(&folio->_nr_pages_mapped) & FOLIO_PAGES_MAPPED; } =20 @@ -721,7 +723,8 @@ static inline void prep_compound_head(struct page *page= , unsigned int order) =20 folio_set_order(folio, order); atomic_set(&folio->_large_mapcount, -1); - atomic_set(&folio->_nr_pages_mapped, 0); + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) + atomic_set(&folio->_nr_pages_mapped, 0); if (IS_ENABLED(CONFIG_MM_ID)) { folio->_mm_ids =3D 0; folio->_mm_id_mapcount[0] =3D -1; diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 08caa92553998..4402672b5d838 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -951,7 +951,8 @@ static int free_tail_page_prepare(struct page *head_pag= e, struct page *page) bad_page(page, "nonzero large_mapcount"); goto out; } - if (unlikely(atomic_read(&folio->_nr_pages_mapped))) { + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT) && + unlikely(atomic_read(&folio->_nr_pages_mapped))) { bad_page(page, "nonzero nr_pages_mapped"); goto out; } diff --git a/mm/rmap.c b/mm/rmap.c index 8de415157bc8d..67bb273dfb80d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1258,6 +1258,16 @@ static __always_inline unsigned int __folio_add_rmap= (struct folio *folio, break; } =20 + if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) { + nr =3D folio_add_return_large_mapcount(folio, orig_nr_pages, vma); + if (nr =3D=3D orig_nr_pages) + /* Was completely unmapped. */ + nr =3D folio_large_nr_pages(folio); + else + nr =3D 0; + break; + } + do { first +=3D atomic_inc_and_test(&page->_mapcount); } while (page++, --nr_pages > 0); @@ -1271,6 +1281,18 @@ static __always_inline unsigned int __folio_add_rmap= (struct folio *folio, case RMAP_LEVEL_PMD: case RMAP_LEVEL_PUD: first =3D atomic_inc_and_test(&folio->_entire_mapcount); + if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) { + if (level =3D=3D RMAP_LEVEL_PMD && first) + *nr_pmdmapped =3D folio_large_nr_pages(folio); + nr =3D folio_inc_return_large_mapcount(folio, vma); + if (nr =3D=3D 1) + /* Was completely unmapped. */ + nr =3D folio_large_nr_pages(folio); + else + nr =3D 0; + break; + } + if (first) { nr =3D atomic_add_return_relaxed(ENTIRELY_MAPPED, mapped); if (likely(nr < ENTIRELY_MAPPED + ENTIRELY_MAPPED)) { @@ -1436,13 +1458,23 @@ static __always_inline void __folio_add_anon_rmap(s= truct folio *folio, break; } } + + VM_WARN_ON_FOLIO(!folio_test_large(folio) && PageAnonExclusive(page) && + atomic_read(&folio->_mapcount) > 0, folio); for (i =3D 0; i < nr_pages; i++) { struct page *cur_page =3D page + i; =20 - /* While PTE-mapping a THP we have a PMD and a PTE mapping. */ - VM_WARN_ON_FOLIO((atomic_read(&cur_page->_mapcount) > 0 || - (folio_test_large(folio) && - folio_entire_mapcount(folio) > 1)) && + VM_WARN_ON_FOLIO(folio_test_large(folio) && + folio_entire_mapcount(folio) > 1 && + PageAnonExclusive(cur_page), folio); + if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) + continue; + + /* + * While PTE-mapping a THP we have a PMD and a PTE + * mapping. + */ + VM_WARN_ON_FOLIO(atomic_read(&cur_page->_mapcount) > 0 && PageAnonExclusive(cur_page), folio); } =20 @@ -1548,20 +1580,23 @@ void folio_add_new_anon_rmap(struct folio *folio, s= truct vm_area_struct *vma, for (i =3D 0; i < nr; i++) { struct page *page =3D folio_page(folio, i); =20 - /* increment count (starts at -1) */ - atomic_set(&page->_mapcount, 0); + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) + /* increment count (starts at -1) */ + atomic_set(&page->_mapcount, 0); if (exclusive) SetPageAnonExclusive(page); } =20 folio_set_large_mapcount(folio, nr, vma); - atomic_set(&folio->_nr_pages_mapped, nr); + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) + atomic_set(&folio->_nr_pages_mapped, nr); } else { nr =3D folio_large_nr_pages(folio); /* increment count (starts at -1) */ atomic_set(&folio->_entire_mapcount, 0); folio_set_large_mapcount(folio, 1, vma); - atomic_set(&folio->_nr_pages_mapped, ENTIRELY_MAPPED); + if (IS_ENABLED(CONFIG_PAGE_MAPCOUNT)) + atomic_set(&folio->_nr_pages_mapped, ENTIRELY_MAPPED); if (exclusive) SetPageAnonExclusive(&folio->page); nr_pmdmapped =3D nr; @@ -1665,6 +1700,19 @@ static __always_inline void __folio_remove_rmap(stru= ct folio *folio, break; } =20 + if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) { + nr =3D folio_sub_return_large_mapcount(folio, nr_pages, vma); + if (!nr) { + /* Now completely unmapped. */ + nr =3D folio_nr_pages(folio); + } else { + partially_mapped =3D nr < folio_large_nr_pages(folio) && + !folio_entire_mapcount(folio); + nr =3D 0; + } + break; + } + folio_sub_large_mapcount(folio, nr_pages, vma); do { last +=3D atomic_add_negative(-1, &page->_mapcount); @@ -1678,6 +1726,22 @@ static __always_inline void __folio_remove_rmap(stru= ct folio *folio, break; case RMAP_LEVEL_PMD: case RMAP_LEVEL_PUD: + if (IS_ENABLED(CONFIG_NO_PAGE_MAPCOUNT)) { + last =3D atomic_add_negative(-1, &folio->_entire_mapcount); + if (level =3D=3D RMAP_LEVEL_PMD && last) + nr_pmdmapped =3D folio_large_nr_pages(folio); + nr =3D folio_dec_return_large_mapcount(folio, vma); + if (!nr) { + /* Now completely unmapped. */ + nr =3D folio_large_nr_pages(folio); + } else { + partially_mapped =3D last && + nr < folio_large_nr_pages(folio); + nr =3D 0; + } + break; + } + folio_dec_large_mapcount(folio, vma); last =3D atomic_add_negative(-1, &folio->_entire_mapcount); if (last) { --=20 2.48.1