Skip to content

Commit

Permalink
block: Fix handling of tasks without ioprio in ioprio_get(2)
Browse files Browse the repository at this point in the history
ioprio_get(2) can be asked to return the best IO priority from several
tasks (IOPRIO_WHO_PGRP, IOPRIO_WHO_USER). Currently the call treats
tasks without set IO priority as having priority
IOPRIO_CLASS_BE/IOPRIO_BE_NORM however this does not really reflect the
IO priority the task will get (which depends on task's nice value).

Fix the code to use the real IO priority task's IO will use. For this we
do some factoring out to share the code converting task's CPU priority
to IO priority and we also have to modify code for
ioprio_get(IOPRIO_WHO_PROCESS) to keep returning IOPRIO_CLASS_NONE
priority for tasks without set IO priority as a special case to maintain
userspace visible API.

Signed-off-by: Jan Kara <jack@suse.cz>
  • Loading branch information
jankara authored and intel-lab-lkp committed Jun 20, 2022
1 parent 8a11964 commit 73f3928
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
49 changes: 42 additions & 7 deletions block/ioprio.c
Expand Up @@ -138,29 +138,64 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
return ret;
}

/*
* If the task has set an I/O priority, use that. Otherwise, return
* the default I/O priority.
*/
int __get_task_ioprio(struct task_struct *p)
{
struct io_context *ioc = p->io_context;
int prio;

if (ioc)
prio = ioc->ioprio;
else
prio = IOPRIO_DEFAULT;

if (IOPRIO_PRIO_CLASS(prio) == IOPRIO_CLASS_NONE)
prio = IOPRIO_PRIO_VALUE(task_nice_ioclass(p),
task_nice_ioprio(p));
return prio;
}
EXPORT_SYMBOL_GPL(__get_task_ioprio);

static int get_task_ioprio(struct task_struct *p)
{
int ret;

ret = security_task_getioprio(p);
if (ret)
goto out;
ret = IOPRIO_DEFAULT;
task_lock(p);
ret = __get_task_ioprio(p);
task_unlock(p);
out:
return ret;
}

/*
* Return raw IO priority value as set by userspace. We use this for
* ioprio_get(pid, IOPRIO_WHO_PROCESS) so that we keep historical behavior and
* also so that userspace can distinguish unset IO priority (which just gets
* overriden based on task's nice value) from IO priority set to some value.
*/
static int get_task_raw_ioprio(struct task_struct *p) { int ret;

ret = security_task_getioprio(p);
if (ret)
goto out;
task_lock(p);
if (p->io_context)
ret = p->io_context->ioprio;
else
ret = IOPRIO_DEFAULT;
task_unlock(p);
out:
return ret;
}

static int ioprio_best(unsigned short aprio, unsigned short bprio)
{
if (!ioprio_valid(aprio))
aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_BE_NORM);
if (!ioprio_valid(bprio))
bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_BE_NORM);

return min(aprio, bprio);
}

Expand All @@ -181,7 +216,7 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
else
p = find_task_by_vpid(who);
if (p)
ret = get_task_ioprio(p);
ret = get_task_raw_ioprio(p);
break;
case IOPRIO_WHO_PGRP:
if (!who)
Expand Down
19 changes: 3 additions & 16 deletions include/linux/ioprio.h
Expand Up @@ -46,24 +46,11 @@ static inline int task_nice_ioclass(struct task_struct *task)
return IOPRIO_CLASS_BE;
}

/*
* If the calling process has set an I/O priority, use that. Otherwise, return
* the default I/O priority.
*/
int __get_task_ioprio(struct task_struct *p);

static inline int get_current_ioprio(void)
{
struct io_context *ioc = current->io_context;
int prio;

if (ioc)
prio = ioc->ioprio;
else
prio = IOPRIO_DEFAULT;

if (IOPRIO_PRIO_CLASS(prio) == IOPRIO_CLASS_NONE)
prio = IOPRIO_PRIO_VALUE(task_nice_ioclass(current),
task_nice_ioprio(current));
return prio;
return __get_task_ioprio(current);
}

extern int set_task_ioprio(struct task_struct *task, int ioprio);
Expand Down

0 comments on commit 73f3928

Please sign in to comment.