From patchwork Sun Jun 18 06:58:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13283772 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 7576FEB64D7 for ; Sun, 18 Jun 2023 06:58:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F20FD6B0078; Sun, 18 Jun 2023 02:58:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ED1698E0002; Sun, 18 Jun 2023 02:58:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D997E8E0001; Sun, 18 Jun 2023 02:58:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C78E06B0078 for ; Sun, 18 Jun 2023 02:58:14 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9F9E21603DD for ; Sun, 18 Jun 2023 06:58:14 +0000 (UTC) X-FDA: 80914964508.06.D8BB79D Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf21.hostedemail.com (Postfix) with ESMTP id E0BE31C0008 for ; Sun, 18 Jun 2023 06:58:12 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=d2187lUk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3A6uOZAoKCL42swv2elqihksskpi.gsqpmry1-qqozego.svk@flex--yosryahmed.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3A6uOZAoKCL42swv2elqihksskpi.gsqpmry1-qqozego.svk@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687071493; 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-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=7fjpxEmRYmfIPy9m8+B6O263cflpsiora2wxjSeJZzc=; b=nZblVoqaTlMJT1AkCxCSal16++/OEhwdeMG3+3Au9VSBP5yBC/6iqRLMJ5M6w6nQZ5rzFg ozK5nrhoN4KK3pButWex0egk5T+16J8O58l3VPgtkL/SikFBQUzW0eQmPj1B+g+7RX5M2O dizOEJ3p8OuM1KW78g3e3G4xjndyHZI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=d2187lUk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3A6uOZAoKCL42swv2elqihksskpi.gsqpmry1-qqozego.svk@flex--yosryahmed.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3A6uOZAoKCL42swv2elqihksskpi.gsqpmry1-qqozego.svk@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687071493; a=rsa-sha256; cv=none; b=egWNjx3WB0pu7q/WlSxx9S8meF7Z0suAC+9SovfQuOaZ6t3p0bo+lk4pBOnk8QL24HxRuB vIgm9wgas1GcV7qWzSCltL8/DsmB6QDJaB7vRqwSplOH1pjsk9Z4/+jsKJT0PWoen+JFJv U36EW3/mSViTTh5EveJofTh19kBNkAQ= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1b508a0873eso11930585ad.3 for ; Sat, 17 Jun 2023 23:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687071492; x=1689663492; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=7fjpxEmRYmfIPy9m8+B6O263cflpsiora2wxjSeJZzc=; b=d2187lUkhz1tQ0LdjKS/RdBcAInoS313wvVyAeapaGuheNx0zAMEP7j5tsde3Pp0ki JLy4R1+trXmmTvJcpQFdSwOcOoWzO74P8BuVHwFwEckpX5HpMtMVxrPYKjjQfQ24J7VO Q835WNmKxjc9DtFkftSG8+8pjhS98I9MXzG/PHODkUae5GHvtG2HidU7XWvjYdDqsGTB UZrvi6kDymzX4lmmYXUipM1X5APyR7LmwCmH4ChdN/SzeOfAhYnGUooORyYrnU8lzDzB US9GH11eoECaMcdkpDFewfWnZcdflNR0OzUJe7cHb8WLhYHIi48dpX5NwSXjwzQDhV86 h4MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687071492; x=1689663492; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=7fjpxEmRYmfIPy9m8+B6O263cflpsiora2wxjSeJZzc=; b=FoDeEBHy+GM+Jjh/71v6hNXXOUQ9x9ey3Dqj3WX+6C+fhTwXssNhT+jNgJQpOx6G3m qNzNsJYKW+adU7o2fMciigbBzFKd9zZColfe/BEcCa9gqoAPSvRG5rPxyyx5s/ER/K1v ATrzaGRKgJ+MRCNH9erwS64FnxLFOs8CVt/C3J07KPQexTVZOiUZQ+6OLOubv3Fy/wBC g8Ga0XqN+Zx47yWPzHnEI/GS1Rj8X15+DqLgEZRP96mAZiG8c7QQr37AFpO7fmXQmExD +n9TvJ7fklDWiZcZZ8tUgz9E8Wlj40LuwPJty47Rsrx5kX/Cakl93Q5vloBDZ+egB61V x1Ww== X-Gm-Message-State: AC+VfDwk2+ZT3UB3RMGR3qJiNqc07//wlZlMbzUOPoF/ofHP7gm/2EGX Sz3HbD7U0Zi/aV0o/FDmXUcDHf7eThtHygj1 X-Google-Smtp-Source: ACHHUZ4KDoG+icnmkKzKlDDiXY31YTjeGkcRrxhpySyxVPdutgNKWixbpYJugmDD23KR/VXk8geceEUZPGubfthm X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a17:902:bb10:b0:1b2:61eb:a73f with SMTP id im16-20020a170902bb1000b001b261eba73fmr881124plb.9.1687071491867; Sat, 17 Jun 2023 23:58:11 -0700 (PDT) Date: Sun, 18 Jun 2023 06:58:09 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230618065809.1364900-1-yosryahmed@google.com> Subject: [RFC PATCH 3/5] mm/mlock: WARN_ON() if mapcount overflows into mlock_count From: Yosry Ahmed To: Andrew Morton Cc: Yu Zhao , "Jan Alexander Steffens (heftig)" , Steven Barrett , Brian Geffon , "T.J. Alumbaugh" , Gaosheng Cui , Suren Baghdasaryan , "Matthew Wilcox (Oracle)" , "Liam R. Howlett" , David Hildenbrand , Jason Gunthorpe , Mathieu Desnoyers , David Howells , Hugh Dickins , Greg Thelen , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspam-User: X-Stat-Signature: 8re1jmobwsj3sbx4wr97zq15ysted547 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E0BE31C0008 X-HE-Tag: 1687071492-985794 X-HE-Meta: U2FsdGVkX19TW7peUFK6kS0jers1gU+ZC/ECk45nct78jm1Jb5NMzHMBwcX/gZyBCO14ifqflcvxOo13T/TMWmn5SYGulmu0UCHBHcdUBMmFfl2QA920CCM08b8sorlbMjF9KCmRl2ZVSXdPGoVGACPt856qdIpeIZQl7mfBnlaNVPlrAGyMrhEV0zxotzrfRQQrivaeXc4Mp/ekjmnnK5bmFztrwFe3temQYUNJqhFckGlwsIOEjjUn7Po3zXO7vvNXkCG3Ze5Q7xl4hueZSl2taUoiMuKuLyPWcmgsoflUTr8TjI3r23FbJSTiFOd8C8Mc0VevXNFqxOk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ojfj8/M6NfmlwvIj0INCJTnKVVX4Ejset9hLZ8H+kR37ds9LugtOwlV4V/azH09OHIaCTKsmT/Y4PnWwOGLt7x6GOVID+lDgLVaIPm5K2JwPeGdqNdGv41fUxgHKAFLZaeRMVJW9lBrnMa8kmVx38GbW9t53DvMYI/FfQcghGkHKKpV3oCdlpgLie8tfQZCzrG75va3WmLIPWBjgaVAikZR5V+H3N3IX3PvdPO4WpxE/rpkePg2Eci8Tr4jDbOHYNOCzXZLpIQP6iAw== 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: Add a hook when a new mapping is added to an order-0 mlocked folio to check if the mapcount overflowed beyond the allocated 20 bits into the mlock_count. This is useful to know if this alarm if this happens frequently enough to cause a problem. We do so by checking if the folio has the lower 20 bits as all 0s. For file-backed folios, we do not hold the folio lock while adding a new mapping, so there's a chance that two mappings are added in quick succession such that the warning doesn't fire. Don't sweat it. Signed-off-by: Yosry Ahmed --- include/linux/mm.h | 4 ++++ mm/mlock.c | 13 +++++++++++++ mm/rmap.c | 2 ++ 3 files changed, 19 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index b341477a83e8..917f81996e22 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1050,6 +1050,7 @@ unsigned long vmalloc_to_pfn(const void *addr); extern bool is_vmalloc_addr(const void *x); extern int is_vmalloc_or_module_addr(const void *x); extern int folio_mlocked_mapcount(struct folio *folio); +extern void folio_mlock_map_check(struct folio *folio); extern void folio_mlock_unmap_check(struct folio *folio); #else static inline bool is_vmalloc_addr(const void *x) @@ -1064,6 +1065,9 @@ static inline int folio_mlocked_mapcount(struct folio *folio) { return 0; } +static inline void folio_mlock_map_check(struct folio *folio) +{ +} static inline void folio_mlock_unmap_check(struct folio *folio) { } diff --git a/mm/mlock.c b/mm/mlock.c index 8261df11d6a6..f8b3fb1b2986 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -140,6 +140,19 @@ static int folio_mlock_count_dec(struct folio *folio) return mlock_count - 1; } +/* + * Call after incrementing the mapcount. WARN_ON() if the mapcount overflows + * beyond the lower 20 bits for order-0 mlocked folios. + */ +void folio_mlock_map_check(struct folio *folio) +{ + int mapcount = atomic_read(&folio->_mapcount) + 1; + + /* WARN if we overflow beyond the lower 20 bits */ + if (unlikely(!folio_test_large(folio) && folio_test_mlocked(folio))) + WARN_ON((mapcount & MLOCK_MAPCOUNT_MASK) == 0); +} + /* * Call after decrementing the mapcount. If the mapcount previously overflowed * beyond the lower 20 bits for an order-0 mlocked folio, munlock() have diff --git a/mm/rmap.c b/mm/rmap.c index 02e558551f15..092529319782 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1228,6 +1228,7 @@ void page_add_anon_rmap(struct page *page, struct vm_area_struct *vma, nr = atomic_inc_return_relaxed(mapped); nr = (nr < COMPOUND_MAPPED); } + folio_mlock_map_check(folio); } else if (folio_test_pmd_mappable(folio)) { /* That test is redundant: it's for safety or to optimize out */ @@ -1330,6 +1331,7 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, nr = atomic_inc_return_relaxed(mapped); nr = (nr < COMPOUND_MAPPED); } + folio_mlock_map_check(folio); } else if (folio_test_pmd_mappable(folio)) { /* That test is redundant: it's for safety or to optimize out */