From patchwork Tue Jul 5 12:35:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12906544 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 53149C433EF for ; Tue, 5 Jul 2022 12:35:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCFE36B0071; Tue, 5 Jul 2022 08:35:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D80676B0073; Tue, 5 Jul 2022 08:35:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C47D56B0074; Tue, 5 Jul 2022 08:35:44 -0400 (EDT) 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 B2C866B0071 for ; Tue, 5 Jul 2022 08:35:44 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 8A95112127B for ; Tue, 5 Jul 2022 12:35:44 +0000 (UTC) X-FDA: 79652992608.19.06A0DCB Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf13.hostedemail.com (Postfix) with ESMTP id 78FCF2001D for ; Tue, 5 Jul 2022 12:35:43 +0000 (UTC) Received: by mail-pj1-f51.google.com with SMTP id g20-20020a17090a579400b001ed52939d72so12400225pji.4 for ; Tue, 05 Jul 2022 05:35:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=P8dLNO3XSVpB5X1jg4KVwtpXwWMCvyBucMCZ4xYCRcE=; b=YvIK0lrmI2mmJifbiC13P9zSa4jhgWrCSf8HfCcWHJeRvq/2xkb1o4YgiN5i3z09tp 9gjXqgJccr/RTi1IiNDqr6D8MVorTHbLO1jtVDyuYjTeI3kwWWXhiQQ2TrQXZp4R3Fyc gAg8b1Xktofgk5Kju+be+WvUsysFwFRsMwUspJoICZi2ITNn1orttaMB7Hu9XGSP2Wmp Wx5L3SOcTvfXsH1MEAI4qEFT/TzHEnmLTkz25L9J0/PjOwfRvTbJ2875wc/+WzeDbrGw QrdhzE9iE0rO0PUQ94DCPnJeTPvzYHio319Xu9Fr3vu3PcLHckf80vLg/ZO9PtWpsvvW eX2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=P8dLNO3XSVpB5X1jg4KVwtpXwWMCvyBucMCZ4xYCRcE=; b=ZvKBZOw487/Z/88YK0trz2MhifcsGfH/VoJuyVwbp5q2ny74Urrbm8g21Il3l+5ZA5 K0VEVzkRpNH6f5wlNC5ZWK5Vimqh3jqeigQx7HWD3Xp1qWoCr4evoFlbg8Bx60HsXek8 GKYbX43F/JBmKZ4IPUIVNLKwfk4z+WdrZDKGoh6U7QKKDxeWUMwWIT0HQf81QyMSnVve FLBBcSuQwU1LyD0AaO18/xINTPgG84xKekG7/tWK+JbT1y4JcnOJHDyChJ1v745DwRy9 oGRYIZnGcJoXDeGdqA2wxNhOnp1ARGWF9/AqNiqnIZcC1e5KPbvGnHnxayn8CtXN7WkT hdzA== X-Gm-Message-State: AJIora+L6B/KfZLsKQYOD6SimwzXExzjSJPg+ufTLldLUr8Z75saLWyB vQKsdtD1Jrf9Py4jnfRX3vehRA== X-Google-Smtp-Source: AGRyM1tGmro/IOsy0F1j/gU/vPekilw0RyPAVRgsAlBkibEGJXezSBOKSTDCC9D3AROIwcb5MCGdfQ== X-Received: by 2002:a17:902:a601:b0:16a:6632:7f14 with SMTP id u1-20020a170902a60100b0016a66327f14mr42460871plq.2.1657024542238; Tue, 05 Jul 2022 05:35:42 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.229]) by smtp.gmail.com with ESMTPSA id k6-20020a63ba06000000b0041278f0025esm1154191pgf.12.2022.07.05.05.35.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Jul 2022 05:35:41 -0700 (PDT) From: Muchun Song To: akpm@linux-foundation.org, willy@infradead.org, jgg@ziepe.ca, jhubbard@nvidia.com, william.kucharski@oracle.com, dan.j.williams@intel.com, jack@suse.cz Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, nvdimm@lists.linux.dev, Muchun Song , stable@vger.kernel.org Subject: [PATCH v2] mm: fix missing wake-up event for FSDAX pages Date: Tue, 5 Jul 2022 20:35:32 +0800 Message-Id: <20220705123532.283-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.1 (Apple Git-133) MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657024544; 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:references:dkim-signature; bh=P8dLNO3XSVpB5X1jg4KVwtpXwWMCvyBucMCZ4xYCRcE=; b=GDY1pJMtSxGtfTyV986EtUgGvmqmql4agJTb2S/IGQHJI9v8wX/HIHXmVnDxsezPUE/1Je 02Dg+WHOjq1r7IdE7MvlfFPdvwdtJltBHUNVqtnTbfRev+6+aS5v5sA/NdzkTokWHclJUU 2CO92AsD6k8khVYRr09xSrhv1JEBruQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=YvIK0lrm; spf=pass (imf13.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657024544; a=rsa-sha256; cv=none; b=lu4fJo3qL1p+vWC6YzS6CQlrjpnV+3+1a4ArCnBeWo706cL42qDUNhIheQYh4ALtUasVth QJZRoaw1ZO3FcYd2tJixPaeyL0OF4okMvsCza+qS4tMqz/RsZWg/afRXDHInU96eK0AByR FAUBgM/JbROheBoR9AMzk6JUMAU9KZs= X-Stat-Signature: 3z3aom1rjaddbqy8k5daoi3ss373scs5 X-Rspamd-Queue-Id: 78FCF2001D Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=YvIK0lrm; spf=pass (imf13.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1657024543-954700 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: FSDAX page refcounts are 1-based, rather than 0-based: if refcount is 1, then the page is freed. The FSDAX pages can be pinned through GUP, then they will be unpinned via unpin_user_page() using a folio variant to put the page, however, folio variants did not consider this special case, the result will be to miss a wakeup event (like the user of __fuse_dax_break_layouts()). Since FSDAX pages are only possible get by GUP users, so fix GUP instead of folio_put() to lower overhead. Fixes: d8ddc099c6b3 ("mm/gup: Add gup_put_folio()") Suggested-by: Matthew Wilcox Signed-off-by: Muchun Song Cc: --- v2: - Fix GUP instead of folio_put() suggested by Matthew. include/linux/mm.h | 14 +++++++++----- mm/gup.c | 6 ++++-- mm/memremap.c | 6 +++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 517f9deba56f..b324c9fa2940 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1157,23 +1157,27 @@ static inline bool is_zone_movable_page(const struct page *page) #if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_FS_DAX) DECLARE_STATIC_KEY_FALSE(devmap_managed_key); -bool __put_devmap_managed_page(struct page *page); -static inline bool put_devmap_managed_page(struct page *page) +bool __put_devmap_managed_page_refs(struct page *page, int refs); +static inline bool put_devmap_managed_page_refs(struct page *page, int refs) { if (!static_branch_unlikely(&devmap_managed_key)) return false; if (!is_zone_device_page(page)) return false; - return __put_devmap_managed_page(page); + return __put_devmap_managed_page_refs(page, refs); } - #else /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */ -static inline bool put_devmap_managed_page(struct page *page) +static inline bool put_devmap_managed_page_refs(struct page *page, int refs) { return false; } #endif /* CONFIG_ZONE_DEVICE && CONFIG_FS_DAX */ +static inline bool put_devmap_managed_page(struct page *page) +{ + return put_devmap_managed_page_refs(page, 1); +} + /* 127: arbitrary random number, small enough to assemble well */ #define folio_ref_zero_or_close_to_overflow(folio) \ ((unsigned int) folio_ref_count(folio) + 127u <= 127u) diff --git a/mm/gup.c b/mm/gup.c index 4e1999402e71..965ba755023f 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -87,7 +87,8 @@ static inline struct folio *try_get_folio(struct page *page, int refs) * belongs to this folio. */ if (unlikely(page_folio(page) != folio)) { - folio_put_refs(folio, refs); + if (!put_devmap_managed_page_refs(&folio->page, refs)) + folio_put_refs(folio, refs); goto retry; } @@ -176,7 +177,8 @@ static void gup_put_folio(struct folio *folio, int refs, unsigned int flags) refs *= GUP_PIN_COUNTING_BIAS; } - folio_put_refs(folio, refs); + if (!put_devmap_managed_page_refs(&folio->page, refs)) + folio_put_refs(folio, refs); } /** diff --git a/mm/memremap.c b/mm/memremap.c index f0955785150f..58b20c3c300b 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -509,7 +509,7 @@ void free_zone_device_page(struct page *page) } #ifdef CONFIG_FS_DAX -bool __put_devmap_managed_page(struct page *page) +bool __put_devmap_managed_page_refs(struct page *page, int refs) { if (page->pgmap->type != MEMORY_DEVICE_FS_DAX) return false; @@ -519,9 +519,9 @@ bool __put_devmap_managed_page(struct page *page) * refcount is 1, then the page is free and the refcount is * stable because nobody holds a reference on the page. */ - if (page_ref_dec_return(page) == 1) + if (page_ref_sub_return(page, refs) == 1) wake_up_var(&page->_refcount); return true; } -EXPORT_SYMBOL(__put_devmap_managed_page); +EXPORT_SYMBOL(__put_devmap_managed_page_refs); #endif /* CONFIG_FS_DAX */