From patchwork Tue May 13 12:11:31 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juri Lelli X-Patchwork-Id: 30044 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qa0-f70.google.com (mail-qa0-f70.google.com [209.85.216.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A98A720446 for ; Tue, 13 May 2014 12:10:52 +0000 (UTC) Received: by mail-qa0-f70.google.com with SMTP id cm18sf595724qab.9 for ; Tue, 13 May 2014 05:10:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:date:from:to:cc:subject:message-id :in-reply-to:references:mime-version:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe:content-type :content-transfer-encoding; bh=eniauqzF5x/s/Tkvkv7PxZnAMBmJrljdqfyUCyJ6krk=; b=QtNH7KvNG8p1Oq94jJ0rNRTb9sTL7p9qO+PpcA7oX/BaXYb9TnYdaI005N6RBBLMg6 chQsSMfbsr62zMe8JbQG0NB6O0Gsnr21mQfOBIa0sFQ4e7Mo4daGu1EnYKpTFHR66b0q JD3YK6KY5AVAUz5OcAp3V3WstyEzubdpSnlEQbQl/NNchXG7VUVQl88FMw98n8ukhBP5 xh74Q6AcZ/d4EHq8oCshuDQCM477EM66rxE5Kr0NtB0zETfgqAPbTlcBIpsdcPr+EO1t YbBJiipvR4YDfaxfBujOuChJZFyVgV2vHaeiN5SOCQ1/6v2SZ015JTcle3RxZfNuo/CP TOWA== X-Gm-Message-State: ALoCoQm9U8TojQYQ+ZDKdtFHjBLGh3JDO8NeBOBPB2OzL36i0jnynxY0bJZce+0fdGMLcHcmG6AJ X-Received: by 10.224.147.72 with SMTP id k8mr16485766qav.5.1399983052484; Tue, 13 May 2014 05:10:52 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.43.135 with SMTP id e7ls1871544qga.70.gmail; Tue, 13 May 2014 05:10:52 -0700 (PDT) X-Received: by 10.52.237.228 with SMTP id vf4mr178752vdc.47.1399983052351; Tue, 13 May 2014 05:10:52 -0700 (PDT) Received: from mail-vc0-x229.google.com (mail-vc0-x229.google.com [2607:f8b0:400c:c03::229]) by mx.google.com with ESMTPS id sn5si2602270vdc.101.2014.05.13.05.10.52 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 13 May 2014 05:10:52 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2607:f8b0:400c:c03::229 as permitted sender) client-ip=2607:f8b0:400c:c03::229; Received: by mail-vc0-f169.google.com with SMTP id ij19so285753vcb.14 for ; Tue, 13 May 2014 05:10:52 -0700 (PDT) X-Received: by 10.52.229.197 with SMTP id ss5mr1921vdc.83.1399983052238; Tue, 13 May 2014 05:10:52 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.221.72 with SMTP id ib8csp145908vcb; Tue, 13 May 2014 05:10:51 -0700 (PDT) X-Received: by 10.68.114.227 with SMTP id jj3mr2289718pbb.61.1399983051203; Tue, 13 May 2014 05:10:51 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id xn5si13097092pab.60.2014.05.13.05.10.50; Tue, 13 May 2014 05:10:50 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933058AbaEMMKm (ORCPT + 27 others); Tue, 13 May 2014 08:10:42 -0400 Received: from mail-we0-f181.google.com ([74.125.82.181]:33654 "EHLO mail-we0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759338AbaEMMKl (ORCPT ); Tue, 13 May 2014 08:10:41 -0400 Received: by mail-we0-f181.google.com with SMTP id w61so278084wes.12 for ; Tue, 13 May 2014 05:10:39 -0700 (PDT) X-Received: by 10.194.90.107 with SMTP id bv11mr26426543wjb.11.1399983039895; Tue, 13 May 2014 05:10:39 -0700 (PDT) Received: from neville (nat-cataldo.sssup.it. [193.205.81.5]) by mx.google.com with ESMTPSA id rw4sm21983070wjb.44.2014.05.13.05.10.37 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 13 May 2014 05:10:38 -0700 (PDT) Date: Tue, 13 May 2014 14:11:31 +0200 From: Juri Lelli To: Peter Zijlstra Cc: "Michael Kerrisk (man-pages)" , Dario Faggioli , Ingo Molnar , lkml , Dave Jones Subject: Re: [BUG] sched_setattr() SCHED_DEADLINE hangs system Message-Id: <20140513141131.20d944f81633ee937f256385@gmail.com> In-Reply-To: <20140513104307.GZ30445@twins.programming.kicks-ass.net> References: <536F8F0E.7020301@gmail.com> <53707007.3080003@gmail.com> <20140512084727.GN30445@twins.programming.kicks-ass.net> <20140512123032.GC13467@laptop.programming.kicks-ass.net> <20140513115749.ebf3eebc64e44aac6f183410@gmail.com> <20140513104307.GZ30445@twins.programming.kicks-ass.net> X-Mailer: Sylpheed 3.2.0beta5 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Original-Sender: juri.lelli@gmail.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2607:f8b0:400c:c03::229 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=neutral (body hash did not verify) header.i=@; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , On Tue, 13 May 2014 12:43:07 +0200 Peter Zijlstra wrote: > On Tue, May 13, 2014 at 11:57:49AM +0200, Juri Lelli wrote: > > static bool > > __checkparam_dl(const struct sched_attr *attr) > > { > > return attr && attr->sched_deadline != 0 && > > (attr->sched_period == 0 || > > - (s64)(attr->sched_period - attr->sched_deadline) >= 0) && > > - (s64)(attr->sched_deadline - attr->sched_runtime ) >= 0 && > > - attr->sched_runtime >= (2 << (DL_SCALE - 1)); > > + (attr->sched_period >= attr->sched_deadline)) && > > + (attr->sched_deadline >= attr->sched_runtime) && > > + attr->sched_runtime >= (1ULL << DL_SCALE) && > > + (attr->sched_deadline < (1ULL << 63) && > > + attr->sched_period < (1ULL << 63)); > > } > > Could we maybe rewrite that function to look less like a ioccc.org > submission? > Right. > static bool > __checkparam_dl(const struct sched_attr *attr) > { > if (!attr) /* possible at all? */ > return false; > I'd say no, removed. > /* runtime <= deadline <= period */ > if (attr->sched_period < attr->sched_deadline || > attr->sched_deadline < attr->sched_runtime) > return false; > > /* > * Since we truncate DL_SCALE bits make sure we're at least that big, > * if runtime > (1 << DL_SCALE), so must the others be per the above > */ > if (attr->sched_runtime <= (1ULL << DL_SCALE)) > return false; > > /* > * Since we use the MSB for wrap-around and sign issues, make > * sure its not set, if period < 2^63, so must the others be. > */ > if (attr->sched_period & (1ULL << 63)) > return false; > > return true; > } > > Did I miss anything? period can be 0, so we have to check also sched_deadline for MSB set. Thanks, - Juri --- >From e0c44d7614127f8dfbafc08c30681cb8098271e8 Mon Sep 17 00:00:00 2001 From: Juri Lelli Date: Tue, 13 May 2014 10:15:59 +0200 Subject: [PATCH] sched/deadline: restrict user params max value to 2^63 ns Michael Kerrisk noticed that creating SCHED_DEADLINE reservations with certain parameters (e.g, a runtime of something near 2^64 ns) can cause a system freeze for some amount of time. The problem is that in the interface we have u64 sched_runtime; while internally we need to have a signed runtime (to cope with budget overruns) s64 runtime; At the time we setup a new dl_entity we copy the first value in the second. The cast turns out with negative values when sched_runtime is too big, and this causes the scheduler to go crazy right from the start. Moreover, considering how we deal with deadlines wraparound (s64)(a - b) < 0 we also have to restrict acceptable values for sched_{deadline,period}. This patch fixes the thing checking that user parameters are always below 2^63 ns (still large enough for everyone). It also rewrites other conditions that we check, since in __checkparam_dl we don't have to deal with deadline wraparounds and what we have now erroneously fails when the difference between values is too big. Reported-by: Michael Kerrisk Suggested-by: Peter Zijlstra Signed-off-by: Juri Lelli --- kernel/sched/core.c | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index d9d8ece..682a986 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3188,17 +3188,40 @@ __getparam_dl(struct task_struct *p, struct sched_attr *attr) * We ask for the deadline not being zero, and greater or equal * than the runtime, as well as the period of being zero or * greater than deadline. Furthermore, we have to be sure that - * user parameters are above the internal resolution (1us); we - * check sched_runtime only since it is always the smaller one. + * user parameters are above the internal resolution of 1us (we + * check sched_runtime only since it is always the smaller one) and + * below 2^63 ns (we have to check both sched_deadline and + * sched_period, as the latter can be zero). */ static bool __checkparam_dl(const struct sched_attr *attr) { - return attr && attr->sched_deadline != 0 && - (attr->sched_period == 0 || - (s64)(attr->sched_period - attr->sched_deadline) >= 0) && - (s64)(attr->sched_deadline - attr->sched_runtime ) >= 0 && - attr->sched_runtime >= (2 << (DL_SCALE - 1)); + /* deadline != 0 */ + if (attr->sched_deadline == 0) + return false; + + /* + * Since we truncate DL_SCALE bits, make sure we're at least + * that big. + */ + if (attr->sched_runtime < (1ULL << DL_SCALE)) + return false; + + /* + * Since we use the MSB for wrap-around and sign issues, make + * sure it's not set (mind that period can be equal to zero). + */ + if (attr->sched_deadline & (1ULL << 63) || + attr->sched_period & (1ULL << 63)) + return false; + + /* runtime <= deadline <= period (if period != 0) */ + if ((attr->sched_period != 0 && + attr->sched_period < attr->sched_deadline) || + attr->sched_deadline < attr->sched_runtime) + return false; + + return true; } /*