Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp928355ybm; Wed, 27 May 2020 11:23:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmditAH1kZxdaDpODdmfJ5fdtprxUkan2CF+WK6KzNJHAERwXXcnOD6vGwOAI/GoZGekbK X-Received: by 2002:a50:bb29:: with SMTP id y38mr8857788ede.358.1590603794148; Wed, 27 May 2020 11:23:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590603794; cv=none; d=google.com; s=arc-20160816; b=wuxEMBVhdOMMMlFwpv9lBlB5QeyGJ5vXnh8hVU7eGjtekRrujANcoXONF7U17P+uig 9fzU1F2II7WRXJxMcwgP0bfLQATLsx/8lZ5HEv/LMoaSjNhdAOZsdLRcBGxWoLfK4CUY E32vCO0wK4I8h9uDjWta60pzz4z6/Ebe8uE2fYBOMYYQ1PrUrFtu2eC3FshAG+bmn1EA cf+Cv29yqPN1T5S5B80uYddPbnqkexP3ycBUzWEnc6s9KcPSfSOPpFdaOC7Ws1VDKunX OQPM3T//8Bjmm49xNZRfniz6cjKMYaHkViA8q9ukIRjGgDSg5WUIUMpNvVIIKQKOrimI vFNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=HBWLqXvQHSGfK57vylpmrnJznYSpUXM1WI30EFnbE+U=; b=qnYhnY5MaGlNJBA4NK2HHhJ2DU8UGLksBnqjSH9MVljIM1/c9KVsCs0QzW4Cv5IJpI QFYdE7JWG+QAvdBF9xInqpzRpLggnXhx2Mp8OITHryWZ51fSwKfGYSBMgnoX8Cy4hkJm 9FkfmZ91eEmDVHrH/ShzX/TQ4dBnJFxTSkLu/3BFk6kIPb1qeuL8EPIV+hxtIJCs6nYS FU91Veh7m1G6yQzc9KNrUVd7ka+XAChmFzhW1Wu88kw4uTkxqQKPFXdnRMjfoouWeSda yey96yiuxSm/d4Efok9QrsYBeiV/Zxlatw3h5/J2nKG5hq/ST0+vsiuwa1Sp4GAfdTRh a94Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c17si2202828ejn.336.2020.05.27.11.22.50; Wed, 27 May 2020 11:23:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388920AbgE0Nwi (ORCPT + 99 others); Wed, 27 May 2020 09:52:38 -0400 Received: from mout.kundenserver.de ([212.227.126.133]:46157 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388713AbgE0Nwi (ORCPT ); Wed, 27 May 2020 09:52:38 -0400 Received: from threadripper.lan ([149.172.98.151]) by mrelayeu.kundenserver.de (mreue009 [212.227.15.129]) with ESMTPA (Nemesis) id 1MMoXE-1jLBAj349U-00Ik9A; Wed, 27 May 2020 15:52:09 +0200 From: Arnd Bergmann To: Henrik Rydberg , Jean Delvare , Guenter Roeck Cc: Arnd Bergmann , Richard Fontana , Dmitry Torokhov , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Subject: [PATCH] hwmon: applesmc: avoid overlong udelay() Date: Wed, 27 May 2020 15:51:57 +0200 Message-Id: <20200527135207.1118624-1-arnd@arndb.de> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:2aeiuLmb1dLgi7CFDVrF6BpL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TbTwTJ79GWZ 4QcDUy7nZTynb+OYFvXnRovk4F0CQk/rH80IU19F7qcd4chv5s= Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Building this driver with "clang -O3" produces a link error after the compiler partially unrolls the loop and 256ms becomes a compile-time constant that triggers the check in udelay(): ld.lld: error: undefined symbol: __bad_udelay >>> referenced by applesmc.c >>> hwmon/applesmc.o:(read_smc) in archive drivers/built-in.a I can see no reason against using a sleeping function here, as no part of the driver runs in atomic context, so instead use usleep_range() with a wide range and use jiffies for the end condition. Signed-off-by: Arnd Bergmann --- drivers/hwmon/applesmc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c index ec93b8d673f5..316618409315 100644 --- a/drivers/hwmon/applesmc.c +++ b/drivers/hwmon/applesmc.c @@ -156,14 +156,19 @@ static struct workqueue_struct *applesmc_led_wq; */ static int wait_read(void) { + unsigned long end = jiffies + (APPLESMC_MAX_WAIT * HZ) / USEC_PER_SEC; u8 status; int us; + for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { - udelay(us); + usleep_range(us, us * 16); status = inb(APPLESMC_CMD_PORT); /* read: wait for smc to settle */ if (status & 0x01) return 0; + /* timeout: give up */ + if (time_after(jiffies, end)) + break; } pr_warn("wait_read() fail: 0x%02x\n", status); @@ -178,10 +183,11 @@ static int send_byte(u8 cmd, u16 port) { u8 status; int us; + unsigned long end = jiffies + (APPLESMC_MAX_WAIT * HZ) / USEC_PER_SEC; outb(cmd, port); for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { - udelay(us); + usleep_range(us, us * 16); status = inb(APPLESMC_CMD_PORT); /* write: wait for smc to settle */ if (status & 0x02) @@ -190,7 +196,7 @@ static int send_byte(u8 cmd, u16 port) if (status & 0x04) return 0; /* timeout: give up */ - if (us << 1 == APPLESMC_MAX_WAIT) + if (time_after(jiffies, end)) break; /* busy: long wait and resend */ udelay(APPLESMC_RETRY_WAIT); -- 2.26.2