[PATCH v2] kernel/sched/fair: Fix the issue of virtual runtime conversion error

Li kunyu posted 1 patch 1 week, 5 days ago
kernel/sched/fair.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH v2] kernel/sched/fair: Fix the issue of virtual runtime conversion error
Posted by Li kunyu 1 week, 5 days ago
The logic of the function here should be to convert the virtual runtime
into actual time by combining it with the weight of the scheduling
entity.
However, it cannot be converted by simply converting actual time into
virtual running time. A reverse conversion is required to obtain the
correct time.

Signed-off-by: Li kunyu <likunyu10@163.com>
---
v2: Use the div64_ul function to perform division operation

 kernel/sched/fair.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 69361c63353a..53f3373c1735 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -7033,7 +7033,9 @@ static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
 			resched_curr(rq);
 		return;
 	}
-	delta = (se->load.weight * vdelta) / NICE_0_LOAD;
+
+	if (unlikely(se->load.weight != NICE_0_LOAD))
+		delta = div64_ul(NICE_0_LOAD * vdelta, se->load.weight);
 
 	/*
 	 * Correct for instantaneous load of other classes.
-- 
2.47.3
Re: [PATCH v2] kernel/sched/fair: Fix the issue of virtual runtime conversion error
Posted by kernel test robot 1 week, 4 days ago
Hi Li,

kernel test robot noticed the following build warnings:

[auto build test WARNING on tip/sched/core]
[also build test WARNING on peterz-queue/sched/core linus/master v7.1-rc5 next-20260527]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Li-kunyu/kernel-sched-fair-Fix-the-issue-of-virtual-runtime-conversion-error/20260527-152554
base:   tip/sched/core
patch link:    https://lore.kernel.org/r/20260527072113.359604-1-likunyu10%40163.com
patch subject: [PATCH v2] kernel/sched/fair: Fix the issue of virtual runtime conversion error
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20260528/202605281756.0qWgdGW5-lkp@intel.com/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260528/202605281756.0qWgdGW5-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202605281756.0qWgdGW5-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> kernel/sched/fair.c:7632:6: warning: variable 'delta' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
    7632 |         if (unlikely(se->load.weight != NICE_0_LOAD))
         |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:77:22: note: expanded from macro 'unlikely'
      77 | # define unlikely(x)    __builtin_expect(!!(x), 0)
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/sched/fair.c:7644:28: note: uninitialized use occurs here
    7644 |         hrtick_start(rq, (scale * delta) / 1024);
         |                                   ^~~~~
   kernel/sched/fair.c:7632:2: note: remove the 'if' if its condition is always true
    7632 |         if (unlikely(se->load.weight != NICE_0_LOAD))
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    7633 |                 delta = div64_ul(NICE_0_LOAD * vdelta, se->load.weight);
   kernel/sched/fair.c:7615:11: note: initialize the variable 'delta' to silence this warning
    7615 |         u64 delta;
         |                  ^
         |                   = 0
   1 warning generated.


vim +7632 kernel/sched/fair.c

  7603	
  7604	/**************************************************
  7605	 * CFS operations on tasks:
  7606	 */
  7607	
  7608	#ifdef CONFIG_SCHED_HRTICK
  7609	static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  7610	{
  7611		struct sched_entity *se = &p->se;
  7612		unsigned long scale = 1024;
  7613		unsigned long util = 0;
  7614		u64 vdelta;
  7615		u64 delta;
  7616	
  7617		WARN_ON_ONCE(task_rq(p) != rq);
  7618	
  7619		if (rq->cfs.h_nr_queued <= 1)
  7620			return;
  7621	
  7622		/*
  7623		 * Compute time until virtual deadline
  7624		 */
  7625		vdelta = se->deadline - se->vruntime;
  7626		if ((s64)vdelta < 0) {
  7627			if (task_current_donor(rq, p))
  7628				resched_curr(rq);
  7629			return;
  7630		}
  7631	
> 7632		if (unlikely(se->load.weight != NICE_0_LOAD))
  7633			delta = div64_ul(NICE_0_LOAD * vdelta, se->load.weight);
  7634	
  7635		/*
  7636		 * Correct for instantaneous load of other classes.
  7637		 */
  7638		util += cpu_util_irq(rq);
  7639		if (util && util < 1024) {
  7640			scale *= 1024;
  7641			scale /= (1024 - util);
  7642		}
  7643	
  7644		hrtick_start(rq, (scale * delta) / 1024);
  7645	}
  7646	

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2] kernel/sched/fair: Fix the issue of virtual runtime conversion error
Posted by Peter Zijlstra 1 week, 5 days ago
On Wed, May 27, 2026 at 03:21:13PM +0800, Li kunyu wrote:
> The logic of the function here should be to convert the virtual runtime
> into actual time by combining it with the weight of the scheduling
> entity.
> However, it cannot be converted by simply converting actual time into
> virtual running time. A reverse conversion is required to obtain the
> correct time.
> 
> Signed-off-by: Li kunyu <likunyu10@163.com>
> ---
> v2: Use the div64_ul function to perform division operation
> 
>  kernel/sched/fair.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 69361c63353a..53f3373c1735 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -7033,7 +7033,9 @@ static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
>  			resched_curr(rq);
>  		return;
>  	}
> -	delta = (se->load.weight * vdelta) / NICE_0_LOAD;
> +
> +	if (unlikely(se->load.weight != NICE_0_LOAD))
> +		delta = div64_ul(NICE_0_LOAD * vdelta, se->load.weight);
>  
>  	/*
>  	 * Correct for instantaneous load of other classes.

This seems wrong. Note that calc_delta_fair() does: 

  vdelta := (delta * NICE_0_LOAD) / se->load.weight

which gets us time -> vtime. Therefore the vtime -> time transform is
the inverse of that:

  delta := (vdelta * se->load.weight) / NICE_0_LOAD;

which is exactly what the current code does.

Specifically:

                  NICE_0_LOAD      se->load.weight
       (delta * ---------------) * --------------- = delta
                se->load.weight      NICE_0_LOAD
Re: [PATCH v2] kernel/sched/fair: Fix the issue of virtual runtime conversion error
Posted by Li kunyu 1 week, 4 days ago
Hello maintainer:
  Sorry, I didn't pay close attention to the brackets when I was looking at the notes. It's indeed correct here.
  I am preparing to resubmit a patch that adds a nice0 judgment.