public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Knut Petersen <Knut_Petersen@t-online.de>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Paul Mackerras <paulus@samba.org>, Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@ghostprotocols.net>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: [PATCH] perf: Prevent divide by zero exception in kernel/events/core.c
Date: Wed, 25 Sep 2013 14:07:35 +0200	[thread overview]
Message-ID: <5242D207.10306@t-online.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 293 bytes --]

Any value written to /proc/sys/kernel/perf_event_max_sample_rate
is accepted and used as a divisor in perf_proc_update_handler().

Obviously that is wrong. 0 will cause a divide by 0 exception,
and negative values are unreasonable.

The attached patch enforces a lower limit of 1.

cu,
  Knut

[-- Attachment #2: 0001-Enforce-1-as-lower-limit-for-perf_event_max_sample_r.patch --]
[-- Type: text/x-patch, Size: 1122 bytes --]

>From 7f468bd7bad2f2fec2e93c89a48008d1cba99001 Mon Sep 17 00:00:00 2001
From: Knut Petersen <Knut_Petersen@t-online.de>
Date: Wed, 25 Sep 2013 13:34:57 +0200
Subject: [PATCH] Enforce 1 as lower limit for perf_event_max_sample_rate

/proc/sys/kernel/perf_event_max_sample_rate will accept
negative values as well as 0.

Negative values are unreasonable, and 0 causes a
divide by zero exception in perf_proc_update_handler.

This patch enforces a lower limit of 1.

Signed-off-by: Knut Petersen <Knut_Petersen@t-online.de>
---
 kernel/events/core.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index dd236b6..350bb53 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -198,6 +198,9 @@ int perf_proc_update_handler(struct ctl_table *table, int write,
 	if (ret || !write)
 		return ret;
 
+	if (sysctl_perf_event_sample_rate <= 0)
+		sysctl_perf_event_sample_rate = 1;
+
 	max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
 	perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
 	update_perf_cpu_limits();
-- 
1.8.1.4


             reply	other threads:[~2013-09-25 12:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-25 12:07 Knut Petersen [this message]
2013-09-25 12:12 ` [PATCH] perf: Prevent divide by zero exception in kernel/events/core.c Peter Zijlstra
2013-09-25 12:46   ` Knut Petersen
2013-10-04 17:32     ` [tip:perf/core] perf: Enforce 1 as lower limit for perf_event_max_sample_rate tip-bot for Knut Petersen

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=5242D207.10306@t-online.de \
    --to=knut_petersen@t-online.de \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@ghostprotocols.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=paulus@samba.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox