Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp727326ybc; Tue, 12 Nov 2019 08:18:18 -0800 (PST) X-Google-Smtp-Source: APXvYqwUG1sdmwdPej4mSKCZ85GTHZj+tgkBmPduKlFDgY2phz6/g/Clsd6G3zF4CAY7BPnVU7+e X-Received: by 2002:a50:c191:: with SMTP id m17mr33903878edf.259.1573575498665; Tue, 12 Nov 2019 08:18:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573575498; cv=none; d=google.com; s=arc-20160816; b=EI76j4UUjADQ3zvKPdVUDRG0lTauWvyFeKmRjRcsruGbSFwf/2BTJL8vbUm6V9sqso JUfBErSc1FhchwXkyXaBIqUUoiYe0fU1zqRgt8StTMBWpyJGmGo1dKj3SAqOIrpzI+ZC b+uHtQpXj77cBDX7Wb5qM3leV+A1hihU/Lyom2IN3X6wmIa7EzfRYQ4cv7f6h0aya2vg bHZ/vRwo5FoGLCplqc4Oa8S45K5K1EXJDaxcouhD4zruCm0LldvOozrneWa8o9MslTg9 PpOqbrXWldYY7xzY5q8U8tI6W7vq6rOK/oiplZRvp9J6OByyAcKyRFVFBAyeKHkaQKjY 7GiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=twt8kJzhtPFZcAO1BhdHJtV6MHvkk/INbWMflmbiAcs=; b=mNtR7UdTHyiqI+JV6X97k7ifl+qRuoUeWjzzOzG+RAeHhgYEZQ+o+UeugAnNi9Lf65 Q2jraXsEPDwskKohLb/LrMjHf5FDfWaUSQaXShJ1ol4W+eTdj2wm+VfZUfhcBbKj/vWU m+c7QTchgXTccgCvQmp0APDqoeqNhP73C4Ka8AjmS9H9pzSXmZy7w6JGtuavv21V3bFQ tR+SivGH/7U9rzIsqfFvjRyV1pmIiPZL84HR8W7j4KVQjhEugAGwOC+hyT8n0jxoec12 360EESYdTpPtZhpPB2c0YbZkCsGjjMMmPB6YE/IYGW/4cNBFiYAzPrz4IBm6FvfaoYfO gIIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=wOJG7TKZ; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s7si15117572eds.215.2019.11.12.08.17.54; Tue, 12 Nov 2019 08:18:18 -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=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=wOJG7TKZ; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727137AbfKLQRD (ORCPT + 99 others); Tue, 12 Nov 2019 11:17:03 -0500 Received: from mail-qt1-f193.google.com ([209.85.160.193]:35752 "EHLO mail-qt1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726994AbfKLQRC (ORCPT ); Tue, 12 Nov 2019 11:17:02 -0500 Received: by mail-qt1-f193.google.com with SMTP id n4so15718302qte.2 for ; Tue, 12 Nov 2019 08:17:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=twt8kJzhtPFZcAO1BhdHJtV6MHvkk/INbWMflmbiAcs=; b=wOJG7TKZxJq4CpTPsx5BBiOVlFuqacNBz9W6xm0qACo/wundYrFHiI0ekBHcU+WcLW 0T9Dxdd/2RVxSjtTw3wgLA9Jj54sD1igfoXfVt4ArPEHkPRgdxcedv0G6T9xusV04IOg RX4muJAyIWzmgRS+D8J1xPyY3yxJgaSS+nf7HgsHNAz83ChbKjYS/W/wai49fs0hKxyY Lg667V1xFa+C2jeV2Dt/miS158NRuX3nDBQAqtQ/hYtjq7JES/R7BV76jHKNjPOrLJVz MbpydXjQrDmqeq7BhMOdhQViCt1nzM6u86VU4s/s5OLqaOwJjFhY5KC8x/eib7GMDP2K qFFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=twt8kJzhtPFZcAO1BhdHJtV6MHvkk/INbWMflmbiAcs=; b=qe/EGrgOeqUpWKUkmEPvoFQw4/Hovr0sTx3u4VywYg7ITP24//lh9o56A4O4fYSv4G KK1jy9PYNpSFk7ePJ/qSZHQeZZfRLgJ8yvCUH6w/EpoHhufgp2tcwS+duRGbD5plp5ZX LRKac/fuhXoaz1eoWLBP4PfDbFQtapkIaSI8bz08XieOtCSjrCqybkVW5zPkLD1Znbzm IMf/PIggVtxoYVzE2BWIzQ3IgxptZUVaS6kzKZzxpk0NJhLemxVtNDEwbC/ewwuRLxKI yTwjtcqvNNZFyCxUFM6hHJAaGW9+M0UNtG9+4EWE9N67M+P2j+RUVJRM57XZMedi/eqb 7dPw== X-Gm-Message-State: APjAAAVqwvS73Sy9X43UvmjJJDIJ+HOk7C/MAXT1hQ1NBilqFCWBAu06 lSEfQdAQUfCBDjwJ+dIvnQJTSw== X-Received: by 2002:ac8:2361:: with SMTP id b30mr31934126qtb.322.1573575420020; Tue, 12 Nov 2019 08:17:00 -0800 (PST) Received: from localhost ([2620:10d:c091:500::aa8c]) by smtp.gmail.com with ESMTPSA id v186sm8821612qkb.42.2019.11.12.08.16.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2019 08:16:59 -0800 (PST) Date: Tue, 12 Nov 2019 11:16:58 -0500 From: Johannes Weiner To: Michal Hocko Cc: Chris Down , Qian Cai , akpm@linux-foundation.org, guro@fb.com, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH -next] mm/vmscan: fix an undefined behavior for zone id Message-ID: <20191112161658.GF168812@cmpxchg.org> References: <20191108204407.1435-1-cai@lca.pw> <64E60F6F-7582-427B-8DD5-EF97B1656F5A@lca.pw> <20191111130516.GA891635@chrisdown.name> <20191111131427.GB891635@chrisdown.name> <20191111132812.GK1396@dhcp22.suse.cz> <20191112145942.GA168812@cmpxchg.org> <20191112152750.GA512@dhcp22.suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191112152750.GA512@dhcp22.suse.cz> User-Agent: Mutt/1.12.2 (2019-09-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 12, 2019 at 04:27:50PM +0100, Michal Hocko wrote: > On Tue 12-11-19 06:59:42, Johannes Weiner wrote: > > Qian, thanks for the report and the fix. > > > > On Mon, Nov 11, 2019 at 02:28:12PM +0100, Michal Hocko wrote: > > > On Mon 11-11-19 13:14:27, Chris Down wrote: > > > > Chris Down writes: > > > > > Ah, I just saw this in my local checkout and thought it was from my > > > > > changes, until I saw it's also on clean mmots checkout. Thanks for the > > > > > fixup! > > > > > > > > Also, does this mean we should change callers that may pass through > > > > zone_idx=MAX_NR_ZONES to become MAX_NR_ZONES-1 in a separate commit, then > > > > remove this interim fixup? I'm worried otherwise we might paper over real > > > > issues in future. > > > > > > Yes, removing this special casing is reasonable. I am not sure > > > MAX_NR_ZONES - 1 is a better choice though. It is error prone and > > > zone_idx is the highest zone we should consider and MAX_NR_ZONES - 1 > > > be ZONE_DEVICE if it is configured. But ZONE_DEVICE is really standing > > > outside of MM reclaim code AFAIK. It would be probably better to have > > > MAX_LRU_ZONE (equal to MOVABLE) and use it instead. > > > > We already use MAX_NR_ZONES - 1 everywhere else in vmscan.c to mean > > "no zone restrictions" - get_scan_count() is the odd one out: > > > > - mem_cgroup_shrink_node() > > - try_to_free_mem_cgroup_pages() > > - balance_pgdat() > > - kswapd() > > - shrink_all_memory() > > > > It's a little odd that it points to ZONE_DEVICE, but it's MUCH less > > subtle than handling both inclusive and exclusive range delimiters. > > > > So I think the better fix would be this: > > lruvec_lru_size is explicitly documented to use MAX_NR_ZONES for all > LRUs and git grep says there are more instances outside of > get_scan_count. So all of them have to be fixed. Which ones? [hannes@computer linux]$ git grep lruvec_lru_size include/linux/mmzone.h:extern unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx); mm/vmscan.c: * lruvec_lru_size - Returns the number of pages on the given LRU list. mm/vmscan.c:unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) mm/vmscan.c: anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES - 1) + mm/vmscan.c: lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES - 1); mm/vmscan.c: file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES - 1) + mm/vmscan.c: lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES - 1); mm/vmscan.c: lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx); [hannes@computer linux]$ The only other user already passes sc->reclaim_idx, which always points to a valid zone, and is initialized to MAX_NR_ZONES - 1 in many places. > I still think that MAX_NR_ZONES - 1 is a very error prone and subtle > construct IMHO and an alias would be better readable. I wouldn't mind a follow-up patch that changes this pattern comprehensively. As it stands, get_scan_count() is the odd one out. The documentation bit is a good point, though. We should fix that. Updated patch: --- From b1b6ce306010554aba6ebd7aac0abffc1576d71a Mon Sep 17 00:00:00 2001 From: Johannes Weiner Date: Mon, 11 Nov 2019 13:46:25 -0800 Subject: [PATCH] mm: vmscan: simplify lruvec_lru_size() fix get_scan_count() passes MAX_NR_ZONES for the reclaim index, which is beyond the range of valid zone indexes, but used to be handled before the patch. Every other callsite in vmscan.c passes MAX_NR_ZONES - 1 to express "all zones, please", so do the same here. Reported-by: Qian Cai Reported-by: Chris Down Signed-off-by: Johannes Weiner --- mm/vmscan.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index df859b1d583c..5eb96a63ad1e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -323,7 +323,7 @@ unsigned long zone_reclaimable_pages(struct zone *zone) * lruvec_lru_size - Returns the number of pages on the given LRU list. * @lruvec: lru vector * @lru: lru to use - * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list) + * @zone_idx: index of the highest zone to include (use MAX_NR_ZONES - 1 for all) */ unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx) { @@ -2322,10 +2322,10 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, * anon in [0], file in [1] */ - anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) + - lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES); - file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) + - lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES); + anon = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES - 1) + + lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES - 1); + file = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES - 1) + + lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES - 1); spin_lock_irq(&pgdat->lru_lock); if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { -- 2.24.0