19308f2f9SLuis R. Rodriguez /* 29308f2f9SLuis R. Rodriguez * proc sysctl test driver 39308f2f9SLuis R. Rodriguez * 49308f2f9SLuis R. Rodriguez * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org> 59308f2f9SLuis R. Rodriguez * 69308f2f9SLuis R. Rodriguez * This program is free software; you can redistribute it and/or modify it 79308f2f9SLuis R. Rodriguez * under the terms of the GNU General Public License as published by the Free 89308f2f9SLuis R. Rodriguez * Software Foundation; either version 2 of the License, or at your option any 99308f2f9SLuis R. Rodriguez * later version; or, when distributed separately from the Linux kernel or 109308f2f9SLuis R. Rodriguez * when incorporated into other software packages, subject to the following 119308f2f9SLuis R. Rodriguez * license: 129308f2f9SLuis R. Rodriguez * 139308f2f9SLuis R. Rodriguez * This program is free software; you can redistribute it and/or modify it 149308f2f9SLuis R. Rodriguez * under the terms of copyleft-next (version 0.3.1 or later) as published 159308f2f9SLuis R. Rodriguez * at http://copyleft-next.org/. 169308f2f9SLuis R. Rodriguez */ 179308f2f9SLuis R. Rodriguez 189308f2f9SLuis R. Rodriguez /* 192d046981SRandy Dunlap * This module provides an interface to the proc sysctl interfaces. This 209308f2f9SLuis R. Rodriguez * driver requires CONFIG_PROC_SYSCTL. It will not normally be loaded by the 219308f2f9SLuis R. Rodriguez * system unless explicitly requested by name. You can also build this driver 229308f2f9SLuis R. Rodriguez * into your kernel. 239308f2f9SLuis R. Rodriguez */ 249308f2f9SLuis R. Rodriguez 259308f2f9SLuis R. Rodriguez #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 269308f2f9SLuis R. Rodriguez 279308f2f9SLuis R. Rodriguez #include <linux/init.h> 289308f2f9SLuis R. Rodriguez #include <linux/list.h> 299308f2f9SLuis R. Rodriguez #include <linux/module.h> 309308f2f9SLuis R. Rodriguez #include <linux/printk.h> 319308f2f9SLuis R. Rodriguez #include <linux/fs.h> 329308f2f9SLuis R. Rodriguez #include <linux/miscdevice.h> 339308f2f9SLuis R. Rodriguez #include <linux/slab.h> 349308f2f9SLuis R. Rodriguez #include <linux/uaccess.h> 359308f2f9SLuis R. Rodriguez #include <linux/async.h> 369308f2f9SLuis R. Rodriguez #include <linux/delay.h> 379308f2f9SLuis R. Rodriguez #include <linux/vmalloc.h> 389308f2f9SLuis R. Rodriguez 399308f2f9SLuis R. Rodriguez static int i_zero; 409308f2f9SLuis R. Rodriguez static int i_one_hundred = 100; 419308f2f9SLuis R. Rodriguez 429308f2f9SLuis R. Rodriguez struct test_sysctl_data { 439308f2f9SLuis R. Rodriguez int int_0001; 44eb965edaSLuis R. Rodriguez int int_0002; 457c43a657SLuis R. Rodriguez int int_0003[4]; 46eb965edaSLuis R. Rodriguez 474f2f682dSVlastimil Babka int boot_int; 484f2f682dSVlastimil Babka 492920fad3SLuis R. Rodriguez unsigned int uint_0001; 502920fad3SLuis R. Rodriguez 519308f2f9SLuis R. Rodriguez char string_0001[65]; 522ea622b8SEric Sandeen 532ea622b8SEric Sandeen #define SYSCTL_TEST_BITMAP_SIZE 65536 542ea622b8SEric Sandeen unsigned long *bitmap_0001; 559308f2f9SLuis R. Rodriguez }; 569308f2f9SLuis R. Rodriguez 579308f2f9SLuis R. Rodriguez static struct test_sysctl_data test_data = { 589308f2f9SLuis R. Rodriguez .int_0001 = 60, 59eb965edaSLuis R. Rodriguez .int_0002 = 1, 60eb965edaSLuis R. Rodriguez 617c43a657SLuis R. Rodriguez .int_0003[0] = 0, 627c43a657SLuis R. Rodriguez .int_0003[1] = 1, 637c43a657SLuis R. Rodriguez .int_0003[2] = 2, 647c43a657SLuis R. Rodriguez .int_0003[3] = 3, 657c43a657SLuis R. Rodriguez 664f2f682dSVlastimil Babka .boot_int = 0, 674f2f682dSVlastimil Babka 682920fad3SLuis R. Rodriguez .uint_0001 = 314, 692920fad3SLuis R. Rodriguez 709308f2f9SLuis R. Rodriguez .string_0001 = "(none)", 719308f2f9SLuis R. Rodriguez }; 729308f2f9SLuis R. Rodriguez 739308f2f9SLuis R. Rodriguez /* These are all under /proc/sys/debug/test_sysctl/ */ 749308f2f9SLuis R. Rodriguez static struct ctl_table test_table[] = { 759308f2f9SLuis R. Rodriguez { 769308f2f9SLuis R. Rodriguez .procname = "int_0001", 779308f2f9SLuis R. Rodriguez .data = &test_data.int_0001, 789308f2f9SLuis R. Rodriguez .maxlen = sizeof(int), 799308f2f9SLuis R. Rodriguez .mode = 0644, 809308f2f9SLuis R. Rodriguez .proc_handler = proc_dointvec_minmax, 819308f2f9SLuis R. Rodriguez .extra1 = &i_zero, 829308f2f9SLuis R. Rodriguez .extra2 = &i_one_hundred, 839308f2f9SLuis R. Rodriguez }, 849308f2f9SLuis R. Rodriguez { 85eb965edaSLuis R. Rodriguez .procname = "int_0002", 86eb965edaSLuis R. Rodriguez .data = &test_data.int_0002, 87eb965edaSLuis R. Rodriguez .maxlen = sizeof(int), 88eb965edaSLuis R. Rodriguez .mode = 0644, 89eb965edaSLuis R. Rodriguez .proc_handler = proc_dointvec, 90eb965edaSLuis R. Rodriguez }, 91eb965edaSLuis R. Rodriguez { 927c43a657SLuis R. Rodriguez .procname = "int_0003", 937c43a657SLuis R. Rodriguez .data = &test_data.int_0003, 947c43a657SLuis R. Rodriguez .maxlen = sizeof(test_data.int_0003), 957c43a657SLuis R. Rodriguez .mode = 0644, 967c43a657SLuis R. Rodriguez .proc_handler = proc_dointvec, 977c43a657SLuis R. Rodriguez }, 987c43a657SLuis R. Rodriguez { 994f2f682dSVlastimil Babka .procname = "boot_int", 1004f2f682dSVlastimil Babka .data = &test_data.boot_int, 1014f2f682dSVlastimil Babka .maxlen = sizeof(test_data.boot_int), 1024f2f682dSVlastimil Babka .mode = 0644, 1034f2f682dSVlastimil Babka .proc_handler = proc_dointvec, 1044f2f682dSVlastimil Babka .extra1 = SYSCTL_ZERO, 1054f2f682dSVlastimil Babka .extra2 = SYSCTL_ONE, 1064f2f682dSVlastimil Babka }, 1074f2f682dSVlastimil Babka { 1082920fad3SLuis R. Rodriguez .procname = "uint_0001", 1092920fad3SLuis R. Rodriguez .data = &test_data.uint_0001, 1102920fad3SLuis R. Rodriguez .maxlen = sizeof(unsigned int), 1112920fad3SLuis R. Rodriguez .mode = 0644, 1122920fad3SLuis R. Rodriguez .proc_handler = proc_douintvec, 1132920fad3SLuis R. Rodriguez }, 1142920fad3SLuis R. Rodriguez { 1159308f2f9SLuis R. Rodriguez .procname = "string_0001", 1169308f2f9SLuis R. Rodriguez .data = &test_data.string_0001, 1179308f2f9SLuis R. Rodriguez .maxlen = sizeof(test_data.string_0001), 1189308f2f9SLuis R. Rodriguez .mode = 0644, 1199308f2f9SLuis R. Rodriguez .proc_handler = proc_dostring, 1209308f2f9SLuis R. Rodriguez }, 1212ea622b8SEric Sandeen { 1222ea622b8SEric Sandeen .procname = "bitmap_0001", 1232ea622b8SEric Sandeen .data = &test_data.bitmap_0001, 1242ea622b8SEric Sandeen .maxlen = SYSCTL_TEST_BITMAP_SIZE, 1252ea622b8SEric Sandeen .mode = 0644, 1262ea622b8SEric Sandeen .proc_handler = proc_do_large_bitmap, 1272ea622b8SEric Sandeen }, 1289308f2f9SLuis R. Rodriguez { } 1299308f2f9SLuis R. Rodriguez }; 1309308f2f9SLuis R. Rodriguez 1319308f2f9SLuis R. Rodriguez static struct ctl_table_header *test_sysctl_header; 1329308f2f9SLuis R. Rodriguez 1339308f2f9SLuis R. Rodriguez static int __init test_sysctl_init(void) 1349308f2f9SLuis R. Rodriguez { 1352ea622b8SEric Sandeen test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL); 1362ea622b8SEric Sandeen if (!test_data.bitmap_0001) 1379308f2f9SLuis R. Rodriguez return -ENOMEM; 138*04bc883cSLuis Chamberlain test_sysctl_header = register_sysctl("debug/test_sysctl", test_table); 1392ea622b8SEric Sandeen if (!test_sysctl_header) { 1402ea622b8SEric Sandeen kfree(test_data.bitmap_0001); 1412ea622b8SEric Sandeen return -ENOMEM; 1422ea622b8SEric Sandeen } 1439308f2f9SLuis R. Rodriguez return 0; 1449308f2f9SLuis R. Rodriguez } 1452f56f845SMasami Hiramatsu module_init(test_sysctl_init); 1469308f2f9SLuis R. Rodriguez 1479308f2f9SLuis R. Rodriguez static void __exit test_sysctl_exit(void) 1489308f2f9SLuis R. Rodriguez { 1492ea622b8SEric Sandeen kfree(test_data.bitmap_0001); 1509308f2f9SLuis R. Rodriguez if (test_sysctl_header) 1519308f2f9SLuis R. Rodriguez unregister_sysctl_table(test_sysctl_header); 1529308f2f9SLuis R. Rodriguez } 1539308f2f9SLuis R. Rodriguez 1549308f2f9SLuis R. Rodriguez module_exit(test_sysctl_exit); 1559308f2f9SLuis R. Rodriguez 1569308f2f9SLuis R. Rodriguez MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>"); 1579308f2f9SLuis R. Rodriguez MODULE_LICENSE("GPL"); 158