original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
6.4 KiB

include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h percpu.h is included by sched.h and module.h and thus ends up being included when building most .c files. percpu.h includes slab.h which in turn includes gfp.h making everything defined by the two files universally available and complicating inclusion dependencies. percpu.h -> slab.h dependency is about to be removed. Prepare for this change by updating users of gfp and slab facilities include those headers directly instead of assuming availability. As this conversion needs to touch large number of source files, the following script is used as the basis of conversion. http://userweb.kernel.org/~tj/misc/slabh-sweep.py The script does the followings. * Scan files for gfp and slab usages and update includes such that only the necessary includes are there. ie. if only gfp is used, gfp.h, if slab is used, slab.h. * When the script inserts a new include, it looks at the include blocks and try to put the new include such that its order conforms to its surrounding. It's put in the include block which contains core kernel includes, in the same order that the rest are ordered - alphabetical, Christmas tree, rev-Xmas-tree or at the end if there doesn't seem to be any matching order. * If the script can't find a place to put a new include (mostly because the file doesn't have fitting include block), it prints out an error message indicating which .h file needs to be added to the file. The conversion was done in the following steps. 1. The initial automatic conversion of all .c files updated slightly over 4000 files, deleting around 700 includes and adding ~480 gfp.h and ~3000 slab.h inclusions. The script emitted errors for ~400 files. 2. Each error was manually checked. Some didn't need the inclusion, some needed manual addition while adding it to implementation .h or embedding .c file was more appropriate for others. This step added inclusions to around 150 files. 3. The script was run again and the output was compared to the edits from #2 to make sure no file was left behind. 4. Several build tests were done and a couple of problems were fixed. e.g. lib/decompress_*.c used malloc/free() wrappers around slab APIs requiring slab.h to be added manually. 5. The script was run on all .h files but without automatically editing them as sprinkling gfp.h and slab.h inclusions around .h files could easily lead to inclusion dependency hell. Most gfp.h inclusion directives were ignored as stuff from gfp.h was usually wildly available and often used in preprocessor macros. Each slab.h inclusion directive was examined and added manually as necessary. 6. percpu.h was updated not to include slab.h. 7. Build test were done on the following configurations and failures were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my distributed build env didn't work with gcov compiles) and a few more options had to be turned off depending on archs to make things build (like ipr on powerpc/64 which failed due to missing writeq). * x86 and x86_64 UP and SMP allmodconfig and a custom test config. * powerpc and powerpc64 SMP allmodconfig * sparc and sparc64 SMP allmodconfig * ia64 SMP allmodconfig * s390 SMP allmodconfig * alpha SMP allmodconfig * um on x86_64 SMP allmodconfig 8. percpu.h modifications were reverted so that it could be applied as a separate patch and serve as bisection point. Given the fact that I had only a couple of failures from tests on step 6, I'm fairly confident about the coverage of this conversion patch. If there is a breakage, it's likely to be something in one of the arch headers which should be easily discoverable easily on most builds of the specific arch. Signed-off-by: Tejun Heo <tj@kernel.org> Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
12 years ago
fs: rcu-walk for path lookup Perform common cases of path lookups without any stores or locking in the ancestor dentry elements. This is called rcu-walk, as opposed to the current algorithm which is a refcount based walk, or ref-walk. This results in far fewer atomic operations on every path element, significantly improving path lookup performance. It also avoids cacheline bouncing on common dentries, significantly improving scalability. The overall design is like this: * LOOKUP_RCU is set in nd->flags, which distinguishes rcu-walk from ref-walk. * Take the RCU lock for the entire path walk, starting with the acquiring of the starting path (eg. root/cwd/fd-path). So now dentry refcounts are not required for dentry persistence. * synchronize_rcu is called when unregistering a filesystem, so we can access d_ops and i_ops during rcu-walk. * Similarly take the vfsmount lock for the entire path walk. So now mnt refcounts are not required for persistence. Also we are free to perform mount lookups, and to assume dentry mount points and mount roots are stable up and down the path. * Have a per-dentry seqlock to protect the dentry name, parent, and inode, so we can load this tuple atomically, and also check whether any of its members have changed. * Dentry lookups (based on parent, candidate string tuple) recheck the parent sequence after the child is found in case anything changed in the parent during the path walk. * inode is also RCU protected so we can load d_inode and use the inode for limited things. * i_mode, i_uid, i_gid can be tested for exec permissions during path walk. * i_op can be loaded. When we reach the destination dentry, we lock it, recheck lookup sequence, and increment its refcount and mountpoint refcount. RCU and vfsmount locks are dropped. This is termed "dropping rcu-walk". If the dentry refcount does not match, we can not drop rcu-walk gracefully at the current point in the lokup, so instead return -ECHILD (for want of a better errno). This signals the path walking code to re-do the entire lookup with a ref-walk. Aside from the final dentry, there are other situations that may be encounted where we cannot continue rcu-walk. In that case, we drop rcu-walk (ie. take a reference on the last good dentry) and continue with a ref-walk. Again, if we can drop rcu-walk gracefully, we return -ECHILD and do the whole lookup using ref-walk. But it is very important that we can continue with ref-walk for most cases, particularly to avoid the overhead of double lookups, and to gain the scalability advantages on common path elements (like cwd and root). The cases where rcu-walk cannot continue are: * NULL dentry (ie. any uncached path element) * parent with d_inode->i_op->permission or ACLs * dentries with d_revalidate * Following links In future patches, permission checks and d_revalidate become rcu-walk aware. It may be possible eventually to make following links rcu-walk aware. Uncached path elements will always require dropping to ref-walk mode, at the very least because i_mutex needs to be grabbed, and objects allocated. Signed-off-by: Nick Piggin <npiggin@kernel.dk>
11 years ago
fs: Limit sys_mount to only request filesystem modules. Modify the request_module to prefix the file system type with "fs-" and add aliases to all of the filesystems that can be built as modules to match. A common practice is to build all of the kernel code and leave code that is not commonly needed as modules, with the result that many users are exposed to any bug anywhere in the kernel. Looking for filesystems with a fs- prefix limits the pool of possible modules that can be loaded by mount to just filesystems trivially making things safer with no real cost. Using aliases means user space can control the policy of which filesystem modules are auto-loaded by editing /etc/modprobe.d/*.conf with blacklist and alias directives. Allowing simple, safe, well understood work-arounds to known problematic software. This also addresses a rare but unfortunate problem where the filesystem name is not the same as it's module name and module auto-loading would not work. While writing this patch I saw a handful of such cases. The most significant being autofs that lives in the module autofs4. This is relevant to user namespaces because we can reach the request module in get_fs_type() without having any special permissions, and people get uncomfortable when a user specified string (in this case the filesystem type) goes all of the way to request_module. After having looked at this issue I don't think there is any particular reason to perform any filtering or permission checks beyond making it clear in the module request that we want a filesystem module. The common pattern in the kernel is to call request_module() without regards to the users permissions. In general all a filesystem module does once loaded is call register_filesystem() and go to sleep. Which means there is not much attack surface exposed by loading a filesytem module unless the filesystem is mounted. In a user namespace filesystems are not mounted unless .fs_flags = FS_USERNS_MOUNT, which most filesystems do not set today. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Kees Cook <keescook@google.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
9 years ago
  1. /*
  2. * linux/fs/filesystems.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * table of configured filesystems
  7. */
  8. #include <linux/syscalls.h>
  9. #include <linux/fs.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/kmod.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <asm/uaccess.h>
  17. /*
  18. * Handling of filesystem drivers list.
  19. * Rules:
  20. * Inclusion to/removals from/scanning of list are protected by spinlock.
  21. * During the unload module must call unregister_filesystem().
  22. * We can access the fields of list element if:
  23. * 1) spinlock is held or
  24. * 2) we hold the reference to the module.
  25. * The latter can be guaranteed by call of try_module_get(); if it
  26. * returned 0 we must skip the element, otherwise we got the reference.
  27. * Once the reference is obtained we can drop the spinlock.
  28. */
  29. static struct file_system_type *file_systems;
  30. static DEFINE_RWLOCK(file_systems_lock);
  31. /* WARNING: This can be used only if we _already_ own a reference */
  32. void get_filesystem(struct file_system_type *fs)
  33. {
  34. __module_get(fs->owner);
  35. }
  36. void put_filesystem(struct file_system_type *fs)
  37. {
  38. module_put(fs->owner);
  39. }
  40. static struct file_system_type **find_filesystem(const char *name, unsigned len)
  41. {
  42. struct file_system_type **p;
  43. for (p=&file_systems; *p; p=&(*p)->next)
  44. if (strlen((*p)->name) == len &&
  45. strncmp((*p)->name, name, len) == 0)
  46. break;
  47. return p;
  48. }
  49. /**
  50. * register_filesystem - register a new filesystem
  51. * @fs: the file system structure
  52. *
  53. * Adds the file system passed to the list of file systems the kernel
  54. * is aware of for mount and other syscalls. Returns 0 on success,
  55. * or a negative errno code on an error.
  56. *
  57. * The &struct file_system_type that is passed is linked into the kernel
  58. * structures and must not be freed until the file system has been
  59. * unregistered.
  60. */
  61. int register_filesystem(struct file_system_type * fs)
  62. {
  63. int res = 0;
  64. struct file_system_type ** p;
  65. BUG_ON(strchr(fs->name, '.'));
  66. if (fs->next)
  67. return -EBUSY;
  68. write_lock(&file_systems_lock);
  69. p = find_filesystem(fs->name, strlen(fs->name));
  70. if (*p)
  71. res = -EBUSY;
  72. else
  73. *p = fs;
  74. write_unlock(&file_systems_lock);
  75. return res;
  76. }
  77. EXPORT_SYMBOL(register_filesystem);
  78. /**
  79. * unregister_filesystem - unregister a file system
  80. * @fs: filesystem to unregister
  81. *
  82. * Remove a file system that was previously successfully registered
  83. * with the kernel. An error is returned if the file system is not found.
  84. * Zero is returned on a success.
  85. *
  86. * Once this function has returned the &struct file_system_type structure
  87. * may be freed or reused.
  88. */
  89. int unregister_filesystem(struct file_system_type * fs)
  90. {
  91. struct file_system_type ** tmp;
  92. write_lock(&file_systems_lock);
  93. tmp = &file_systems;
  94. while (*tmp) {
  95. if (fs == *tmp) {
  96. *tmp = fs->next;
  97. fs->next = NULL;
  98. write_unlock(&file_systems_lock);
  99. synchronize_rcu();
  100. return 0;
  101. }
  102. tmp = &(*tmp)->next;
  103. }
  104. write_unlock(&file_systems_lock);
  105. return -EINVAL;
  106. }
  107. EXPORT_SYMBOL(unregister_filesystem);
  108. static int fs_index(const char __user * __name)
  109. {
  110. struct file_system_type * tmp;
  111. struct filename *name;
  112. int err, index;
  113. name = getname(__name);
  114. err = PTR_ERR(name);
  115. if (IS_ERR(name))
  116. return err;
  117. err = -EINVAL;
  118. read_lock(&file_systems_lock);
  119. for (tmp=file_systems, index=0 ; tmp ; tmp=tmp->next, index++) {
  120. if (strcmp(tmp->name, name->name) == 0) {
  121. err = index;
  122. break;
  123. }
  124. }
  125. read_unlock(&file_systems_lock);
  126. putname(name);
  127. return err;
  128. }
  129. static int fs_name(unsigned int index, char __user * buf)
  130. {
  131. struct file_system_type * tmp;
  132. int len, res;
  133. read_lock(&file_systems_lock);
  134. for (tmp = file_systems; tmp; tmp = tmp->next, index--)
  135. if (index <= 0 && try_module_get(tmp->owner))
  136. break;
  137. read_unlock(&file_systems_lock);
  138. if (!tmp)
  139. return -EINVAL;
  140. /* OK, we got the reference, so we can safely block */
  141. len = strlen(tmp->name) + 1;
  142. res = copy_to_user(buf, tmp->name, len) ? -EFAULT : 0;
  143. put_filesystem(tmp);
  144. return res;
  145. }
  146. static int fs_maxindex(void)
  147. {
  148. struct file_system_type * tmp;
  149. int index;
  150. read_lock(&file_systems_lock);
  151. for (tmp = file_systems, index = 0 ; tmp ; tmp = tmp->next, index++)
  152. ;
  153. read_unlock(&file_systems_lock);
  154. return index;
  155. }
  156. /*
  157. * Whee.. Weird sysv syscall.
  158. */
  159. SYSCALL_DEFINE3(sysfs, int, option, unsigned long, arg1, unsigned long, arg2)
  160. {
  161. int retval = -EINVAL;
  162. switch (option) {
  163. case 1:
  164. retval = fs_index((const char __user *) arg1);
  165. break;
  166. case 2:
  167. retval = fs_name(arg1, (char __user *) arg2);
  168. break;
  169. case 3:
  170. retval = fs_maxindex();
  171. break;
  172. }
  173. return retval;
  174. }
  175. int __init get_filesystem_list(char *buf)
  176. {
  177. int len = 0;
  178. struct file_system_type * tmp;
  179. read_lock(&file_systems_lock);
  180. tmp = file_systems;
  181. while (tmp && len < PAGE_SIZE - 80) {
  182. len += sprintf(buf+len, "%s\t%s\n",
  183. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  184. tmp->name);
  185. tmp = tmp->next;
  186. }
  187. read_unlock(&file_systems_lock);
  188. return len;
  189. }
  190. #ifdef CONFIG_PROC_FS
  191. static int filesystems_proc_show(struct seq_file *m, void *v)
  192. {
  193. struct file_system_type * tmp;
  194. read_lock(&file_systems_lock);
  195. tmp = file_systems;
  196. while (tmp) {
  197. seq_printf(m, "%s\t%s\n",
  198. (tmp->fs_flags & FS_REQUIRES_DEV) ? "" : "nodev",
  199. tmp->name);
  200. tmp = tmp->next;
  201. }
  202. read_unlock(&file_systems_lock);
  203. return 0;
  204. }
  205. static int filesystems_proc_open(struct inode *inode, struct file *file)
  206. {
  207. return single_open(file, filesystems_proc_show, NULL);
  208. }
  209. static const struct file_operations filesystems_proc_fops = {
  210. .open = filesystems_proc_open,
  211. .read = seq_read,
  212. .llseek = seq_lseek,
  213. .release = single_release,
  214. };
  215. static int __init proc_filesystems_init(void)
  216. {
  217. proc_create("filesystems", 0, NULL, &filesystems_proc_fops);
  218. return 0;
  219. }
  220. module_init(proc_filesystems_init);
  221. #endif
  222. static struct file_system_type *__get_fs_type(const char *name, int len)
  223. {
  224. struct file_system_type *fs;
  225. read_lock(&file_systems_lock);
  226. fs = *(find_filesystem(name, len));
  227. if (fs && !try_module_get(fs->owner))
  228. fs = NULL;
  229. read_unlock(&file_systems_lock);
  230. return fs;
  231. }
  232. struct file_system_type *get_fs_type(const char *name)
  233. {
  234. struct file_system_type *fs;
  235. const char *dot = strchr(name, '.');
  236. int len = dot ? dot - name : strlen(name);
  237. fs = __get_fs_type(name, len);
  238. if (!fs && (request_module("fs-%.*s", len, name) == 0))
  239. fs = __get_fs_type(name, len);
  240. if (dot && fs && !(fs->fs_flags & FS_HAS_SUBTYPE)) {
  241. put_filesystem(fs);
  242. fs = NULL;
  243. }
  244. return fs;
  245. }
  246. EXPORT_SYMBOL(get_fs_type);