Commit 948927ee authored by David Rientjes's avatar David Rientjes Committed by Linus Torvalds

mm, mempolicy: make mpol_to_str robust and always succeed

mpol_to_str() should not fail.  Currently, it either fails because the
string buffer is too small or because a string hasn't been defined for a
mempolicy mode.

If a new mempolicy mode is introduced and no string is defined for it,
just warn and return "unknown".

If the buffer is too small, just truncate the string and return, the
same behavior as snprintf().

This also fixes a bug where there was no NULL-byte termination when doing
*p++ = '=' and *p++ ':' and maxlen has been reached.
Signed-off-by: default avatarDavid Rientjes <rientjes@google.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Chen Gang <gang.chen@asianux.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Dave Jones <davej@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 40c3baa7
...@@ -1387,8 +1387,8 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid) ...@@ -1387,8 +1387,8 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
struct mm_struct *mm = vma->vm_mm; struct mm_struct *mm = vma->vm_mm;
struct mm_walk walk = {}; struct mm_walk walk = {};
struct mempolicy *pol; struct mempolicy *pol;
int n; char buffer[64];
char buffer[50]; int nid;
if (!mm) if (!mm)
return 0; return 0;
...@@ -1404,10 +1404,8 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid) ...@@ -1404,10 +1404,8 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
walk.mm = mm; walk.mm = mm;
pol = get_vma_policy(task, vma, vma->vm_start); pol = get_vma_policy(task, vma, vma->vm_start);
n = mpol_to_str(buffer, sizeof(buffer), pol); mpol_to_str(buffer, sizeof(buffer), pol);
mpol_cond_put(pol); mpol_cond_put(pol);
if (n < 0)
return n;
seq_printf(m, "%08lx %s", vma->vm_start, buffer); seq_printf(m, "%08lx %s", vma->vm_start, buffer);
...@@ -1460,9 +1458,9 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid) ...@@ -1460,9 +1458,9 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
if (md->writeback) if (md->writeback)
seq_printf(m, " writeback=%lu", md->writeback); seq_printf(m, " writeback=%lu", md->writeback);
for_each_node_state(n, N_MEMORY) for_each_node_state(nid, N_MEMORY)
if (md->node[n]) if (md->node[nid])
seq_printf(m, " N%d=%lu", n, md->node[n]); seq_printf(m, " N%d=%lu", nid, md->node[nid]);
out: out:
seq_putc(m, '\n'); seq_putc(m, '\n');
......
...@@ -169,7 +169,7 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from, ...@@ -169,7 +169,7 @@ int do_migrate_pages(struct mm_struct *mm, const nodemask_t *from,
extern int mpol_parse_str(char *str, struct mempolicy **mpol); extern int mpol_parse_str(char *str, struct mempolicy **mpol);
#endif #endif
extern int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol); extern void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol);
/* Check if a vma is migratable */ /* Check if a vma is migratable */
static inline int vma_migratable(struct vm_area_struct *vma) static inline int vma_migratable(struct vm_area_struct *vma)
...@@ -307,9 +307,8 @@ static inline int mpol_parse_str(char *str, struct mempolicy **mpol) ...@@ -307,9 +307,8 @@ static inline int mpol_parse_str(char *str, struct mempolicy **mpol)
} }
#endif #endif
static inline int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) static inline void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
{ {
return 0;
} }
static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma, static inline int mpol_misplaced(struct page *page, struct vm_area_struct *vma,
......
...@@ -2914,62 +2914,45 @@ int mpol_parse_str(char *str, struct mempolicy **mpol) ...@@ -2914,62 +2914,45 @@ int mpol_parse_str(char *str, struct mempolicy **mpol)
* @maxlen: length of @buffer * @maxlen: length of @buffer
* @pol: pointer to mempolicy to be formatted * @pol: pointer to mempolicy to be formatted
* *
* Convert a mempolicy into a string. * Convert @pol into a string. If @buffer is too short, truncate the string.
* Returns the number of characters in buffer (if positive) * Recommend a @maxlen of at least 32 for the longest mode, "interleave", the
* or an error (negative) * longest flag, "relative", and to display at least a few node ids.
*/ */
int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
{ {
char *p = buffer; char *p = buffer;
int l; nodemask_t nodes = NODE_MASK_NONE;
nodemask_t nodes; unsigned short mode = MPOL_DEFAULT;
unsigned short mode; unsigned short flags = 0;
unsigned short flags = pol ? pol->flags : 0;
/*
* Sanity check: room for longest mode, flag and some nodes
*/
VM_BUG_ON(maxlen < strlen("interleave") + strlen("relative") + 16);
if (!pol || pol == &default_policy) if (pol && pol != &default_policy) {
mode = MPOL_DEFAULT;
else
mode = pol->mode; mode = pol->mode;
flags = pol->flags;
}
switch (mode) { switch (mode) {
case MPOL_DEFAULT: case MPOL_DEFAULT:
nodes_clear(nodes);
break; break;
case MPOL_PREFERRED: case MPOL_PREFERRED:
nodes_clear(nodes);
if (flags & MPOL_F_LOCAL) if (flags & MPOL_F_LOCAL)
mode = MPOL_LOCAL; mode = MPOL_LOCAL;
else else
node_set(pol->v.preferred_node, nodes); node_set(pol->v.preferred_node, nodes);
break; break;
case MPOL_BIND: case MPOL_BIND:
/* Fall through */
case MPOL_INTERLEAVE: case MPOL_INTERLEAVE:
nodes = pol->v.nodes; nodes = pol->v.nodes;
break; break;
default: default:
return -EINVAL; WARN_ON_ONCE(1);
snprintf(p, maxlen, "unknown");
return;
} }
l = strlen(policy_modes[mode]); p += snprintf(p, maxlen, policy_modes[mode]);
if (buffer + maxlen < p + l + 1)
return -ENOSPC;
strcpy(p, policy_modes[mode]);
p += l;
if (flags & MPOL_MODE_FLAGS) { if (flags & MPOL_MODE_FLAGS) {
if (buffer + maxlen < p + 2) p += snprintf(p, buffer + maxlen - p, "=");
return -ENOSPC;
*p++ = '=';
/* /*
* Currently, the only defined flags are mutually exclusive * Currently, the only defined flags are mutually exclusive
...@@ -2981,10 +2964,7 @@ int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) ...@@ -2981,10 +2964,7 @@ int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol)
} }
if (!nodes_empty(nodes)) { if (!nodes_empty(nodes)) {
if (buffer + maxlen < p + 2) p += snprintf(p, buffer + maxlen - p, ":");
return -ENOSPC;
*p++ = ':';
p += nodelist_scnprintf(p, buffer + maxlen - p, nodes); p += nodelist_scnprintf(p, buffer + maxlen - p, nodes);
} }
return p - buffer;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment