From patchwork Wed Sep 13 12:51:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13382963 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 CD4B1EDEC4D for ; Wed, 13 Sep 2023 12:51:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C6216B0186; Wed, 13 Sep 2023 08:51:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3FD766B0187; Wed, 13 Sep 2023 08:51:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 277DC6B0188; Wed, 13 Sep 2023 08:51:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0C39B6B0186 for ; Wed, 13 Sep 2023 08:51:32 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DE9C31A0A62 for ; Wed, 13 Sep 2023 12:51:31 +0000 (UTC) X-FDA: 81231560382.11.0F96603 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 2BFC2180019 for ; Wed, 13 Sep 2023 12:51:29 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iC+mDOYS; spf=pass (imf24.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694609490; 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=TPywS+lMvCdRAxxYldVBzyiy9uXPhmFWj9jbUK5aT08=; b=XoLDUhazgcHEb9azsVdSRmzVtn/PqGBJA1SXTo+EimSpPrDdphc4+i/fdQt04L6Sm+fuRV 1YZaOfIeGZ8hrWpwzKLgMokVJXocX7mBVfEy1cKrvzbe5uQnVdm8GomATOkwx8HeT1D6jL tupbupDGNU4krpSnxmQMjRtT3O2U8wc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iC+mDOYS; spf=pass (imf24.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694609490; a=rsa-sha256; cv=none; b=nz1ePfcKfIj2/SW3Uma/9QLNeyoAdvLdxz0oeyWESUVAIkQOL+od5CE516gaIMLgSz8uov 8GpiLe1D99vHzsCa+rhmTWOhiuA8oI/ufFULfdnaxjBr4glKYpcGTwCe75RmoS/0WR0OZt AEH+5RABXxmLWHkwedxC4eLBosMolRY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694609489; 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=TPywS+lMvCdRAxxYldVBzyiy9uXPhmFWj9jbUK5aT08=; b=iC+mDOYSGZ2M5QSMV9oPoL/zrYTcP94U71W9OrHfEl8JkrgkSnKP6dv94lvBAp0ieQN/+U sA63yt5NCLkJNushvUiQKEkOlZbdtWiPtkX0fLPKIaaRAqO2i723do8rgxznnyXK++pV/X D7AJ1LwGQ+T6BwXJOWnsJW1i7xBOlbM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-41-FbYiyCTvMFu_-aiiWy3VKA-1; Wed, 13 Sep 2023 08:51:24 -0400 X-MC-Unique: FbYiyCTvMFu_-aiiWy3VKA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BD850800045; Wed, 13 Sep 2023 12:51:23 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id A749F21B2414; Wed, 13 Sep 2023 12:51:21 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Mike Kravetz , Muchun Song Subject: [PATCH v1 3/6] mm/rmap: move folio_test_anon() check out of __folio_set_anon() Date: Wed, 13 Sep 2023 14:51:10 +0200 Message-ID: <20230913125113.313322-4-david@redhat.com> In-Reply-To: <20230913125113.313322-1-david@redhat.com> References: <20230913125113.313322-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspamd-Queue-Id: 2BFC2180019 X-Rspam-User: X-Stat-Signature: 9mp59qyf4ko63oxqbz7yzgbnih39ndp4 X-Rspamd-Server: rspam01 X-HE-Tag: 1694609489-695569 X-HE-Meta: U2FsdGVkX18CEh/IZNV/krAwlcQmDzoeAz5FX2XrL9h9PDrpHOrK02B4D7dfSI6mY22KWdDrAhwG7oWjT0I+eg0FvpUcaR/pz/XJ5U38uRbfJv9/Z0v7Tl2Nnxsd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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: Let's handle it in the caller; no need for the "first" check based on the mapcount. We really only end up with !anon pages in page_add_anon_rmap() via do_swap_page(), where we hold the folio lock. So races are not possible. Add a VM_WARN_ON_FOLIO() to make sure that we really hold the folio lock. In the future, we might want to let do_swap_page() use folio_add_new_anon_rmap() on new pages instead: however, we might have to pass then whether the folio is exclusive or not. So keep it in there for now. For hugetlb we never expect to have a non-anon page in hugepage_add_anon_rmap(). Remove that code, along with some other checks that are either not required or were checked in hugepage_add_new_anon_rmap() already. Signed-off-by: David Hildenbrand --- mm/rmap.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index ab16baa0fcb3..1ac5bd1b8169 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1135,9 +1135,6 @@ static void __folio_set_anon(struct folio *folio, struct vm_area_struct *vma, BUG_ON(!anon_vma); - if (folio_test_anon(folio)) - return; - /* * If the folio isn't exclusive to this vma, we must use the _oldest_ * possible anon_vma for the folio mapping! @@ -1239,12 +1236,12 @@ void page_add_anon_rmap(struct page *page, struct vm_area_struct *vma, if (nr) __lruvec_stat_mod_folio(folio, NR_ANON_MAPPED, nr); - if (likely(!folio_test_ksm(folio))) { - if (first) - __folio_set_anon(folio, vma, address, - !!(flags & RMAP_EXCLUSIVE)); - else - __page_check_anon_rmap(folio, page, vma, address); + if (unlikely(!folio_test_anon(folio))) { + VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); + __folio_set_anon(folio, vma, address, + !!(flags & RMAP_EXCLUSIVE)); + } else if (likely(!folio_test_ksm(folio))) { + __page_check_anon_rmap(folio, page, vma, address); } if (flags & RMAP_EXCLUSIVE) SetPageAnonExclusive(page); @@ -2528,17 +2525,13 @@ void hugepage_add_anon_rmap(struct page *page, struct vm_area_struct *vma, unsigned long address, rmap_t flags) { struct folio *folio = page_folio(page); - struct anon_vma *anon_vma = vma->anon_vma; int first; - BUG_ON(!folio_test_locked(folio)); - BUG_ON(!anon_vma); + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); + first = atomic_inc_and_test(&folio->_entire_mapcount); VM_BUG_ON_PAGE(!first && (flags & RMAP_EXCLUSIVE), page); VM_BUG_ON_PAGE(!first && PageAnonExclusive(page), page); - if (first) - __folio_set_anon(folio, vma, address, - !!(flags & RMAP_EXCLUSIVE)); if (flags & RMAP_EXCLUSIVE) SetPageAnonExclusive(page); }