regset: new method and helpers for it

->regset_get() takes task+regset+buffer, returns the amount of free space
left in the buffer on success and -E... on error.

buffer is represented as struct membuf - a pair of (kernel) pointer
and amount of space left

Primitives for writing to such:
	* membuf_write(buf, data, size)
	* membuf_zero(buf, size)
	* membuf_store(buf, value)

These are implemented as inlines (in case of membuf_store - a macro).
All writes are sequential; they become no-ops when there's no space
left.  Return value of all primitives is the amount of space left
after the operation, so they can be used as return values of ->regset_get().

Example of use:

// stores pt_regs of task + 64 bytes worth of zeroes + 32bit PID of task
int foo_get(struct task_struct *task, const struct regset *regset,
	    struct membuf to)
{
	membuf_write(&to, task_pt_regs(task), sizeof(struct pt_regs));
	membuf_zero(&to, 64);
	return membuf_store(&to, (u32)task_tgid_vnr(task));
}

regset_get()/regset_get_alloc() taught to use that thing if present.
By the end of the series all users of ->get() will be converted;
then ->get() and ->get_size() can go.

Note that unlike ->get() this thing always starts at offset 0 and,
since it only writes to kernel buffer, can't fail on copyout.
It can, of course, fail for other reasons, but those tend to
be less numerous.

The caller guarantees that the buffer size won't be bigger than
regset->n * regset->size.  That simplifies life for quite a few
instances.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
This commit is contained in:
Al Viro 2020-02-20 20:48:16 -05:00
parent dc12d7968f
commit 7717cb9bdd
2 changed files with 62 additions and 1 deletions

View File

@ -17,6 +17,52 @@
struct task_struct;
struct user_regset;
struct membuf {
void *p;
size_t left;
};
static inline int membuf_zero(struct membuf *s, size_t size)
{
if (s->left) {
if (size > s->left)
size = s->left;
memset(s->p, 0, size);
s->p += size;
s->left -= size;
}
return s->left;
}
static inline int membuf_write(struct membuf *s, const void *v, size_t size)
{
if (s->left) {
if (size > s->left)
size = s->left;
memcpy(s->p, v, size);
s->p += size;
s->left -= size;
}
return s->left;
}
/* current s->p must be aligned for v; v must be a scalar */
#define membuf_store(s, v) \
({ \
struct membuf *__s = (s); \
if (__s->left) { \
typeof(v) __v = (v); \
size_t __size = sizeof(__v); \
if (unlikely(__size > __s->left)) { \
__size = __s->left; \
memcpy(__s->p, &__v, __size); \
} else { \
*(typeof(__v + 0) *)__s->p = __v; \
} \
__s->p += __size; \
__s->left -= __size; \
} \
__s->left;})
/**
* user_regset_active_fn - type of @active function in &struct user_regset
@ -57,6 +103,10 @@ typedef int user_regset_get_fn(struct task_struct *target,
unsigned int pos, unsigned int count,
void *kbuf, void __user *ubuf);
typedef int user_regset_get2_fn(struct task_struct *target,
const struct user_regset *regset,
struct membuf to);
/**
* user_regset_set_fn - type of @set function in &struct user_regset
* @target: thread being examined
@ -186,6 +236,7 @@ typedef unsigned int user_regset_get_size_fn(struct task_struct *target,
*/
struct user_regset {
user_regset_get_fn *get;
user_regset_get2_fn *regset_get;
user_regset_set_fn *set;
user_regset_active_fn *active;
user_regset_writeback_fn *writeback;

View File

@ -11,7 +11,7 @@ static int __regset_get(struct task_struct *target,
void *p = *data, *to_free = NULL;
int res;
if (!regset->get)
if (!regset->get && !regset->regset_get)
return -EOPNOTSUPP;
if (size > regset->n * regset->size)
size = regset->n * regset->size;
@ -20,6 +20,16 @@ static int __regset_get(struct task_struct *target,
if (!p)
return -ENOMEM;
}
if (regset->regset_get) {
res = regset->regset_get(target, regset,
(struct membuf){.p = p, .left = size});
if (res < 0) {
kfree(to_free);
return res;
}
*data = p;
return size - res;
}
res = regset->get(target, regset, 0, size, p, NULL);
if (unlikely(res < 0)) {
kfree(to_free);