Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cleancache: forbid overriding cleancache_ops

Currently, cleancache_register_ops returns the previous value of
cleancache_ops to allow chaining. However, chaining, as it is
implemented now, is extremely dangerous due to possible pool id
collisions. Suppose, a new cleancache driver is registered after the
previous one assigned an id to a super block. If the new driver assigns
the same id to another super block, which is perfectly possible, we will
have two different filesystems using the same id. No matter if the new
driver implements chaining or not, we are likely to get data corruption
with such a configuration eventually.

This patch therefore disables the ability to override cleancache_ops
altogether as potentially dangerous. If there is already cleancache
driver registered, all further calls to cleancache_register_ops will
return EBUSY. Since no user of cleancache implements chaining, we only
need to make minor changes to the code outside the cleancache core.

Signed-off-by: Vladimir Davydov <vdavydov@parallels.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: David Vrabel <david.vrabel@citrix.com>
Cc: Mark Fasheh <mfasheh@suse.com>
Cc: Joel Becker <jlbec@evilplan.org>
Cc: Stefan Hengelein <ilendir@googlemail.com>
Cc: Florian Schmaus <fschmaus@gmail.com>
Cc: Andor Daam <andor.daam@googlemail.com>
Cc: Dan Magenheimer <dan.magenheimer@oracle.com>
Cc: Bob Liu <lliubbo@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Vladimir Davydov and committed by
Linus Torvalds
53d85c98 9de16262

+18 -17
+1 -3
Documentation/vm/cleancache.txt
··· 28 28 A cleancache "backend" that provides transcendent memory registers itself 29 29 to the kernel's cleancache "frontend" by calling cleancache_register_ops, 30 30 passing a pointer to a cleancache_ops structure with funcs set appropriately. 31 - Note that cleancache_register_ops returns the previous settings so that 32 - chaining can be performed if desired. The functions provided must conform to 33 - certain semantics as follows: 31 + The functions provided must conform to certain semantics as follows: 34 32 35 33 Most important, cleancache is "ephemeral". Pages which are copied into 36 34 cleancache have an indefinite lifetime which is completely unknowable
+9 -7
drivers/xen/tmem.c
··· 397 397 #ifdef CONFIG_CLEANCACHE 398 398 BUG_ON(sizeof(struct cleancache_filekey) != sizeof(struct tmem_oid)); 399 399 if (tmem_enabled && cleancache) { 400 - char *s = ""; 401 - struct cleancache_ops *old_ops = 402 - cleancache_register_ops(&tmem_cleancache_ops); 403 - if (old_ops) 404 - s = " (WARNING: cleancache_ops overridden)"; 405 - pr_info("cleancache enabled, RAM provided by Xen Transcendent Memory%s\n", 406 - s); 400 + int err; 401 + 402 + err = cleancache_register_ops(&tmem_cleancache_ops); 403 + if (err) 404 + pr_warn("xen-tmem: failed to enable cleancache: %d\n", 405 + err); 406 + else 407 + pr_info("cleancache enabled, RAM provided by " 408 + "Xen Transcendent Memory\n"); 407 409 } 408 410 #endif 409 411 #ifdef CONFIG_XEN_SELFBALLOONING
+1 -2
include/linux/cleancache.h
··· 33 33 void (*invalidate_fs)(int); 34 34 }; 35 35 36 - extern struct cleancache_ops * 37 - cleancache_register_ops(struct cleancache_ops *ops); 36 + extern int cleancache_register_ops(struct cleancache_ops *ops); 38 37 extern void __cleancache_init_fs(struct super_block *); 39 38 extern void __cleancache_init_shared_fs(struct super_block *); 40 39 extern int __cleancache_get_page(struct page *);
+7 -5
mm/cleancache.c
··· 106 106 */ 107 107 108 108 /* 109 - * Register operations for cleancache, returning previous thus allowing 110 - * detection of multiple backends and possible nesting. 109 + * Register operations for cleancache. Returns 0 on success. 111 110 */ 112 - struct cleancache_ops *cleancache_register_ops(struct cleancache_ops *ops) 111 + int cleancache_register_ops(struct cleancache_ops *ops) 113 112 { 114 - struct cleancache_ops *old = cleancache_ops; 115 113 int i; 116 114 117 115 mutex_lock(&poolid_mutex); 116 + if (cleancache_ops) { 117 + mutex_unlock(&poolid_mutex); 118 + return -EBUSY; 119 + } 118 120 for (i = 0; i < MAX_INITIALIZABLE_FS; i++) { 119 121 if (fs_poolid_map[i] == FS_NO_BACKEND) 120 122 fs_poolid_map[i] = ops->init_fs(PAGE_SIZE); ··· 132 130 barrier(); 133 131 cleancache_ops = ops; 134 132 mutex_unlock(&poolid_mutex); 135 - return old; 133 + return 0; 136 134 } 137 135 EXPORT_SYMBOL(cleancache_register_ops); 138 136