From patchwork Fri Nov 24 13:26:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13467672 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 08AB0C636CB for ; Fri, 24 Nov 2023 13:27:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 177BB8D0087; Fri, 24 Nov 2023 08:27:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DCA48D0084; Fri, 24 Nov 2023 08:27:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E70C98D0087; Fri, 24 Nov 2023 08:27:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id CAA548D0084 for ; Fri, 24 Nov 2023 08:27:44 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A48BF8025B for ; Fri, 24 Nov 2023 13:27:44 +0000 (UTC) X-FDA: 81492925248.16.83EADA8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf05.hostedemail.com (Postfix) with ESMTP id 05341100009 for ; Fri, 24 Nov 2023 13:27:42 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Hizn47st; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf05.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700832463; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BnEHwgivpdbaZgh54Ih7L1AobW4tDWwZEN96Ch5d2sI=; b=aXTuN9CTz8/Ag01FQj/S9NSnyP4Hjv9vcJZLQas6ErPZHYfOKbvOkFls05bU+CP6OEZrRW /4VWwV/ojutrxX01cwgIT13E7TyGTwSd3PzTXPqpAAqKIqJ6QUx25XW+4gzHAfff9mabmj d50eaVvS5xwDIOjuv1JgtDXEVIv+tVk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Hizn47st; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf05.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700832463; a=rsa-sha256; cv=none; b=wSq/VPlECxJYYCxdPjIXuWaDeQ8s/5EiecU9jxwy6ubGnffaPe9pe/juLjCBykFzEhVG/I JwJBNva9CCUbz/QgrV6qu8t1tjKgLLyG0vDdZ1m+VeSVLUxt1+ILicd7Oigne1g5xUAN0W uZPDq6OArRy1CPQP+YAC18amtYKly0s= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700832462; 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=BnEHwgivpdbaZgh54Ih7L1AobW4tDWwZEN96Ch5d2sI=; b=Hizn47stOY0bjcCAapanjWMo1DctKX6iH+q4hCiCWA0XhmT2hRBOUWfHfCXBdsEZQQsJiU PwgKXtiLA4TJss8Dutnh2w5T8tDYEclkU4NKPAOWGGLBOp0UWz2wZUCxxhYje3Xa4IQL8/ TGJ7vu7hxWsuWfYCnpXM3IRr5scvSJ8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-542-EU4DmTN2O1ar0sjJ8gqpbQ-1; Fri, 24 Nov 2023 08:27:41 -0500 X-MC-Unique: EU4DmTN2O1ar0sjJ8gqpbQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3AD05185A783; Fri, 24 Nov 2023 13:27:40 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id 550AE2166B2A; Fri, 24 Nov 2023 13:27:37 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Linus Torvalds , Ryan Roberts , Matthew Wilcox , Hugh Dickins , Yin Fengwei , Yang Shi , Ying Huang , Zi Yan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , "Paul E. McKenney" Subject: [PATCH WIP v1 19/20] mm/rmap: factor out removing folio range into __folio_remove_rmap_range() Date: Fri, 24 Nov 2023 14:26:24 +0100 Message-ID: <20231124132626.235350-20-david@redhat.com> In-Reply-To: <20231124132626.235350-1-david@redhat.com> References: <20231124132626.235350-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 05341100009 X-Stat-Signature: rooe8jmwpmz1e7wgutdjhk7zz5xczse3 X-Rspam-User: X-HE-Tag: 1700832462-979557 X-HE-Meta: U2FsdGVk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naDi8xX/wlO1W46zSQkIwnNvNgeMTXjDBNq2d9b/gdZpFvDjEYRetsUxPVrAzmXv0tEC9dmHW9O9uPYAkvSUMR7NciaDu1D0liRqiVEoELH5Zb72XFQKOKscuK7 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Let's factor it out, optimize for small folios, and compact it a bit. Well, we're adding the range part, but that will surely come in handy soon -- and it's now wasier to compare it with __folio_add_rmap_range(). Signed-off-by: David Hildenbrand --- mm/rmap.c | 90 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index da7fa46a18fc..80ac53633332 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1155,6 +1155,57 @@ static unsigned int __folio_add_rmap_range(struct folio *folio, return nr; } +static unsigned int __folio_remove_rmap_range(struct folio *folio, + struct page *page, unsigned int nr_pages, + struct vm_area_struct *vma, bool compound, int *nr_pmdmapped) +{ + atomic_t *mapped = &folio->_nr_pages_mapped; + int last, count, nr = 0; + + VM_WARN_ON_FOLIO(compound && page != &folio->page, folio); + VM_WARN_ON_FOLIO(compound && !folio_test_pmd_mappable(folio), folio); + VM_WARN_ON_FOLIO(compound && nr_pages != folio_nr_pages(folio), folio); + VM_WARN_ON_FOLIO(!folio_test_large(folio) && nr_pages != 1, folio); + + if (likely(!folio_test_large(folio))) + return atomic_add_negative(-1, &page->_mapcount); + + /* Is page being unmapped by PTE? Is this its last map to be removed? */ + if (!compound) { + folio_add_large_mapcount(folio, -nr_pages, vma); + count = nr_pages; + do { + last = atomic_add_negative(-1, &page->_mapcount); + if (last) { + last = atomic_dec_return_relaxed(mapped); + if (last < COMPOUND_MAPPED) + nr++; + } + } while (page++, --count > 0); + } else if (folio_test_pmd_mappable(folio)) { + /* That test is redundant: it's for safety or to optimize out */ + + folio_dec_large_mapcount(folio, vma); + last = atomic_add_negative(-1, &folio->_entire_mapcount); + if (last) { + nr = atomic_sub_return_relaxed(COMPOUND_MAPPED, mapped); + if (likely(nr < COMPOUND_MAPPED)) { + *nr_pmdmapped = folio_nr_pages(folio); + nr = *nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); + /* Raced ahead of another remove and an add? */ + if (unlikely(nr < 0)) + nr = 0; + } else { + /* An add of COMPOUND_MAPPED raced ahead */ + nr = 0; + } + } + } else { + VM_WARN_ON_ONCE_FOLIO(true, folio); + } + return nr; +} + /** * folio_move_anon_rmap - move a folio to our anon_vma * @folio: The folio to move to our anon_vma @@ -1439,13 +1490,10 @@ void page_remove_rmap(struct page *page, struct vm_area_struct *vma, bool compound) { struct folio *folio = page_folio(page); - atomic_t *mapped = &folio->_nr_pages_mapped; - int nr = 0, nr_pmdmapped = 0; - bool last; + unsigned long nr_pages = compound ? folio_nr_pages(folio) : 1; + unsigned int nr, nr_pmdmapped = 0; enum node_stat_item idx; - VM_BUG_ON_PAGE(compound && !PageHead(page), page); - /* Hugetlb pages are not counted in NR_*MAPPED */ if (unlikely(folio_test_hugetlb(folio))) { /* hugetlb pages are always mapped with pmds */ @@ -1454,36 +1502,8 @@ void page_remove_rmap(struct page *page, struct vm_area_struct *vma, return; } - if (folio_test_large(folio)) - folio_dec_large_mapcount(folio, vma); - - /* Is page being unmapped by PTE? Is this its last map to be removed? */ - if (likely(!compound)) { - last = atomic_add_negative(-1, &page->_mapcount); - nr = last; - if (last && folio_test_large(folio)) { - nr = atomic_dec_return_relaxed(mapped); - nr = (nr < COMPOUND_MAPPED); - } - } else if (folio_test_pmd_mappable(folio)) { - /* That test is redundant: it's for safety or to optimize out */ - - last = atomic_add_negative(-1, &folio->_entire_mapcount); - if (last) { - nr = atomic_sub_return_relaxed(COMPOUND_MAPPED, mapped); - if (likely(nr < COMPOUND_MAPPED)) { - nr_pmdmapped = folio_nr_pages(folio); - nr = nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); - /* Raced ahead of another remove and an add? */ - if (unlikely(nr < 0)) - nr = 0; - } else { - /* An add of COMPOUND_MAPPED raced ahead */ - nr = 0; - } - } - } - + nr = __folio_remove_rmap_range(folio, page, nr_pages, vma, compound, + &nr_pmdmapped); if (nr_pmdmapped) { if (folio_test_anon(folio)) idx = NR_ANON_THPS;