Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp123698ybl; Thu, 30 Jan 2020 18:23:43 -0800 (PST) X-Google-Smtp-Source: APXvYqxXPqBXcix85+ImJM3yLy+EFl4tyFk470KsH6KBz96YTLYGQFw8fBiKG9MrhSSmzv2qrp2l X-Received: by 2002:a05:6808:244:: with SMTP id m4mr5008783oie.125.1580437423374; Thu, 30 Jan 2020 18:23:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580437423; cv=none; d=google.com; s=arc-20160816; b=MDE3fa+Ut+hRLIPdjvzHrAbn7cwSLajtHTYNwWAdB3x8k4LCILHyieIfhRKkL1UaHR KwjQRkwK6DvtvK30tuHP92tgQyPxhldY5bKv525yROf1ICP7xB2QIQbBwRgZv2dAyLqw JqHdZ7ARcQpPed6QyY6OZ2MSWywsSs4kuLRJ4+n6X48wGojI8WPbbSIJ1unzwQcurwv3 LF4LI8ntIBq9EeBRhj9WlVEbvuSdwlOCghrY/cI1q7m+oxGFfHYs55o3QKrVO8zXckFG BI7qQU+7K+XwCOSCTsBkruo8EwSbipbq1YDPgLf/LXcJSSx7C+ogVRLpwSDVuaYYzS5V Lu4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=WL0FLgSacIAqtCGMkthh4uOIeyhw7X/ubIyKLP7UcMQ=; b=AY0AlrVD8RAWuFOh/TNYsVPwLZ8mgWT8AWOT8G+BytlWPSEP7m0rCyXL5LDtwSNrff LLhv4WLIhz1Vf4+QVYQxCqhcNQizV5cPjDBBexQjqO4Zo51wAz7sD2HKZbPHUfi5MTFs g4uxnAzOMw34TeNKHl8TdFp4iIRkGJimzbObxOr9WGUiMS6Wj60vFXQc6QFqAaElFPns pcru52UV4+y5anoPdpQRIa8OhufyvWLsEPYptn/Gi1I3MrAuqLDDPElQo/SKaHRxKzen PdoVMEDPp3SCaINsttF5Wt1xIGBVn938/+vgQbIh/LGBYcugB4WQBcQyPzZZEjRBXTu8 J9Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=ZAGlDzEg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h18si4015399otk.227.2020.01.30.18.23.29; Thu, 30 Jan 2020 18:23:43 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=ZAGlDzEg; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727976AbgAaCWI (ORCPT + 99 others); Thu, 30 Jan 2020 21:22:08 -0500 Received: from mail-qk1-f194.google.com ([209.85.222.194]:40008 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727963AbgAaCWI (ORCPT ); Thu, 30 Jan 2020 21:22:08 -0500 Received: by mail-qk1-f194.google.com with SMTP id t204so5127013qke.7 for ; Thu, 30 Jan 2020 18:22:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=WL0FLgSacIAqtCGMkthh4uOIeyhw7X/ubIyKLP7UcMQ=; b=ZAGlDzEgoEB6R7NedCVOXEjZKZzovnmhhKhJX7mrctCT0WxPjz2pT7AYk70aYaUSXV YOLsGZRFqCjyLeHfBbb+3oTJ5bhG+C5v9YugT1VlbFlVKRIJizIsRgHHbVX8zl+Yo1nK TGkAjNH48Wtqm+0e4kmJN+vtnce23YuHP0G4rns0WRLERVJhRs6UOLYeeBrBzVv16ftN DP0JQkV0BkkFYMpHG9+WC11jTS7Aq+t7RJmkoMrrlVOQdREPeYQfbbk0L22wmclJIUrt 1HnmuAsOqFI1dzq24676j4PRVcC/32mqTtjhlKJQgsWQd7VG4tsbDqzqPvFCYsQ0GsLQ U2FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=WL0FLgSacIAqtCGMkthh4uOIeyhw7X/ubIyKLP7UcMQ=; b=BUGAgSDWgiann1izzvK7fYrsFvE5K0Ls/9IMOVL3AIvSMPcn3RUTNKMZC7iePk0EGX tHLVvMNmlgmrL8MZzza8Qbp+7PW7yOUwyCtE6Ye0nyzdyG8bcghjiwt4Q5z9SnQehyFF xUx/necEOU+0OWVHbQYvn8XySXswCvTzVuS8oihq2Z7h0HbhJ9jwtZ7GHNghnLog+CUa XczHGoVTnEyRJ2+FqWLOQ2Ukmrm7ubGfncZ4OgJpaKz+bskuCqGulxbcD8i8L35nJ/SM Iua4EspBNU0pY8IF1e9WOh6dqF+pHy3oFCcK/enyIzYadw/6X275PIlWAg8ojJI9j2AN VJjw== X-Gm-Message-State: APjAAAVHnOM9190oj9iPvag6Quwx+eOg7Og28IEYpvXswOIz6CYCMxJz fG+yB1OZOcQL9AdDJ4hExoD2BA== X-Received: by 2002:a37:308:: with SMTP id 8mr8346060qkd.98.1580437326892; Thu, 30 Jan 2020 18:22:06 -0800 (PST) Received: from [192.168.1.153] (pool-71-184-117-43.bstnma.fios.verizon.net. [71.184.117.43]) by smtp.gmail.com with ESMTPSA id x3sm3934273qts.35.2020.01.30.18.22.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Jan 2020 18:22:06 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.40.2.2.4\)) Subject: Re: [PATCH] mm/util: fix a data race in __vm_enough_memory() From: Qian Cai In-Reply-To: <20200130181834.633c201c7d0a2638aacbc7ba@linux-foundation.org> Date: Thu, 30 Jan 2020 21:22:05 -0500 Cc: Marco Elver , Matthew Wilcox , dennis@kernel.org, tj@kernel.org, Christoph Lameter , Linux Memory Management List , LKML Content-Transfer-Encoding: quoted-printable Message-Id: <9B86F7FF-C892-4F58-A24E-E0728D2637BC@lca.pw> References: <20200130042011.GI6615@bombadil.infradead.org> <1135BD67-4CCB-4700-8150-44E7E323D385@lca.pw> <20200130181834.633c201c7d0a2638aacbc7ba@linux-foundation.org> To: Andrew Morton X-Mailer: Apple Mail (2.3608.40.2.2.4) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Jan 30, 2020, at 9:18 PM, Andrew Morton = wrote: >=20 > On Thu, 30 Jan 2020 13:35:18 +0100 Marco Elver = wrote: >=20 >> On Thu, 30 Jan 2020 at 12:50, Qian Cai wrote: >>>=20 >>>> On Jan 29, 2020, at 11:20 PM, Matthew Wilcox = wrote: >>>>=20 >>>> I'm really not a fan of exposing the internals of a percpu_counter = outside >>>> the percpu_counter.h file. Why shouldn't this be fixed by putting = the >>>> READ_ONCE() inside percpu_counter_read()? >>>=20 >>> It is because not all places suffer from a data race. For example, = in __wb_update_bandwidth(), it was protected by a lock. I was a bit = worry about blindly adding READ_ONCE() inside percpu_counter_read() = might has unexpected side-effect. For example, it is unnecessary to have = READ_ONCE() for a volatile variable. So, I thought just to keep the = change minimal with a trade off by exposing a bit internal details as = you mentioned. >>>=20 >>> However, I had also copied the percpu maintainers to see if they = have any preferences? >>=20 >> I would not add READ_ONCE to percpu_counter_read(), given the writes >> (increments) are not atomic either, so not much is gained. >>=20 >> Notice that this is inside a WARN_ONCE, so you may argue that a data >> race here doesn't matter to the correct behaviour of the system >> (except if you have panic_on_warn on). >>=20 >> For the warning to trigger, vm_committed_as must decrease. Assume = that >> a data race (assuming bad compiler optimizations) can somehow >> accomplish this, then the load or write must cause a transient value >> to somehow be less than a stable value. My hypothesis is this is very >> unlikely. >>=20 >> Given the fact this is a WARN_ONCE, and the fact that a transient >> decrease in the value is unlikely, you may consider >> 'VM_WARN_ONCE(data_race(percpu_counter_read(&vm_committed_as)) < >> ...)'. That way you won't modify percpu_counter_read and still catch >> unintended races elsewhere. >>=20 >=20 > That, or add an alternative version of per_cpu_counter_read() to the > percpu API. A very carefully commented version! I send a patch to use data_race() which should be sufficient, https://lore.kernel.org/linux-mm/20200130145649.1240-1-cai@lca.pw/=