From: Fenghua Yu <fenghua...@intel.com>

The sysadmin may need to know which PKS keys are currently being used.

Add a debugfs file to show the allocated PKS keys and their names.

Signed-off-by: Fenghua Yu <fenghua...@intel.com>
---
 arch/x86/mm/pkeys.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/arch/x86/mm/pkeys.c b/arch/x86/mm/pkeys.c
index 16f735c12fcd..e565fadd74d7 100644
--- a/arch/x86/mm/pkeys.c
+++ b/arch/x86/mm/pkeys.c
@@ -328,3 +328,43 @@ void pks_key_free(int pkey)
        mutex_unlock(&pks_lock);
 }
 EXPORT_SYMBOL_GPL(pks_key_free);
+
+static int pks_keys_allocated_show(struct seq_file *m, void *p)
+{
+       int i;
+
+       mutex_lock(&pks_lock);
+       for (i = PKS_KERN_DEFAULT_KEY; i < PKS_NUM_KEYS; i++) {
+               /* It is ok for pks_key_users[i] to be NULL */
+               if (test_bit(i, &pks_key_allocation_map))
+                       seq_printf(m, "%d: %s\n", i, pks_key_users[i]);
+       }
+       mutex_unlock(&pks_lock);
+
+       return 0;
+}
+
+static int pks_keys_allocated_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, pks_keys_allocated_show, NULL);
+}
+
+static const struct file_operations pks_keys_allocated_fops = {
+       .open           = pks_keys_allocated_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+};
+
+static int __init pks_keys_initcall(void)
+{
+       if (cpu_feature_enabled(X86_FEATURE_PKS)) {
+               /* Create a debugfs file to show allocated PKS keys. */
+               debugfs_create_file("pks_keys_allocated", 0400,
+                                   arch_debugfs_dir, NULL,
+                                   &pks_keys_allocated_fops);
+       }
+
+       return 0;
+}
+late_initcall(pks_keys_initcall);
-- 
2.28.0.rc0.12.gb6a658bd00c9

Reply via email to