From patchwork Fri Nov 24 13:26:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13467667 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 B7F30C61D97 for ; Fri, 24 Nov 2023 13:27:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5481D8D0082; Fri, 24 Nov 2023 08:27:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D18C8D006E; Fri, 24 Nov 2023 08:27:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FD388D0082; Fri, 24 Nov 2023 08:27:30 -0500 (EST) 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 1874A8D006E for ; Fri, 24 Nov 2023 08:27:30 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E2461A0379 for ; Fri, 24 Nov 2023 13:27:29 +0000 (UTC) X-FDA: 81492924618.14.5070BEF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 29B0F8001E for ; Fri, 24 Nov 2023 13:27:27 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TYfau4I+; spf=pass (imf30.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=1700832448; 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=8wkdTzr4ovr6JqvWUuBFMPrmTwPV2fAdlmREPfnLqjk=; b=g01SjQ1ZgOi+s/JAk3KfTSmBlUw+sUJ+CZqAuG2kwx/IYIl1VdkJxOcZolLJnNfqx7zaMZ PNuqjSi8FSyThfpYOEfI9FinDKGNoF/WIIgub6w+Ftftpvfh+SPVauJoEb+DCwwARNCGi7 pJIAqXpPz+zT8jKbe8wHWgkct2sn4Ww= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700832448; a=rsa-sha256; cv=none; b=4vUTmp2XjgDdXGNcI7PLuowBLW9W0VHa12LIqp5L4nCT0NZ+NaWT1ZLYKAOHV7XcoNTSAT LNEx43EPBDnXJvAIbgsa+Uu7+TIN2hW1noJpg7dOT5rkmgOuEaLEf48UpnqabEWZzPPyj1 am3+MYPbHgrjhfoPflROayM9p7cceP4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=TYfau4I+; spf=pass (imf30.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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700832447; 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=8wkdTzr4ovr6JqvWUuBFMPrmTwPV2fAdlmREPfnLqjk=; b=TYfau4I+oMaNyqwKaqIB55GH8yEKtlFTQ4NL9nDH7yNom0puCSrQ6hoIl05J4g3ooDXu5J PAL7k+qDMVfRG+0z7BnltbPpsSr/y+ni2OzXde5wrpBVW20OvLy+ycsL9TdhgnBK5NtxZ0 FNe4+zlhtLvspvY5SO2EqRpuM6Sk2pY= 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-631-Mzyg-U9HMR2q8VnSaYE9mw-1; Fri, 24 Nov 2023 08:27:23 -0500 X-MC-Unique: Mzyg-U9HMR2q8VnSaYE9mw-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 7F79C85A58C; Fri, 24 Nov 2023 13:27:22 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id E251F2166B2B; Fri, 24 Nov 2023 13:27:18 +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 14/20] mm/huge_memory: avoid folio_refcount() < folio_mapcount() in __split_huge_pmd_locked() Date: Fri, 24 Nov 2023 14:26:19 +0100 Message-ID: <20231124132626.235350-15-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-Queue-Id: 29B0F8001E X-Rspam-User: X-Stat-Signature: uzfdowpy8qyk5h8w5tipogig8bf4tt9t X-Rspamd-Server: rspam03 X-HE-T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7mf2X7b/CwAb+k4vqnqugrtyWHFag/9yPlHz58yW/il837K3TeJTJumgQswdtd9BTARtVqnMWYOYa0CgU4qLnr+h+ZBHNXHPkPZRdHmk/CJdRkXJxz5+LKFawSRA7jvXPWlWe+ZNTB1cZm0qNO1wLO8Q3Agma58hLkeoaNQ4bkSCwDuR26+lr/Iys6M6dMedqgMm8CsTgM= 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: Currently, there is a short period in time where the refcount is smaller than the mapcount. Let's just make sure we obey the rules of refcount vs. mapcount: increment the refcount before incrementing the mapcount and decrement the refcount after decrementing the mapcount. While this could make code like can_split_folio() fail to detect other folio references, such code is (currently) racy already and this change shouldn't actually be considered a real fix but rather an improvement/ cleanup. The refcount vs. mapcount changes are now well balanced in the code, with the cost of one additional refcount change, which really shouldn't matter here that much -- we're usually touching >= 512 subpage mapcounts and much more after all. Found while playing with some sanity checks to detect such cases, which we might add at some later point. Signed-off-by: David Hildenbrand --- mm/huge_memory.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f47971d1afbf..9639b4edc8a5 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2230,7 +2230,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, if (!freeze) { rmap_t rmap_flags = RMAP_NONE; - folio_ref_add(folio, HPAGE_PMD_NR - 1); + folio_ref_add(folio, HPAGE_PMD_NR); if (anon_exclusive) rmap_flags = RMAP_EXCLUSIVE; folio_add_anon_rmap_range(folio, page, HPAGE_PMD_NR, @@ -2294,10 +2294,10 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, } pte_unmap(pte - 1); - if (!pmd_migration) + if (!pmd_migration) { page_remove_rmap(page, vma, true); - if (freeze) put_page(page); + } smp_wmb(); /* make pte visible before pmd */ pmd_populate(mm, pmd, pgtable);