Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3642508imm; Mon, 18 Jun 2018 01:24:37 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKJssqAb+78fRZlIxKzrmmk7067HLhj3qcH6UwfiucUHpvocYnz68Fde8ZbnRAlwTJ+7qmw X-Received: by 2002:a17:902:ba87:: with SMTP id k7-v6mr12891309pls.271.1529310277465; Mon, 18 Jun 2018 01:24:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529310277; cv=none; d=google.com; s=arc-20160816; b=QgV/DVY5hD28q8B57Tgtx+Uv+JRldu5sGA8tnrD18/1D4Chg4nXdk/IQhqWv1V5Bsd dpQdc/W3iWNSvZhiOAMiGtRtrpm/Rp5E2MsikcJusAHlXHzFj6shXM+h+oIWkER1fzrn Uj2S+/AYNX6hmLfM2RQ8lrDirchLK8i7JR6xLDH32YF36Rv+MSuEQ/3OqrdiQEIJpK38 rSpP+mqLloQLiuaaRblvYtMqPVPECfwZypGPojo7/+T901sp6/G0z4W0an+0fthxqVgG J5vilGu3PTq7cEjk5A/Nl8yAHQWMthHaetuKqxJMGt1+SMc0YZ6H5TxverxVTQ6v4Kub rl8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=8AKKq4++/lGEybrcKRnaZC7W5Jd0Aco5jHPLooz6dlw=; b=RHbhYXUE6sQO3aWPnXdd9Jt5Epcgn/tn2uPUpENbYx0WyL/554HfXJ/TKXofAx8tYG tIOWzHGLhoj2Tv5u+qxVQmX7diHg1fun/CPoHj8QuoQQ8JlBH7kYOyKVA6v0ozY6v6iA sdk+T+AQnFjYvuvHVtohXBdqdjW7ldLXVZW8cGr4zlAAJiZjyujSU4QhdNtuT8h3JtT8 G0EtbwLoqIDhQD1M6sXwByQJvcLRhhn6xRjm+nimdfwj7LMEcoKZocBHfT0kmdJjnlsn r/oCG5A3cbY1CTBS7dahjLHzU+XaHu6AIyfF4LZZtOuR5ic9DGHE70XPslwTuS7UJI0C tdUw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m67-v6si12082256pgm.517.2018.06.18.01.24.23; Mon, 18 Jun 2018 01:24:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935842AbeFRIWl (ORCPT + 99 others); Mon, 18 Jun 2018 04:22:41 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:55594 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965575AbeFRIWi (ORCPT ); Mon, 18 Jun 2018 04:22:38 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 3E429C8D; Mon, 18 Jun 2018 08:22:37 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Gaurav Kohli , "Peter Zijlstra (Intel)" , Oleg Nesterov , Linus Torvalds , Thomas Gleixner , Ingo Molnar , Sasha Levin Subject: [PATCH 4.16 160/279] kthread, sched/wait: Fix kthread_parkme() wait-loop Date: Mon, 18 Jun 2018 10:12:25 +0200 Message-Id: <20180618080615.443831198@linuxfoundation.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180618080608.851973560@linuxfoundation.org> References: <20180618080608.851973560@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Peter Zijlstra [ Upstream commit 741a76b350897604c48fb12beff1c9b77724dc96 ] Gaurav reported a problem with __kthread_parkme() where a concurrent try_to_wake_up() could result in competing stores to ->state which, when the TASK_PARKED store got lost bad things would happen. The comment near set_current_state() actually mentions this competing store, but only mentions the case against TASK_RUNNING. This same store, with different timing, can happen against a subsequent !RUNNING store. This normally is not a problem, because as per that same comment, the !RUNNING state store is inside a condition based wait-loop: for (;;) { set_current_state(TASK_UNINTERRUPTIBLE); if (!need_sleep) break; schedule(); } __set_current_state(TASK_RUNNING); If we loose the (first) TASK_UNINTERRUPTIBLE store to a previous (concurrent) wakeup, the schedule() will NO-OP and we'll go around the loop once more. The problem here is that the TASK_PARKED store is not inside the KTHREAD_SHOULD_PARK condition wait-loop. There is a genuine issue with sleeps that do not have a condition; this is addressed in a subsequent patch. Reported-by: Gaurav Kohli Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Oleg Nesterov Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- kernel/kthread.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -177,12 +177,13 @@ void *kthread_probe_data(struct task_str static void __kthread_parkme(struct kthread *self) { - __set_current_state(TASK_PARKED); - while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) { + for (;;) { + set_current_state(TASK_PARKED); + if (!test_bit(KTHREAD_SHOULD_PARK, &self->flags)) + break; if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags)) complete(&self->parked); schedule(); - __set_current_state(TASK_PARKED); } clear_bit(KTHREAD_IS_PARKED, &self->flags); __set_current_state(TASK_RUNNING);