Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp334945pxb; Thu, 5 Nov 2020 01:00:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJw+kNnVMsVIPsksJk09LT+MfsvQ6ujzPwfUxRS8y+4UiTiRUqLzOjld7TkWucCZXnW12kfl X-Received: by 2002:a50:9518:: with SMTP id u24mr1580150eda.124.1604566822280; Thu, 05 Nov 2020 01:00:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604566822; cv=none; d=google.com; s=arc-20160816; b=QsPPl/d+T2HFRuARaZPVXjltP9RTv2bxg89NCIXYtvCk7ghfBrMrfe4gZ7+lV/hhcc 6hMoboNRRkDHsazWvkc3TegbWWYKayl8qeuDxmJzVV35pehFEuNKxneIJXTutv2hem6e rONKHT//pCiO9xB60VWIOzUXWPbTxrTNJ3LyuUQ/0skMgkrj5EntC6P55JozV5pOsmLH ARxtIAaDEKnz4PuF1dVHQ6M9y0K3E3rD+8VhdUHrUEiAUQk1ZaeaosOByqlBMPVuqwAW CPNko59bwiAPMJp32Yl0S2PZsNSbjl4kvFk73sjIWbwyjfwWCESmn/Tisd1qgIlF0f8p eyeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=MXNpnUetzDFZOMsvLisz85OC7cBFTe3HWRSAq+GAPgA=; b=qYQQ0YHaFzjyJSZDiVbUEeitM1Yo5gYdgkYeTaFJ2osfiJzCCEkZ/Pwn/PcHTJ9jW0 tOHCuJHbH8a+FYIrd/l6oymFa2bkm1osnduJSh9niqnWpVJwHwb/3hiKQ527qGjO5K4z MwT/EGyFfpT7ZAmWXY5NKZ5V6YfqReidtmOTVhc1D3GLsIHLWv1Qk9MUQXZ7qUPyaxwM imU1DmVtmKo9giYoOxDoyYhj7JlwPhXaGwzz+JrEDxaMEiiShzFB6B29RRnCs7nT71qC SNyW3BPYcyhMfLrWIFd3TloTrNJ5KgJiGw3Hrizt3XmC03r4BTqLmEeEV2W6MzTSk3NF fLeA== 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 q3si683573edv.85.2020.11.05.00.59.58; Thu, 05 Nov 2020 01:00:22 -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 S1731514AbgKEI5k (ORCPT + 99 others); Thu, 5 Nov 2020 03:57:40 -0500 Received: from out30-42.freemail.mail.aliyun.com ([115.124.30.42]:56619 "EHLO out30-42.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726849AbgKEI4Q (ORCPT ); Thu, 5 Nov 2020 03:56:16 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=alimailimapcm10staff010182156082;MF=alex.shi@linux.alibaba.com;NM=1;PH=DS;RN=22;SR=0;TI=SMTPD_---0UEJC3Fv_1604566567; Received: from aliy80.localdomain(mailfrom:alex.shi@linux.alibaba.com fp:SMTPD_---0UEJC3Fv_1604566567) by smtp.aliyun-inc.com(127.0.0.1); Thu, 05 Nov 2020 16:56:10 +0800 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 Subject: [PATCH v21 06/19] mm/rmap: stop store reordering issue on page->mapping Date: Thu, 5 Nov 2020 16:55:36 +0800 Message-Id: <1604566549-62481-7-git-send-email-alex.shi@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1604566549-62481-1-git-send-email-alex.shi@linux.alibaba.com> References: <1604566549-62481-1-git-send-email-alex.shi@linux.alibaba.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mm/rmap.c b/mm/rmap.c index 1b84945d655c..078d54da59d4 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1054,8 +1054,13 @@ static void __page_set_anon_rmap(struct page *page, if (!exclusive) anon_vma = anon_vma->root; + /* + * Prevent page->mapping from pointing to an anon_vma without + * the PAGE_MAPPING_ANON bit set. This could happen if the + * compiler stores anon_vma and then adds PAGE_MAPPING_ANON to it. + */ 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