From patchwork Fri Jul 5 14:32:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13725213 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50E9FC3271E for ; Fri, 5 Jul 2024 14:32:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C06F26B00A1; Fri, 5 Jul 2024 10:32:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BB6FF6B00A2; Fri, 5 Jul 2024 10:32:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E6D96B00A4; Fri, 5 Jul 2024 10:32:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 73BC06B00A1 for ; Fri, 5 Jul 2024 10:32:28 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2F61D4018D for ; Fri, 5 Jul 2024 14:32:28 +0000 (UTC) X-FDA: 82305939576.13.C16F886 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by imf28.hostedemail.com (Postfix) with ESMTP id 7AA58C0006 for ; Fri, 5 Jul 2024 14:32:25 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=ibShQAqn; spf=pass (imf28.hostedemail.com: domain of tursulin@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=tursulin@igalia.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720189910; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vlhZyqR/0gBL25Tskih19pvHfLCMomlyzy7IQpXMJFc=; b=kfpTfkcodgKVYySIM2Tyom6jqJcuOSSTqvyGYJ38l0lfgDR3O3RcDdhtU5HCZtOXd/q13F cxOUOzRLUeZ50xV5ML13N1IbGooueBoG7ogVH1satkvhQ/CXkjvOahP0Ncw/lnmWzdUepP wh50oHIg67d8Inlif/W1CAbJ8X3pwes= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=igalia.com header.s=20170329 header.b=ibShQAqn; spf=pass (imf28.hostedemail.com: domain of tursulin@igalia.com designates 178.60.130.6 as permitted sender) smtp.mailfrom=tursulin@igalia.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720189910; a=rsa-sha256; cv=none; b=IQYQJbI6xGDSsHE5DtSMYC3qxjbL+oZgOB3vXyS3ZBhaN9/I1xYA0SrdHrB3KKYmjIpg7U CG3UtDUPzAaicpAl0MEfvopcyZ2NtuKrmvcaB463H1BDCnVnOLLUtHEHA0j2eCgiJqjHZL UhecqEByUnKCzIcLwtv8KajkUjTEVEU= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=vlhZyqR/0gBL25Tskih19pvHfLCMomlyzy7IQpXMJFc=; b=ibShQAqn8rMZi0rHJYdxrpgBNp Dc/vBE6tzbu6t4UzIrLp8uxUpBrXsI1TJ5rP3mHPdrkRqlyEjzZJDMLD8MI+ksgbHtJFGyHEK59uz KXSTe+/otO9hGmMmGeOUsn7oPBohNzo0V4oKl8kQUEXNad2LxB5RLpSax9EQEbaj0ochtT6SbhsAP 5RsJpwRKKqU6VsdJrqqyM6VdQyPovE/ZeqJd/Hs1f7cGRJ0CMZmykeTvmBR4SESy1RqHYzLFbi9eC mgWC6yOfjEzvFAKhwqpq5/frOIYED6RkUuFpDavhVXZ5+z6Z9Pl21sA3fuIdqXTeFiPmBSPjoIEuh t1F7weyA==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sPjz3-00BSmD-VJ; Fri, 05 Jul 2024 16:32:22 +0200 From: Tvrtko Ursulin To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, kernel-dev@igalia.com, Tvrtko Ursulin , Huang Ying , Andrew Morton Subject: [PATCH 2/3] mm/mempolicy: Use flags lookup array in mpol_to_str Date: Fri, 5 Jul 2024 15:32:17 +0100 Message-ID: <20240705143218.21258-3-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240705143218.21258-1-tursulin@igalia.com> References: <20240705143218.21258-1-tursulin@igalia.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 7AA58C0006 X-Stat-Signature: zcr93mz19f7frnwhog8y1579wm85fxxp X-Rspam-User: X-HE-Tag: 1720189945-566978 X-HE-Meta: U2FsdGVkX19vv3ol0Sufaq9r0bNezfHwJQK42whBEtrj8UPcjz0DUPYpHuYTMkKR6Iea/jrqTOnLdRK/D/joNxvkfG406e/ysGP6XzbBj4Va02w7Qsh0cxpAarEZomrjmceIlqAwKNmAevFVuXngNw7ItNKfjNirzWe0732sj9ZqWJSkReSo6IoWrI2YVjY4cNqZuM+iptX+FqYnTbmlEmmMOEyRKN1xEOVtIW10VdZHdyMXhk9t+hKWm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yDR1 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Tvrtko Ursulin Generalise displaying of flags in mpol_to_str() with the benefit of removing the knowledge of which flags are mutually exclusive and which are not. Also add a warning for unknown flags which slightly "hardens" against possible future omissions to add handling for new stuff, as had happened in bda420b98505. Simplify by switching to scnprintf while at it. Signed-off-by: Tvrtko Ursulin References: bda420b98505 ("numa balancing: migrate on fault among multiple bound nodes") Cc: Huang Ying Cc: Andrew Morton --- mm/mempolicy.c | 58 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 1bfb6c73a39c..77488878d8ca 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -3150,6 +3150,17 @@ static const char * const policy_modes[] = [MPOL_PREFERRED_MANY] = "prefer (many)", }; +/* + * Lookup array containing only uapi flags where the lowest user flag starts at + * array index zero. + */ +#define MPOL_FLAG_STR_INDEX(f) (ilog2(f) - __builtin_ffs(MPOL_MODE_FLAGS) + 1) +static const char * const policy_flags[] = { + [MPOL_FLAG_STR_INDEX(MPOL_F_STATIC_NODES)] = "static", + [MPOL_FLAG_STR_INDEX(MPOL_F_RELATIVE_NODES)] = "relative", + [MPOL_FLAG_STR_INDEX(MPOL_F_NUMA_BALANCING)] = "balancing", +}; + #ifdef CONFIG_TMPFS /** * mpol_parse_str - parse string to mempolicy, for tmpfs mpol mount option. @@ -3302,14 +3313,21 @@ void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) char *p = buffer; nodemask_t nodes = NODE_MASK_NONE; unsigned short mode = MPOL_DEFAULT; - unsigned short flags = 0; + unsigned int bit, cnt; + unsigned long flags = 0; + int res; if (pol && pol != &default_policy && !(pol >= &preferred_node_policy[0] && pol <= &preferred_node_policy[MAX_NUMNODES - 1])) { mode = pol->mode; - flags = pol->flags; + /* + * Filter out internal flags and also move user flags to lsb for + * easy lookup, matching the policy_flags[] indices. + */ + flags = (pol->flags & MPOL_MODE_FLAGS) >> + __ffs(MPOL_MODE_FLAGS); } switch (mode) { @@ -3329,29 +3347,31 @@ void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) return; } - p += snprintf(p, maxlen, "%s", policy_modes[mode]); + res = scnprintf(p, maxlen, "%s", policy_modes[mode]); + p += res; + maxlen -= res; - if (flags & MPOL_MODE_FLAGS) { - p += snprintf(p, buffer + maxlen - p, "="); + cnt = 0; + for_each_set_bit(bit, &flags, + __fls(MPOL_MODE_FLAGS) - __ffs(MPOL_MODE_FLAGS) + 1) { + char prefix; - /* - * Static and relative are mutually exclusive. - */ - if (flags & MPOL_F_STATIC_NODES) - p += snprintf(p, buffer + maxlen - p, "static"); - else if (flags & MPOL_F_RELATIVE_NODES) - p += snprintf(p, buffer + maxlen - p, "relative"); + if (WARN_ON_ONCE(bit >= ARRAY_SIZE(policy_flags) || + !policy_flags[bit])) + continue; - if (flags & MPOL_F_NUMA_BALANCING) { - if (hweight16(flags & MPOL_MODE_FLAGS) > 1) - p += snprintf(p, buffer + maxlen - p, "|"); - p += snprintf(p, buffer + maxlen - p, "balancing"); - } + if (cnt++ == 0) + prefix = '='; + else + prefix = '|'; + + res = scnprintf(p, maxlen, "%c%s", prefix, policy_flags[bit]); + p += res; + maxlen -= res; } if (!nodes_empty(nodes)) - p += scnprintf(p, buffer + maxlen - p, ":%*pbl", - nodemask_pr_args(&nodes)); + scnprintf(p, maxlen, ":%*pbl", nodemask_pr_args(&nodes)); } #ifdef CONFIG_SYSFS