Add a function to convert a policy into a string. The policy passed to this function must be properly protected. There are two cases of policies that are of interest: 1. The memory policies in vm_area_struct Mmap_sem lock must be held in order to insure that the policy is not changed under us. The patch that extends smap to display the memory policy holds the mmap_sem when displaying the memory policy. 2. The policy in task_struct The policy is not protected by any lock and may therefore only be accessed in the context of the task. Syntax of textual representation: default preferred= bind= interleave= Signed-off-by: Christoph Lameter Index: linux-2.6.13-rc6/mm/mempolicy.c =================================================================== --- linux-2.6.13-rc6.orig/mm/mempolicy.c 2005-08-15 10:57:55.000000000 -0700 +++ linux-2.6.13-rc6/mm/mempolicy.c 2005-08-15 10:58:26.000000000 -0700 @@ -1224,3 +1224,67 @@ void numa_default_policy(void) { sys_set_mempolicy(MPOL_DEFAULT, NULL, 0); } + +static const char *policy_types[] = { "default", "prefer", "bind", "interleave" }; + +/* + * Convert a mempolicy into a string. + * Returns the number of characters in buffer (if positive) + * or an error (negative) + * + * Can only be used to access policies in vm_area_struct and must + * hold a readlock on mmap_sem to prevent the policy to be changed + * under us. + * + * There is no protection for the policy pointer in the task + * struct. Therefore this function may not be used outside + * of the context of a task to display the task policy. + */ +int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) +{ + char *p = buffer; + int l; + nodemask_t nodes; + int mode = pol ? pol->policy : MPOL_DEFAULT; + + switch (mode) { + case MPOL_DEFAULT: + nodes_clear(nodes); + break; + + case MPOL_PREFERRED: + nodes_clear(nodes); + node_set(pol->v.preferred_node, nodes); + break; + + case MPOL_BIND: + get_zonemask(pol, &nodes); + break; + + case MPOL_INTERLEAVE: + nodes = pol->v.nodes; + break; + + default: + BUG(); + return -EFAULT; + } + + l = strlen(policy_types[mode]); + if (buffer + maxlen < p + l + 1) + return -ENOSPC; + + strcpy(p, policy_types[mode]); + p += l; + + if (!nodes_empty(nodes)) { + + if (buffer + maxlen < p + 2) + return -ENOSPC; + + *p++ = '='; + p += nodelist_scnprintf(p, buffer + maxlen - p, nodes); + } + return p - buffer; +} + Index: linux-2.6.13-rc6/include/linux/mempolicy.h =================================================================== --- linux-2.6.13-rc6.orig/include/linux/mempolicy.h 2005-08-15 10:48:49.000000000 -0700 +++ linux-2.6.13-rc6/include/linux/mempolicy.h 2005-08-15 10:58:04.000000000 -0700 @@ -153,6 +153,9 @@ struct mempolicy *mpol_shared_policy_loo extern void numa_default_policy(void); extern void numa_policy_init(void); +/* Conversion functions */ +int mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol); + #else struct mempolicy {};