Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1053409iob; Fri, 13 May 2022 21:15:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxFmEdPC4W9EqsD2o+b0MEGPwyffKCDku9SpzRWWTFGkrVV5FWfbvl1sSdc2fmHCx43MJ1G X-Received: by 2002:a05:6000:15c7:b0:20c:5794:1899 with SMTP id y7-20020a05600015c700b0020c57941899mr6476193wry.571.1652501729522; Fri, 13 May 2022 21:15:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652501729; cv=none; d=google.com; s=arc-20160816; b=1ENy6UvYhv13M3fPUCJqmvqjXX7aZvLb60Exka3AiqtdKuzIoMlKhSYkHWO+Xvvs7Z 1cFDLKTQbzB2Os4oin0+zShANgoIxCRHcgkny9flRqCi3d2XdX2Yq2OPETlNY3krtmbL JddstG3ab1+W904KslT4hmLnU/sl8IOiHB6fEq+t4WOoTKhOkaI6Zyoj4wLBnl3P18s3 91zAC/vhbp3vavxJ2XJvLx6Xj0zL5IbcV6OHAzEhCUrw9h8l0yXFA3mpCROG8XuUyUwd ExqP0/7Ud2tV7nDwa5g029AKE10fjoVau6kk3KJMZbWwanezO9caDse29QFqm0IMPbsE 71QA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=uUZ1B/7xqCMbgStyA7Ho3ErPD4yvMkTwqgpBaMdYV0U=; b=05H7sdx5o7h2XbfQbYxJOZ1JroLmLrPwkOEEteQ0jLMILjPmZnPbVE3mXI/pEXzIw/ HuN4wXYobgA8/63auq+BUduyHqLPedPZRDLDXjcm2oT/CHFX2AWkL2ocGcP64gNNLxWK UO+MiFAf7IzyDLX+vpUYKg3P0GbXILrAye47aVE9JP6srfO+sO4tNo2wu8kSCagVjecc bVh805S6DrtQanaamyHubEuRMGP5OJ++o9uuyKjz5ugUhEFGi++l2yyR2MlMr8exAx7S 7qqekgupWFd2QbxArAY6J9jdub7G8xR7tVbneQjeyBRX2D6+7RwYENRGNbQMszOIncKQ 5Jrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=itmq0KKZ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id h10-20020adff18a000000b0020ad88e3b41si3864475wro.460.2022.05.13.21.15.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 21:15:29 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=itmq0KKZ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 43A624E7EBB; Fri, 13 May 2022 17:46:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358408AbiELUlt (ORCPT + 99 others); Thu, 12 May 2022 16:41:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358007AbiELUls (ORCPT ); Thu, 12 May 2022 16:41:48 -0400 Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 103E9579B2 for ; Thu, 12 May 2022 13:41:48 -0700 (PDT) Received: by mail-pf1-x432.google.com with SMTP id i24so5854191pfa.7 for ; Thu, 12 May 2022 13:41:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=uUZ1B/7xqCMbgStyA7Ho3ErPD4yvMkTwqgpBaMdYV0U=; b=itmq0KKZk0ljNSSnvPSn0aOhIoNlC18SbogAnFyZ5XL5esAH48sAtb9WYMZ7EbhpwN wNbS5ZZ9fwGNt8vEWxxcgzQqt3VTjbPH11NwxZ6Tq1XwLRkRJjeTLmkFkrBkBUop9Wnh JM0ksbxepebVzGOJdTrcvP6Vk/2t7NC6AFgd+Y3pqcZaLcFjaja6zrDQaiTThs2cDw0o O4GuUjtiL8BWmtJbUABVXggMDL/7+GKWVSiRDpWo8LmAnqDhk/KKZqMnrtKZjNurtlgp wOgKmSZsBy/N59qD4/pl4Mbo54uiZSW4o28FMyMZmzhkMJY8o9uS9Mze60pdDLRNysmZ vZMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=uUZ1B/7xqCMbgStyA7Ho3ErPD4yvMkTwqgpBaMdYV0U=; b=cDjjAGY/SzJChPAcNuAHRZCruNivlLBTZv+ScMQcungvZua9k19qwMcSv2vo6CO54T LS3gsTq76lmMXU+pwC+15krqsvSYNizAWm7WO6aXzssWZt0hk+8IaLYA77beABU9Gzlf CbRM4IYTk5iy64sl1r7OEZa+ORHn+nf/Ck0TWHiRiBOkAbNheHrI+H6BrbermcyQucw8 TkSxORAFGvUHBBrX92DyNxtCFT/68LUWvy5lbom3jo0Z0OFUliI8GGSfsalmfganFadn UsQa5riOs/IJeSDz1znpdvBDdm+zlZETNNBZpJ2KzsBTu5ygDQJ6J0xXQ8iiblfnGyYK Zvlg== X-Gm-Message-State: AOAM530R/4jYD9z3744NfFRQFAOdiaDNTa5dDWLdeNqcZJnKIsHsqExU V/EtCP/5WVGzQjjyxlzONtI= X-Received: by 2002:a05:6a00:2402:b0:4e1:46ca:68bd with SMTP id z2-20020a056a00240200b004e146ca68bdmr1431739pfh.70.1652388107535; Thu, 12 May 2022 13:41:47 -0700 (PDT) Received: from bbox-1.mtv.corp.google.com ([2620:15c:211:201:872f:bbca:8e23:fae5]) by smtp.gmail.com with ESMTPSA id l6-20020a170903244600b00160d358a888sm90912pls.32.2022.05.12.13.41.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 May 2022 13:41:46 -0700 (PDT) Sender: Minchan Kim From: Minchan Kim To: Andrew Morton Cc: linux-mm , LKML , "Paul E . McKenney" , John Hubbard , John Dias , Minchan Kim , David Hildenbrand Subject: [PATCH v5] mm: fix is_pinnable_page against on cma page Date: Thu, 12 May 2022 13:41:43 -0700 Message-Id: <20220512204143.3961150-1-minchan@kernel.org> X-Mailer: git-send-email 2.36.0.550.gb090851708-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Pages on CMA area could have MIGRATE_ISOLATE as well as MIGRATE_CMA so current is_pinnable_page could miss CMA pages which has MIGRATE_ ISOLATE. It ends up pinning CMA pages as longterm at pin_user_pages APIs so CMA allocation keep failed until the pin is released. CPU 0 CPU 1 - Task B cma_alloc alloc_contig_range pin_user_pages_fast(FOLL_LONGTERM) change pageblock as MIGRATE_ISOLATE internal_get_user_pages_fast lockless_pages_from_mm gup_pte_range try_grab_folio is_pinnable_page return true; So, pinned the page successfully. page migration failure with pinned page .. .. After 30 sec unpin_user_page(page) CMA allocation succeeded after 30 sec. The CMA allocation path protects the migration type change race using zone->lock but what GUP path need to know is just whether the page is on CMA area or not rather than exact migration type. Thus, we don't need zone->lock but just checks migration type in either of (MIGRATE_ISOLATE and MIGRATE_CMA). Adding the MIGRATE_ISOLATE check in is_pinnable_page could cause rejecting of pinning pages on MIGRATE_ISOLATE pageblocks even though it's neither CMA nor movable zone if the page is temporarily unmovable. However, such a migration failure by unexpected temporal refcount holding is general issue, not only come from MIGRATE_ISOLATE and the MIGRATE_ISOLATE is also transient state like other temporal elevated refcount problem. Cc: "Paul E . McKenney" Cc: John Hubbard Cc: David Hildenbrand Signed-off-by: Minchan Kim --- * from v4 - https://lore.kernel.org/all/20220510211743.95831-1-minchan@kernel.org/ * clarification why we need READ_ONCE - Paul * Adding a comment about READ_ONCE - John * from v3 - https://lore.kernel.org/all/20220509153430.4125710-1-minchan@kernel.org/ * Fix typo and adding more description - akpm * from v2 - https://lore.kernel.org/all/20220505064429.2818496-1-minchan@kernel.org/ * Use __READ_ONCE instead of volatile - akpm * from v1 - https://lore.kernel.org/all/20220502173558.2510641-1-minchan@kernel.org/ * fix build warning - lkp * fix refetching issue of migration type * add side effect on !ZONE_MOVABLE and !MIGRATE_CMA in description - david include/linux/mm.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 6acca5cecbc5..2d7a5d87decd 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1625,8 +1625,20 @@ static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma, #ifdef CONFIG_MIGRATION static inline bool is_pinnable_page(struct page *page) { - return !(is_zone_movable_page(page) || is_migrate_cma_page(page)) || - is_zero_pfn(page_to_pfn(page)); +#ifdef CONFIG_CMA + /* + * Defend against future compiler LTO features, or code refactoring + * that inlines the above function, by forcing a single read. Because, + * this routine races with set_pageblock_migratetype(), and we want to + * avoid reading zero, when actually one or the other flags was set. + */ + int mt = __READ_ONCE(__mt); + + if (mt & (MIGRATE_CMA | MIGRATE_ISOLATE)) + return false; +#endif + + return !(is_zone_movable_page(page) || is_zero_pfn(page_to_pfn(page))); } #else static inline bool is_pinnable_page(struct page *page) -- 2.36.0.550.gb090851708-goog