16cad1ecdSLuis Chamberlain // SPDX-License-Identifier: GPL-2.0-or-later OR copyleft-next-0.3.1 29308f2f9SLuis R. Rodriguez /* 39308f2f9SLuis R. Rodriguez * proc sysctl test driver 49308f2f9SLuis R. Rodriguez * 59308f2f9SLuis R. Rodriguez * Copyright (C) 2017 Luis R. Rodriguez <mcgrof@kernel.org> 69308f2f9SLuis R. Rodriguez */ 79308f2f9SLuis R. Rodriguez 89308f2f9SLuis R. Rodriguez /* 92d046981SRandy Dunlap * This module provides an interface to the proc sysctl interfaces. This 109308f2f9SLuis R. Rodriguez * driver requires CONFIG_PROC_SYSCTL. It will not normally be loaded by the 119308f2f9SLuis R. Rodriguez * system unless explicitly requested by name. You can also build this driver 129308f2f9SLuis R. Rodriguez * into your kernel. 139308f2f9SLuis R. Rodriguez */ 149308f2f9SLuis R. Rodriguez 159308f2f9SLuis R. Rodriguez #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 169308f2f9SLuis R. Rodriguez 179308f2f9SLuis R. Rodriguez #include <linux/init.h> 189308f2f9SLuis R. Rodriguez #include <linux/list.h> 199308f2f9SLuis R. Rodriguez #include <linux/module.h> 209308f2f9SLuis R. Rodriguez #include <linux/printk.h> 219308f2f9SLuis R. Rodriguez #include <linux/fs.h> 229308f2f9SLuis R. Rodriguez #include <linux/miscdevice.h> 239308f2f9SLuis R. Rodriguez #include <linux/slab.h> 249308f2f9SLuis R. Rodriguez #include <linux/uaccess.h> 259308f2f9SLuis R. Rodriguez #include <linux/async.h> 269308f2f9SLuis R. Rodriguez #include <linux/delay.h> 279308f2f9SLuis R. Rodriguez #include <linux/vmalloc.h> 289308f2f9SLuis R. Rodriguez 299308f2f9SLuis R. Rodriguez static int i_zero; 309308f2f9SLuis R. Rodriguez static int i_one_hundred = 100; 3157b19468STonghao Zhang static int match_int_ok = 1; 329308f2f9SLuis R. Rodriguez 339308f2f9SLuis R. Rodriguez struct test_sysctl_data { 349308f2f9SLuis R. Rodriguez int int_0001; 35eb965edaSLuis R. Rodriguez int int_0002; 367c43a657SLuis R. Rodriguez int int_0003[4]; 37eb965edaSLuis R. Rodriguez 384f2f682dSVlastimil Babka int boot_int; 394f2f682dSVlastimil Babka 402920fad3SLuis R. Rodriguez unsigned int uint_0001; 412920fad3SLuis R. Rodriguez 429308f2f9SLuis R. Rodriguez char string_0001[65]; 432ea622b8SEric Sandeen 442ea622b8SEric Sandeen #define SYSCTL_TEST_BITMAP_SIZE 65536 452ea622b8SEric Sandeen unsigned long *bitmap_0001; 469308f2f9SLuis R. Rodriguez }; 479308f2f9SLuis R. Rodriguez 489308f2f9SLuis R. Rodriguez static struct test_sysctl_data test_data = { 499308f2f9SLuis R. Rodriguez .int_0001 = 60, 50eb965edaSLuis R. Rodriguez .int_0002 = 1, 51eb965edaSLuis R. Rodriguez 527c43a657SLuis R. Rodriguez .int_0003[0] = 0, 537c43a657SLuis R. Rodriguez .int_0003[1] = 1, 547c43a657SLuis R. Rodriguez .int_0003[2] = 2, 557c43a657SLuis R. Rodriguez .int_0003[3] = 3, 567c43a657SLuis R. Rodriguez 574f2f682dSVlastimil Babka .boot_int = 0, 584f2f682dSVlastimil Babka 592920fad3SLuis R. Rodriguez .uint_0001 = 314, 602920fad3SLuis R. Rodriguez 619308f2f9SLuis R. Rodriguez .string_0001 = "(none)", 629308f2f9SLuis R. Rodriguez }; 639308f2f9SLuis R. Rodriguez 649308f2f9SLuis R. Rodriguez /* These are all under /proc/sys/debug/test_sysctl/ */ 659308f2f9SLuis R. Rodriguez static struct ctl_table test_table[] = { 669308f2f9SLuis R. Rodriguez { 679308f2f9SLuis R. Rodriguez .procname = "int_0001", 689308f2f9SLuis R. Rodriguez .data = &test_data.int_0001, 699308f2f9SLuis R. Rodriguez .maxlen = sizeof(int), 709308f2f9SLuis R. Rodriguez .mode = 0644, 719308f2f9SLuis R. Rodriguez .proc_handler = proc_dointvec_minmax, 729308f2f9SLuis R. Rodriguez .extra1 = &i_zero, 739308f2f9SLuis R. Rodriguez .extra2 = &i_one_hundred, 749308f2f9SLuis R. Rodriguez }, 759308f2f9SLuis R. Rodriguez { 76eb965edaSLuis R. Rodriguez .procname = "int_0002", 77eb965edaSLuis R. Rodriguez .data = &test_data.int_0002, 78eb965edaSLuis R. Rodriguez .maxlen = sizeof(int), 79eb965edaSLuis R. Rodriguez .mode = 0644, 80eb965edaSLuis R. Rodriguez .proc_handler = proc_dointvec, 81eb965edaSLuis R. Rodriguez }, 82eb965edaSLuis R. Rodriguez { 837c43a657SLuis R. Rodriguez .procname = "int_0003", 847c43a657SLuis R. Rodriguez .data = &test_data.int_0003, 857c43a657SLuis R. Rodriguez .maxlen = sizeof(test_data.int_0003), 867c43a657SLuis R. Rodriguez .mode = 0644, 877c43a657SLuis R. Rodriguez .proc_handler = proc_dointvec, 887c43a657SLuis R. Rodriguez }, 897c43a657SLuis R. Rodriguez { 9057b19468STonghao Zhang .procname = "match_int", 9157b19468STonghao Zhang .data = &match_int_ok, 9257b19468STonghao Zhang .maxlen = sizeof(match_int_ok), 9357b19468STonghao Zhang .mode = 0444, 9457b19468STonghao Zhang .proc_handler = proc_dointvec, 9557b19468STonghao Zhang }, 9657b19468STonghao Zhang { 974f2f682dSVlastimil Babka .procname = "boot_int", 984f2f682dSVlastimil Babka .data = &test_data.boot_int, 994f2f682dSVlastimil Babka .maxlen = sizeof(test_data.boot_int), 1004f2f682dSVlastimil Babka .mode = 0644, 1014f2f682dSVlastimil Babka .proc_handler = proc_dointvec, 1024f2f682dSVlastimil Babka .extra1 = SYSCTL_ZERO, 1034f2f682dSVlastimil Babka .extra2 = SYSCTL_ONE, 1044f2f682dSVlastimil Babka }, 1054f2f682dSVlastimil Babka { 1062920fad3SLuis R. Rodriguez .procname = "uint_0001", 1072920fad3SLuis R. Rodriguez .data = &test_data.uint_0001, 1082920fad3SLuis R. Rodriguez .maxlen = sizeof(unsigned int), 1092920fad3SLuis R. Rodriguez .mode = 0644, 1102920fad3SLuis R. Rodriguez .proc_handler = proc_douintvec, 1112920fad3SLuis R. Rodriguez }, 1122920fad3SLuis R. Rodriguez { 1139308f2f9SLuis R. Rodriguez .procname = "string_0001", 1149308f2f9SLuis R. Rodriguez .data = &test_data.string_0001, 1159308f2f9SLuis R. Rodriguez .maxlen = sizeof(test_data.string_0001), 1169308f2f9SLuis R. Rodriguez .mode = 0644, 1179308f2f9SLuis R. Rodriguez .proc_handler = proc_dostring, 1189308f2f9SLuis R. Rodriguez }, 1192ea622b8SEric Sandeen { 1202ea622b8SEric Sandeen .procname = "bitmap_0001", 1212ea622b8SEric Sandeen .data = &test_data.bitmap_0001, 1222ea622b8SEric Sandeen .maxlen = SYSCTL_TEST_BITMAP_SIZE, 1232ea622b8SEric Sandeen .mode = 0644, 1242ea622b8SEric Sandeen .proc_handler = proc_do_large_bitmap, 1252ea622b8SEric Sandeen }, 1269308f2f9SLuis R. Rodriguez { } 1279308f2f9SLuis R. Rodriguez }; 1289308f2f9SLuis R. Rodriguez 129*e009bd5eSJoel Granados static void test_sysctl_calc_match_int_ok(void) 1309308f2f9SLuis R. Rodriguez { 13157b19468STonghao Zhang int i; 13257b19468STonghao Zhang 13357b19468STonghao Zhang struct { 13457b19468STonghao Zhang int defined; 13557b19468STonghao Zhang int wanted; 13657b19468STonghao Zhang } match_int[] = { 13757b19468STonghao Zhang {.defined = *(int *)SYSCTL_ZERO, .wanted = 0}, 13857b19468STonghao Zhang {.defined = *(int *)SYSCTL_ONE, .wanted = 1}, 13957b19468STonghao Zhang {.defined = *(int *)SYSCTL_TWO, .wanted = 2}, 14057b19468STonghao Zhang {.defined = *(int *)SYSCTL_THREE, .wanted = 3}, 14157b19468STonghao Zhang {.defined = *(int *)SYSCTL_FOUR, .wanted = 4}, 14257b19468STonghao Zhang {.defined = *(int *)SYSCTL_ONE_HUNDRED, .wanted = 100}, 14357b19468STonghao Zhang {.defined = *(int *)SYSCTL_TWO_HUNDRED, .wanted = 200}, 14457b19468STonghao Zhang {.defined = *(int *)SYSCTL_ONE_THOUSAND, .wanted = 1000}, 14557b19468STonghao Zhang {.defined = *(int *)SYSCTL_THREE_THOUSAND, .wanted = 3000}, 14657b19468STonghao Zhang {.defined = *(int *)SYSCTL_INT_MAX, .wanted = INT_MAX}, 14757b19468STonghao Zhang {.defined = *(int *)SYSCTL_MAXOLDUID, .wanted = 65535}, 14857b19468STonghao Zhang {.defined = *(int *)SYSCTL_NEG_ONE, .wanted = -1}, 14957b19468STonghao Zhang }; 15057b19468STonghao Zhang 15157b19468STonghao Zhang for (i = 0; i < ARRAY_SIZE(match_int); i++) 15257b19468STonghao Zhang if (match_int[i].defined != match_int[i].wanted) 15357b19468STonghao Zhang match_int_ok = 0; 154*e009bd5eSJoel Granados } 15557b19468STonghao Zhang 156*e009bd5eSJoel Granados static struct ctl_table_header *test_sysctl_header; 157*e009bd5eSJoel Granados 158*e009bd5eSJoel Granados static int test_sysctl_setup_node_tests(void) 159*e009bd5eSJoel Granados { 160*e009bd5eSJoel Granados test_sysctl_calc_match_int_ok(); 1612ea622b8SEric Sandeen test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL); 1622ea622b8SEric Sandeen if (!test_data.bitmap_0001) 1639308f2f9SLuis R. Rodriguez return -ENOMEM; 16404bc883cSLuis Chamberlain test_sysctl_header = register_sysctl("debug/test_sysctl", test_table); 1652ea622b8SEric Sandeen if (!test_sysctl_header) { 1662ea622b8SEric Sandeen kfree(test_data.bitmap_0001); 1672ea622b8SEric Sandeen return -ENOMEM; 1682ea622b8SEric Sandeen } 169*e009bd5eSJoel Granados 1709308f2f9SLuis R. Rodriguez return 0; 1719308f2f9SLuis R. Rodriguez } 172*e009bd5eSJoel Granados 173*e009bd5eSJoel Granados static int __init test_sysctl_init(void) 174*e009bd5eSJoel Granados { 175*e009bd5eSJoel Granados int err; 176*e009bd5eSJoel Granados 177*e009bd5eSJoel Granados err = test_sysctl_setup_node_tests(); 178*e009bd5eSJoel Granados 179*e009bd5eSJoel Granados return err; 180*e009bd5eSJoel Granados } 1812f56f845SMasami Hiramatsu module_init(test_sysctl_init); 1829308f2f9SLuis R. Rodriguez 1839308f2f9SLuis R. Rodriguez static void __exit test_sysctl_exit(void) 1849308f2f9SLuis R. Rodriguez { 1852ea622b8SEric Sandeen kfree(test_data.bitmap_0001); 1869308f2f9SLuis R. Rodriguez if (test_sysctl_header) 1879308f2f9SLuis R. Rodriguez unregister_sysctl_table(test_sysctl_header); 1889308f2f9SLuis R. Rodriguez } 1899308f2f9SLuis R. Rodriguez 1909308f2f9SLuis R. Rodriguez module_exit(test_sysctl_exit); 1919308f2f9SLuis R. Rodriguez 1929308f2f9SLuis R. Rodriguez MODULE_AUTHOR("Luis R. Rodriguez <mcgrof@kernel.org>"); 1939308f2f9SLuis R. Rodriguez MODULE_LICENSE("GPL"); 194