Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp121676ybl; Thu, 30 Jan 2020 18:20:38 -0800 (PST) X-Google-Smtp-Source: APXvYqzXLj3Gna4oWNcH30CUxADBEy2keJPnPiZxBnT5lZsBqGTBwExVNlR/Jzi43YN1XzPXW1sE X-Received: by 2002:aca:b1d5:: with SMTP id a204mr4828547oif.82.1580437238649; Thu, 30 Jan 2020 18:20:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580437238; cv=none; d=google.com; s=arc-20160816; b=R3SX9lXQJRK5z6A29i7CDSkBbmAdr2DC0pLzZK3T8CFjzZ69Vrktrt8q99+q3EYxAX 1wFx+i8le/EwxT5Mn5WdfaQijmqoV81q1Hs7TDoJ2mb9Weq/MGqhGuDO3DTF+tHMcuvL ZKhiPC8yhkIMH9iGXG0AoATIjtVACV+XKT55vwkoZ6xqXwklbfzpwwYhcelvyG0taJDi OFUqXiKN8YuKvIJ85bpKFMYZS6jeD8gIfZjUfk4Q2rxz6mNBhjcdpCUDff3wdObCwToQ wdOy+aTimvH9JbtGYWKGYKlP6Jp0GNFiMq//3aQWz6IRXMkKf50t1VdiKymMMcyEiv+L /8NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=Tv0sb6P4aO0VfeVg6Ezix8dtWOXTHqHz8syGNVIcKsk=; b=K2W9Nkbx5TrOyThb9y78vPsPDxEsMnavNuGDFdAp/xCwIW19F72DKMVWZwqEi4vf6a TzbzszlT8CUdvJb3EI7Jre0ffcQ5AYZogcfOaZXFyP5aZqxhb8rPE2UZfjd+BOevDsHp kPTOqiSWK2e+ieTGhqDni9rT/zYAZar+V8b7EPlVUV/Smjtpsk6TOyUFma4qmjFC7es9 bkLW7Ov0gwMSUY0bjsksairWdT8hX15a+Y5sVCChbp5vGGf6BxLW9LUIRKv8ps1b7CQO AyYI/PCxNXX8fQ5YRDxUY9ZXZ5zxNZ58BY38XpHAWM/CFFnANivxBW4QG2cHIekFICxg Zf2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eae+eNxY; 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 j1si3814472otr.207.2020.01.30.18.20.24; Thu, 30 Jan 2020 18:20:38 -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=@kernel.org header.s=default header.b=eae+eNxY; 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 S1727878AbgAaCSi (ORCPT + 99 others); Thu, 30 Jan 2020 21:18:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:50124 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727741AbgAaCSh (ORCPT ); Thu, 30 Jan 2020 21:18:37 -0500 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4E478206F0; Fri, 31 Jan 2020 02:18:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580437115; bh=pZJmJTyhSz18bLrQ2/dhn2g7VLznLenFg9l2cjRSjBM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=eae+eNxY53/34/Xj41ogh1eNWzPt3CuS3icd2Nnz4wsszfgQfFIHkZgGlqlGnlRiV jfJ+fgE+ng/iNr0jgOoDzmUhwbRp9MRk4ypKv1emeGlge5LUq4OwwNZJT52M7WCYGp x60f2KmIEGy8npPLiemBH9VcUgSAeWQNFIaC9JR4= Date: Thu, 30 Jan 2020 18:18:34 -0800 From: Andrew Morton To: Marco Elver Cc: Qian Cai , Matthew Wilcox , dennis@kernel.org, tj@kernel.org, Christoph Lameter , Linux Memory Management List , LKML Subject: Re: [PATCH] mm/util: fix a data race in __vm_enough_memory() Message-Id: <20200130181834.633c201c7d0a2638aacbc7ba@linux-foundation.org> In-Reply-To: References: <20200130042011.GI6615@bombadil.infradead.org> <1135BD67-4CCB-4700-8150-44E7E323D385@lca.pw> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 30 Jan 2020 13:35:18 +0100 Marco Elver wrote: > On Thu, 30 Jan 2020 at 12:50, Qian Cai wrote: > > > > > On Jan 29, 2020, at 11:20 PM, Matthew Wilcox wrote: > > > > > > 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()? > > > > 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. > > > > However, I had also copied the percpu maintainers to see if they have any preferences? > > I would not add READ_ONCE to percpu_counter_read(), given the writes > (increments) are not atomic either, so not much is gained. > > 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). > > 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. > > 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. > That, or add an alternative version of per_cpu_counter_read() to the percpu API. A very carefully commented version!