Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp921486pxb; Thu, 5 Nov 2020 17:22:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJwqeTXJ5RBQD/Pm0X77GNrJw2xFWOxN65gj1892/eb3WFWJuui4nagP0CQbzsZmTb5mp0De X-Received: by 2002:a17:906:86c7:: with SMTP id j7mr5138242ejy.301.1604625734408; Thu, 05 Nov 2020 17:22:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604625734; cv=none; d=google.com; s=arc-20160816; b=OPgYyPmte6jJEa/Fbry2P8Ii9/7kTgOmzMhn8vz16PwsjYhujLdUE+qmUzkTgUkmi1 uNmgWB6jnweGPqvsdDFWubL47NhaHWo/74WgO5ScLei7Ta6Df7A/WvyTxgIo5mbKoEcS KK3CnFVU8DsTcBuH9HwF7CmDmbt9O4Ca6Egg8dgSqTW+QaAJ0422lpDM9UJn+Nqv8fJU PQgSNCRPsE/HLjE0q4uqt71wb1Kqqw5lJ/kg+8WCrZKpvFK/C4aLzRAzxJyAMYQOC/DT WZAvQ4Dr6uQLHk+OOU+kUXa8gBXblPL381bjF6Uy28HKjUs4WlPIzrzRqfOSXb8XSULo 1RfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:references:cc:to:from :subject; bh=GyqbhteQrUlNHsWIQe4bQUkGnxm5uebFRazxJmdC4cQ=; b=ymH1/V8WaKL1gRxppF0hKcSjtEmZnsiOT0/P1pd2tpmzWPTH1rSjM5tuokUjc/qHqI jkrKqPOIVshFpPR11v3tz313x4/tLVaTG66OsQdx3rcl9aHjgPBfK/RM8/7kkD9iSAFk xCnnoYvGDmROZVfruXvxDJ71bE8qrlIiXpOgoSIfjDJnp1AAC70OuN2UGObleTVwWhbQ Ux89qIpgh3DIYQhwosLc0/xLWR2/oURa24GBt+l3vTK2ov05Rl872ZG7AdrhsPJ4ShLe gPcXu2v3vY/BlzagxzKj2I6t/+ofpFr/YVEPG/AKX4YijLyu3XIXgOAv06+yu0WvC98G 1KHA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e14si2314777edl.270.2020.11.05.17.21.48; Thu, 05 Nov 2020 17:22:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731111AbgKFBUV (ORCPT + 99 others); Thu, 5 Nov 2020 20:20:21 -0500 Received: from out30-54.freemail.mail.aliyun.com ([115.124.30.54]:57345 "EHLO out30-54.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730246AbgKFBUU (ORCPT ); Thu, 5 Nov 2020 20:20:20 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R121e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04407;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=22;SR=0;TI=SMTPD_---0UEN0K6x_1604625615; Received: from IT-FVFX43SYHV2H.local(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0UEN0K6x_1604625615) by smtp.aliyun-inc.com(127.0.0.1); Fri, 06 Nov 2020 09:20:16 +0800 Subject: Re: [PATCH v21 06/19] mm/rmap: stop store reordering issue on page->mapping From: Alex Shi To: akpm@linux-foundation.org, mgorman@techsingularity.net, tj@kernel.org, hughd@google.com, khlebnikov@yandex-team.ru, daniel.m.jordan@oracle.com, willy@infradead.org, hannes@cmpxchg.org, lkp@intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, shakeelb@google.com, iamjoonsoo.kim@lge.com, richard.weiyang@gmail.com, kirill@shutemov.name, alexander.duyck@gmail.com, rong.a.chen@intel.com, mhocko@suse.com, vdavydov.dev@gmail.com, shy828301@gmail.com Cc: Minchan Kim References: <1604566549-62481-1-git-send-email-alex.shi@linux.alibaba.com> <1604566549-62481-7-git-send-email-alex.shi@linux.alibaba.com> Message-ID: Date: Fri, 6 Nov 2020 09:20:04 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <1604566549-62481-7-git-send-email-alex.shi@linux.alibaba.com> Content-Type: text/plain; charset=gbk Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org updated for comments change from Johannes From 2fd278b1ca6c3e260ad249808b62f671d8db5a7b Mon Sep 17 00:00:00 2001 From: Alex Shi Date: Thu, 5 Nov 2020 11:38:24 +0800 Subject: [PATCH v21 06/19] mm/rmap: stop store reordering issue on page->mapping Hugh Dickins and Minchan Kim observed a long time issue which discussed here, but actully the mentioned fix missed. https://lore.kernel.org/lkml/20150504031722.GA2768@blaptop/ The store reordering may cause problem in the scenario: CPU 0 CPU1 do_anonymous_page page_add_new_anon_rmap() page->mapping = anon_vma + PAGE_MAPPING_ANON lru_cache_add_inactive_or_unevictable() spin_lock(lruvec->lock) SetPageLRU() spin_unlock(lruvec->lock) /* idletacking judged it as LRU * page so pass the page in * page_idle_clear_pte_refs */ page_idle_clear_pte_refs rmap_walk if PageAnon(page) Johannes give detailed examples how the store reordering could cause a trouble: "The concern is the SetPageLRU may get reorder before 'page->mapping' setting, That would make CPU 1 will observe at page->mapping after observing PageLRU set on the page. 1. anon_vma + PAGE_MAPPING_ANON That's the in-order scenario and is fine. 2. NULL That's possible if the page->mapping store gets reordered to occur after SetPageLRU. That's fine too because we check for it. 3. anon_vma without the PAGE_MAPPING_ANON bit That would be a problem and could lead to all kinds of undesirable behavior including crashes and data corruption. Is it possible? AFAICT the compiler is allowed to tear the store to page->mapping and I don't see anything that would prevent it. That said, I also don't see how the reader testing PageLRU under the lru_lock would prevent that in the first place. AFAICT we need that WRITE_ONCE() around the page->mapping assignment." Signed-off-by: Alex Shi Cc: Johannes Weiner Cc: Andrew Morton Cc: Hugh Dickins Cc: Matthew Wilcox Cc: Minchan Kim Cc: Vladimir Davydov Cc: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org --- mm/rmap.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/rmap.c b/mm/rmap.c index 1b84945d655c..380c6b9956c2 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1054,8 +1054,14 @@ static void __page_set_anon_rmap(struct page *page, if (!exclusive) anon_vma = anon_vma->root; + /* + * page_idle does a lockless/optimistic rmap scan on page->mapping. + * Make sure the compiler doesn't split the stores of anon_vma and + * the PAGE_MAPPING_ANON type identifier, otherwise the rmap code + * could mistake the mapping for a struct address_space and crash. + */ anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON; - page->mapping = (struct address_space *) anon_vma; + WRITE_ONCE(page->mapping, (struct address_space *) anon_vma); page->index = linear_page_index(vma, address); } -- 1.8.3.1