Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp404339pxj; Fri, 11 Jun 2021 02:01:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXO86i0/m1p/O0XIthcjNg/U6770JIENJ9BRfGy/nM1dIKVtccwhDGNifrHnFb2Rkh+tqu X-Received: by 2002:a17:906:1682:: with SMTP id s2mr2689583ejd.172.1623402074232; Fri, 11 Jun 2021 02:01:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623402074; cv=none; d=google.com; s=arc-20160816; b=t3WbdnyRnKkVXGRGHJL9Tv363p5wANzlEe9Om2O3ejMwyeAUELHm25Z6Cdq0MC/Scn emNtvVk7/3jsdQyKlzIg7iMQ57NPMh2HW/QOcBwCb3Lg7V7dJzGoSkDX6fgFiDVWmPsJ u72tUEuk7EqwNrbbECM7pzK7Cba+eJfTYQukxz6pUAkhw4V9MsZTqmE6e+mvUwEjbDKn 9cH9bq4w8iSGJPKRAEqALXOtvdm5P1zMHQSAyIaeaLJIyXiz5PScfQwEHy97q9MXaJab zD3WOU+1YooHsZhypgCBR7FyMIERizx1D5tC/sFuTw3JEGrJYlSg1bCJkx5E+lIogRjj ic8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=fm4NieyZO1B6E6vbYeXiPNjsnE4SfIY6ysKXOfsWV1A=; b=ekp98dqtDp9Q8/HKGFu+J8PNLpN96AWvHkYqqxguA6FgM/gw6rlPVCWIA5Vvf2pd9p eYMH8nOkHBepdWQWmfBXzR1x3LmnfjrROvfA8rziLuc0rQVcC7HNbf6JNERwLNy4Uy2X DTH9OrFNmrNLkbGaaWXT0FXElylp80bRTiCHmgFNSk0gNb/tYz5wSH1GavZ3rXzZDmnQ Q4f42Chvl79QqK8dMt5B5AXzbi1N5D4gOuXfueeX++z/Syk8+ZN+IKsW15LaECjm+Rdb 8YnuD+6JHPNUa94Ec8Q+xaTeXd4osuemXPESwxqpue+usf85tGlfubP/TcUgf1tXmGdm ZYmg== 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=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b29si4881481ejl.339.2021.06.11.02.00.51; Fri, 11 Jun 2021 02:01:14 -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; 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=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231445AbhFKJBR (ORCPT + 99 others); Fri, 11 Jun 2021 05:01:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:60262 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231458AbhFKJBO (ORCPT ); Fri, 11 Jun 2021 05:01:14 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0B4A660BD3; Fri, 11 Jun 2021 08:59:15 +0000 (UTC) Date: Fri, 11 Jun 2021 09:59:13 +0100 From: Catalin Marinas To: Yanfei Xu Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/kmemleak: use READ_ONCE() for accessing jiffies_scan_wait Message-ID: <20210611085913.GA8132@arm.com> References: <20210609155657.26972-1-yanfei.xu@windriver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210609155657.26972-1-yanfei.xu@windriver.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 09, 2021 at 11:56:57PM +0800, Yanfei Xu wrote: > The stop_scan_thread() and start_scan_thread() cannot really solve > the problem of concurrent accessing the global jiffies_scan_wait. > > kmemleak_write kmemleak_scan_thread > while (!kthread_should_stop()) > stop_scan_thread > jiffies_scan_wait = xxx timeout = jiffies_scan_wait > start_scan_thread > > We could replace these with a READ_ONCE() when reading > jiffies_scan_wait. It also can prevent compiler from reordering the > jiffies_scan_wait which is in while loop. I'm ok with READ_ONCE but your patch introduces functional changes. > diff --git a/mm/kmemleak.c b/mm/kmemleak.c > index 92a2d4885808..5ccf3969b7fe 100644 > --- a/mm/kmemleak.c > +++ b/mm/kmemleak.c > @@ -1567,7 +1567,7 @@ static int kmemleak_scan_thread(void *arg) > } > > while (!kthread_should_stop()) { > - signed long timeout = jiffies_scan_wait; > + signed long timeout = READ_ONCE(jiffies_scan_wait); > > mutex_lock(&scan_mutex); > kmemleak_scan(); > @@ -1812,11 +1812,8 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf, > ret = kstrtoul(buf + 5, 0, &secs); > if (ret < 0) > goto out; > - stop_scan_thread(); > - if (secs) { > + if (secs) > jiffies_scan_wait = msecs_to_jiffies(secs * 1000); For symmetry, I'd add a WRITE_ONCE here as well. > - start_scan_thread(); > - } The reason for stop/start_scan_thread() wasn't to protect against jiffies_scan_wait access but rather to force a new delay. Let's say you start by default with a 10min delay between scans (default) but you want to lower it to 1min. With the above removal of stop/start, you'd still have to wait for 10min until the scanning thread will notice the change. Also, with secs=0, the expectations is that the thread won't be restarted but this is removed by your patch. -- Catalin