From patchwork Sat Sep 14 10:06:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjie Ruan X-Patchwork-Id: 13804332 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EE791448C5; Sat, 14 Sep 2024 09:57:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.190 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726307840; cv=none; b=KesvaZOHSREZaRduYkQOoY7u6DDj93gcdKO4Wej1w7Vh77N7wmh1mG+NCspi1LxdDK6hz3Y7BBlOo9J/1radxE//1eKt+oKsUFS0NaUMPuXGqyY1GjxlOnvhLsqVOSFwdeZCo9tKqGiw9E0ehuNMQQiaUOE7IZCAmFYsGBTZkJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726307840; c=relaxed/simple; bh=bExolXvkJwwhhylCHDxGsUjYZWzbgOCGMqAZeN/sitk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ts5wO5IJ7O1Svqa6DDOsxCBhxvDsdPzkijeUue8WQX4WZUwjf60ry+NzENNx06vC+e+rGwmOI1vsW1xhCQhStoH2+HeeoAcOSMv8fqQ9nAT5Ar4nmHUvioIkJVKwKu6q5pb6jcmuD7e97eu7NKAnA7uA06PuWB7vu/TAFS2ml+M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.190 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4X5RQx5ljyz2CpTV; Sat, 14 Sep 2024 17:56:41 +0800 (CST) Received: from kwepemh500013.china.huawei.com (unknown [7.202.181.146]) by mail.maildlp.com (Postfix) with ESMTPS id EBCC914037D; Sat, 14 Sep 2024 17:57:14 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemh500013.china.huawei.com (7.202.181.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Sat, 14 Sep 2024 17:57:14 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , CC: Subject: [PATCH -next v4 1/2] posix-clock: Check timespec64 before call clock_settime() Date: Sat, 14 Sep 2024 18:06:24 +0800 Message-ID: <20240914100625.414013-2-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240914100625.414013-1-ruanjinjie@huawei.com> References: <20240914100625.414013-1-ruanjinjie@huawei.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemh500013.china.huawei.com (7.202.181.146) As Andrew pointed out, it will make sense that the PTP core checked timespec64 struct's tv_sec and tv_nsec range before calling ptp->info->settime64(). As the man mannul of clock_settime() said, if tp.tv_sec is negative or tp.tv_nsec is outside the range [0..999,999,999], it shuld return EINVAL, which include Dynamic clocks which handles PTP clock, and the condition is consistent with timespec64_valid(). So check it ahead using timespec64_valid() in pc_clock_settime() and return -EINVAL if not valid. There are some drivers that use tp->tv_sec and tp->tv_nsec directly to write registers without validity checks and assume that the higher layer has checked it, which is dangerous and will benefit from this, such as hclge_ptp_settime(), igb_ptp_settime_i210(), _rcar_gen4_ptp_settime(), and some drivers can remove the checks of itself. Suggested-by: Andrew Lunn Signed-off-by: Jinjie Ruan --- v4: - Check it in pc_clock_settime(). - Update the commit message. v3: - Adjust to check in more higher layer clock_settime(). - Remove the NULL check. - Update the commit message and subject. v2: - Adjust to check in ptp_clock_settime(). --- kernel/time/posix-clock.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/time/posix-clock.c b/kernel/time/posix-clock.c index 4782edcbe7b9..89e39f9bd7ae 100644 --- a/kernel/time/posix-clock.c +++ b/kernel/time/posix-clock.c @@ -319,6 +319,9 @@ static int pc_clock_settime(clockid_t id, const struct timespec64 *ts) goto out; } + if (!timespec64_valid(ts)) + return -EINVAL; + if (cd.clk->ops.clock_settime) err = cd.clk->ops.clock_settime(cd.clk, ts); else From patchwork Sat Sep 14 10:06:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjie Ruan X-Patchwork-Id: 13804331 X-Patchwork-Delegate: kuba@kernel.org Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3C961CDFD5; Sat, 14 Sep 2024 09:57:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726307840; cv=none; b=uIbTdzVIoqgNvF1tkjp42uAZ0KiELO6uKOlPnkaz5j8hjCtOfnZEES1t3Ca3kLxu63GZvaY0wbgo1wUx4bdxyEgS+bDnf5UyZ9jiA2qwtOWKz35/NXdwDJmz90GfUl75ktr5OHpLSAKTOVjrcqaLEGBX9fGI4pi1nZtR9bzTbQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726307840; c=relaxed/simple; bh=cpU6ipXWa9Zv3VptYWD2uUpDNTPuz9LPWpsU9qNRoSs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tShQbBxll48VJZPGMZJTFVjFuLJvLs0FW9HOKWU8jIgVQIbFFqwqMYE/3sQQ0GV/86q0pbZKYIP7GFSqw6wHGR5PFQ+Rqelx+o3Rd313dHiMTiYWE9WWDkWUtSgyWdi/U80uGKd82ZS2AQCdny9LyGikO6+8H7EdEpPojA55yR0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.194]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4X5RRQ0TdrzFqYt; Sat, 14 Sep 2024 17:57:06 +0800 (CST) Received: from kwepemh500013.china.huawei.com (unknown [7.202.181.146]) by mail.maildlp.com (Postfix) with ESMTPS id 79F691401F1; Sat, 14 Sep 2024 17:57:15 +0800 (CST) Received: from huawei.com (10.90.53.73) by kwepemh500013.china.huawei.com (7.202.181.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Sat, 14 Sep 2024 17:57:14 +0800 From: Jinjie Ruan To: , , , , , , , , , , , , , , CC: Subject: [PATCH -next v4 2/2] net: lan743x: Remove duplicate check Date: Sat, 14 Sep 2024 18:06:25 +0800 Message-ID: <20240914100625.414013-3-ruanjinjie@huawei.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240914100625.414013-1-ruanjinjie@huawei.com> References: <20240914100625.414013-1-ruanjinjie@huawei.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemh500013.china.huawei.com (7.202.181.146) X-Patchwork-Delegate: kuba@kernel.org Since timespec64_valid() has been checked in higher layer pc_clock_settime(), the duplicate check in lan743x_ptpci_settime64() can be removed. Signed-off-by: Jinjie Ruan --- v4: - Update the commit message. --- v2: - Check it in ptp core instead of using NSEC_PER_SEC macro. - Remove the NULL check. --- drivers/net/ethernet/microchip/lan743x_ptp.c | 35 ++++++++------------ 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/drivers/net/ethernet/microchip/lan743x_ptp.c b/drivers/net/ethernet/microchip/lan743x_ptp.c index dcea6652d56d..4a777b449ecd 100644 --- a/drivers/net/ethernet/microchip/lan743x_ptp.c +++ b/drivers/net/ethernet/microchip/lan743x_ptp.c @@ -401,28 +401,21 @@ static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci, u32 nano_seconds = 0; u32 seconds = 0; - if (ts) { - if (ts->tv_sec > 0xFFFFFFFFLL || - ts->tv_sec < 0) { - netif_warn(adapter, drv, adapter->netdev, - "ts->tv_sec out of range, %lld\n", - ts->tv_sec); - return -ERANGE; - } - if (ts->tv_nsec >= 1000000000L || - ts->tv_nsec < 0) { - netif_warn(adapter, drv, adapter->netdev, - "ts->tv_nsec out of range, %ld\n", - ts->tv_nsec); - return -ERANGE; - } - seconds = ts->tv_sec; - nano_seconds = ts->tv_nsec; - lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0); - } else { - netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n"); - return -EINVAL; + if (ts->tv_sec > 0xFFFFFFFFLL) { + netif_warn(adapter, drv, adapter->netdev, + "ts->tv_sec out of range, %lld\n", + ts->tv_sec); + return -ERANGE; + } + if (ts->tv_nsec < 0) { + netif_warn(adapter, drv, adapter->netdev, + "ts->tv_nsec out of range, %ld\n", + ts->tv_nsec); + return -ERANGE; } + seconds = ts->tv_sec; + nano_seconds = ts->tv_nsec; + lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0); return 0; }