linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Qian Cai <cai@lca.pw>, Andrew Morton <akpm@linux-foundation.org>,
	Marco Elver <elver@google.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Sasha Levin <sashal@kernel.org>
Subject: [PATCH AUTOSEL 4.14 17/30] percpu_counter: fix a data race at vm_committed_as
Date: Wed, 15 Apr 2020 07:46:58 -0400	[thread overview]
Message-ID: <20200415114711.15381-17-sashal@kernel.org> (raw)
In-Reply-To: <20200415114711.15381-1-sashal@kernel.org>

From: Qian Cai <cai@lca.pw>

[ Upstream commit 7e2345200262e4a6056580f0231cccdaffc825f3 ]

"vm_committed_as.count" could be accessed concurrently as reported by
KCSAN,

 BUG: KCSAN: data-race in __vm_enough_memory / percpu_counter_add_batch

 write to 0xffffffff9451c538 of 8 bytes by task 65879 on cpu 35:
  percpu_counter_add_batch+0x83/0xd0
  percpu_counter_add_batch at lib/percpu_counter.c:91
  __vm_enough_memory+0xb9/0x260
  dup_mm+0x3a4/0x8f0
  copy_process+0x2458/0x3240
  _do_fork+0xaa/0x9f0
  __do_sys_clone+0x125/0x160
  __x64_sys_clone+0x70/0x90
  do_syscall_64+0x91/0xb05
  entry_SYSCALL_64_after_hwframe+0x49/0xbe

 read to 0xffffffff9451c538 of 8 bytes by task 66773 on cpu 19:
  __vm_enough_memory+0x199/0x260
  percpu_counter_read_positive at include/linux/percpu_counter.h:81
  (inlined by) __vm_enough_memory at mm/util.c:839
  mmap_region+0x1b2/0xa10
  do_mmap+0x45c/0x700
  vm_mmap_pgoff+0xc0/0x130
  ksys_mmap_pgoff+0x6e/0x300
  __x64_sys_mmap+0x33/0x40
  do_syscall_64+0x91/0xb05
  entry_SYSCALL_64_after_hwframe+0x49/0xbe

The read is outside percpu_counter::lock critical section which results in
a data race.  Fix it by adding a READ_ONCE() in
percpu_counter_read_positive() which could also service as the existing
compiler memory barrier.

Signed-off-by: Qian Cai <cai@lca.pw>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Marco Elver <elver@google.com>
Link: http://lkml.kernel.org/r/1582302724-2804-1-git-send-email-cai@lca.pw
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 include/linux/percpu_counter.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h
index 73a7bf30fe9a3..3f3cece31148d 100644
--- a/include/linux/percpu_counter.h
+++ b/include/linux/percpu_counter.h
@@ -78,9 +78,9 @@ static inline s64 percpu_counter_read(struct percpu_counter *fbc)
  */
 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
 {
-	s64 ret = fbc->count;
+	/* Prevent reloads of fbc->count */
+	s64 ret = READ_ONCE(fbc->count);
 
-	barrier();		/* Prevent reloads of fbc->count */
 	if (ret >= 0)
 		return ret;
 	return 0;
-- 
2.20.1


  parent reply	other threads:[~2020-04-15 12:08 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-15 11:46 [PATCH AUTOSEL 4.14 01/30] clk: at91: usb: continue if clk_hw_round_rate() return zero Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 02/30] power: supply: bq27xxx_battery: Silence deferred-probe error Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 03/30] clk: tegra: Fix Tegra PMC clock out parents Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 04/30] soc: imx: gpc: fix power up sequencing Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 05/30] rtc: 88pm860x: fix possible race condition Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 06/30] NFSv4/pnfs: Return valid stateids in nfs_layout_find_inode_by_stateid() Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 07/30] NFS: direct.c: Fix memory leak of dreq when nfs_get_lock_context fails Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 08/30] s390/cpuinfo: fix wrong output when CPU0 is offline Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 09/30] btrfs: handle NULL roots in btrfs_put/btrfs_grab_fs_root Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 10/30] powerpc/maple: Fix declaration made after definition Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 11/30] ext4: do not commit super on read-only bdev Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 12/30] ext4: fix incorrect group count in ext4_fill_super error message Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 13/30] ext4: fix incorrect inodes per group in " Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 14/30] slcan: Don't transmit uninitialized stack data in padding Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 15/30] net: stmmac: dwmac1000: fix out-of-bounds mac address reg setting Sasha Levin
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 16/30] include/linux/swapops.h: correct guards for non_swap_entry() Sasha Levin
2020-04-15 11:46 ` Sasha Levin [this message]
2020-04-15 11:46 ` [PATCH AUTOSEL 4.14 18/30] compiler.h: fix error in BUILD_BUG_ON() reporting Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 19/30] KVM: s390: vsie: Fix possible race when shadowing region 3 tables Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 20/30] net: dsa: bcm_sf2: Ensure correct sub-node is parsed Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 21/30] x86: ACPI: fix CPU hotplug deadlock Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 22/30] net: phy: micrel: kszphy_resume(): add delay after genphy_resume() before accessing PHY registers Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 23/30] drm/amdkfd: kfree the wrong pointer Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 24/30] NFS: Fix memory leaks in nfs_pageio_stop_mirroring() Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 25/30] mfd: dln2: Fix sanity checking for endpoints Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 26/30] iommu/vt-d: Fix mm reference leak Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 27/30] ext2: fix empty body warnings when -Wextra is used Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 28/30] ext2: fix debug reference to ext2_xattr_cache Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 29/30] libnvdimm: Out of bounds read in __nd_ioctl() Sasha Levin
2020-04-15 11:47 ` [PATCH AUTOSEL 4.14 30/30] iommu/amd: Fix the configuration of GCR3 table root pointer Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200415114711.15381-17-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=cai@lca.pw \
    --cc=elver@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).