Received: by 2002:a05:7412:a9a8:b0:f9:92ae:e617 with SMTP id o40csp24591rdh; Wed, 20 Dec 2023 14:49:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IEYCW931QzlT8S3RYyQZ48kBnU2yiDt9wshqURojZ28mXwMloYQqO0bzPliaAkSXiqpE4bU X-Received: by 2002:a05:6808:124a:b0:3b8:b063:6bb1 with SMTP id o10-20020a056808124a00b003b8b0636bb1mr24801006oiv.96.1703112576354; Wed, 20 Dec 2023 14:49:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703112576; cv=none; d=google.com; s=arc-20160816; b=z3bCoMHp815OjVOtHT9e/fBstDHK/eSM6wI63mQYRGnsD+L346TkxA4SppnY4B4wmv P02rlyof9fx/R/Hefckh1lWpYphrKRtUjm5z4VSwCU01vYEF0AFZMJ2FuEnfdjPjuuSb cvdVph2ycUeDVZpoOIoeq02XKVu8Cxzs7P6cd8B8XclHymKRWKQ8U4/as+kLHW/qW9MV ZUCEU7NMCOTNnW0gfFwVc9E1YtJuxzRbzP1EKUC/0/BjcNsG/1p5dfW/BWBV3TkWG7ax womO4hVObci8GjoKRFarOM+Ii1cpjsiUC02xdbw+rKjGV3Azv5Cti+iyPbdfor8X4yP7 iWpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Pjqwl2otIfh4GV5tfgKtj45x9lDmjEhBWckBey16zKE=; fh=UlFXU+hKOMcm6idRzNfe3Pu+B5L1f7aeir3C6i/NWQA=; b=zbcq7JdSBhMWvol0fz98ytGevXwnvqt5i4INilzA3bps+oiYeXVKoASSF6uwLpvzfV yk4UlVPLhz5FCCU+vSdDEpP2dERBqqlIvlMRbWFDW1TkOAPMdyUeWEzU15DCxWO20IAI jSjaIgkuJarXCX52n+q9+ruP5pbOjwAcpSgUyv3ixOq/DRthb+xd4ab5AMfYBZYg5l3R y7L81RzERR9dye8DEHiKim8itJfbmxs+Ezozv+pYxkgWsu0nZ1HjowzRicZIgIVGLqOi Dmc7SDAW0P4g81bnBC1dw+H0QrfaYA5iltaSZ3P8h6zD05K07Vc0h2r8KbaM9OHXSJuE VijQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PGmg6der; spf=pass (google.com: domain of linux-kernel+bounces-7538-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7538-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id b24-20020a639318000000b005cd8707f85dsi423956pge.854.2023.12.20.14.49.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 14:49:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7538-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PGmg6der; spf=pass (google.com: domain of linux-kernel+bounces-7538-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7538-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id DF899B229FF for ; Wed, 20 Dec 2023 22:48:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77B594D59C; Wed, 20 Dec 2023 22:45:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PGmg6der" X-Original-To: linux-kernel@vger.kernel.org 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 4973E4D590 for ; Wed, 20 Dec 2023 22:45:47 +0000 (UTC) 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703112346; 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=Pjqwl2otIfh4GV5tfgKtj45x9lDmjEhBWckBey16zKE=; b=PGmg6derIDtEEDBlAoqjfM7oyAk+wPSxiy2flCbT4jezi8eb5GjaDxU0scxjSS+ZOSIcE1 2jeK8hwqlHLugmwuAjCAc7r1gx4xnpy8n/mXCySod2D2DoUF5KFRxOJA9S0oUv+d8dvtHB DaT8FEaHuSmVAtdoNtcT2LqBipxOxqw= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-581-jJV6QouwMIO0ofo9VqPsZw-1; Wed, 20 Dec 2023 17:45:42 -0500 X-MC-Unique: jJV6QouwMIO0ofo9VqPsZw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 A8CCB3C23FD0; Wed, 20 Dec 2023 22:45:41 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.101]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7129740C6EB9; Wed, 20 Dec 2023 22:45:39 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Hugh Dickins , Ryan Roberts , Yin Fengwei , Mike Kravetz , Muchun Song , Peter Xu Subject: [PATCH v2 13/40] mm/rmap: factor out adding folio mappings into __folio_add_rmap() Date: Wed, 20 Dec 2023 23:44:37 +0100 Message-ID: <20231220224504.646757-14-david@redhat.com> In-Reply-To: <20231220224504.646757-1-david@redhat.com> References: <20231220224504.646757-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: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 Let's factor it out to prepare for reuse as we convert page_add_anon_rmap() to folio_add_anon_rmap_[pte|ptes|pmd](). Make the compiler always special-case on the granularity by using __always_inline. Reviewed-by: Yin Fengwei Signed-off-by: David Hildenbrand --- mm/rmap.c | 78 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 5ab5ef10fbf5e..895a8534a935d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1157,6 +1157,48 @@ int folio_total_mapcount(struct folio *folio) return mapcount; } +static __always_inline unsigned int __folio_add_rmap(struct folio *folio, + struct page *page, int nr_pages, enum rmap_level level, + int *nr_pmdmapped) +{ + atomic_t *mapped = &folio->_nr_pages_mapped; + int first, nr = 0; + + __folio_rmap_sanity_checks(folio, page, nr_pages, level); + + switch (level) { + case RMAP_LEVEL_PTE: + do { + first = atomic_inc_and_test(&page->_mapcount); + if (first && folio_test_large(folio)) { + first = atomic_inc_return_relaxed(mapped); + first = (first < COMPOUND_MAPPED); + } + + if (first) + nr++; + } while (page++, --nr_pages > 0); + break; + case RMAP_LEVEL_PMD: + first = atomic_inc_and_test(&folio->_entire_mapcount); + if (first) { + nr = atomic_add_return_relaxed(COMPOUND_MAPPED, mapped); + if (likely(nr < COMPOUND_MAPPED + COMPOUND_MAPPED)) { + *nr_pmdmapped = folio_nr_pages(folio); + nr = *nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); + /* Raced ahead of a remove and another add? */ + if (unlikely(nr < 0)) + nr = 0; + } else { + /* Raced ahead of a remove of COMPOUND_MAPPED */ + nr = 0; + } + } + break; + } + return nr; +} + /** * folio_move_anon_rmap - move a folio to our anon_vma * @folio: The folio to move to our anon_vma @@ -1382,43 +1424,11 @@ static __always_inline void __folio_add_file_rmap(struct folio *folio, struct page *page, int nr_pages, struct vm_area_struct *vma, enum rmap_level level) { - atomic_t *mapped = &folio->_nr_pages_mapped; - int nr = 0, nr_pmdmapped = 0, first; + int nr, nr_pmdmapped = 0; VM_WARN_ON_FOLIO(folio_test_anon(folio), folio); - __folio_rmap_sanity_checks(folio, page, nr_pages, level); - - switch (level) { - case RMAP_LEVEL_PTE: - do { - first = atomic_inc_and_test(&page->_mapcount); - if (first && folio_test_large(folio)) { - first = atomic_inc_return_relaxed(mapped); - first = (first < COMPOUND_MAPPED); - } - - if (first) - nr++; - } while (page++, --nr_pages > 0); - break; - case RMAP_LEVEL_PMD: - first = atomic_inc_and_test(&folio->_entire_mapcount); - if (first) { - nr = atomic_add_return_relaxed(COMPOUND_MAPPED, mapped); - if (likely(nr < COMPOUND_MAPPED + COMPOUND_MAPPED)) { - nr_pmdmapped = folio_nr_pages(folio); - nr = nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); - /* Raced ahead of a remove and another add? */ - if (unlikely(nr < 0)) - nr = 0; - } else { - /* Raced ahead of a remove of COMPOUND_MAPPED */ - nr = 0; - } - } - break; - } + nr = __folio_add_rmap(folio, page, nr_pages, level, &nr_pmdmapped); if (nr_pmdmapped) __lruvec_stat_mod_folio(folio, folio_test_swapbacked(folio) ? NR_SHMEM_PMDMAPPED : NR_FILE_PMDMAPPED, nr_pmdmapped); -- 2.43.0