2014-03-05 03:34:03 +00:00
|
|
|
/*
|
2014-04-21 16:49:58 +00:00
|
|
|
* Copyright (C) 2013-2014 Josh Poimboeuf <jpoimboe@redhat.com>
|
2014-03-05 03:34:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA,
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2014-02-11 16:25:48 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/printk.h>
|
2014-03-14 21:41:00 +00:00
|
|
|
#include <linux/slab.h>
|
2014-02-13 03:53:05 +00:00
|
|
|
#include "kpatch.h"
|
2014-03-14 21:41:00 +00:00
|
|
|
#include "kpatch-patch.h"
|
2014-02-11 16:25:48 +00:00
|
|
|
|
2014-04-28 21:18:29 +00:00
|
|
|
static bool replace;
|
|
|
|
module_param(replace, bool, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(replace, "replace all previously loaded patch modules");
|
|
|
|
|
2014-02-11 16:25:48 +00:00
|
|
|
extern char __kpatch_patches, __kpatch_patches_end;
|
|
|
|
|
2014-04-21 16:29:01 +00:00
|
|
|
static struct kpatch_module kpmod;
|
2014-04-26 04:05:26 +00:00
|
|
|
static struct kobject *patch_kobj;
|
|
|
|
|
|
|
|
static ssize_t patch_enabled_show(struct kobject *kobj,
|
|
|
|
struct kobj_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
return sprintf(buf, "%d\n", kpmod.enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t patch_enabled_store(struct kobject *kobj,
|
|
|
|
struct kobj_attribute *attr, char *buf,
|
|
|
|
size_t count)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long val;
|
|
|
|
|
|
|
|
/* only disabling is supported */
|
|
|
|
if (!kpmod.enabled)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = kstrtoul(buf, 10, &val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
val = !!val;
|
|
|
|
|
|
|
|
/* only disabling is supported */
|
|
|
|
if (val)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = kpatch_unregister(&kpmod);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
static struct kobj_attribute patch_enabled_attr =
|
|
|
|
__ATTR(enabled, 0644, patch_enabled_show, patch_enabled_store);
|
2014-03-14 21:41:00 +00:00
|
|
|
|
2014-02-11 16:25:48 +00:00
|
|
|
static int __init patch_init(void)
|
|
|
|
{
|
2014-03-14 21:41:00 +00:00
|
|
|
struct kpatch_patch *patches;
|
2014-04-22 13:54:21 +00:00
|
|
|
int i, ret;
|
2014-03-14 21:41:00 +00:00
|
|
|
|
|
|
|
patches = (struct kpatch_patch *)&__kpatch_patches;
|
2014-04-21 16:29:01 +00:00
|
|
|
|
|
|
|
kpmod.mod = THIS_MODULE;
|
|
|
|
kpmod.num_funcs = (&__kpatch_patches_end - &__kpatch_patches) /
|
|
|
|
sizeof(*patches);
|
|
|
|
kpmod.funcs = kmalloc(kpmod.num_funcs * sizeof(struct kpatch_func),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!kpmod.funcs)
|
2014-03-14 21:41:00 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-04-21 16:29:01 +00:00
|
|
|
for (i = 0; i < kpmod.num_funcs; i++) {
|
|
|
|
kpmod.funcs[i].old_addr = patches[i].old_addr;
|
|
|
|
kpmod.funcs[i].old_size = patches[i].old_size;
|
|
|
|
kpmod.funcs[i].new_addr = patches[i].new_addr;
|
2014-04-21 16:49:58 +00:00
|
|
|
kpmod.funcs[i].new_size = patches[i].new_size;
|
2014-03-14 21:41:00 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 04:05:26 +00:00
|
|
|
patch_kobj = kobject_create_and_add(THIS_MODULE->name,
|
|
|
|
kpatch_patches_kobj);
|
|
|
|
if (!patch_kobj) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sysfs_create_file(patch_kobj, &patch_enabled_attr.attr);
|
|
|
|
if (ret)
|
|
|
|
goto err_put;
|
|
|
|
|
2014-04-28 21:18:29 +00:00
|
|
|
ret = kpatch_register(&kpmod, replace);
|
2014-04-22 13:54:21 +00:00
|
|
|
if (ret)
|
2014-04-26 04:05:26 +00:00
|
|
|
goto err_sysfs;
|
2014-04-22 13:54:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-04-26 04:05:26 +00:00
|
|
|
err_sysfs:
|
|
|
|
sysfs_remove_file(patch_kobj, &patch_enabled_attr.attr);
|
|
|
|
err_put:
|
|
|
|
kobject_put(patch_kobj);
|
2014-04-22 13:54:21 +00:00
|
|
|
err_free:
|
2014-04-21 16:29:01 +00:00
|
|
|
kfree(kpmod.funcs);
|
2014-04-22 13:54:21 +00:00
|
|
|
return ret;
|
2014-02-11 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit patch_exit(void)
|
|
|
|
{
|
2014-04-26 04:05:26 +00:00
|
|
|
WARN_ON(kpmod.enabled);
|
|
|
|
sysfs_remove_file(patch_kobj, &patch_enabled_attr.attr);
|
|
|
|
kobject_put(patch_kobj);
|
2014-04-21 16:29:01 +00:00
|
|
|
kfree(kpmod.funcs);
|
2014-02-11 16:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(patch_init);
|
|
|
|
module_exit(patch_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|