mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
To: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Cc: linux-kernel@vger.kernel.org, mingo@elte.hu,
	laijs@cn.fujitsu.com, dipankar@in.ibm.com,
	akpm@linux-foundation.org, josh@joshtriplett.org,
	dvhltc@us.ibm.com, niv@us.ibm.com, tglx@linutronix.de,
	peterz@infradead.org, rostedt@goodmis.org,
	Valdis.Kletnieks@vt.edu, dhowells@redhat.com
Subject: Re: [PATCH tip/core/rcu 17/18] rcu: stop overflowing signed integers
Date: Wed, 17 Feb 2010 16:30:39 -0500	[thread overview]
Message-ID: <20100217213039.GB15539@Krystal> (raw)
In-Reply-To: <1266438584-29173-17-git-send-email-paulmck@linux.vnet.ibm.com>

* Paul E. McKenney (paulmck@linux.vnet.ibm.com) wrote:
> The C standard does not specify the result of an operation that overflows
> a signed integer, so such operations need to be avoided.  This patch
> changes the type of several fields from "long" to "unsigned long" and
> adjusts operations as needed.  ULONG_CMP_GE() and ULONG_CMP_LT() macros
> are introduced to do the modular comparisons that are appropriate given
> that overflow is an expected event.
> 
> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>

I had to do something similar for liburcu QSBR to follow the standard.

Acked-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

