mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	pstore: Refactor compression initialization
This refactors compression initialization slightly to better handle getting potentially called twice (via early pstore_register() calls and later pstore_init()) and improves the comments and reporting to be more verbose. Signed-off-by: Kees Cook <keescook@chromium.org> Tested-by: Guenter Roeck <groeck@chromium.org>
This commit is contained in:
		
							parent
							
								
									416031653e
								
							
						
					
					
						commit
						95047b0519
					
				
					 1 changed files with 35 additions and 17 deletions
				
			
		| 
						 | 
					@ -274,36 +274,56 @@ static int pstore_decompress(void *in, void *out,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void allocate_buf_for_compression(void)
 | 
					static void allocate_buf_for_compression(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						struct crypto_comp *ctx;
 | 
				
			||||||
 | 
						int size;
 | 
				
			||||||
 | 
						char *buf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Skip if not built-in or compression backend not selected yet. */
 | 
				
			||||||
	if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
 | 
						if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend)
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Skip if no pstore backend yet or compression init already done. */
 | 
				
			||||||
 | 
						if (!psinfo || tfm)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!crypto_has_comp(zbackend->name, 0, 0)) {
 | 
						if (!crypto_has_comp(zbackend->name, 0, 0)) {
 | 
				
			||||||
		pr_err("No %s compression\n", zbackend->name);
 | 
							pr_err("Unknown compression: %s\n", zbackend->name);
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	big_oops_buf_sz = zbackend->zbufsize(psinfo->bufsize);
 | 
						size = zbackend->zbufsize(psinfo->bufsize);
 | 
				
			||||||
	if (big_oops_buf_sz <= 0)
 | 
						if (size <= 0) {
 | 
				
			||||||
		return;
 | 
							pr_err("Invalid compression size for %s: %d\n",
 | 
				
			||||||
 | 
							       zbackend->name, size);
 | 
				
			||||||
	big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
 | 
					 | 
				
			||||||
	if (!big_oops_buf) {
 | 
					 | 
				
			||||||
		pr_err("allocate compression buffer error!\n");
 | 
					 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tfm = crypto_alloc_comp(zbackend->name, 0, 0);
 | 
						buf = kmalloc(size, GFP_KERNEL);
 | 
				
			||||||
	if (IS_ERR_OR_NULL(tfm)) {
 | 
						if (!buf) {
 | 
				
			||||||
		kfree(big_oops_buf);
 | 
							pr_err("Failed %d byte compression buffer allocation for: %s\n",
 | 
				
			||||||
		big_oops_buf = NULL;
 | 
							       size, zbackend->name);
 | 
				
			||||||
		pr_err("crypto_alloc_comp() failed!\n");
 | 
					 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctx = crypto_alloc_comp(zbackend->name, 0, 0);
 | 
				
			||||||
 | 
						if (IS_ERR_OR_NULL(ctx)) {
 | 
				
			||||||
 | 
							kfree(buf);
 | 
				
			||||||
 | 
							pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name,
 | 
				
			||||||
 | 
							       PTR_ERR(ctx));
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* A non-NULL big_oops_buf indicates compression is available. */
 | 
				
			||||||
 | 
						tfm = ctx;
 | 
				
			||||||
 | 
						big_oops_buf_sz = size;
 | 
				
			||||||
 | 
						big_oops_buf = buf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pr_info("Using compression: %s\n", zbackend->name);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void free_buf_for_compression(void)
 | 
					static void free_buf_for_compression(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && !IS_ERR_OR_NULL(tfm))
 | 
						if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && tfm)
 | 
				
			||||||
		crypto_free_comp(tfm);
 | 
							crypto_free_comp(tfm);
 | 
				
			||||||
	kfree(big_oops_buf);
 | 
						kfree(big_oops_buf);
 | 
				
			||||||
	big_oops_buf = NULL;
 | 
						big_oops_buf = NULL;
 | 
				
			||||||
| 
						 | 
					@ -774,7 +794,6 @@ void __init pstore_choose_compression(void)
 | 
				
			||||||
	for (step = zbackends; step->name; step++) {
 | 
						for (step = zbackends; step->name; step++) {
 | 
				
			||||||
		if (!strcmp(compress, step->name)) {
 | 
							if (!strcmp(compress, step->name)) {
 | 
				
			||||||
			zbackend = step;
 | 
								zbackend = step;
 | 
				
			||||||
			pr_info("using %s compression\n", zbackend->name);
 | 
					 | 
				
			||||||
			return;
 | 
								return;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -791,7 +810,6 @@ static int __init pstore_init(void)
 | 
				
			||||||
	 * initialize compression because crypto was not ready. If so,
 | 
						 * initialize compression because crypto was not ready. If so,
 | 
				
			||||||
	 * initialize compression now.
 | 
						 * initialize compression now.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	if (psinfo && !tfm)
 | 
					 | 
				
			||||||
	allocate_buf_for_compression();
 | 
						allocate_buf_for_compression();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ret = pstore_init_fs();
 | 
						ret = pstore_init_fs();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue