| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- /*
- * kernel/power/tuxonice_modules.c
- *
- * Copyright (C) 2004-2014 Nigel Cunningham (nigel at tuxonice net)
- *
- */
- #include <linux/suspend.h>
- #include "tuxonice.h"
- #include "tuxonice_modules.h"
- #include "tuxonice_sysfs.h"
- #include "tuxonice_ui.h"
- LIST_HEAD(toi_filters);
- LIST_HEAD(toiAllocators);
- LIST_HEAD(toi_modules);
- EXPORT_SYMBOL_GPL(toi_modules);
- struct toi_module_ops *toiActiveAllocator;
- EXPORT_SYMBOL_GPL(toiActiveAllocator);
- static int toi_num_filters;
- int toiNumAllocators, toi_num_modules;
- /*
- * toi_header_storage_for_modules
- *
- * Returns the amount of space needed to store configuration
- * data needed by the modules prior to copying back the original
- * kernel. We can exclude data for pageset2 because it will be
- * available anyway once the kernel is copied back.
- */
- long toi_header_storage_for_modules(void)
- {
- struct toi_module_ops *this_module;
- int bytes = 0;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (!this_module->enabled ||
- (this_module->type == WRITER_MODULE && toiActiveAllocator != this_module))
- continue;
- if (this_module->storage_needed) {
- int this = this_module->storage_needed() +
- sizeof(struct toi_module_header) + sizeof(int);
- this_module->header_requested = this;
- bytes += this;
- }
- }
- /* One more for the empty terminator */
- return bytes + sizeof(struct toi_module_header);
- }
- void print_toi_header_storage_for_modules(void)
- {
- struct toi_module_ops *this_module;
- int bytes = 0;
- pr_debug("Header storage:\n");
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (!this_module->enabled ||
- (this_module->type == WRITER_MODULE && toiActiveAllocator != this_module))
- continue;
- if (this_module->storage_needed) {
- int this = this_module->storage_needed() +
- sizeof(struct toi_module_header) + sizeof(int);
- this_module->header_requested = this;
- bytes += this;
- pr_debug("+ %16s : %-4d/%d.\n",
- this_module->name, this_module->header_used, this);
- }
- }
- pr_debug("+ empty terminator : %zu.\n", sizeof(struct toi_module_header));
- pr_debug(" ====\n");
- pr_debug(" %zu\n", bytes + sizeof(struct toi_module_header));
- }
- EXPORT_SYMBOL_GPL(print_toi_header_storage_for_modules);
- /*
- * toi_memory_for_modules
- *
- * Returns the amount of memory requested by modules for
- * doing their work during the cycle.
- */
- long toi_memory_for_modules(int print_parts)
- {
- long bytes = 0, result;
- struct toi_module_ops *this_module;
- if (print_parts)
- pr_warn("Memory for modules:\n===================\n");
- list_for_each_entry(this_module, &toi_modules, module_list) {
- int this;
- if (!this_module->enabled)
- continue;
- if (this_module->memory_needed) {
- this = this_module->memory_needed();
- if (print_parts)
- pr_warn("%10d bytes (%5ld pages) for module '%s'.\n", this,
- DIV_ROUND_UP(this, PAGE_SIZE), this_module->name);
- bytes += this;
- }
- }
- result = DIV_ROUND_UP(bytes, PAGE_SIZE);
- if (print_parts)
- pr_warn(" => %ld bytes, %ld pages.\n", bytes, result);
- return result;
- }
- /*
- * toi_expected_compression_ratio
- *
- * Returns the compression ratio expected when saving the image.
- */
- int toi_expected_compression_ratio(void)
- {
- int ratio = 100;
- struct toi_module_ops *this_module;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (!this_module->enabled)
- continue;
- if (this_module->expected_compression)
- ratio = ratio * this_module->expected_compression()
- / 100;
- }
- return ratio;
- }
- #ifdef CONFIG_TOI_ENHANCE
- /*
- * toi_actual_compression_ratio
- *
- * Returns the actual compression ratio when saving the image.
- */
- int toi_actual_compression_ratio(void)
- {
- int ratio = 0;
- struct toi_module_ops *this_module;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (!this_module->enabled)
- continue;
- if (this_module->actual_compression)
- ratio = this_module->actual_compression();
- }
- return ratio;
- }
- #endif /* CONFIG_TOI_ENHANCE */
- /* toi_find_module_given_dir
- * Functionality : Return a module (if found), given a pointer
- * to its directory name
- */
- static struct toi_module_ops *toi_find_module_given_dir(char *name)
- {
- struct toi_module_ops *this_module, *found_module = NULL;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (!strcmp(name, this_module->directory)) {
- found_module = this_module;
- break;
- }
- }
- return found_module;
- }
- /* toi_find_module_given_name
- * Functionality : Return a module (if found), given a pointer
- * to its name
- */
- struct toi_module_ops *toi_find_module_given_name(char *name)
- {
- struct toi_module_ops *this_module, *found_module = NULL;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (!strcmp(name, this_module->name)) {
- found_module = this_module;
- break;
- }
- }
- return found_module;
- }
- /*
- * toi_print_module_debug_info
- * Functionality : Get debugging info from modules into a buffer.
- */
- int toi_print_module_debug_info(char *buffer, int buffer_size)
- {
- struct toi_module_ops *this_module;
- int len = 0;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (!this_module->enabled)
- continue;
- if (this_module->print_debug_info) {
- int result;
- result = this_module->print_debug_info(buffer + len, buffer_size - len);
- len += result;
- }
- }
- /* Ensure null terminated */
- buffer[buffer_size] = 0;
- return len;
- }
- /*
- * toi_register_module
- *
- * Register a module.
- */
- int toi_register_module(struct toi_module_ops *module)
- {
- int i;
- struct kobject *kobj;
- if (!hibernation_available())
- return -ENODEV;
- module->enabled = 1;
- if (toi_find_module_given_name(module->name)) {
- pr_warn("TuxOnIce: Trying to load module %s, which is already registered.\n", module->name);
- return -EBUSY;
- }
- switch (module->type) {
- case FILTER_MODULE:
- list_add_tail(&module->type_list, &toi_filters);
- toi_num_filters++;
- break;
- case WRITER_MODULE:
- list_add_tail(&module->type_list, &toiAllocators);
- toiNumAllocators++;
- break;
- case MISC_MODULE:
- case MISC_HIDDEN_MODULE:
- case BIO_ALLOCATOR_MODULE:
- break;
- default:
- pr_err("Hmmm. Module '%s' has an invalid type. It has been ignored.\n", module->name);
- return -EINVAL;
- }
- list_add_tail(&module->module_list, &toi_modules);
- toi_num_modules++;
- if ((!module->directory && !module->shared_directory) ||
- !module->sysfs_data || !module->num_sysfs_entries)
- return 0;
- /*
- * Modules may share a directory, but those with shared_dir
- * set must be loaded (via symbol dependencies) after parents
- * and unloaded beforehand.
- */
- if (module->shared_directory) {
- struct toi_module_ops *shared = toi_find_module_given_dir(module->shared_directory);
- if (!shared) {
- pr_err("TuxOnIce: Module %s wants to share %s's directory but %s isn't loaded",
- module->name, module->shared_directory, module->shared_directory);
- toi_unregister_module(module);
- return -ENODEV;
- }
- kobj = shared->dir_kobj;
- } else {
- if (!strncmp(module->directory, "[ROOT]", 6))
- kobj = tuxonice_kobj;
- else
- kobj = make_toi_sysdir(module->directory);
- }
- module->dir_kobj = kobj;
- for (i = 0; i < module->num_sysfs_entries; i++) {
- int result = toi_register_sysfs_file(kobj,
- &module->sysfs_data[i]);
- if (result)
- return result;
- }
- return 0;
- }
- EXPORT_SYMBOL_GPL(toi_register_module);
- /*
- * toi_unregister_module
- *
- * Remove a module.
- */
- void toi_unregister_module(struct toi_module_ops *module)
- {
- int i;
- if (module->dir_kobj)
- for (i = 0; i < module->num_sysfs_entries; i++)
- toi_unregister_sysfs_file(module->dir_kobj, &module->sysfs_data[i]);
- if (!module->shared_directory && module->directory &&
- strncmp(module->directory, "[ROOT]", 6))
- remove_toi_sysdir(module->dir_kobj);
- switch (module->type) {
- case FILTER_MODULE:
- list_del(&module->type_list);
- toi_num_filters--;
- break;
- case WRITER_MODULE:
- list_del(&module->type_list);
- toiNumAllocators--;
- if (toiActiveAllocator == module) {
- toiActiveAllocator = NULL;
- clear_toi_state(TOI_CAN_RESUME);
- clear_toi_state(TOI_CAN_HIBERNATE);
- }
- break;
- case MISC_MODULE:
- case MISC_HIDDEN_MODULE:
- case BIO_ALLOCATOR_MODULE:
- break;
- default:
- pr_err("Module '%s' has an invalid type. It has been ignored", module->name);
- return;
- }
- list_del(&module->module_list);
- toi_num_modules--;
- }
- EXPORT_SYMBOL_GPL(toi_unregister_module);
- /*
- * toi_move_module_tail
- *
- * Rearrange modules when reloading the config.
- */
- void toi_move_module_tail(struct toi_module_ops *module)
- {
- switch (module->type) {
- case FILTER_MODULE:
- if (toi_num_filters > 1)
- list_move_tail(&module->type_list, &toi_filters);
- break;
- case WRITER_MODULE:
- if (toiNumAllocators > 1)
- list_move_tail(&module->type_list, &toiAllocators);
- break;
- case MISC_MODULE:
- case MISC_HIDDEN_MODULE:
- case BIO_ALLOCATOR_MODULE:
- break;
- default:
- pr_err("Module '%s' has an invalid type. It has been ignored.\n", module->name);
- return;
- }
- if ((toi_num_filters + toiNumAllocators) > 1)
- list_move_tail(&module->module_list, &toi_modules);
- }
- /*
- * toi_initialise_modules
- *
- * Get ready to do some work!
- */
- int toi_initialise_modules(int starting_cycle, int early)
- {
- struct toi_module_ops *this_module;
- int result;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- this_module->header_requested = 0;
- this_module->header_used = 0;
- if (!this_module->enabled)
- continue;
- if (this_module->early != early)
- continue;
- if (this_module->initialise) {
- result = this_module->initialise(starting_cycle);
- if (result) {
- toi_cleanup_modules(starting_cycle);
- return result;
- }
- this_module->initialised = 1;
- }
- }
- return 0;
- }
- /*
- * toi_cleanup_modules
- *
- * Tell modules the work is done.
- */
- void toi_cleanup_modules(int finishing_cycle)
- {
- struct toi_module_ops *this_module;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (!this_module->enabled || !this_module->initialised)
- continue;
- if (this_module->cleanup)
- this_module->cleanup(finishing_cycle);
- this_module->initialised = 0;
- }
- }
- /*
- * toi_pre_atomic_restore_modules
- *
- * Get ready to do some work!
- */
- void toi_pre_atomic_restore_modules(struct toi_boot_kernel_data *bkd)
- {
- struct toi_module_ops *this_module;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (this_module->enabled && this_module->pre_atomic_restore)
- this_module->pre_atomic_restore(bkd);
- }
- }
- /*
- * toi_post_atomic_restore_modules
- *
- * Get ready to do some work!
- */
- void toi_post_atomic_restore_modules(struct toi_boot_kernel_data *bkd)
- {
- struct toi_module_ops *this_module;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (this_module->enabled && this_module->post_atomic_restore)
- this_module->post_atomic_restore(bkd);
- }
- }
- /*
- * toi_get_next_filter
- *
- * Get the next filter in the pipeline.
- */
- struct toi_module_ops *toi_get_next_filter(struct toi_module_ops *filter_sought)
- {
- struct toi_module_ops *last_filter = NULL, *this_filter = NULL;
- list_for_each_entry(this_filter, &toi_filters, type_list) {
- if (!this_filter->enabled)
- continue;
- if ((last_filter == filter_sought) || (!filter_sought))
- return this_filter;
- last_filter = this_filter;
- }
- return toiActiveAllocator;
- }
- EXPORT_SYMBOL_GPL(toi_get_next_filter);
- /**
- * toi_show_modules: Printk what support is loaded.
- */
- void toi_print_modules(void)
- {
- struct toi_module_ops *this_module;
- int prev = 0;
- pr_warn("TuxOnIce " TOI_CORE_VERSION ", with support for");
- list_for_each_entry(this_module, &toi_modules, module_list) {
- if (this_module->type == MISC_HIDDEN_MODULE)
- continue;
- pr_warn("%s %s%s%s", prev ? "," : "",
- this_module->enabled ? "" : "[",
- this_module->name, this_module->enabled ? "" : "]");
- prev = 1;
- }
- pr_warn(".\n");
- }
- /* toi_get_modules
- *
- * Take a reference to modules so they can't go away under us.
- */
- int toi_get_modules(void)
- {
- struct toi_module_ops *this_module;
- list_for_each_entry(this_module, &toi_modules, module_list) {
- struct toi_module_ops *this_module2;
- if (try_module_get(this_module->module))
- continue;
- /* Failed! Reverse gets and return error */
- list_for_each_entry(this_module2, &toi_modules, module_list) {
- if (this_module == this_module2)
- return -EINVAL;
- module_put(this_module2->module);
- }
- }
- return 0;
- }
- /* toi_put_modules
- *
- * Release our references to modules we used.
- */
- void toi_put_modules(void)
- {
- struct toi_module_ops *this_module;
- list_for_each_entry(this_module, &toi_modules, module_list)
- module_put(this_module->module);
- }
|