Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  Floating proportions with flexible aging period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *   Copyright (C) 2011, SUSE, Jan Kara <jack@suse.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * The goal of this code is: Given different types of event, measure proportion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * of each type of event over time. The proportions are measured with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * exponentially decaying history to give smooth transitions. A formula
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * expressing proportion of event of type 'j' is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *   p_{j} = (\Sum_{i>=0} x_{i,j}/2^{i+1})/(\Sum_{i>=0} x_i/2^{i+1})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Where x_{i,j} is j's number of events in i-th last time period and x_i is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * total number of events in i-th last time period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * Note that p_{j}'s are normalised, i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *   \Sum_{j} p_{j} = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * This formula can be straightforwardly computed by maintaining denominator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * (let's call it 'd') and for each event type its numerator (let's call it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * 'n_j'). When an event of type 'j' happens, we simply need to do:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *   n_j++; d++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * When a new period is declared, we could do:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *   d /= 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *   for each j
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *     n_j /= 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * To avoid iteration over all event types, we instead shift numerator of event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * j lazily when someone asks for a proportion of event j or when event j
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * occurs. This can bit trivially implemented by remembering last period in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * which something happened with proportion of type j.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/flex_proportions.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) int fprop_global_init(struct fprop_global *p, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	p->period = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	/* Use 1 to avoid dealing with periods with 0 events... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	err = percpu_counter_init(&p->events, 1, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	seqcount_init(&p->sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) void fprop_global_destroy(struct fprop_global *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	percpu_counter_destroy(&p->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * Declare @periods new periods. It is upto the caller to make sure period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * transitions cannot happen in parallel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * The function returns true if the proportions are still defined and false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * if aging zeroed out all events. This can be used to detect whether declaring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * further periods has any effect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) bool fprop_new_period(struct fprop_global *p, int periods)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	s64 events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	events = percpu_counter_sum(&p->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	 * Don't do anything if there are no events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (events <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	write_seqcount_begin(&p->sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (periods < 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		events -= events >> periods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/* Use addition to avoid losing events happening between sum and set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	percpu_counter_add(&p->events, -events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	p->period += periods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	write_seqcount_end(&p->sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * ---- SINGLE ----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) int fprop_local_init_single(struct fprop_local_single *pl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	pl->events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	pl->period = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	raw_spin_lock_init(&pl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void fprop_local_destroy_single(struct fprop_local_single *pl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static void fprop_reflect_period_single(struct fprop_global *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 					struct fprop_local_single *pl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	unsigned int period = p->period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	/* Fast path - period didn't change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (pl->period == period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	raw_spin_lock_irqsave(&pl->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	/* Someone updated pl->period while we were spinning? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (pl->period >= period) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		raw_spin_unlock_irqrestore(&pl->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* Aging zeroed our fraction? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (period - pl->period < BITS_PER_LONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		pl->events >>= period - pl->period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		pl->events = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	pl->period = period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	raw_spin_unlock_irqrestore(&pl->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* Event of type pl happened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void __fprop_inc_single(struct fprop_global *p, struct fprop_local_single *pl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	fprop_reflect_period_single(p, pl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	pl->events++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	percpu_counter_add(&p->events, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Return fraction of events of type pl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) void fprop_fraction_single(struct fprop_global *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			   struct fprop_local_single *pl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			   unsigned long *numerator, unsigned long *denominator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	unsigned int seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	s64 num, den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		seq = read_seqcount_begin(&p->sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		fprop_reflect_period_single(p, pl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		num = pl->events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		den = percpu_counter_read_positive(&p->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	} while (read_seqcount_retry(&p->sequence, seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * Make fraction <= 1 and denominator > 0 even in presence of percpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * counter errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (den <= num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		if (num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			den = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			den = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	*denominator = den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	*numerator = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * ---- PERCPU ----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) #define PROP_BATCH (8*(1+ilog2(nr_cpu_ids)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int fprop_local_init_percpu(struct fprop_local_percpu *pl, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	err = percpu_counter_init(&pl->events, 0, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	pl->period = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	raw_spin_lock_init(&pl->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) void fprop_local_destroy_percpu(struct fprop_local_percpu *pl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	percpu_counter_destroy(&pl->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static void fprop_reflect_period_percpu(struct fprop_global *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 					struct fprop_local_percpu *pl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	unsigned int period = p->period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	/* Fast path - period didn't change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (pl->period == period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	raw_spin_lock_irqsave(&pl->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	/* Someone updated pl->period while we were spinning? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (pl->period >= period) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		raw_spin_unlock_irqrestore(&pl->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	/* Aging zeroed our fraction? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (period - pl->period < BITS_PER_LONG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		s64 val = percpu_counter_read(&pl->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		if (val < (nr_cpu_ids * PROP_BATCH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			val = percpu_counter_sum(&pl->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		percpu_counter_add_batch(&pl->events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			-val + (val >> (period-pl->period)), PROP_BATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		percpu_counter_set(&pl->events, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	pl->period = period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	raw_spin_unlock_irqrestore(&pl->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* Event of type pl happened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) void __fprop_inc_percpu(struct fprop_global *p, struct fprop_local_percpu *pl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	fprop_reflect_period_percpu(p, pl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	percpu_counter_add_batch(&pl->events, 1, PROP_BATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	percpu_counter_add(&p->events, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) void fprop_fraction_percpu(struct fprop_global *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			   struct fprop_local_percpu *pl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			   unsigned long *numerator, unsigned long *denominator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	unsigned int seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	s64 num, den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		seq = read_seqcount_begin(&p->sequence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		fprop_reflect_period_percpu(p, pl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		num = percpu_counter_read_positive(&pl->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		den = percpu_counter_read_positive(&p->events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	} while (read_seqcount_retry(&p->sequence, seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	 * Make fraction <= 1 and denominator > 0 even in presence of percpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 * counter errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (den <= num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if (num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			den = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			den = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	*denominator = den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	*numerator = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * Like __fprop_inc_percpu() except that event is counted only if the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  * type has fraction smaller than @max_frac/FPROP_FRAC_BASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) void __fprop_inc_percpu_max(struct fprop_global *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			    struct fprop_local_percpu *pl, int max_frac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (unlikely(max_frac < FPROP_FRAC_BASE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		unsigned long numerator, denominator;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		fprop_fraction_percpu(p, pl, &numerator, &denominator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (numerator >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		    (((u64)denominator) * max_frac) >> FPROP_FRAC_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	__fprop_inc_percpu(p, pl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }