From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Arnd Bergmann <arnd@kernel.org>,
Gregory Price <gourry@gourry.net>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>, larips <larips950@gmail.com>,
stable@vger.kernel.org, Zi Yan <ziy@nvidia.com>,
Joshua Hahn <joshua.hahnjy@gmail.com>,
Rakie Kim <rakie.kim@sk.com>,
Ying Huang <ying.huang@linux.alibaba.com>,
"Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>,
linux-mm@kvack.org
Subject: Re: [PATCH 2/2] [v3] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes()
Date: Sat, 21 Nov 2026 13:41:12 +0100 [thread overview]
Message-ID: <bc42fbba-1c35-4d81-b28f-6b1324b34ebd@kernel.org> (raw)
In-Reply-To: <20261120185248.823415-2-arnd@kernel.org>
On 11/20/26 19:52, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> The get_nodes() function handles copying node masks from userspace,
> while copy_nodes_to_user() copies the same masks to userspace, but
> with a different argument order and slightly different meaning of the
> same variables.
>
> To make that less confusing, change copy_nodes_to_user() to look more
> like get_nodes(), using similar name and arguments as well as adjusting
> the maxnode value once, and restructure it for readability.
Could briefly mention here the change in get_nodes() for consistency.
>
> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
> mm/mempolicy.c | 48 +++++++++++++++++++++++++++++-------------------
> 1 file changed, 29 insertions(+), 19 deletions(-)
>
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index afe614efb77c..b240acaa8cf1 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -1736,7 +1736,9 @@ static int get_bitmap(unsigned long *mask, const unsigned long __user *nmask,
> static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
> unsigned long maxnode)
> {
> - --maxnode;
> + /* User space passes the number of bits plus 1. Fix that up. */
> + maxnode -= 1;
> +
> nodes_clear(*nodes);
> if (maxnode == 0 || !nmask)
> return 0;
> @@ -1769,31 +1771,39 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
> }
>
> /* Copy a kernel node mask to user space */
> -static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
> - nodemask_t *nodes)
> +static int put_nodes(unsigned long __user *mask, nodemask_t *nodes,
> + unsigned long maxnode)
Two tabs here here is the preferred way in MM. Can fix that up when applying.
> {
> - unsigned long copy = ALIGN(maxnode-1, BITS_PER_LONG) / 8;
> - unsigned int nbytes = BITS_TO_LONGS(nr_node_ids) * sizeof(long);
> - bool compat = in_compat_syscall();
> + const bool compat = in_compat_syscall();
> + const unsigned int word_bits = compat ? BITS_PER_COMPAT_LONG : BITS_PER_LONG;
> + unsigned long bits, node_bits;
>
> - if (compat) {
> - copy = ALIGN(maxnode-1, BITS_PER_COMPAT_LONG) / 8;
> - nbytes = BITS_TO_COMPAT_LONGS(nr_node_ids) * sizeof(compat_long_t);
> - }
> + /* User space passes the number of bits plus 1. Fix that up. */
> + maxnode -= 1;
>
> - if (copy > nbytes) {
> - if (copy > PAGE_SIZE)
> - return -EINVAL;
> - if (clear_user((char __user *)mask + nbytes, copy - nbytes))
> + /* Don't exceed what would fit into a single page. */
> + if (maxnode > PAGE_SIZE * BITS_PER_BYTE)
> + return -EINVAL;
> +
> + /* Process full words, which depend on the user space ABI. */
> + bits = ALIGN(maxnode, word_bits);
> + node_bits = ALIGN(nr_node_ids, word_bits);
> +
> + /* Clear anything we won't copy below. */
> + if (bits > node_bits) {
> + if (clear_user((char __user *)mask + node_bits / BITS_PER_BYTE,
> + (bits - node_bits) / BITS_PER_BYTE))
> return -EFAULT;
> - copy = nbytes;
> + bits = node_bits;
> }
>
> + /* Finally, copy the bitmap. */
> if (compat)
> return compat_put_bitmap((compat_ulong_t __user *)mask,
> - nodes_addr(*nodes), copy * 8);
> -
> - return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
> + nodes_addr(*nodes), bits);
> + if (copy_to_user(mask, nodes_addr(*nodes), bits / BITS_PER_BYTE))
> + return -EFAULT;
> + return 0;
> }
>
> /* Basic parameter sanity check used by both mbind() and set_mempolicy() */
> @@ -2054,7 +2064,7 @@ static int kernel_get_mempolicy(int __user *policy,
> return -EFAULT;
>
> if (nmask)
> - err = copy_nodes_to_user(nmask, maxnode, &nodes);
> + err = put_nodes(nmask, &nodes, maxnode);
>
> return err;
> }
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
next prev parent reply other threads:[~2026-11-21 12:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 18:52 [PATCH 1/2] [v3] mm/mempolicy: fix copy_nodes_to_user() for odd inputs Arnd Bergmann
2026-11-20 18:52 ` [PATCH 2/2] [v3] mm/mempolicy: replace copy_nodes_to_user() interface with put_nodes() Arnd Bergmann
2026-11-21 12:41 ` David Hildenbrand (Arm) [this message]
2026-11-21 12:39 ` [PATCH 1/2] [v3] mm/mempolicy: fix copy_nodes_to_user() for odd inputs David Hildenbrand (Arm)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bc42fbba-1c35-4d81-b28f-6b1324b34ebd@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=arnd@kernel.org \
--cc=gourry@gourry.net \
--cc=hannes@cmpxchg.org \
--cc=joshua.hahnjy@gmail.com \
--cc=larips950@gmail.com \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=rakie.kim@sk.com \
--cc=stable@vger.kernel.org \
--cc=ying.huang@linux.alibaba.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox