Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp873457pxk; Thu, 17 Sep 2020 19:56:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJLj0AXzJoT4Uf5oqMgAhltuw/BsEZ63PNb6iYaAzy2Z5VhQ05rw6LyvsB9rXhGlN5QBl3 X-Received: by 2002:aa7:c2d6:: with SMTP id m22mr36003239edp.311.1600397801496; Thu, 17 Sep 2020 19:56:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600397801; cv=none; d=google.com; s=arc-20160816; b=iZqxnHxDGyhriihSQ7glzQzYrExEvAzArDOA4wMkFMXhNEiv6PPdODEkRuxvmHSMZz NXTBSCT6Vw9RVtXePfv93xRqymNg2E0YTJBLKLhubtUP+xCKk5b0lDFknAsm6Z7uiEcC qg/cFzqFimi2INtgMMlpXHmkk+SQPZp3iT+bGW8YXOGHaMfexGFeEkmHEvIqUnPpru3i mzj2yZSGoFb/vy8ZMeTVXlGkUA7TUK6y+DLmW8idecrxsbZTL8dKVVI5inv8O+bcm3Or y+QXRY3+yEiO1Cu+tfXDT8jsBfR+mfaXxZrWI229at0U00Jw776h7FYm9NKrFUMjvzH6 YG5g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Sf4sMcNrCqZ6i2gMe53m4Rq+rZ869OKSjWXPnOduk2I=; b=L0YFBd2tf+zMYvRDijsFqBIaArRmlR72uc86sFXxQ0S847YIughd1p978fzzM1vXv3 1Ti3Om+htQ2QqE7IGzgblFFBLeKKM+zkw2aKE1mLCDAUQofdKO7mw9wbnygcvoP84LJQ ygX9RWuunnLRPZmRLDaocw8NNub30Jeo5x+Uf9PINSQKpJ/zAZqTRU+ul9lJptlGRccY FzsyWsyG/C/QMlbSRWJzruDftde4VeDiWCN8KqjtN/tsmqFX9zucHzkEBcZr/x7ywooY FdGoFiNa+0RCDIW+Mom7jzQH/7hwUJhwhRAeMHxJsh0PUNAopLmEJLQ9QE4zEVsMPEl5 MhLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AWGSYwfs; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ot13si1205961ejb.450.2020.09.17.19.56.17; Thu, 17 Sep 2020 19:56:41 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=default header.b=AWGSYwfs; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727868AbgIRCy1 (ORCPT + 99 others); Thu, 17 Sep 2020 22:54:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:57840 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727933AbgIRCHa (ORCPT ); Thu, 17 Sep 2020 22:07:30 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E30D92395B; Fri, 18 Sep 2020 02:07:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394849; bh=VTQoAoqYGfRwWaGjxQi8u51L4JKtV0UYPVCsZYsx8Yg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AWGSYwfsHsvabrddcUo/HGvhE80kYQPBCh8EkGUKJvURCDXG30xtMShtQ9FQ07UYR 1yuE32y+moZVcCb9ijQPufUhBcM+QFsMjEf7TWqBxy5eF+NkjBTmAGJ499mvKSkerx rXT5xaa+sHIJrb2Oq7kBzpFiMCA+r1CHoPZi7te8= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Qian Cai , Andrew Morton , Marco Elver , Hugh Dickins , Linus Torvalds , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 5.4 306/330] mm/swap_state: fix a data race in swapin_nr_pages Date: Thu, 17 Sep 2020 22:00:46 -0400 Message-Id: <20200918020110.2063155-306-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qian Cai [ Upstream commit d6c1f098f2a7ba62627c9bc17cda28f534ef9e4a ] "prev_offset" is a static variable in swapin_nr_pages() that can be accessed concurrently with only mmap_sem held in read mode as noticed by KCSAN, BUG: KCSAN: data-race in swap_cluster_readahead / swap_cluster_readahead write to 0xffffffff92763830 of 8 bytes by task 14795 on cpu 17: swap_cluster_readahead+0x2a6/0x5e0 swapin_readahead+0x92/0x8dc do_swap_page+0x49b/0xf20 __handle_mm_fault+0xcfb/0xd70 handle_mm_fault+0xfc/0x2f0 do_page_fault+0x263/0x715 page_fault+0x34/0x40 1 lock held by (dnf)/14795: #0: ffff897bd2e98858 (&mm->mmap_sem#2){++++}-{3:3}, at: do_page_fault+0x143/0x715 do_user_addr_fault at arch/x86/mm/fault.c:1405 (inlined by) do_page_fault at arch/x86/mm/fault.c:1535 irq event stamp: 83493 count_memcg_event_mm+0x1a6/0x270 count_memcg_event_mm+0x119/0x270 __do_softirq+0x365/0x589 irq_exit+0xa2/0xc0 read to 0xffffffff92763830 of 8 bytes by task 1 on cpu 22: swap_cluster_readahead+0xfd/0x5e0 swapin_readahead+0x92/0x8dc do_swap_page+0x49b/0xf20 __handle_mm_fault+0xcfb/0xd70 handle_mm_fault+0xfc/0x2f0 do_page_fault+0x263/0x715 page_fault+0x34/0x40 1 lock held by systemd/1: #0: ffff897c38f14858 (&mm->mmap_sem#2){++++}-{3:3}, at: do_page_fault+0x143/0x715 irq event stamp: 43530289 count_memcg_event_mm+0x1a6/0x270 count_memcg_event_mm+0x119/0x270 __do_softirq+0x365/0x589 irq_exit+0xa2/0xc0 Signed-off-by: Qian Cai Signed-off-by: Andrew Morton Cc: Marco Elver Cc: Hugh Dickins Link: http://lkml.kernel.org/r/20200402213748.2237-1-cai@lca.pw Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/swap_state.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index 4ce014dc4571a..7c434fcfff0dd 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -511,10 +511,11 @@ static unsigned long swapin_nr_pages(unsigned long offset) return 1; hits = atomic_xchg(&swapin_readahead_hits, 0); - pages = __swapin_nr_pages(prev_offset, offset, hits, max_pages, + pages = __swapin_nr_pages(READ_ONCE(prev_offset), offset, hits, + max_pages, atomic_read(&last_readahead_pages)); if (!hits) - prev_offset = offset; + WRITE_ONCE(prev_offset, offset); atomic_set(&last_readahead_pages, pages); return pages; -- 2.25.1