From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D5483EA66 for ; Tue, 4 Aug 2026 00:48:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785804500; cv=none; b=tuFFMqTeKODKXr/DL295/aVsjly7Fnm+rNS4klOuH6+lKrBCDzukjmCsVctKNnSsKVB9At6v/3EqiWV+JjG1oD5o4/3lQfzJab8NTixiUmHIpW+DRilPdYb4aBI+foBihj9oUHVo8WEJdagXIY3F/uRljyJVrdio73oiFm3raNQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785804500; c=relaxed/simple; bh=jpM6VNElAkj9rK433z5K/bvX14Yt0etN35TeN0sgFyU=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=ZszDgrxQp/Lw5ZjifyCUgMl5PB+mlVdnLAigXgDugwKGJKcgcMGD0oH+rcz2AZVzoaL0k2Ikz+pwECGRrfS48sR5c3sD332tdqM4zDuOY8Ibw0GsqWqtT2WEjxZQ6nsOQZV7UIL7XJof8vtsK8c53+uld8NiKC6vxw3V4KGmmIs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=0ul8hbrq; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="0ul8hbrq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 855DB1F000E9; Tue, 4 Aug 2026 00:48:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1785804498; bh=O4oN01pKxXleD62/K3oM93oB2liXo28WHpZIpaHzpd0=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=0ul8hbrqIKeDIJj9barxvxfqz6Ous+6nX1Dg7PAeNaYGmXf9ojrUfuE6gfaFMJMH5 +aQXGg4Se7W4LkDwINcyz5zhbFC3O80CX7bh3dj5Iif9qiHc/owJbYIfUKiuhSoeJ6 QomfVXit1dnk3Wlw0nlBKveNOnMrwoOuTyx1/Biw= Date: Mon, 3 Aug 2026 17:48:17 -0700 From: Andrew Morton To: Jianlin Shi Cc: linux-mm@kvack.org, vbabka@kernel.org, hannes@cmpxchg.org, surenb@google.com, mhocko@suse.com, jackmanb@google.com, ziy@nvidia.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4] mm/page_alloc: only update lowmem_reserve_ratio on sysctl write Message-Id: <20260803174817.faeebcc4fc67a053a47548b4@linux-foundation.org> In-Reply-To: References: X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 2 Aug 2026 22:57:07 +0800 Jianlin Shi wrote: > lowmem_reserve_ratio_sysctl_handler() ignores the return value of > proc_dointvec_minmax() and always calls setup_per_zone_lowmem_reserve(), > even for read operations. > > Fix three issues: > > 1. Propagate errors from proc_dointvec_minmax() instead of always > returning success. For example, writing non-integer garbage to the > sysctl now returns an error instead of silently succeeding with > unchanged values. > > 2. Only call setup_per_zone_lowmem_reserve() when the sysctl is > actually written, matching the write-only refresh pattern of > min_free_kbytes and watermark_scale_factor handlers. > > 3. On write, parse into a temporary ratio[] array and only copy into > sysctl_lowmem_reserve_ratio[] and refresh derived state after the > full vector is validated. This avoids leaving the ratio array > partially updated while skipping setup when proc_dointvec_minmax() > returns an error on a later element (suggested by Andrew Morton). > > Drop the manual "< 1 -> 0" sanitization loop and set .extra1 = > SYSCTL_ZERO on the ctl_table entry so proc_dointvec_minmax() enforces > the minimum on write; negative values now return -EINVAL instead of > being silently coerced to 0 (suggested by Vlastimil Babka). > > ... > > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -6673,8 +6673,8 @@ static int sysctl_min_slab_ratio_sysctl_handler(const struct ctl_table *table, i > > /* > * lowmem_reserve_ratio_sysctl_handler - just a wrapper around > - * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve() > - * whenever sysctl_lowmem_reserve_ratio changes. > + * proc_dointvec_minmax() so that we can call > + * setup_per_zone_lowmem_reserve() when the sysctl is written. > * > * The reserve ratio obviously has absolutely no relation with the > * minimum watermarks. The lowmem reserve ratio can only make sense > @@ -6683,16 +6683,23 @@ static int sysctl_min_slab_ratio_sysctl_handler(const struct ctl_table *table, i > static int lowmem_reserve_ratio_sysctl_handler(const struct ctl_table *table, > int write, void *buffer, size_t *length, loff_t *ppos) > { > - int i; > + struct ctl_table tmp = *table; > + int ratio[MAX_NR_ZONES]; > + int rc; > > - proc_dointvec_minmax(table, write, buffer, length, ppos); > + if (!write) > + return proc_dointvec_minmax(table, write, buffer, length, ppos); > > - for (i = 0; i < MAX_NR_ZONES; i++) { > - if (sysctl_lowmem_reserve_ratio[i] < 1) > - sysctl_lowmem_reserve_ratio[i] = 0; > - } > + memcpy(ratio, sysctl_lowmem_reserve_ratio, sizeof(ratio)); > + tmp.data = ratio; > > + rc = proc_dointvec_minmax(&tmp, write, buffer, length, ppos); > + if (rc) > + return rc; > + > + memcpy(sysctl_lowmem_reserve_ratio, ratio, sizeof(ratio)); > setup_per_zone_lowmem_reserve(); > + > return 0; > } > > @@ -6791,6 +6798,7 @@ static const struct ctl_table page_alloc_sysctl_table[] = { > .maxlen = sizeof(sysctl_lowmem_reserve_ratio), > .mode = 0644, > .proc_handler = lowmem_reserve_ratio_sysctl_handler, > + .extra1 = SYSCTL_ZERO, > }, > #ifdef CONFIG_NUMA lgtm, thanks. I find this a little tidier: --- a/mm/page_alloc.c~mm-page_alloc-only-update-lowmem_reserve_ratio-on-sysctl-write-fix +++ a/mm/page_alloc.c @@ -6926,7 +6926,7 @@ static int lowmem_reserve_ratio_sysctl_h int write, void *buffer, size_t *length, loff_t *ppos) { struct ctl_table tmp = *table; - int ratio[MAX_NR_ZONES]; + int ratio[ARRAY_SIZE(sysctl_lowmem_reserve_ratio)]; int rc; if (!write) A bit more self-documenting and future-proof. What do you think?