blob: ac40796cfb15b8d41d1b0e5f45cd093c31bf7da2 [file] [log] [blame]
Ingo Molnarc33fa9f2008-04-17 20:05:36 +02001/*
2 * Access kernel memory without faulting.
3 */
4#include <linux/uaccess.h>
5#include <linux/module.h>
6#include <linux/mm.h>
7
8/**
9 * probe_kernel_read(): safely attempt to read from a location
10 * @dst: pointer to the buffer that shall take the data
11 * @src: address to read from
12 * @size: size of the data chunk
13 *
14 * Safely read from address @src to the buffer at @dst. If a kernel fault
15 * happens, handle that and return -EFAULT.
16 */
17long probe_kernel_read(void *dst, void *src, size_t size)
18{
19 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060020 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020021
Jason Wesselb4b8ac52008-02-20 13:33:38 -060022 set_fs(KERNEL_DS);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020023 pagefault_disable();
24 ret = __copy_from_user_inatomic(dst,
25 (__force const void __user *)src, size);
26 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060027 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020028
29 return ret ? -EFAULT : 0;
30}
31EXPORT_SYMBOL_GPL(probe_kernel_read);
32
33/**
34 * probe_kernel_write(): safely attempt to write to a location
35 * @dst: address to write to
36 * @src: pointer to the data that shall be written
37 * @size: size of the data chunk
38 *
39 * Safely write to address @dst from the buffer at @src. If a kernel fault
40 * happens, handle that and return -EFAULT.
41 */
42long probe_kernel_write(void *dst, void *src, size_t size)
43{
44 long ret;
Jason Wesselb4b8ac52008-02-20 13:33:38 -060045 mm_segment_t old_fs = get_fs();
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020046
Jason Wesselb4b8ac52008-02-20 13:33:38 -060047 set_fs(KERNEL_DS);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020048 pagefault_disable();
49 ret = __copy_to_user_inatomic((__force void __user *)dst, src, size);
50 pagefault_enable();
Jason Wesselb4b8ac52008-02-20 13:33:38 -060051 set_fs(old_fs);
Ingo Molnarc33fa9f2008-04-17 20:05:36 +020052
53 return ret ? -EFAULT : 0;
54}
55EXPORT_SYMBOL_GPL(probe_kernel_write);