All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Slaby <jslaby@novell.com>
To: jirislaby@gmail.com
Cc: mingo@elte.hu, nhorman@tuxdriver.com, sfr@canb.auug.org.au,
	linux-kernel@vger.kernel.org, akpm@linux-foundation.org,
	marcin.slusarz@gmail.com, tglx@linutronix.de, mingo@redhat.com,
	hpa@zytor.com, torvalds@linux-foundation.org,
	Jiri Slaby <jslaby@novell.com>, James Morris <jmorris@namei.org>,
	Heiko Carstens <heiko.carstens@de.ibm.com>,
	Peter Zijlstra <peterz@infradead.org>
Subject: [PATCH 10/16] core: use ACCESS_ONCE for rlimits
Date: Wed, 18 Nov 2009 15:51:56 +0100	[thread overview]
Message-ID: <1258555922-2064-10-git-send-email-jslaby@novell.com> (raw)
In-Reply-To: <4B040A03.2020508@gmail.com>

Make sure compiler won't do weird things with limits. E.g. fetching
them twice may return 2 different values after writable limits are
implemented.

Signed-off-by: Jiri Slaby <jslaby@novell.com>
Cc: James Morris <jmorris@namei.org>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Peter Zijlstra <peterz@infradead.org>
---
 kernel/fork.c             |   10 ++++++----
 kernel/perf_event.c       |    3 ++-
 kernel/posix-cpu-timers.c |   16 +++++++++-------
 kernel/sched.c            |    6 ++++--
 kernel/sched_rt.c         |    5 +++--
 kernel/signal.c           |    4 ++--
 kernel/sys.c              |    4 ++--
 7 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index 166b8c4..dab13f2 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -822,6 +822,8 @@ void __cleanup_sighand(struct sighand_struct *sighand)
  */
 static void posix_cpu_timers_init_group(struct signal_struct *sig)
 {
+	unsigned long cpu_limit;
+
 	/* Thread group counters. */
 	thread_group_cputime_init(sig);
 
@@ -836,9 +838,9 @@ static void posix_cpu_timers_init_group(struct signal_struct *sig)
 	sig->cputime_expires.virt_exp = cputime_zero;
 	sig->cputime_expires.sched_exp = 0;
 
-	if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
-		sig->cputime_expires.prof_exp =
-			secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
+	cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
+	if (cpu_limit != RLIM_INFINITY) {
+		sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
 		sig->cputimer.running = 1;
 	}
 
@@ -1028,7 +1030,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 #endif
 	retval = -EAGAIN;
 	if (atomic_read(&p->real_cred->user->processes) >=
-			p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
+			ACCESS_ONCE(p->signal->rlim[RLIMIT_NPROC].rlim_cur)) {
 		if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
 		    p->real_cred->user != INIT_USER)
 			goto bad_fork_free;
diff --git a/kernel/perf_event.c b/kernel/perf_event.c
index 7f29643..229ce9a 100644
--- a/kernel/perf_event.c
+++ b/kernel/perf_event.c
@@ -2420,7 +2420,8 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
 	if (user_locked > user_lock_limit)
 		extra = user_locked - user_lock_limit;
 
-	lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
+	lock_limit = ACCESS_ONCE(current->signal->
+			rlim[RLIMIT_MEMLOCK].rlim_cur);
 	lock_limit >>= PAGE_SHIFT;
 	locked = vma->vm_mm->locked_vm + extra;
 
diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index a7dcce1..4c11521 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -640,7 +640,7 @@ static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
 				if (expires_le(sig->it[CPUCLOCK_PROF].expires,
 					       exp->cpu))
 					break;
-				i = sig->rlim[RLIMIT_CPU].rlim_cur;
+				i = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
 				if (i != RLIM_INFINITY &&
 				    i <= cputime_to_secs(exp->cpu))
 					break;
@@ -1032,9 +1032,10 @@ static void check_thread_timers(struct task_struct *tsk,
 	/*
 	 * Check for the special case thread timers.
 	 */
-	soft = sig->rlim[RLIMIT_RTTIME].rlim_cur;
+	soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
 	if (soft != RLIM_INFINITY) {
-		unsigned long hard = sig->rlim[RLIMIT_RTTIME].rlim_max;
+		unsigned long hard = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].
+				rlim_max);
 
 		if (hard != RLIM_INFINITY &&
 		    tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
@@ -1122,7 +1123,7 @@ static void check_process_timers(struct task_struct *tsk,
 	unsigned long long sum_sched_runtime, sched_expires;
 	struct list_head *timers = sig->cpu_timers;
 	struct task_cputime cputime;
-	unsigned long cpu_cur_lim = sig->rlim[RLIMIT_CPU].rlim_cur;
+	unsigned long cpu_cur_lim = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
 
 	/*
 	 * Don't sample the current process CPU clocks if there are no timers.
@@ -1198,7 +1199,8 @@ static void check_process_timers(struct task_struct *tsk,
 
 	if (cpu_cur_lim != RLIM_INFINITY) {
 		unsigned long psecs = cputime_to_secs(ptime);
-		unsigned long hard = sig->rlim[RLIMIT_CPU].rlim_max;
+		unsigned long hard =
+			ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
 		cputime_t x;
 		if (psecs >= hard) {
 			/*
@@ -1385,7 +1387,7 @@ static inline int fastpath_timer_check(struct task_struct *tsk)
 			return 1;
 	}
 
-	return sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY;
+	return ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur) != RLIM_INFINITY;
 }
 
 /*
@@ -1483,7 +1485,7 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
 		 * If the RLIMIT_CPU timer will expire before the
 		 * ITIMER_PROF timer, we have nothing else to do.
 		 */
-		if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
+		if (ACCESS_ONCE(tsk->signal->rlim[RLIMIT_CPU].rlim_cur)
 		    < cputime_to_secs(*newval))
 			return;
 	}
diff --git a/kernel/sched.c b/kernel/sched.c
index 3c11ae0..15172ea 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -6072,7 +6072,8 @@ int can_nice(const struct task_struct *p, const int nice)
 	/* convert nice value [19,-20] to rlimit style value [1,40] */
 	int nice_rlim = 20 - nice;
 
-	return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
+	return (nice_rlim <= ACCESS_ONCE(p->signal->
+				rlim[RLIMIT_NICE].rlim_cur) ||
 		capable(CAP_SYS_NICE));
 }
 
@@ -6257,7 +6258,8 @@ recheck:
 
 			if (!lock_task_sighand(p, &flags))
 				return -ESRCH;
-			rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
+			rlim_rtprio = ACCESS_ONCE(p->signal->
+					rlim[RLIMIT_RTPRIO].rlim_cur);
 			unlock_task_sighand(p, &flags);
 
 			/* can't set/change the rt policy */
diff --git a/kernel/sched_rt.c b/kernel/sched_rt.c
index a4d790c..99d4490 100644
--- a/kernel/sched_rt.c
+++ b/kernel/sched_rt.c
@@ -1683,8 +1683,9 @@ static void watchdog(struct rq *rq, struct task_struct *p)
 	if (!p->signal)
 		return;
 
-	soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
-	hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
+	/* max may change after cur was read, this will be fixed next tick */
+	soft = ACCESS_ONCE(p->signal->rlim[RLIMIT_RTTIME].rlim_cur);
+	hard = ACCESS_ONCE(p->signal->rlim[RLIMIT_RTTIME].rlim_max);
 
 	if (soft != RLIM_INFINITY) {
 		unsigned long next;
diff --git a/kernel/signal.c b/kernel/signal.c
index 6705320..e33ece0 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -208,8 +208,8 @@ static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
 	user = get_uid(__task_cred(t)->user);
 	atomic_inc(&user->sigpending);
 	if (override_rlimit ||
-	    atomic_read(&user->sigpending) <=
-			t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
+	    atomic_read(&user->sigpending) <= ACCESS_ONCE(t->signal->
+	    rlim[RLIMIT_SIGPENDING].rlim_cur))
 		q = kmem_cache_alloc(sigqueue_cachep, flags);
 	if (unlikely(q == NULL)) {
 		atomic_dec(&user->sigpending);
diff --git a/kernel/sys.c b/kernel/sys.c
index 0f86199..52200d4 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -572,8 +572,8 @@ static int set_user(struct cred *new)
 		return -EINVAL;
 	}
 
-	if (atomic_read(&new_user->processes) >=
-				current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
+	if (atomic_read(&new_user->processes) >= ACCESS_ONCE(current->signal->
+				rlim[RLIMIT_NPROC].rlim_cur) &&
 			new_user != INIT_USER) {
 		free_uid(new_user);
 		return -EAGAIN;
-- 
1.6.4.2


  parent reply	other threads:[~2009-11-18 14:53 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-28 20:06 [PATCH] proc: augment /proc/pid/limits to allow setting of process limits Neil Horman
2009-09-28 22:44 ` Andrew Morton
2009-09-29  1:14   ` Neil Horman
2009-09-29 20:25   ` [PATCH] proc: augment /proc/pid/limits to allow setting of process limits (v2) Neil Horman
2009-09-29 20:46     ` Andrew Morton
2009-09-30  0:59       ` Neil Horman
2009-10-01 17:15 ` [PATCH 0/3] extend get/setrlimit to support setting rlimits external to a process (v3) Neil Horman
2009-10-01 17:16   ` [PATCH 1/3] " Neil Horman
2009-10-04 12:14     ` Marcin Slusarz
2009-10-04 16:50       ` Neil Horman
2009-10-04 20:04         ` Marcin Slusarz
2009-10-04 23:10           ` Neil Horman
2009-10-04 20:30     ` Marcin Slusarz
2009-10-01 17:21   ` [PATCH 2/3] " Neil Horman
2009-10-01 17:22   ` [PATCH 3/3] " Neil Horman
2009-10-05  0:26   ` [PATCH 0/3] extend get/setrlimit to support setting rlimits external to a process (v4) Neil Horman
2009-10-05  0:53     ` [PATCH 1/3] " Neil Horman
2009-10-08 21:32       ` Marcin Slusarz
2009-10-09  2:00         ` Neil Horman
2009-10-05  0:54     ` [PATCH 2/3] " Neil Horman
2009-10-05  1:57       ` Américo Wang
2009-10-05 12:32         ` Neil Horman
2009-10-05  0:54     ` [PATCH 3/3] " Neil Horman
2009-10-12 16:13   ` [PATCH 0/3] extend get/setrlimit to support setting rlimits external to a process (v5) Neil Horman
2009-10-12 16:20     ` [PATCH 1/3] " Neil Horman
2009-10-12 16:25     ` [PATCH 2/3] " Neil Horman
2009-10-12 16:27     ` [PATCH 3/3] " Neil Horman
2009-10-12 20:13     ` [PATCH 0/3] extend get/setrlimit to support setting rlimits external to a process (v6) Neil Horman
2009-10-12 20:20       ` [PATCH 1/3] " Neil Horman
2009-10-12 20:23       ` [PATCH 2/3] " Neil Horman
2009-10-12 20:25       ` [PATCH 3/3] " Neil Horman
2009-10-20  0:52       ` [PATCH 0/3] extend get/setrlimit to support setting rlimits external to a process (v7) Neil Horman
2009-10-20  0:53         ` [PATCH 1/3] " Neil Horman
2009-10-20  0:54         ` [PATCH 2/3] " Neil Horman
2009-11-02 15:10           ` Ingo Molnar
2009-11-02 17:40             ` Neil Horman
2009-10-20  0:55         ` [PATCH 3/3] " Neil Horman
2009-10-28 14:44         ` [PATCH 0/3] " Neil Horman
2009-10-30 18:24           ` Neil Horman
2009-11-02 15:25         ` Ingo Molnar
2009-11-02 17:54           ` Neil Horman
2009-11-02 18:51             ` Ingo Molnar
2009-11-03  0:23               ` Neil Horman
2009-11-04 11:26                 ` Ingo Molnar
2009-11-05 20:48                   ` Neil Horman
2009-11-06  9:26                     ` Ingo Molnar
2009-11-06 10:00                       ` Jiri Slaby
2009-11-08 10:36                         ` Ingo Molnar
2009-11-09  0:10                           ` Neil Horman
2009-11-09  8:32                             ` Jiri Slaby
2009-11-09 13:34                               ` Neil Horman
2009-11-09  8:54                       ` Jiri Slaby
2009-11-09  9:01                         ` Ingo Molnar
2009-11-09  9:22                           ` Jiri Slaby
2009-11-09  9:26                             ` Ingo Molnar
2009-11-09 13:35                               ` Neil Horman
2009-11-09 15:56                           ` Jiri Slaby
2009-11-09 16:40                             ` Oleg Nesterov
2009-11-09 17:15                               ` Jiri Slaby
2009-11-09 17:26                                 ` Linus Torvalds
2009-11-09 17:36                                 ` Oleg Nesterov
2009-11-18 14:51                                   ` Jiri Slaby
2009-11-18 14:51                                     ` [PATCH 01/16] core: posix-cpu-timers, cleanup rlimits usage Jiri Slaby
2009-11-18 16:48                                       ` Peter Zijlstra
2009-11-18 14:51                                     ` [PATCH 02/16] core: do security check under task_lock Jiri Slaby
2009-11-18 21:47                                       ` James Morris
2009-11-18 14:51                                     ` [PATCH 03/16] IA64: use ACCESS_ONCE for rlimits Jiri Slaby
2009-11-18 14:51                                       ` Jiri Slaby
2009-11-18 18:56                                       ` Luck, Tony
2009-11-18 18:56                                         ` Luck, Tony
2009-11-18 19:48                                         ` Linus Torvalds
2009-11-18 19:48                                           ` Linus Torvalds
2009-11-19  2:28                                           ` Ingo Molnar
2009-11-19  2:28                                             ` Ingo Molnar
2009-11-18 14:51                                     ` [PATCH 04/16] PPC: " Jiri Slaby
2009-11-18 14:51                                       ` Jiri Slaby
2009-11-18 14:51                                     ` [PATCH 05/16] S390: " Jiri Slaby
2009-11-18 14:51                                     ` [PATCH 06/16] SPARC: " Jiri Slaby
2009-11-18 14:51                                       ` Jiri Slaby
2009-11-18 17:55                                       ` David Miller
2009-11-18 17:55                                         ` David Miller
2009-11-18 18:09                                         ` Linus Torvalds
2009-11-18 18:09                                           ` Linus Torvalds
2009-11-18 14:51                                     ` [PATCH 07/16] X86: " Jiri Slaby
2009-11-18 14:51                                     ` [PATCH 08/16] FS: " Jiri Slaby
2009-11-18 14:51                                     ` [PATCH 09/16] MM: " Jiri Slaby
2009-11-18 14:51                                       ` Jiri Slaby
2009-11-18 15:29                                       ` Linus Torvalds
2009-11-18 15:29                                         ` Linus Torvalds
2009-11-18 14:51                                     ` Jiri Slaby [this message]
     [not found]                                     ` <4B040A03.2020508-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2009-11-18 14:51                                       ` [PATCH 11/16] misc: " Jiri Slaby
2009-11-18 14:51                                         ` Jiri Slaby
2009-11-18 14:51                                     ` [PATCH 12/16] core: rename setrlimit to do_setrlimit Jiri Slaby
2009-11-20  6:10                                       ` Américo Wang
2009-11-18 14:51                                     ` [PATCH 13/16] core: implement getprlimit and setprlimit syscalls Jiri Slaby
2009-11-20 13:14                                       ` Neil Horman
2009-11-18 14:52                                     ` [PATCH 14/16] unistd: add __NR_[get|set]prlimit syscall numbers Jiri Slaby
2009-11-18 14:52                                     ` [PATCH 15/16] COMPAT: add get/put_compat_rlimit Jiri Slaby
2009-12-30 23:55                                       ` Arnd Bergmann
2010-01-06  9:35                                         ` Jiri Slaby
2009-11-18 14:52                                     ` [PATCH 16/16] x86: add ia32 compat prlimit syscalls Jiri Slaby
2009-11-18 23:15                                     ` [PATCH 0/3] extend get/setrlimit to support setting rlimits external to a process (v7) Oleg Nesterov
2009-11-19 15:43                                       ` Jiri Slaby
2009-11-20  2:11                                         ` acct_file_reopen() && do_acct_process() (Was: [PATCH 0/3] extend get/setrlimit to support setting rlimits external to a process (v7)) Oleg Nesterov
2009-11-20 10:27                                           ` Jiri Slaby
2009-10-12 21:58     ` [PATCH 0/3] extend get/setrlimit to support setting rlimits external to a process (v5) Andrew Morton
2009-10-13  0:06       ` Neil Horman

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=1258555922-2064-10-git-send-email-jslaby@novell.com \
    --to=jslaby@novell.com \
    --cc=akpm@linux-foundation.org \
    --cc=heiko.carstens@de.ibm.com \
    --cc=hpa@zytor.com \
    --cc=jirislaby@gmail.com \
    --cc=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcin.slusarz@gmail.com \
    --cc=mingo@elte.hu \
    --cc=mingo@redhat.com \
    --cc=nhorman@tuxdriver.com \
    --cc=peterz@infradead.org \
    --cc=sfr@canb.auug.org.au \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.