From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752906AbbIQQpP (ORCPT ); Thu, 17 Sep 2015 12:45:15 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47312 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751659AbbIQQpG (ORCPT ); Thu, 17 Sep 2015 12:45:06 -0400 Subject: single_task_running() vs. preemption warnings (was Re: [PATCH] kvm: fix preemption warnings in kvm_vcpu_block) To: Dominik Dingel References: <1442507270-67227-1-git-send-email-dingel@linux.vnet.ibm.com> Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, "David S. Miller" , herbert@gondor.apana.org.au, Ingo Molnar , Peter Zijlstra , linux-crypto@vger.kernel.org, Tim Chen From: Paolo Bonzini X-Enigmail-Draft-Status: N1110 Message-ID: <55FAEE0C.60904@redhat.com> Date: Thu, 17 Sep 2015 18:45:00 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <1442507270-67227-1-git-send-email-dingel@linux.vnet.ibm.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 17/09/2015 18:27, Dominik Dingel wrote: > + preempt_disable(); > + solo = single_task_running(); > + preempt_enable(); > + > cur = ktime_get(); > - } while (single_task_running() && ktime_before(cur, stop)); That's the obvious way to fix it, but the TOCTTOU problem (which was in the buggy code too) is obvious too. :) And the only other user of single_task_running() in drivers/crypto/mcryptd.c has the same issue. In fact, because of the way the function is used ("maybe I can do a little bit of work before going to sleep") it will likely be called many times in a loop. This in turn means that: - any wrong result due to a concurrent process migration would be rectified very soon - preempt_disable()/preempt_enable() can actually be just as expensive or more expensive than single_task_running() itself. Therefore, I wonder if single_task_running() should just use raw_smp_processor_id(). At least the TOCTTOU issue can be clearly documented in the function comment, instead of being hidden behind each of the callers. Thanks, Paolo