Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp324236imu; Mon, 5 Nov 2018 01:16:58 -0800 (PST) X-Google-Smtp-Source: AJdET5c0PsnsR/1Kc5WRQoWsB3zEgsXZkomQAjcocA9gEUMCOh+OdnJK9oU0nZDmv22ZEiR5p2J9 X-Received: by 2002:a17:902:9009:: with SMTP id a9-v6mr21511663plp.134.1541409418120; Mon, 05 Nov 2018 01:16:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541409418; cv=none; d=google.com; s=arc-20160816; b=IOWm3Q3+Ys4kJ7B05a7Vj2yITFMOkAz79C9VI7W3an04uqxb2hvIK7d847n348qlMJ 1ev8REq0HzvesDgpoyNsUS6taBCMezCQOIuLVv5N1Px1G+7MA3MXzRrtUpvygg47HSsF CYLu5DKNCwLq6miWifCgr9DULY4T+NQBMvukHPDxBzWy6VyK4kRGh44O0K2CKZc0Tct0 R+2oP+IFnvXTJi0uyQEMOLd27SRaGWr2qUW+AEzv/r8F/zK8gDZKQ3hj1HvKrKgJqRDL LR6Eg1FDcM1vGkLoKBR1TMxeU8+7erG9U270h+Z/XCy2+VQ/3Q7m++b8PMUkGXjSwmk6 Rs7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=kc8wR7hYig0dO/0a3pBZjnuuBPBi5OS3qenFqfEJwng=; b=Frqgjl+2DXRXnl+/4H/S8iI6ezkFGNpASIYexl6/m2xfqY7rjJFBDG6lMnMWsI8c7B 11B3QKB5BfWrmlHVKjd7BRa3NMK21YLxQE+Ll/p1Ghcob4oBWSBUADwUwRS2Rh6/SHvc eMkFMdc4rY8+GS3kSfk63zYXZ3oztI1lDxd0YfMLe4a6p2z3utgwstSXth7KIenJvwpK gMuTnHNbwtqeHHTAJ90WnDmXlLbzk7+G+ijzQdodPLuc32OaTXIKRz/htYCL81sZGiae Sp0d3zVnwCINAkm86P5jWeFOXwSd3DF4tSyvUA587zAzovxvg+7SUA2LFhfUzZW0puLv JZ8Q== 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 x33-v6si35511826plb.49.2018.11.05.01.16.42; Mon, 05 Nov 2018 01:16:58 -0800 (PST) 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 S1727868AbeKESe7 (ORCPT + 99 others); Mon, 5 Nov 2018 13:34:59 -0500 Received: from Chamillionaire.breakpoint.cc ([146.0.238.67]:33464 "EHLO Chamillionaire.breakpoint.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726125AbeKESe7 (ORCPT ); Mon, 5 Nov 2018 13:34:59 -0500 Received: from bigeasy by Chamillionaire.breakpoint.cc with local (Exim 4.89) (envelope-from ) id 1gJazL-0003gS-Ly; Mon, 05 Nov 2018 10:16:03 +0100 Date: Mon, 5 Nov 2018 10:16:03 +0100 From: Sebastian Andrzej Siewior To: David Miller Cc: kurt@linutronix.de, anirudh@xilinx.com, John.Linn@xilinx.com, michal.simek@xilinx.com, radhey.shyam.pandey@xilinx.com, andrew@lunn.ch, yuehaibing@huawei.com, netdev@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] net: axienet: recheck condition after timeout in mdio_wait() Message-ID: <20181105091601.5dgwkcd4wcsw2cvp@breakpoint.cc> References: <20181030093139.10226-1-kurt@linutronix.de> <20181030093139.10226-2-kurt@linutronix.de> <20181030.112511.957438343014682098.davem@davemloft.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20181030.112511.957438343014682098.davem@davemloft.net> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-10-30 11:25:11 [-0700], David Miller wrote: > From: Kurt Kanzenbach > Date: Tue, 30 Oct 2018 10:31:38 +0100 > > > The function could report a false positive if it gets preempted between reading > > the XAE_MDIO_MCR_OFFSET register and checking for the timeout. In such a case, > > the condition has to be rechecked to avoid false positives. > > > > Therefore, check for expected condition even after the timeout occurred. > > > > Signed-off-by: Kurt Kanzenbach > ... > > if (time_before_eq(end, jiffies)) { > > - WARN_ON(1); > > - return -ETIMEDOUT; > > + val = axienet_ior(lp, XAE_MDIO_MCR_OFFSET); > > + break; > > } > > + > > udelay(1); > > } > > - return 0; > > + if (val & XAE_MDIO_MCR_READY_MASK) > > + return 0; > > + > > + WARN_ON(1); > > + return -ETIMEDOUT; > > You are not fundamentally changing the situation at all. > The condtion could change right after your last read of > XAR_MDIO_MCR_OFFSET, which is the same thing that happens before your > modifications to this code. > > It sounds more like the timeout is slightly too short, and that's the > real problem that causes whatever behavior you think you are fixing > here. There is a timeout of two jiffies. If the condition is not true within those two jiffies it will attempt to check condition one last time after the timeout occured. If the task got preempted after the reading from the register but before the timeout it is possible that the task gets back on the CPU after the timeout occured. And since the timeout occured it won't check if the condition changed: Time 0 +---+ | c | Check for condition (false) | c | | c | | c | | c | | P | Task gets preempted | | | O | Condition is true, task still preempted, no check | | 2 | T | The timeout is true | | | | | | | p | Task gets back on the CPU, no re-check of condition In the last step, there is no checking of the condition after the timeout occured and it wrongly assumes that the condition is not true. Increasing the timeout would help as long as the task gets not preempted past the new timeout. The same pattern (check condition after timeout) is also used in wait_event_timeout() or readx_poll_timeout(). Would you prefer to refactor this with readx_poll_timeout() instead? > I'm not applying this. Please reconsider. Sebastian