├── Makefile ├── .gitignore ├── src ├── mpu_drv.h ├── mpu_ioctl.h ├── mpu_syscall_hook.h ├── mpu_nv.h ├── mpu_drv.c ├── mpu_syscall_hook.c └── mpu_ioctl.c ├── DEBUG_CONFIG.md ├── README.md └── LICENSE /Makefile: -------------------------------------------------------------------------------- 1 | obj-m += mpu.o 2 | mpu-objs := src/mpu_drv.o src/mpu_syscall_hook.o src/mpu_ioctl.o 3 | 4 | KVERSION := $(shell uname -r) 5 | KDIR := /lib/modules/$(KVERSION)/build 6 | PWD := $(shell pwd) 7 | 8 | default: 9 | $(MAKE) -C $(KDIR) M=$(PWD) modules 10 | 11 | clean: 12 | $(MAKE) -C $(KDIR) M=$(PWD) clean 13 | 14 | install: 15 | $(MAKE) -C $(KDIR) M=$(PWD) modules_install 16 | depmod 17 | insmod mpu.ko 18 | echo mpu > /etc/modules-load.d/matpool-mpu.conf 19 | 20 | uninstall: 21 | rmmod mpu.ko 22 | depmod 23 | rm /etc/modules-load.d/matpool-mpu.conf 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | .DS_Store -------------------------------------------------------------------------------- /src/mpu_drv.h: -------------------------------------------------------------------------------- 1 | // MPU, A shim driver allows in-docker nvidia-smi showing correct process list without modify anything 2 | // Copyright (C) 2021, Matpool 3 | // 4 | // This program is free software; you can redistribute it and/or modify 5 | // it under the terms of the GNU General Public License as published by 6 | // the Free Software Foundation; either version 2 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License along 15 | // with this program; if not, write to the Free Software Foundation, Inc., 16 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | 18 | #ifndef __MPU_DRV_H__ 19 | #define __MPU_DRV_H__ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | struct mpu_ioctl_call_s; 27 | struct mpu_nv_handlers_s; 28 | 29 | typedef struct mpu_ctx_s 30 | { 31 | // read-only 32 | struct mpu_nv_handlers_s *hs; 33 | } mpu_ctx_t; 34 | 35 | typedef struct mpu_module_s 36 | { 37 | /** 38 | * @param ioctl_c do NOT retain it 39 | * @param rdev is requested nv rdev 40 | */ 41 | long (*ioctl)(mpu_ctx_t *ctx, struct mpu_ioctl_call_s *ioctl_c, dev_t rdev); 42 | } mpu_module_t; 43 | 44 | // Add debug function declaration 45 | extern bool mpu_debug_enabled(void); 46 | 47 | #endif // __MPU_DRV_H__ -------------------------------------------------------------------------------- /src/mpu_ioctl.h: -------------------------------------------------------------------------------- 1 | // MPU, A shim driver allows in-docker nvidia-smi showing correct process list without modify anything 2 | // Copyright (C) 2021, Matpool 3 | // 4 | // This program is free software; you can redistribute it and/or modify 5 | // it under the terms of the GNU General Public License as published by 6 | // the Free Software Foundation; either version 2 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License along 15 | // with this program; if not, write to the Free Software Foundation, Inc., 16 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | 18 | #ifndef __MPU_IOCTL_H__ 19 | #define __MPU_IOCTL_H__ 20 | 21 | #include "mpu_drv.h" 22 | 23 | typedef struct mpu_nv_handler_s 24 | { 25 | int cmd; 26 | /** 27 | * @param pid request process global pid 28 | */ 29 | long (*handle)(struct mpu_ioctl_call_s *ioctl_c, dev_t rdev); 30 | } mpu_nv_handler_t; 31 | 32 | /** 33 | * @param hs handlers 34 | * @param cmd incoming request command 35 | * @return valid handler or ERR_PTR 36 | * and you should not retain the ptr returned from mpu_find_nv_handler 37 | */ 38 | extern mpu_nv_handler_t *mpu_find_nv_handler(struct mpu_nv_handlers_s *hs, int cmd); 39 | 40 | /** 41 | * @return valid handlers or null when out of memory 42 | */ 43 | extern struct mpu_nv_handlers_s *mpu_init_nv_handlers(void); 44 | extern void mpu_free_nv_handlers(struct mpu_nv_handlers_s *hs); 45 | 46 | extern void mpu_print_nv_handlers(struct mpu_nv_handlers_s *hs); 47 | 48 | #endif // __MPU_IOCTL_H__ -------------------------------------------------------------------------------- /src/mpu_syscall_hook.h: -------------------------------------------------------------------------------- 1 | // MPU, A shim driver allows in-docker nvidia-smi showing correct process list without modify anything 2 | // Copyright (C) 2021, Matpool 3 | // 4 | // This program is free software; you can redistribute it and/or modify 5 | // it under the terms of the GNU General Public License as published by 6 | // the Free Software Foundation; either version 2 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License along 15 | // with this program; if not, write to the Free Software Foundation, Inc., 16 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | 18 | #ifndef __MPU_SYSCALL_HOOK__ 19 | #define __MPU_SYSCALL_HOOK__ 20 | 21 | #include "mpu_drv.h" 22 | 23 | // NV device major number 24 | #define NV_MAJOR_DEVICE_NUMBER 195 25 | 26 | /** 27 | * wrapping syscall ioctl into one structure 28 | * invoke @fn mpu_call_ioctl with such as param 29 | */ 30 | typedef struct mpu_ioctl_call_s 31 | { 32 | unsigned int fd; 33 | unsigned int cmd; 34 | unsigned long arg; 35 | } mpu_ioctl_call_t; 36 | 37 | /* 38 | * On Linux kernels 5.7+, kallsyms_lookup_name() is no longer exported, 39 | * so we have to use kprobes to get the address. 40 | */ 41 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0) 42 | #define KPROBE_LOOKUP 1 43 | #include 44 | static struct kprobe kp = { 45 | .symbol_name = "kallsyms_lookup_name" 46 | }; 47 | #endif 48 | 49 | /** 50 | * starting hook syscall ioctl based on 51 | * @param module hooked ioctl will invoke module's ioctl with @param ctx provided 52 | * @param ctx module's ioctl param 53 | * @return <0 means error 54 | * NOTE: there's no initialized check, thus we can't assure you what would happen call it twice or more 55 | */ 56 | int mpu_init_ioctl_hook(mpu_module_t *module, mpu_ctx_t *ctx); 57 | 58 | /** 59 | * release initialized hook instance 60 | */ 61 | void mpu_exit_ioctl_hook(void); 62 | 63 | long mpu_call_ioctl(mpu_ioctl_call_t *c); 64 | 65 | #endif // __MPU_SYSCALL_HOOK__ -------------------------------------------------------------------------------- /DEBUG_CONFIG.md: -------------------------------------------------------------------------------- 1 | # MPU Debug Configuration Guide 2 | 3 | ## Overview 4 | The `print_pids` function is used to print debug information for process ID conversion. By default, this debug output is disabled and can be controlled through module parameters to enable or disable it. 5 | 6 | ## Configuration Method 7 | 8 | ### Runtime Parameter Control 9 | 10 | Set debug parameters when loading the module: 11 | ```bash 12 | # Load module and enable debug 13 | sudo insmod mpu.ko debug_enabled=1 14 | 15 | # Or if module is already loaded, dynamically modify 16 | echo 1 | sudo tee /sys/module/mpu/parameters/debug_enabled 17 | 18 | # Disable debug 19 | echo 0 | sudo tee /sys/module/mpu/parameters/debug_enabled 20 | 21 | # Check current status 22 | cat /sys/module/mpu/parameters/debug_enabled 23 | ``` 24 | 25 | ### Complete Operation Example 26 | 27 | ```bash 28 | # 1. Confirm module is not loaded 29 | lsmod | grep mpu 30 | 31 | # 2. Load module and enable debug 32 | sudo insmod mpu.ko debug_enabled=1 33 | 34 | # 3. Confirm debug is enabled 35 | cat /sys/module/mpu/parameters/debug_enabled 36 | # Should output: Y 37 | 38 | # 4. View debug logs 39 | dmesg | grep "mpu:" | tail -10 40 | 41 | # 5. If need to disable debug 42 | echo 0 | sudo tee /sys/module/mpu/parameters/debug_enabled 43 | 44 | # 6. Unload module 45 | sudo rmmod mpu 46 | ``` 47 | 48 | ## Log Viewing 49 | 50 | Debug information is output to kernel logs, which can be viewed with the following commands: 51 | ```bash 52 | # View kernel logs 53 | dmesg | grep "mpu:" 54 | 55 | # View logs in real-time 56 | dmesg -w | grep "mpu:" 57 | 58 | # View /var/log/kern.log 59 | tail -f /var/log/kern.log | grep "mpu:" 60 | ``` 61 | 62 | ## Log Format 63 | 64 | The debug log format is: 65 | ``` 66 | mpu: [tag] dump process pid [pid_number] 67 | ``` 68 | 69 | Example: 70 | ``` 71 | mpu: before: u32 dump process pid 1234 72 | mpu: after: u32 dump process pid 5678 73 | ``` 74 | 75 | ## Important Notes 76 | 77 | 1. **Permission Requirements**: Root privileges are required to load/unload modules and modify parameters 78 | 2. **Module Path**: Ensure the `mpu.ko` file is in the current directory or specify the correct path 79 | 3. **Log Level**: Ensure kernel log level allows displaying `KERN_DEBUG` level information 80 | 4. **Real-time Effect**: Parameter changes take effect immediately without needing to reload the module 81 | 5. **Default Disabled**: Debug functionality is disabled by default to avoid unnecessary log output -------------------------------------------------------------------------------- /src/mpu_nv.h: -------------------------------------------------------------------------------- 1 | // MPU, A shim driver allows in-docker nvidia-smi showing correct process list without modify anything 2 | // Copyright (C) 2021, Matpool 3 | // 4 | // This program is free software; you can redistribute it and/or modify 5 | // it under the terms of the GNU General Public License as published by 6 | // the Free Software Foundation; either version 2 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License along 15 | // with this program; if not, write to the Free Software Foundation, Inc., 16 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | 18 | #ifndef __MPU_NV_H__ 19 | #define __MPU_NV_H__ 20 | 21 | // This file declares nv types 22 | 23 | #include 24 | 25 | #if defined(__x86_64) || defined(AMD64) || defined(_M_AMD64) 26 | #define MPU_64_BITS 27 | #endif 28 | 29 | #if !defined(MPU_64_BITS) 30 | #error("mpu-drv: must be compiled with 64-bits") 31 | #endif // MPU_64_BITS 32 | 33 | #define ALIGN_BYTES(size) __attribute__ ((aligned (size))) 34 | 35 | /// 36 | // ioctl commands 37 | /// 38 | #define NV_IOCTL_DEV_QUERY 0x2a 39 | 40 | /// 41 | // below types are the work of reverse engineering based on ioctl syscall 42 | // if you haven't got what it's meaning, type the variable with '_' prefix and end it with sequential numbers. 43 | // to avoid incorrect memory access, if the pointer is pointing to an UVA, you must start the variable with 'u_' 44 | /// 45 | 46 | // for driver device query 47 | typedef struct mpu_dev_query { 48 | u32 _0; 49 | u32 _1; 50 | u32 _2; 51 | void * u_ptr ALIGN_BYTES(8); // internal pointer 52 | u32 tag; 53 | } mpu_dev_query_t; 54 | 55 | typedef struct mpu_nvml_process_list { 56 | u32 _0; 57 | u32 _1; 58 | u32 cnt; 59 | u32 pl[]; 60 | } mpu_nvml_process_list_t; 61 | 62 | // ver. 440 63 | typedef struct mpu_nvml_process_mem_item_1f48 { 64 | u32 pid; 65 | u32 _0[9]; 66 | } mpu_nvml_process_mem_item_1f48_t; 67 | 68 | typedef struct mpu_nvml_process_mem_list_1f48 { 69 | u32 cnt; 70 | u32 _0; 71 | mpu_nvml_process_mem_item_1f48_t pl[]; 72 | } mpu_nvml_process_mem_list_1f48_t; 73 | 74 | // ver. 460 75 | typedef struct mpu_nvml_process_mem_item_2588 { 76 | u32 pid; 77 | u32 _0[11]; 78 | } mpu_nvml_process_mem_item_2588_t; 79 | 80 | typedef struct mpu_nvml_process_mem_list_2588 { 81 | u32 cnt; 82 | u32 _0; 83 | mpu_nvml_process_mem_item_2588_t pl[]; 84 | } mpu_nvml_process_mem_list_2588_t; 85 | 86 | // ver. 530 87 | typedef struct mpu_nvml_process_mem_item_3848 { 88 | u32 pid; 89 | u32 _0[17]; 90 | } mpu_nvml_process_mem_item_3848_t; 91 | 92 | typedef struct mpu_nvml_process_mem_list_3848 { 93 | u32 cnt; 94 | u32 _0; 95 | mpu_nvml_process_mem_item_3848_t pl[]; 96 | } mpu_nvml_process_mem_list_3848_t; 97 | 98 | #endif // __MPU_NV_H__ 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MPU 2 | A shim driver allows in-docker nvidia-smi showing correct process list without modify anything. 3 | 4 | # The problems 5 | The NVIDIA driver is not aware of the PID namespace and nvidia-smi has no capability to map global pid to virtual pid, thus it shows nothing. 6 | What's more, The NVIDIA driver is proprietary and we have no idea what's going on inside even small part of the Linux NVIDIA driver is open sourced. 7 | 8 | # The alternatives 9 | - add 'hostPID: true' to the pod specification 10 | - add '--pid=host' when starting a docker instance 11 | 12 | # Installation 13 | 14 | - for debian, to get kernel headers installed with `sudo apt install linux-headers-$(uname -r)`. run `sudo apt-get install build-essential` to get `make` toolset installed. 15 | - clone this repo 16 | - `cd` and `make` 17 | - after build succeeded, `sudo make install` to install the module 18 | - using docker to create `--gpu` enabled instance and run several cases and check process list via `nvidia-smi` to see if all associated processes have been correctly shown 19 | 20 | # The steps 21 | - figure out the basic mechanism of the NVIDIA driver with the open sourced part 22 | - do some reverse engineering tests on the driver via GDB tools and several scripts (cuda/NVML) 23 | - use our module to intercept syscalls and re-write fields of data strucuture with the knowledge of reverse engineering 24 | - run the nvidia-smi with our module with several test cases 25 | 26 | # The details 27 | - nvidia-smi requests `0x20` ioctl command with `0xee4` flag to getting the global PID list (under `init_pid_ns`) **①** 28 | - after getting non-empty PID list, it'll request `0x20` ioctl command with `0x1f48` flag with previous returned pids as input arguments to getting the process GPU memory consumptions **②** 29 | - we hook the syscalls in system-wide approaching and intercept only NVIDIA device ioctl syscall (device major number is `195` and minor is `255` (control dev) which is defined in NVIDIA header file) 30 | - check if request task is under any PID namespace, do nothing if it's global one (under `init_pid_ns`) 31 | - if so, **①** convert the PID list from global to virtual 32 | - however, **②** is a little more complicated which contains two-way interceptors--pre and post. 33 | - on pre-stage, before invoking NVIDIA ioctl, the virtual PIDs (returned from **①**, converted) must convert back to global ones, since NVIDIA driver only recognize global PIDs. 34 | - and one post-stage, after NVIDIA ioctl invoked, cast global PIDs back 35 | 36 | --- 37 | ![71614489144_ pic](https://user-images.githubusercontent.com/14119758/109408926-28831a00-79c9-11eb-8abf-a8382f5a897a.jpg) 38 | --- 39 | ![61614489023_ pic](https://user-images.githubusercontent.com/14119758/109408930-2d47ce00-79c9-11eb-8ec3-90f4324c6dd3.jpg) 40 | --- 41 | 42 | # NOTE 43 | 44 | Tested on: 45 | 46 | - _kernel 4.15.0-136 x64_ , _docker 19.03.15_ , _NVIDIA driver 440.64_ 47 | - _kernel 4.19.0-14 x64_, _NVIDIA driver 460.32_ 48 | - _kernel 5.14.0-404 x64_, _LXC 4.0.12_, _NVIDIA driver 535.129.03_ 49 | 50 | --- 51 | 52 | NOTE: Kernel 5.7.7 build routines don't export kallsyms kernel functions any longer. Although it has been patched, this module may not work properly in future higher versions. 53 | 54 | Afterwords, we'd like to maintain the project with fully tested and more kernels and NVIDIA drivers supported. 55 | However we sincerely hope NVIDIA will fix this with simplicity and professionalism. Thx. 56 | -------------------------------------------------------------------------------- /src/mpu_drv.c: -------------------------------------------------------------------------------- 1 | // MPU, A shim driver allows in-docker nvidia-smi showing correct process list without modify anything 2 | // Copyright (C) 2021, Matpool 3 | // 4 | // This program is free software; you can redistribute it and/or modify 5 | // it under the terms of the GNU General Public License as published by 6 | // the Free Software Foundation; either version 2 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License along 15 | // with this program; if not, write to the Free Software Foundation, Inc., 16 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | 18 | #include "mpu_drv.h" 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include "mpu_syscall_hook.h" 25 | #include "mpu_ioctl.h" 26 | 27 | MODULE_LICENSE("GPL"); 28 | MODULE_AUTHOR("Magnus "); 29 | MODULE_DESCRIPTION("A shim driver allows in-docker nvidia-smi showing correct process list without modify anything"); 30 | MODULE_VERSION("0.1.1-pre"); 31 | 32 | // Add module parameter for debug control 33 | static bool debug_enabled = false; 34 | module_param(debug_enabled, bool, 0644); 35 | MODULE_PARM_DESC(debug_enabled, "Enable debug output for PID operations"); 36 | 37 | // Export the debug flag for use in other files 38 | bool mpu_debug_enabled(void) 39 | { 40 | return debug_enabled; 41 | } 42 | EXPORT_SYMBOL(mpu_debug_enabled); 43 | 44 | #define MODULE_NAME "mpu" 45 | #define DEVICE_NAME MODULE_NAME 46 | 47 | /** 48 | * internal funcs 49 | */ 50 | static void mpu_drv_on_exit(void); 51 | static long mpu_module_ioctl(mpu_ctx_t *ctx, struct mpu_ioctl_call_s *ioctl_c, dev_t rdev); 52 | static mpu_ctx_t *mpu_init_ctx(void); 53 | static void mpu_free_ctx(mpu_ctx_t *ctx); 54 | /** 55 | * check the task pid-namespace pid is not equal to init namespace pid 56 | * @return 57 | * for most container runtime, if current task is inside a container(under container pid-namespace), this function will return true 58 | */ 59 | static bool mpu_is_task_under_pid_ns(struct task_struct *tsk); 60 | 61 | static mpu_ctx_t *mpu_ctx = NULL; 62 | static mpu_module_t mpu_fops = { 63 | .ioctl = mpu_module_ioctl, 64 | }; 65 | 66 | static mpu_ctx_t *mpu_init_ctx(void) 67 | { 68 | mpu_ctx_t *ctx; 69 | ctx = kzalloc(sizeof(mpu_ctx_t), GFP_KERNEL); 70 | if (ctx == NULL) 71 | return NULL; 72 | 73 | ctx->hs = mpu_init_nv_handlers(); 74 | if (ctx->hs == NULL) 75 | { 76 | kfree(ctx); 77 | return NULL; 78 | } 79 | 80 | return ctx; 81 | } 82 | 83 | static void mpu_free_ctx(mpu_ctx_t *ctx) 84 | { 85 | if (ctx == NULL) 86 | return; 87 | 88 | mpu_free_nv_handlers(ctx->hs); 89 | kfree(ctx); 90 | } 91 | 92 | long mpu_module_ioctl(mpu_ctx_t *ctx, struct mpu_ioctl_call_s *ioctl_c, dev_t rdev) 93 | { 94 | mpu_nv_handler_t *h; 95 | if (MAJOR(rdev) == NV_MAJOR_DEVICE_NUMBER) 96 | { 97 | // only works in container 98 | if (mpu_is_task_under_pid_ns(current)) 99 | { 100 | h = mpu_find_nv_handler(ctx->hs, _IOC_NR(ioctl_c->cmd)); 101 | if (IS_ERR(h)) 102 | { 103 | if (PTR_ERR(h) != -ENOENT) 104 | printk(KERN_ERR "mpu: find ioctl handler with an error 0x%lx\n", -PTR_ERR(h)); 105 | } 106 | else 107 | return h->handle(ioctl_c, rdev); 108 | } 109 | } 110 | 111 | return mpu_call_ioctl(ioctl_c); 112 | } 113 | 114 | static bool mpu_is_task_under_pid_ns(struct task_struct *tsk) 115 | { 116 | return task_pid_nr(tsk) != task_pid_vnr(tsk); 117 | } 118 | 119 | static void mpu_drv_on_exit(void) 120 | { 121 | if (mpu_ctx) 122 | { 123 | mpu_free_ctx(mpu_ctx); 124 | } 125 | } 126 | 127 | static int __init mpu_drv_init(void) 128 | { 129 | int ret; 130 | mpu_ctx = mpu_init_ctx(); 131 | if (!mpu_ctx) 132 | { 133 | ret = -ENOMEM; 134 | goto error; 135 | } 136 | ret = mpu_init_ioctl_hook(&mpu_fops, mpu_ctx); 137 | if (ret < 0) 138 | goto error; 139 | 140 | printk(KERN_INFO "mpu: mpu driver initialized.\n"); 141 | mpu_print_nv_handlers(mpu_ctx->hs); 142 | return 0; 143 | 144 | error: 145 | mpu_drv_on_exit(); 146 | return ret; 147 | } 148 | 149 | static void __exit mpu_drv_exit(void) 150 | { 151 | mpu_exit_ioctl_hook(); 152 | mpu_drv_on_exit(); 153 | } 154 | 155 | module_init(mpu_drv_init); 156 | module_exit(mpu_drv_exit); -------------------------------------------------------------------------------- /src/mpu_syscall_hook.c: -------------------------------------------------------------------------------- 1 | // MPU, A shim driver allows in-docker nvidia-smi showing correct process list without modify anything 2 | // Copyright (C) 2021, Matpool 3 | // 4 | // This program is free software; you can redistribute it and/or modify 5 | // it under the terms of the GNU General Public License as published by 6 | // the Free Software Foundation; either version 2 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License along 15 | // with this program; if not, write to the Free Software Foundation, Inc., 16 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | 18 | #include "mpu_syscall_hook.h" 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)) 27 | #define FILE_INODE(f) (f->f_inode) 28 | #else 29 | #define FILE_INODE(f) (f->f_path.dentry->d_inode) 30 | #endif // FILE_INODE 31 | 32 | // kernel 4.17 introduces syscall wrapper 33 | #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER 34 | typedef asmlinkage long (*ioctl_fn)(const struct pt_regs *); 35 | 36 | typedef struct mpu_ioctl_private_s 37 | { 38 | mpu_ioctl_call_t c; 39 | ioctl_fn ioctl; 40 | const struct pt_regs *regs; 41 | } mpu_ioctl_private_t; 42 | 43 | static asmlinkage long mpu_hooked_ioctl(const struct pt_regs *regs); 44 | #else 45 | typedef asmlinkage long (*ioctl_fn)(unsigned int fd, unsigned int cmd, unsigned long arg); 46 | 47 | typedef struct mpu_ioctl_private_s 48 | { 49 | mpu_ioctl_call_t c; 50 | ioctl_fn ioctl; 51 | } mpu_ioctl_private_t; 52 | 53 | static asmlinkage long mpu_hooked_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg); 54 | #endif // CONFIG_ARCH_HAS_SYSCALL_WRAPPER 55 | 56 | // all its fields are immutable without ownership 57 | typedef struct mpu_syscall_hook_s 58 | { 59 | mpu_module_t *module; 60 | mpu_ctx_t *ctx; 61 | unsigned long **syscall_tbl; 62 | ioctl_fn ioctl; 63 | } mpu_syscall_hook_t; 64 | 65 | static mpu_syscall_hook_t mpu_hook_instance; 66 | 67 | static dev_t get_rdev(unsigned int fd) 68 | { 69 | struct fd f = fdget(fd); 70 | struct inode *inode; 71 | dev_t rdev; 72 | 73 | if (f.file) 74 | { 75 | inode = FILE_INODE(f.file); 76 | if (inode) 77 | { 78 | rdev = inode->i_rdev; 79 | } 80 | fdput(f); 81 | } 82 | return rdev; 83 | } 84 | 85 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0) 86 | static inline void write_cr0_forced(unsigned long val) 87 | { 88 | unsigned long __force_order; 89 | 90 | asm volatile( 91 | "mov %0, %%cr0" 92 | : "+r"(val), "+m"(__force_order)); 93 | } 94 | #define WRITE_CR0(f) write_cr0_forced(f) 95 | #else 96 | #define WRITE_CR0(f) write_cr0(f) 97 | #endif 98 | 99 | static void write_syscall(unsigned long **syscall_tbl, ioctl_fn sys_ioctl) 100 | { 101 | unsigned long local_cr0; 102 | 103 | local_cr0 = read_cr0(); 104 | WRITE_CR0(local_cr0 & ~0x00010000); 105 | syscall_tbl[__NR_ioctl] = (unsigned long *)sys_ioctl; 106 | WRITE_CR0(local_cr0); 107 | } 108 | 109 | int mpu_init_ioctl_hook(mpu_module_t *module, mpu_ctx_t *ctx) 110 | { 111 | unsigned long **syscall_tbl; 112 | ioctl_fn sys_ioctl; 113 | 114 | #ifdef KPROBE_LOOKUP 115 | typedef unsigned long (*kallsyms_lookup_name_t)(const char *name); 116 | kallsyms_lookup_name_t kallsyms_lookup_name; 117 | register_kprobe(&kp); 118 | kallsyms_lookup_name = (kallsyms_lookup_name_t) kp.addr; 119 | unregister_kprobe(&kp); 120 | #endif 121 | 122 | if (!module || !module->ioctl || !ctx) 123 | { 124 | return -EINVAL; 125 | } 126 | 127 | syscall_tbl = (unsigned long **)(kallsyms_lookup_name("sys_call_table")); 128 | if (!syscall_tbl) 129 | { 130 | return -ENXIO; 131 | } 132 | sys_ioctl = (ioctl_fn)syscall_tbl[__NR_ioctl]; 133 | 134 | mpu_hook_instance.module = module; 135 | mpu_hook_instance.ctx = ctx; 136 | mpu_hook_instance.syscall_tbl = syscall_tbl; 137 | mpu_hook_instance.ioctl = sys_ioctl; 138 | 139 | // prevent any re-ordering causing accessing null mpu_hook_instance when hook triggered 140 | barrier(); 141 | write_syscall(syscall_tbl, mpu_hooked_ioctl); 142 | 143 | return 0; 144 | } 145 | 146 | // if module is un-loaded but still retain hooked ioctl address 147 | // the kernel will be panic 148 | void mpu_exit_ioctl_hook(void) 149 | { 150 | if (mpu_hook_instance.syscall_tbl && mpu_hook_instance.ioctl) 151 | { 152 | write_syscall(mpu_hook_instance.syscall_tbl, mpu_hook_instance.ioctl); 153 | } 154 | } 155 | 156 | #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER 157 | static asmlinkage long mpu_hooked_ioctl(const struct pt_regs *regs) 158 | { 159 | mpu_ioctl_private_t pc = { 160 | .c = { 161 | .fd = (unsigned int)regs->di, 162 | .cmd = (unsigned int)regs->si, 163 | .arg = (unsigned long)regs->dx, 164 | }, 165 | .ioctl = mpu_hook_instance.ioctl, 166 | .regs = regs, 167 | }; 168 | dev_t dev = get_rdev(pc.c.fd); 169 | return mpu_hook_instance.module->ioctl(mpu_hook_instance.ctx, &pc.c, dev); 170 | } 171 | 172 | long mpu_call_ioctl(mpu_ioctl_call_t *c) 173 | { 174 | mpu_ioctl_private_t *pc = container_of(c, mpu_ioctl_private_t, c); 175 | return pc->ioctl(pc->regs); 176 | } 177 | #else 178 | static asmlinkage long mpu_hooked_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg) 179 | { 180 | mpu_ioctl_private_t pc = { 181 | .c = { 182 | .fd = fd, 183 | .cmd = cmd, 184 | .arg = arg, 185 | }, 186 | .ioctl = mpu_hook_instance.ioctl, 187 | }; 188 | dev_t dev = get_rdev(fd); 189 | return mpu_hook_instance.module->ioctl(mpu_hook_instance.ctx, &pc.c, dev); 190 | } 191 | 192 | long mpu_call_ioctl(mpu_ioctl_call_t *c) 193 | { 194 | mpu_ioctl_private_t *pc = container_of(c, mpu_ioctl_private_t, c); 195 | return pc->ioctl(c->fd, c->cmd, c->arg); 196 | } 197 | #endif // CONFIG_ARCH_HAS_SYSCALL_WRAPPER -------------------------------------------------------------------------------- /src/mpu_ioctl.c: -------------------------------------------------------------------------------- 1 | // MPU, A shim driver allows in-docker nvidia-smi showing correct process list without modify anything 2 | // Copyright (C) 2021, Matpool 3 | // 4 | // This program is free software; you can redistribute it and/or modify 5 | // it under the terms of the GNU General Public License as published by 6 | // the Free Software Foundation; either version 2 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // This program is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU General Public License along 15 | // with this program; if not, write to the Free Software Foundation, Inc., 16 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 | 18 | #include "mpu_ioctl.h" 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "mpu_nv.h" 26 | #include "mpu_syscall_hook.h" 27 | 28 | #define MAX_HANDLER_SLOTS 4 29 | 30 | struct mpu_nv_handlers_s 31 | { 32 | int len; 33 | mpu_nv_handler_t *vals; 34 | }; 35 | 36 | static long nv_handle_dev_query(struct mpu_ioctl_call_s *ioctl_c, dev_t rdev); 37 | 38 | static mpu_nv_handler_t handlers[] = { 39 | {NV_IOCTL_DEV_QUERY, nv_handle_dev_query}, 40 | }; 41 | 42 | mpu_nv_handler_t *mpu_find_nv_handler(struct mpu_nv_handlers_s *hs, int cmd) 43 | { 44 | int i = 0; 45 | for (; i < hs->len; i++) 46 | { 47 | if (hs->vals[i].cmd == cmd) 48 | { 49 | return &hs->vals[i]; 50 | } 51 | } 52 | return ERR_PTR(-ENOENT); 53 | } 54 | 55 | struct mpu_nv_handlers_s *mpu_init_nv_handlers(void) 56 | { 57 | struct mpu_nv_handlers_s *hs = kmalloc(sizeof(struct mpu_nv_handlers_s), GFP_KERNEL); 58 | if (hs == NULL) 59 | { 60 | return NULL; 61 | } 62 | 63 | hs->vals = handlers; 64 | hs->len = sizeof(handlers) / sizeof(handlers[0]); 65 | return hs; 66 | } 67 | 68 | void mpu_free_nv_handlers(struct mpu_nv_handlers_s *hs) 69 | { 70 | if (hs) 71 | kfree(hs); 72 | } 73 | 74 | void mpu_print_nv_handlers(struct mpu_nv_handlers_s *hs) 75 | { 76 | printk(KERN_INFO "mpu: %d nv handlers registered.\n", hs->len); 77 | } 78 | 79 | /** 80 | * in-place update pids from global pids to current pid-ns pids 81 | * rcu read protected 82 | * @param pids len(pids) equals to off*cnt 83 | */ 84 | static void cast_vnr_pids(u32 *pids, u32 off, u32 cnt) 85 | { 86 | u32 i; 87 | pid_t pid; 88 | struct pid *found = NULL; 89 | struct pid_namespace *current_ns; 90 | 91 | rcu_read_lock(); 92 | current_ns = task_active_pid_ns(current); 93 | for (i = 0; i < cnt; i++, pids += off) 94 | { 95 | found = find_pid_ns(*pids, &init_pid_ns); 96 | if (found) 97 | { 98 | pid = pid_nr_ns(found, current_ns); 99 | *pids = (u32)pid; 100 | } 101 | } 102 | rcu_read_unlock(); 103 | } 104 | 105 | /** 106 | * in-place update pids from current pid-ns pids to global pids 107 | * rcu read protected 108 | * @param pids len(pids) equals to off*cnt 109 | */ 110 | static void cast_nr_pids(u32 *pids, u32 off, u32 cnt) 111 | { 112 | u32 i; 113 | pid_t pid; 114 | struct pid *found = NULL; 115 | struct pid_namespace *current_ns; 116 | 117 | rcu_read_lock(); 118 | current_ns = task_active_pid_ns(current); 119 | for (i = 0; i < cnt; i++, pids += off) 120 | { 121 | if (*pids != 0) 122 | { 123 | if ((found = find_pid_ns(*pids, current_ns)) != NULL) 124 | { 125 | pid = pid_nr_ns(found, &init_pid_ns); 126 | *pids = (u32)pid; 127 | } 128 | } 129 | } 130 | rcu_read_unlock(); 131 | } 132 | 133 | static void print_pids(u32 *pids, u32 off, u32 cnt, const char *tag) 134 | { 135 | u32 i; 136 | 137 | // Only print if debug is enabled via module parameter 138 | if (!mpu_debug_enabled()) 139 | return; 140 | 141 | for (i = 0; i < cnt; i++, pids += off) 142 | { 143 | printk(KERN_DEBUG "mpu: %s dump process pid %u\n", tag, *pids); 144 | } 145 | } 146 | 147 | #define MPU_NV_CAST_PIDS_IMPL(ptr, list_type, item_type, cast) \ 148 | { \ 149 | long ret = 0; \ 150 | list_type pl; \ 151 | u32 *pids = NULL; /* temp buffer for saving global pids or ns pids */ \ 152 | size_t len; /* total items length in bytes */ \ 153 | size_t elen; \ 154 | \ 155 | if (copy_from_user(&pl, ptr, sizeof(list_type))) \ 156 | { \ 157 | printk(KERN_ERR "mpu: read NVML userspace type " #list_type " failed\n"); \ 158 | ret = -EFAULT; \ 159 | goto done; \ 160 | } \ 161 | \ 162 | if (pl.cnt > 0) \ 163 | { \ 164 | len = pl.cnt * sizeof(item_type); \ 165 | elen = sizeof(item_type) / sizeof(u32); \ 166 | pids = kmalloc(len, GFP_KERNEL); \ 167 | if (pids == NULL) \ 168 | { \ 169 | ret = -ENOMEM; \ 170 | goto done; \ 171 | } \ 172 | \ 173 | if (copy_from_user(pids, ptr + offsetof(list_type, pl), len)) \ 174 | { \ 175 | ret = -EFAULT; \ 176 | goto done; \ 177 | } \ 178 | \ 179 | print_pids(pids, elen, pl.cnt, "before: " #item_type); \ 180 | cast(pids, elen, pl.cnt); \ 181 | print_pids(pids, 1, pl.cnt, "after: " #item_type); \ 182 | \ 183 | if (copy_to_user(qm->u_ptr + offsetof(list_type, pl), pids, len)) \ 184 | { \ 185 | printk(KERN_ERR "mpu: write NVML userspace type " #list_type " failed\n"); \ 186 | ret = -EFAULT; \ 187 | } \ 188 | } \ 189 | done: \ 190 | if (pids) \ 191 | kfree(pids); \ 192 | return ret; \ 193 | } 194 | 195 | static long nv_handle_query_nvml_processes(mpu_dev_query_t *qm) 196 | MPU_NV_CAST_PIDS_IMPL(qm->u_ptr, mpu_nvml_process_list_t, u32, cast_vnr_pids) 197 | 198 | static long nv_handle_query_nvml_process_mem_1f48_pre(mpu_dev_query_t *qm) 199 | MPU_NV_CAST_PIDS_IMPL(qm->u_ptr, mpu_nvml_process_mem_list_1f48_t, mpu_nvml_process_mem_item_1f48_t, cast_nr_pids) 200 | 201 | static long nv_handle_query_nvml_process_mem_1f48_post(mpu_dev_query_t *qm) 202 | MPU_NV_CAST_PIDS_IMPL(qm->u_ptr, mpu_nvml_process_mem_list_1f48_t, mpu_nvml_process_mem_item_1f48_t, cast_vnr_pids) 203 | 204 | static long nv_handle_query_nvml_process_mem_2588_pre(mpu_dev_query_t *qm) 205 | MPU_NV_CAST_PIDS_IMPL(qm->u_ptr, mpu_nvml_process_mem_list_2588_t, mpu_nvml_process_mem_item_2588_t, cast_nr_pids) 206 | 207 | static long nv_handle_query_nvml_process_mem_2588_post(mpu_dev_query_t *qm) 208 | MPU_NV_CAST_PIDS_IMPL(qm->u_ptr, mpu_nvml_process_mem_list_2588_t, mpu_nvml_process_mem_item_2588_t, cast_vnr_pids) 209 | 210 | static long nv_handle_query_nvml_process_mem_3848_pre(mpu_dev_query_t *qm) 211 | MPU_NV_CAST_PIDS_IMPL(qm->u_ptr, mpu_nvml_process_mem_list_3848_t, mpu_nvml_process_mem_item_3848_t, cast_nr_pids) 212 | 213 | static long nv_handle_query_nvml_process_mem_3848_post(mpu_dev_query_t *qm) 214 | MPU_NV_CAST_PIDS_IMPL(qm->u_ptr, mpu_nvml_process_mem_list_3848_t, mpu_nvml_process_mem_item_3848_t, cast_vnr_pids) 215 | 216 | static long nv_handle_dev_query(struct mpu_ioctl_call_s *ioctl_c, dev_t rdev) 217 | { 218 | size_t arg_size = _IOC_SIZE(ioctl_c->cmd); 219 | mpu_dev_query_t *arg_copy = NULL; 220 | void *arg_ptr = (void *)ioctl_c->arg; 221 | long ret = 0; 222 | 223 | if (arg_size != sizeof(mpu_dev_query_t)) 224 | { 225 | return mpu_call_ioctl(ioctl_c); 226 | } 227 | 228 | arg_copy = (mpu_dev_query_t *)kmalloc(arg_size, GFP_KERNEL); 229 | if (arg_copy == NULL) 230 | { 231 | ret = -ENOMEM; 232 | goto done; 233 | } 234 | else if (copy_from_user(arg_copy, arg_ptr, arg_size)) 235 | { 236 | ret = -EINVAL; 237 | goto done; 238 | } 239 | 240 | switch (arg_copy->tag) 241 | { 242 | case 0x1f48: 243 | ret = nv_handle_query_nvml_process_mem_1f48_pre(arg_copy); 244 | break; 245 | case 0x2588: 246 | ret = nv_handle_query_nvml_process_mem_2588_pre(arg_copy); 247 | break; 248 | case 0x3848: 249 | ret = nv_handle_query_nvml_process_mem_3848_pre(arg_copy); 250 | break; 251 | } 252 | if (ret < 0) 253 | goto done; 254 | 255 | ret = mpu_call_ioctl(ioctl_c); 256 | if (ret < 0) 257 | goto done; 258 | 259 | switch (arg_copy->tag) 260 | { 261 | case 0xee4: 262 | ret = nv_handle_query_nvml_processes(arg_copy); 263 | break; 264 | case 0x1f48: 265 | ret = nv_handle_query_nvml_process_mem_1f48_post(arg_copy); 266 | break; 267 | case 0x2588: 268 | ret = nv_handle_query_nvml_process_mem_2588_post(arg_copy); 269 | break; 270 | case 0x3848: 271 | ret = nv_handle_query_nvml_process_mem_3848_post(arg_copy); 272 | break; 273 | } 274 | 275 | done: 276 | if (ret < 0) 277 | printk(KERN_ERR "mpu: mpu_dev_query_t failed with an error 0x%lx\n", -ret); 278 | if (arg_copy) 279 | kfree(arg_copy); 280 | 281 | return ret; 282 | } 283 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | --------------------------------------------------------------------------------