> ---
>  kernel/rcutree.c       |   11 +++++------
>  kernel/rcutree.h       |   33 ++++++++++++++++++---------------
>  kernel/rcutree_trace.c |   14 +++++++-------
>  3 files changed, 30 insertions(+), 28 deletions(-)
> 
> diff --git a/kernel/rcutree.c b/kernel/rcutree.c
> index 29d88c0..dd0d31d 100644
> --- a/kernel/rcutree.c
> +++ b/kernel/rcutree.c
> @@ -500,7 +500,7 @@ static void print_cpu_stall(struct rcu_state *rsp)
>  	trigger_all_cpu_backtrace();
>  
>  	spin_lock_irqsave(&rnp->lock, flags);
> -	if ((long)(jiffies - rsp->jiffies_stall) >= 0)
> +	if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall))
>  		rsp->jiffies_stall =
>  			jiffies + RCU_SECONDS_TILL_STALL_RECHECK;
>  	spin_unlock_irqrestore(&rnp->lock, flags);
> @@ -1216,8 +1216,7 @@ static void force_quiescent_state(struct rcu_state *rsp, int relaxed)
>  		rsp->n_force_qs_lh++; /* Inexact, can lose counts.  Tough! */
>  		return;	/* Someone else is already on the job. */
>  	}
> -	if (relaxed &&
> -	    (long)(rsp->jiffies_force_qs - jiffies) >= 0)
> +	if (relaxed && ULONG_CMP_GE(rsp->jiffies_force_qs, jiffies))
>  		goto unlock_fqs_ret; /* no emergency and done recently. */
>  	rsp->n_force_qs++;
>  	spin_lock(&rnp->lock);  /* irqs already disabled */
> @@ -1295,7 +1294,7 @@ __rcu_process_callbacks(struct rcu_state *rsp, struct rcu_data *rdp)
>  	 * If an RCU GP has gone long enough, go check for dyntick
>  	 * idle CPUs and, if needed, send resched IPIs.
>  	 */
> -	if ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)
> +	if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
>  		force_quiescent_state(rsp, 1);
>  
>  	/*
> @@ -1392,7 +1391,7 @@ __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu),
>  			force_quiescent_state(rsp, 0);
>  		rdp->n_force_qs_snap = rsp->n_force_qs;
>  		rdp->qlen_last_fqs_check = rdp->qlen;
> -	} else if ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)
> +	} else if (ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies))
>  		force_quiescent_state(rsp, 1);
>  	local_irq_restore(flags);
>  }
> @@ -1525,7 +1524,7 @@ static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp)
>  
>  	/* Has an RCU GP gone long enough to send resched IPIs &c? */
>  	if (rcu_gp_in_progress(rsp) &&
> -	    ((long)(ACCESS_ONCE(rsp->jiffies_force_qs) - jiffies) < 0)) {
> +	    ULONG_CMP_LT(ACCESS_ONCE(rsp->jiffies_force_qs), jiffies)) {
>  		rdp->n_rp_need_fqs++;
>  		return 1;
>  	}
> diff --git a/kernel/rcutree.h b/kernel/rcutree.h
> index d9d032a..7495fed 100644
> --- a/kernel/rcutree.h
> +++ b/kernel/rcutree.h
> @@ -92,10 +92,10 @@ struct rcu_dynticks {
>  struct rcu_node {
>  	spinlock_t lock;	/* Root rcu_node's lock protects some */
>  				/*  rcu_state fields as well as following. */
> -	long	gpnum;		/* Current grace period for this node. */
> +	unsigned long gpnum;	/* Current grace period for this node. */
>  				/*  This will either be equal to or one */
>  				/*  behind the root rcu_node's gpnum. */
> -	long	completed;	/* Last grace period completed for this node. */
> +	unsigned long completed; /* Last GP completed for this node. */
>  				/*  This will either be equal to or one */
>  				/*  behind the root rcu_node's gpnum. */
>  	unsigned long qsmask;	/* CPUs or groups that need to switch in */
> @@ -161,11 +161,11 @@ struct rcu_node {
>  /* Per-CPU data for read-copy update. */
>  struct rcu_data {
>  	/* 1) quiescent-state and grace-period handling : */
> -	long		completed;	/* Track rsp->completed gp number */
> +	unsigned long	completed;	/* Track rsp->completed gp number */
>  					/*  in order to detect GP end. */
> -	long		gpnum;		/* Highest gp number that this CPU */
> +	unsigned long	gpnum;		/* Highest gp number that this CPU */
>  					/*  is aware of having started. */
> -	long		passed_quiesc_completed;
> +	unsigned long	passed_quiesc_completed;
>  					/* Value of completed at time of qs. */
>  	bool		passed_quiesc;	/* User-mode/idle loop etc. */
>  	bool		qs_pending;	/* Core waits for quiesc state. */
> @@ -221,14 +221,14 @@ struct rcu_data {
>  	unsigned long resched_ipi;	/* Sent a resched IPI. */
>  
>  	/* 5) __rcu_pending() statistics. */
> -	long n_rcu_pending;		/* rcu_pending() calls since boot. */
> -	long n_rp_qs_pending;
> -	long n_rp_cb_ready;
> -	long n_rp_cpu_needs_gp;
> -	long n_rp_gp_completed;
> -	long n_rp_gp_started;
> -	long n_rp_need_fqs;
> -	long n_rp_need_nothing;
> +	unsigned long n_rcu_pending;	/* rcu_pending() calls since boot. */
> +	unsigned long n_rp_qs_pending;
> +	unsigned long n_rp_cb_ready;
> +	unsigned long n_rp_cpu_needs_gp;
> +	unsigned long n_rp_gp_completed;
> +	unsigned long n_rp_gp_started;
> +	unsigned long n_rp_need_fqs;
> +	unsigned long n_rp_need_nothing;
>  
>  	int cpu;
>  };
> @@ -255,6 +255,9 @@ struct rcu_data {
>  
>  #endif /* #ifdef CONFIG_RCU_CPU_STALL_DETECTOR */
>  
> +#define ULONG_CMP_GE(a, b)	(ULONG_MAX / 2 >= (a) - (b))
> +#define ULONG_CMP_LT(a, b)	(ULONG_MAX / 2 < (a) - (b))
> +
>  /*
>   * RCU global state, including node hierarchy.  This hierarchy is
>   * represented in "heap" form in a dense array.  The root (first level)
> @@ -283,8 +286,8 @@ struct rcu_state {
>  						/*  period because */
>  						/*  force_quiescent_state() */
>  						/*  was running. */
> -	long	gpnum;				/* Current gp number. */
> -	long	completed;			/* # of last completed gp. */
> +	unsigned long gpnum;			/* Current gp number. */
> +	unsigned long completed;		/* # of last completed gp. */
>  
>  	/* End of fields guarded by root rcu_node's lock. */
>  
> diff --git a/kernel/rcutree_trace.c b/kernel/rcutree_trace.c
> index 9d2c884..d45db2e 100644
> --- a/kernel/rcutree_trace.c
> +++ b/kernel/rcutree_trace.c
> @@ -50,7 +50,7 @@ static void print_one_rcu_data(struct seq_file *m, struct rcu_data *rdp)
>  {
>  	if (!rdp->beenonline)
>  		return;
> -	seq_printf(m, "%3d%cc=%ld g=%ld pq=%d pqc=%ld qp=%d",
> +	seq_printf(m, "%3d%cc=%lu g=%lu pq=%d pqc=%lu qp=%d",
>  		   rdp->cpu,
>  		   cpu_is_offline(rdp->cpu) ? '!' : ' ',
>  		   rdp->completed, rdp->gpnum,
> @@ -105,7 +105,7 @@ static void print_one_rcu_data_csv(struct seq_file *m, struct rcu_data *rdp)
>  {
>  	if (!rdp->beenonline)
>  		return;
> -	seq_printf(m, "%d,%s,%ld,%ld,%d,%ld,%d",
> +	seq_printf(m, "%d,%s,%lu,%lu,%d,%lu,%d",
>  		   rdp->cpu,
>  		   cpu_is_offline(rdp->cpu) ? "\"N\"" : "\"Y\"",
>  		   rdp->completed, rdp->gpnum,
> @@ -155,13 +155,13 @@ static const struct file_operations rcudata_csv_fops = {
>  
>  static void print_one_rcu_state(struct seq_file *m, struct rcu_state *rsp)
>  {
> -	long gpnum;
> +	unsigned long gpnum;
>  	int level = 0;
>  	int phase;
>  	struct rcu_node *rnp;
>  
>  	gpnum = rsp->gpnum;
> -	seq_printf(m, "c=%ld g=%ld s=%d jfq=%ld j=%x "
> +	seq_printf(m, "c=%lu g=%lu s=%d jfq=%ld j=%x "
>  		      "nfqs=%lu/nfqsng=%lu(%lu) fqlh=%lu oqlen=%ld\n",
>  		   rsp->completed, gpnum, rsp->signaled,
>  		   (long)(rsp->jiffies_force_qs - jiffies),
> @@ -215,12 +215,12 @@ static const struct file_operations rcuhier_fops = {
>  static int show_rcugp(struct seq_file *m, void *unused)
>  {
>  #ifdef CONFIG_TREE_PREEMPT_RCU
> -	seq_printf(m, "rcu_preempt: completed=%ld  gpnum=%ld\n",
> +	seq_printf(m, "rcu_preempt: completed=%ld  gpnum=%lu\n",
>  		   rcu_preempt_state.completed, rcu_preempt_state.gpnum);
>  #endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
> -	seq_printf(m, "rcu_sched: completed=%ld  gpnum=%ld\n",
> +	seq_printf(m, "rcu_sched: completed=%ld  gpnum=%lu\n",
>  		   rcu_sched_state.completed, rcu_sched_state.gpnum);
> -	seq_printf(m, "rcu_bh: completed=%ld  gpnum=%ld\n",
> +	seq_printf(m, "rcu_bh: completed=%ld  gpnum=%lu\n",
>  		   rcu_bh_state.completed, rcu_bh_state.gpnum);
>  	return 0;
>  }
> -- 
> 1.6.6
> 

-- 
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68

  reply	other threads:[~2010-02-17 21:30 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-17 20:29 [PATCH tip/core/rcu 0/18] v5 add lockdep-based diagnostics to rcu_dereference() Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 01/18] rcu: introduce lockdep-based checking to RCU read-side primitives Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 02/18] rcu: add lockdep-enabled variants of rcu_dereference() Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 03/18] rcu: integrate rcu_dereference_check() message into lockdep Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 04/18] rcu: disable lockdep checking in RCU list-traversal primitives Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 05/18] net: add checking to rcu_dereference() primitives Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 06/18] sched: use lockdep-based checking on rcu_dereference() Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 07/18] sched: better name for for_each_domain_rd Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 08/18] vfs: apply lockdep-based checking to rcu_dereference() uses Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 09/18] vfs: abstract rcu_dereference_check for files-fdtable use Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 10/18] radix-tree: disable RCU lockdep checking in radix tree Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 11/18] idr: apply lockdep-based diagnostics to rcu_dereference() uses Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 12/18] security: apply lockdep-based checking " Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 13/18] rcu: documentation update for CONFIG_PROVE_RCU Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 14/18] rcu: fix citation of Mathieu's dissertation Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 15/18] rcu: accelerate grace period if last non-dynticked CPU Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 16/18] rcu: use canonical URL for Mathieu's dissertation Paul E. McKenney
2010-02-17 20:29 ` [PATCH tip/core/rcu 17/18] rcu: stop overflowing signed integers Paul E. McKenney
2010-02-17 21:30   ` Mathieu Desnoyers [this message]
2010-02-17 20:29 ` [PATCH tip/core/rcu 18/18] The spinlocks in rcutree need to be real spinlocks in preempt-rt. Convert them to raw_spinlocks Paul E. McKenney
2010-02-18 16:57   ` [PATCH tip/core/rcu 18/18 amended] rcu: Convert " Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100217213039.GB15539@Krystal \
    --to=mathieu.desnoyers@polymtl.ca \
    --cc=Valdis.Kletnieks@vt.edu \
    --cc=akpm@linux-foundation.org \
    --cc=dhowells@redhat.com \
    --cc=dipankar@in.ibm.com \
    --cc=dvhltc@us.ibm.com \
    --cc=josh@joshtriplett.org \
    --cc=laijs@cn.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=niv@us.ibm.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®