18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/kernel.h>
38c2ecf20Sopenharmony_ci#include <linux/module.h>
48c2ecf20Sopenharmony_ci#include <linux/memory.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include "notifier-error-inject.h"
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_cistatic int priority;
98c2ecf20Sopenharmony_cimodule_param(priority, int, 0);
108c2ecf20Sopenharmony_ciMODULE_PARM_DESC(priority, "specify memory notifier priority");
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic struct notifier_err_inject memory_notifier_err_inject = {
138c2ecf20Sopenharmony_ci	.actions = {
148c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(MEM_GOING_ONLINE) },
158c2ecf20Sopenharmony_ci		{ NOTIFIER_ERR_INJECT_ACTION(MEM_GOING_OFFLINE) },
168c2ecf20Sopenharmony_ci		{}
178c2ecf20Sopenharmony_ci	}
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic struct dentry *dir;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic int err_inject_init(void)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	int err;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	dir = notifier_err_inject_init("memory", notifier_err_inject_dir,
278c2ecf20Sopenharmony_ci					&memory_notifier_err_inject, priority);
288c2ecf20Sopenharmony_ci	if (IS_ERR(dir))
298c2ecf20Sopenharmony_ci		return PTR_ERR(dir);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	err = register_memory_notifier(&memory_notifier_err_inject.nb);
328c2ecf20Sopenharmony_ci	if (err)
338c2ecf20Sopenharmony_ci		debugfs_remove_recursive(dir);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	return err;
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic void err_inject_exit(void)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	unregister_memory_notifier(&memory_notifier_err_inject.nb);
418c2ecf20Sopenharmony_ci	debugfs_remove_recursive(dir);
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cimodule_init(err_inject_init);
458c2ecf20Sopenharmony_cimodule_exit(err_inject_exit);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("memory notifier error injection module");
488c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
50