From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 ADFD528B414; Fri, 20 Jun 2025 13:24:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425871; cv=none; b=vC5grL+7JpjG4/35ufm4QHfSmskZJN6GkQNjy71aDU3XHTQJsSyk+o04QWEay8T1PAj3y6auMozuq/IXFxBXku/ZUVffFjuZzJ+Fgxxo9nv9ddKhhml4sJUmaqTY2RLFGtddpnnWQ3ZtWKoAK3NXacRW5eJhpkstOqv0X2M1Y4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425871; c=relaxed/simple; bh=D0hNaC1los/+J8Ec10aBDfo3+sIyv8yVvL4pcYj2rwU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=r2x5Om6joO/AVrJvZQY4Y08VrpEHlYpZTUemIvKMZLibDq+ooaoTsHPEDgkj49GSKjmkCOK6Gxn7PfH0q4UQVg3tEvsLPt5fIiEXiOhnFXSFXyqI5dzelwiMtFLAtF2qOhtoOVVQ+UfsdWAX4lnvHdbRXK3VJt3g6LfX0acZYOw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=S5/LTCYH; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hQ4m2N5c; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="S5/LTCYH"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hQ4m2N5c" Message-ID: <20250620131943.779603403@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425868; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=+/B4z0/jfyKkYbKfvt/J+B+BcvknysPOJc978SNnbXk=; b=S5/LTCYHMUx9td4gltSAXIReafhuJAe1hSApdvlwHfCLGWOOrOTS2jeDtYxHYt2cNp7X6d oyWvZDs9Tn3N5GGRsovKsI0ugwoTQ/7WRGfWtI1C91fZPV9P821leQhVCCBQX93atOmPgE iFOXDHs9/ECRDOj/W0Qa/quJND/Uix6B9ajLuNPpBw3lXjOSsTEEWGDM43rGpyGe+MH4Y3 wnhf7+kt81F5EAXslNNpvOOGDJIDVcRpr+7J/ax9EV5ZU7y6fwXvoZRZJBUondg+dDUXpt P900l5GflrIBH4JXmKcAvCHWlarIiM0HymG9uZCIKeAoFufmGS102Q0dxYhC7w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425868; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=+/B4z0/jfyKkYbKfvt/J+B+BcvknysPOJc978SNnbXk=; b=hQ4m2N5cpNUswwzDNB3NFlUUqmd2vompIPmOdI+SkpRf6GQbKzAW4ILkA1M85njJkqBs4N GdxlBw/h0imjmgDA== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 01/13] ptp: Split out PTP_CLOCK_GETCAPS ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:27 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ptp_ioctl() is an inpenetrable letter soup with a gazillion of case (scope) specific variables defined at the top of the function and pointless breaks and gotos. Start cleaning it up by splitting out the PTP_CLOCK_GETCAPS ioctl code into a helper function. Use a argument pointer with a single sparse compliant type cast instead of proliferating the type cast all over the place. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -157,6 +157,26 @@ int ptp_release(struct posix_clock_conte return 0; } =20 +static long ptp_clock_getcaps(struct ptp_clock *ptp, void __user *arg) +{ + struct ptp_clock_caps caps =3D { + .max_adj =3D ptp->info->max_adj, + .n_alarm =3D ptp->info->n_alarm, + .n_ext_ts =3D ptp->info->n_ext_ts, + .n_per_out =3D ptp->info->n_per_out, + .pps =3D ptp->info->pps, + .n_pins =3D ptp->info->n_pins, + .cross_timestamping =3D ptp->info->getcrosststamp !=3D NULL, + .adjust_phase =3D ptp->info->adjphase !=3D NULL && + ptp->info->getmaxphase !=3D NULL, + }; + + if (caps.adjust_phase) + caps.max_phase_adj =3D ptp->info->getmaxphase(ptp->info); + + return copy_to_user(arg, &caps, sizeof(caps)) ? -EFAULT : 0; +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { @@ -171,37 +191,22 @@ long ptp_ioctl(struct posix_clock_contex struct timestamp_event_queue *tsevq; struct ptp_system_timestamp sts; struct ptp_clock_request req; - struct ptp_clock_caps caps; struct ptp_clock_time *pct; struct ptp_pin_desc pd; struct timespec64 ts; int enable, err =3D 0; + void __user *argptr; =20 if (in_compat_syscall() && cmd !=3D PTP_ENABLE_PPS && cmd !=3D PTP_ENABLE= _PPS2) arg =3D (unsigned long)compat_ptr(arg); + argptr =3D (void __force __user *)arg; =20 tsevq =3D pccontext->private_clkdata; =20 switch (cmd) { - case PTP_CLOCK_GETCAPS: case PTP_CLOCK_GETCAPS2: - memset(&caps, 0, sizeof(caps)); - - caps.max_adj =3D ptp->info->max_adj; - caps.n_alarm =3D ptp->info->n_alarm; - caps.n_ext_ts =3D ptp->info->n_ext_ts; - caps.n_per_out =3D ptp->info->n_per_out; - caps.pps =3D ptp->info->pps; - caps.n_pins =3D ptp->info->n_pins; - caps.cross_timestamping =3D ptp->info->getcrosststamp !=3D NULL; - caps.adjust_phase =3D ptp->info->adjphase !=3D NULL && - ptp->info->getmaxphase !=3D NULL; - if (caps.adjust_phase) - caps.max_phase_adj =3D ptp->info->getmaxphase(ptp->info); - if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) - err =3D -EFAULT; - break; + return ptp_clock_getcaps(ptp, argptr); =20 case PTP_EXTTS_REQUEST: case PTP_EXTTS_REQUEST2: From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 6B7F828E576; Fri, 20 Jun 2025 13:24:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425875; cv=none; b=RAmFdfBtWrfpna1gsIKUJICwEBfrEvxCIYiRf2Ajtj+VY42JNKFeT553MoEQF38Xvdi+k5VaP6q1jJUwS/iLDPHDVMatYnrtAAe7CwPt8nXBKK0tmlnrbJcDNsSbsotIal0lcqBt+D0XVuMxq8laTW8cuSa5+mBXrhLlc7VQXNU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425875; c=relaxed/simple; bh=4uSa6gwPUParCICWn+l34Lh046IYe02V7OwC7v6mlB0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=pyXjU7U3ekE/RFQsUOnCE/EgdS56Jw+jQfR1sSBzhruglPEfeYZ5fyLdz8QoL5NGUY6tKgnAB0TItcNAl8p0FCQgwhiuRNndYNFblAm7jp5ZsB76kCf358zxHVz2zRobRMcwSPRiouf+rckD77OaijaLo8GESzebx5G/ZMa81dY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Dc4m4j+/; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qfeo3p7P; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Dc4m4j+/"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qfeo3p7P" Message-ID: <20250620131943.842871495@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425870; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=TAfEr+4WrC24WCfcf0MBVVB3Is+yKhCwqq1vSI7Px4s=; b=Dc4m4j+/6yV/Umqw0A9//E+w1S69XgmBI2bUMGROhs4Lft0bAOx/avy+pNiRE7kuJtgm6b xyRW/PWXQnvPdIp92MiCO0yLBQGGJM2GdpYNmdra5YzTMrqDkBYJDX+wj/pWfuepkdLwXb 8xL8lzK4PzvZiX6sSvPlFENoi5bpO18G8UUKIzXqGqdKmOM6HZKb20WsX8UvHChIEBiUrm NSyHkFCbQXyEScYmcaphpesq9c3TzqY7C1NgNsJg2aPN9J1Fu2sLIUO9Tn7y9HyqPfbAN9 XnaHxsW6JYdfpbKHuXzWHeEAvXKqBBRQCZkPP4TWHpAYknt0lKccdp9n916zLg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425870; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=TAfEr+4WrC24WCfcf0MBVVB3Is+yKhCwqq1vSI7Px4s=; b=qfeo3p7PZCAeJTt3W4Q4JzUXu0dAuCiC6s6ZVuj9hEUhiiSTU8u9tcfCtUm09sv1X4fT0S JSGmqv5C/ZSePrBQ== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 02/13] ptp: Split out PTP_EXTTS_REQUEST ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:29 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_EXTTS_REQUEST ioctl code into a helper function. Convert to a lock guard while at it. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 105 +++++++++++++++++++++--------------------= ----- 1 file changed, 50 insertions(+), 55 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -177,12 +177,57 @@ static long ptp_clock_getcaps(struct ptp return copy_to_user(arg, &caps, sizeof(caps)) ? -EFAULT : 0; } =20 +static long ptp_extts_request(struct ptp_clock *ptp, unsigned int cmd, voi= d __user *arg) +{ + struct ptp_clock_request req =3D { .type =3D PTP_CLK_REQ_EXTTS }; + struct ptp_clock_info *ops =3D ptp->info; + unsigned int supported_extts_flags; + + if (copy_from_user(&req.extts, arg, sizeof(req.extts))) + return -EFAULT; + + if (cmd =3D=3D PTP_EXTTS_REQUEST2) { + /* Tell the drivers to check the flags carefully. */ + req.extts.flags |=3D PTP_STRICT_FLAGS; + /* Make sure no reserved bit is set. */ + if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) || + req.extts.rsv[0] || req.extts.rsv[1]) + return -EINVAL; + + /* Ensure one of the rising/falling edge bits is set. */ + if ((req.extts.flags & PTP_ENABLE_FEATURE) && + (req.extts.flags & PTP_EXTTS_EDGES) =3D=3D 0) + return -EINVAL; + } else { + req.extts.flags &=3D PTP_EXTTS_V1_VALID_FLAGS; + memset(req.extts.rsv, 0, sizeof(req.extts.rsv)); + } + + if (req.extts.index >=3D ops->n_ext_ts) + return -EINVAL; + + supported_extts_flags =3D ptp->info->supported_extts_flags; + /* The PTP_ENABLE_FEATURE flag is always supported. */ + supported_extts_flags |=3D PTP_ENABLE_FEATURE; + /* If the driver does not support strictly checking flags, the + * PTP_RISING_EDGE and PTP_FALLING_EDGE flags are merely hints + * which are not enforced. + */ + if (!(supported_extts_flags & PTP_STRICT_FLAGS)) + supported_extts_flags |=3D PTP_EXTTS_EDGES; + /* Reject unsupported flags */ + if (req.extts.flags & ~supported_extts_flags) + return -EOPNOTSUPP; + + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) + return ops->enable(ops, &req, req.extts.flags & PTP_ENABLE_FEATURE ? 1 := 0); +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, clock); - unsigned int i, pin_index, supported_extts_flags; struct ptp_sys_offset_extended *extoff =3D NULL; struct ptp_sys_offset_precise precise_offset; struct system_device_crosststamp xtstamp; @@ -192,6 +237,7 @@ long ptp_ioctl(struct posix_clock_contex struct ptp_system_timestamp sts; struct ptp_clock_request req; struct ptp_clock_time *pct; + unsigned int i, pin_index; struct ptp_pin_desc pd; struct timespec64 ts; int enable, err =3D 0; @@ -210,60 +256,9 @@ long ptp_ioctl(struct posix_clock_contex =20 case PTP_EXTTS_REQUEST: case PTP_EXTTS_REQUEST2: - if ((pccontext->fp->f_mode & FMODE_WRITE) =3D=3D 0) { - err =3D -EACCES; - break; - } - memset(&req, 0, sizeof(req)); - - if (copy_from_user(&req.extts, (void __user *)arg, - sizeof(req.extts))) { - err =3D -EFAULT; - break; - } - if (cmd =3D=3D PTP_EXTTS_REQUEST2) { - /* Tell the drivers to check the flags carefully. */ - req.extts.flags |=3D PTP_STRICT_FLAGS; - /* Make sure no reserved bit is set. */ - if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) || - req.extts.rsv[0] || req.extts.rsv[1]) { - err =3D -EINVAL; - break; - } - /* Ensure one of the rising/falling edge bits is set. */ - if ((req.extts.flags & PTP_ENABLE_FEATURE) && - (req.extts.flags & PTP_EXTTS_EDGES) =3D=3D 0) { - err =3D -EINVAL; - break; - } - } else if (cmd =3D=3D PTP_EXTTS_REQUEST) { - req.extts.flags &=3D PTP_EXTTS_V1_VALID_FLAGS; - req.extts.rsv[0] =3D 0; - req.extts.rsv[1] =3D 0; - } - if (req.extts.index >=3D ops->n_ext_ts) { - err =3D -EINVAL; - break; - } - supported_extts_flags =3D ptp->info->supported_extts_flags; - /* The PTP_ENABLE_FEATURE flag is always supported. */ - supported_extts_flags |=3D PTP_ENABLE_FEATURE; - /* If the driver does not support strictly checking flags, the - * PTP_RISING_EDGE and PTP_FALLING_EDGE flags are merely - * hints which are not enforced. - */ - if (!(supported_extts_flags & PTP_STRICT_FLAGS)) - supported_extts_flags |=3D PTP_EXTTS_EDGES; - /* Reject unsupported flags */ - if (req.extts.flags & ~supported_extts_flags) - return -EOPNOTSUPP; - req.type =3D PTP_CLK_REQ_EXTTS; - enable =3D req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; - if (mutex_lock_interruptible(&ptp->pincfg_mux)) - return -ERESTARTSYS; - err =3D ops->enable(ops, &req, enable); - mutex_unlock(&ptp->pincfg_mux); - break; + if ((pccontext->fp->f_mode & FMODE_WRITE) =3D=3D 0) + return -EACCES; + return ptp_extts_request(ptp, cmd, argptr); =20 case PTP_PEROUT_REQUEST: case PTP_PEROUT_REQUEST2: From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 A904328E5E6; Fri, 20 Jun 2025 13:24:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425875; cv=none; b=JIJXBL3b6U2jagcAkshDJMaHjDnMCJD+QZXC587oxm/dUvJDNrJtvEhMhgFncaEhmCsJaYwT0qEQHWPCV+Dmq4SfkGRGO4nWGBC07/ypXqJfnMYZsRexR6YQaKHzA4pdSR38W0sH8mHSyaMkcjkSeqjjOXsOzdIPJV373Qfdmmg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425875; c=relaxed/simple; bh=OJYci7OAeG3QER5ikqywHKfyNhsvjsZ52UiSoWmu/R0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Z27rmnelma7fg1gK9OzrRSMFm+47p7shWhbTTnB0z0YRVouujJyJ1Q5gyXJDHPbD/cGNJ+8c3whzVHusZsh+afEjzAS47Uj8L48e8EYSI86wrODRnuH3wooicyqJkLGq34+PONJ67A9N1+O4mRtuwAsxP6nImdsutBD44EnF2vU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Xz5H99Uw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AjXQvH6p; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Xz5H99Uw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AjXQvH6p" Message-ID: <20250620131943.905398183@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425871; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Kp6AT3TDorAUKTxQ1OJvaUJt91Mt2okSRa5ponWtJlY=; b=Xz5H99UwulA/KNUGm6P0KSmoV3Pd5YE1/k6DIU9ztNeg6xUnHxafWAVr3TIm+VihHR1Hqa gkB3nVRmCZiZNjlxypIWTlStLBCNcsyeNlqDOZgta3ISncvgQbqwLYcI47MlzTKR1d5rcz 1+ZHCgk+IE4Vz+WrqEpQUZDgHn+xOWFvcGBV4vIUjITawGCNsl5mcIDYe1WvoOux5HhAul LftcQhso6sVpF2XBy0B+FbfIEmLnw1PKddraH9wASKlUGGiImZ5ZYh4kv86E9GaYt8XjR7 tLutsfLPsjq5oGnQ7n7c3Ke22kwPVAmh9ph7qjYlEIwdAzA3mXr0u6xzRd2R9A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425871; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Kp6AT3TDorAUKTxQ1OJvaUJt91Mt2okSRa5ponWtJlY=; b=AjXQvH6pPCGau3qk9ZpCuoY16iFI56xNsGXiYvbHSpmxcSDxWSaoXBGvGEXxLenajf+E/e 8cUq6Bc12An/QOCg== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 03/13] ptp: Split out PTP_PEROUT_REQUEST ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:31 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_PEROUT_REQUEST ioctl code into a helper function. Convert to a lock guard while at it. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 129 ++++++++++++++++++++---------------------= ----- 1 file changed, 58 insertions(+), 71 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -223,6 +223,61 @@ static long ptp_extts_request(struct ptp return ops->enable(ops, &req, req.extts.flags & PTP_ENABLE_FEATURE ? 1 := 0); } =20 +static long ptp_perout_request(struct ptp_clock *ptp, unsigned int cmd, vo= id __user *arg) +{ + struct ptp_clock_request req =3D { .type =3D PTP_CLK_REQ_PEROUT }; + struct ptp_perout_request *perout =3D &req.perout; + struct ptp_clock_info *ops =3D ptp->info; + + if (copy_from_user(perout, arg, sizeof(*perout))) + return -EFAULT; + + if (cmd =3D=3D PTP_PEROUT_REQUEST2) { + if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) + return -EINVAL; + + /* + * The "on" field has undefined meaning if + * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat it + * as reserved, which must be set to zero. + */ + if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) && + !mem_is_zero(perout->rsv, sizeof(perout->rsv))) + return -EINVAL; + + if (perout->flags & PTP_PEROUT_DUTY_CYCLE) { + /* The duty cycle must be subunitary. */ + if (perout->on.sec > perout->period.sec || + (perout->on.sec =3D=3D perout->period.sec && + perout->on.nsec > perout->period.nsec)) + return -ERANGE; + } + + if (perout->flags & PTP_PEROUT_PHASE) { + /* + * The phase should be specified modulo the period, + * therefore anything equal or larger than 1 period + * is invalid. + */ + if (perout->phase.sec > perout->period.sec || + (perout->phase.sec =3D=3D perout->period.sec && + perout->phase.nsec >=3D perout->period.nsec)) + return -ERANGE; + } + } else { + perout->flags &=3D PTP_PEROUT_V1_VALID_FLAGS; + memset(perout->rsv, 0, sizeof(perout->rsv)); + } + + if (perout->index >=3D ops->n_per_out) + return -EINVAL; + if (perout->flags & ~ops->supported_perout_flags) + return -EOPNOTSUPP; + + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) + return ops->enable(ops, &req, perout->period.sec || perout->period.nsec); +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { @@ -262,77 +317,9 @@ long ptp_ioctl(struct posix_clock_contex =20 case PTP_PEROUT_REQUEST: case PTP_PEROUT_REQUEST2: - if ((pccontext->fp->f_mode & FMODE_WRITE) =3D=3D 0) { - err =3D -EACCES; - break; - } - memset(&req, 0, sizeof(req)); - - if (copy_from_user(&req.perout, (void __user *)arg, - sizeof(req.perout))) { - err =3D -EFAULT; - break; - } - if (cmd =3D=3D PTP_PEROUT_REQUEST2) { - struct ptp_perout_request *perout =3D &req.perout; - - if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) { - err =3D -EINVAL; - break; - } - /* - * The "on" field has undefined meaning if - * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat - * it as reserved, which must be set to zero. - */ - if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) && - (perout->rsv[0] || perout->rsv[1] || - perout->rsv[2] || perout->rsv[3])) { - err =3D -EINVAL; - break; - } - if (perout->flags & PTP_PEROUT_DUTY_CYCLE) { - /* The duty cycle must be subunitary. */ - if (perout->on.sec > perout->period.sec || - (perout->on.sec =3D=3D perout->period.sec && - perout->on.nsec > perout->period.nsec)) { - err =3D -ERANGE; - break; - } - } - if (perout->flags & PTP_PEROUT_PHASE) { - /* - * The phase should be specified modulo the - * period, therefore anything equal or larger - * than 1 period is invalid. - */ - if (perout->phase.sec > perout->period.sec || - (perout->phase.sec =3D=3D perout->period.sec && - perout->phase.nsec >=3D perout->period.nsec)) { - err =3D -ERANGE; - break; - } - } - } else if (cmd =3D=3D PTP_PEROUT_REQUEST) { - req.perout.flags &=3D PTP_PEROUT_V1_VALID_FLAGS; - req.perout.rsv[0] =3D 0; - req.perout.rsv[1] =3D 0; - req.perout.rsv[2] =3D 0; - req.perout.rsv[3] =3D 0; - } - if (req.perout.index >=3D ops->n_per_out) { - err =3D -EINVAL; - break; - } - if (req.perout.flags & ~ptp->info->supported_perout_flags) - return -EOPNOTSUPP; - req.type =3D PTP_CLK_REQ_PEROUT; - enable =3D req.perout.period.sec || req.perout.period.nsec; - if (mutex_lock_interruptible(&ptp->pincfg_mux)) - return -ERESTARTSYS; - err =3D ops->enable(ops, &req, enable); - mutex_unlock(&ptp->pincfg_mux); - break; + if ((pccontext->fp->f_mode & FMODE_WRITE) =3D=3D 0) + return -EACCES; + return ptp_perout_request(ptp, cmd, argptr); =20 case PTP_ENABLE_PPS: case PTP_ENABLE_PPS2: From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 7DD7128F52D; Fri, 20 Jun 2025 13:24:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425877; cv=none; b=UmO+1Fv1a1wTJyC6dIxJuhqy2qNyd4S7DzyPU/cSGEprGTaiuN8XEnAAHOW9MYEJrSuawd/F5qjVbxm71iBv43VJTkjCYHhYpl64n01Bwc6QBeYSOlAIahb3x7qgaxrwF4QSGrsVfLgFKJM7aWkAbYY22IYO2DWjqDjKaSJl1T0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425877; c=relaxed/simple; bh=1ffvVUtDslZFgqARFdSMUvwAUQoQaUnI+ZvOogB8ab0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Ksvg8gb+ToN73W7lCo3s5Z7JI3LCoOXu6HQyUhqdjABKUfrsPDVE7z4CNHe0x0y19h9r+kuYFl0VHS/ugXDBZKkTqfYvEq96k0PFNZEgeWgUQat9SvpILQxwgHTlRQySDqV171xJyFtss85N/K0E9oLPPYFtcwK/x+vXeUikB9M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hiB6hqbu; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=fMaBQy2l; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hiB6hqbu"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="fMaBQy2l" Message-ID: <20250620131943.967761848@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425873; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=aMKIuxOkpXMVYGZJ5jyLUcRi2op3oUU+5LAHlOiOAbI=; b=hiB6hqbuFB7BENlFQv7JtIVPsDQoynenuxk971GW4g4+1Hd39gpahTf2qMqOrX+NTTKXuA htFr4QezvzDuI9eKYSx5WywqUi5TJDjoe69puBrRmUWFK+wRboYNxqfafWwN3fahDAjQJM 9dSHDx9cT+8SJ8+Cu4la2abG2EBPIuPUUpJThJ74uFEcVEfjZYwp98qPGhlLqHwi5MsT/H 6kWjbTylxXKP8KsQpz0Pnj6omY6ikJy67ui9T6leLk4SK9Bz/75xbyvRETngMc1IZZLd0G PCk9Dnf9lsJ72FWWND7zk+jC1fW/8NhT981X32V/NXpskXarlIgXPwv43TFlvQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425873; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=aMKIuxOkpXMVYGZJ5jyLUcRi2op3oUU+5LAHlOiOAbI=; b=fMaBQy2lHnpSEOQeBFC6zc8CzJXhX1mwr3xTd33RqC0K1MrR0g5lLFrB3XRd5YTfPlFwSI 3eNKojRKrwyHXdCA== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 04/13] ptp: Split out PTP_ENABLE_PPS ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:32 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_ENABLE_PPS ioctl code into a helper function. Convert to a lock guard while at it. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -278,6 +278,18 @@ static long ptp_perout_request(struct pt return ops->enable(ops, &req, perout->period.sec || perout->period.nsec); } =20 +static long ptp_enable_pps(struct ptp_clock *ptp, bool enable) +{ + struct ptp_clock_request req =3D { .type =3D PTP_CLK_REQ_PPS }; + struct ptp_clock_info *ops =3D ptp->info; + + if (!capable(CAP_SYS_TIME)) + return -EPERM; + + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) + return ops->enable(ops, &req, enable); +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { @@ -290,13 +302,12 @@ long ptp_ioctl(struct posix_clock_contex struct ptp_sys_offset *sysoff =3D NULL; struct timestamp_event_queue *tsevq; struct ptp_system_timestamp sts; - struct ptp_clock_request req; struct ptp_clock_time *pct; unsigned int i, pin_index; struct ptp_pin_desc pd; struct timespec64 ts; - int enable, err =3D 0; void __user *argptr; + int err =3D 0; =20 if (in_compat_syscall() && cmd !=3D PTP_ENABLE_PPS && cmd !=3D PTP_ENABLE= _PPS2) arg =3D (unsigned long)compat_ptr(arg); @@ -323,21 +334,9 @@ long ptp_ioctl(struct posix_clock_contex =20 case PTP_ENABLE_PPS: case PTP_ENABLE_PPS2: - if ((pccontext->fp->f_mode & FMODE_WRITE) =3D=3D 0) { - err =3D -EACCES; - break; - } - memset(&req, 0, sizeof(req)); - - if (!capable(CAP_SYS_TIME)) - return -EPERM; - req.type =3D PTP_CLK_REQ_PPS; - enable =3D arg ? 1 : 0; - if (mutex_lock_interruptible(&ptp->pincfg_mux)) - return -ERESTARTSYS; - err =3D ops->enable(ops, &req, enable); - mutex_unlock(&ptp->pincfg_mux); - break; + if ((pccontext->fp->f_mode & FMODE_WRITE) =3D=3D 0) + return -EACCES; + return ptp_enable_pps(ptp, !!arg); =20 case PTP_SYS_OFFSET_PRECISE: case PTP_SYS_OFFSET_PRECISE2: From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 3083B2949F1; Fri, 20 Jun 2025 13:24:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425878; cv=none; b=OgKatmTiuuWPErPRDOHclLUeJwCBKxbyJxXNCSbAjPjU46cNHtGSpKdly7kIMXhNVd2mG9QZsd/h3yAHRsfkyh2mN+LVL9dud1XHgRhi3fJwS97F3PFyy0I62nCZsvF5Wz+ORIRJ+TiM9BZXyhkor2h3QCbkNW7FwWrxNZdjXmQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425878; c=relaxed/simple; bh=NC51TTMY8nxd0DctA58uSZUFQbFQHQ5MFBFjo4Eq+5M=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=XkX4nidQK6WelOeTqP0119ZY0kgxpUNL+gHg3bUio6KxPgm5k63azwVmTjpmjJavIe9o+xOJEi3PpjmVAIcZfhLpnF9Ptmf5E7RP5sT7NGkk3gf0ajU946KZp99hRr5nInOhkmMyQ1aqZXGcVw5anSsv3kM1yfADnOJ1pHG0DJo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hq78WkkP; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=WcxgTl0N; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hq78WkkP"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="WcxgTl0N" Message-ID: <20250620131944.029142425@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425875; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=QH7kIDL5N6aeykFRlvUbUloVqPiTRJHBYLpkOQpllVo=; b=hq78WkkPMIBpFsZ9kcNZmJv4aklR9uzP+BYDoNQfFOVm11ffwqdCc4uNz7QOb5hM0GzX1l MDwWhTidHnb2NTVnud0qrXJm1BSg/6mkRnyjwi3g/lRtiZRzsFWdpfSCfPbnDkJyW9iLNt zW21zmuJgJ7dQ+dFdy+4aK/vcfZQJuyWybrWxYIUEy02n7MHKBw/IwZ+N317iwYl5D6ThH RWH0tnilRec0b3cdFpc9cjaFIVZ5/eX67XNngVHyj0luUYac4Bwxv+INfbf79WcmXQO4bg IA3UUjoInsOk3iddmaZNQFqLtv7HainAxhFNY2hJN3gOz+jEBF5pwTQw421R2A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425875; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=QH7kIDL5N6aeykFRlvUbUloVqPiTRJHBYLpkOQpllVo=; b=WcxgTl0NOIkQYI4PAT9NrSvE/BZ+yWiNkkSunEE/sM1wn2W/+Lq1R9Z7htla0wgaPNE92k jVDO97mwfINM8rDg== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 05/13] ptp: Split out PTP_SYS_OFFSET_PRECISE ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:34 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_SYS_OFFSET_PRECISE ioctl code into a helper function. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 53 +++++++++++++++++++++++++----------------= ----- 1 file changed, 29 insertions(+), 24 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -291,14 +291,40 @@ static long ptp_enable_pps(struct ptp_cl return ops->enable(ops, &req, enable); } =20 +static long ptp_sys_offset_precise(struct ptp_clock *ptp, void __user *arg) +{ + struct ptp_sys_offset_precise precise_offset; + struct system_device_crosststamp xtstamp; + struct timespec64 ts; + int err; + + if (!ptp->info->getcrosststamp) + return -EOPNOTSUPP; + + err =3D ptp->info->getcrosststamp(ptp->info, &xtstamp); + if (err) + return err; + + memset(&precise_offset, 0, sizeof(precise_offset)); + ts =3D ktime_to_timespec64(xtstamp.device); + precise_offset.device.sec =3D ts.tv_sec; + precise_offset.device.nsec =3D ts.tv_nsec; + ts =3D ktime_to_timespec64(xtstamp.sys_realtime); + precise_offset.sys_realtime.sec =3D ts.tv_sec; + precise_offset.sys_realtime.nsec =3D ts.tv_nsec; + ts =3D ktime_to_timespec64(xtstamp.sys_monoraw); + precise_offset.sys_monoraw.sec =3D ts.tv_sec; + precise_offset.sys_monoraw.nsec =3D ts.tv_nsec; + + return copy_to_user(arg, &precise_offset, sizeof(precise_offset)) ? -EFAU= LT : 0; +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, clock); struct ptp_sys_offset_extended *extoff =3D NULL; - struct ptp_sys_offset_precise precise_offset; - struct system_device_crosststamp xtstamp; struct ptp_clock_info *ops =3D ptp->info; struct ptp_sys_offset *sysoff =3D NULL; struct timestamp_event_queue *tsevq; @@ -341,28 +367,7 @@ long ptp_ioctl(struct posix_clock_contex =20 case PTP_SYS_OFFSET_PRECISE: case PTP_SYS_OFFSET_PRECISE2: - if (!ptp->info->getcrosststamp) { - err =3D -EOPNOTSUPP; - break; - } - err =3D ptp->info->getcrosststamp(ptp->info, &xtstamp); - if (err) - break; - - memset(&precise_offset, 0, sizeof(precise_offset)); - ts =3D ktime_to_timespec64(xtstamp.device); - precise_offset.device.sec =3D ts.tv_sec; - precise_offset.device.nsec =3D ts.tv_nsec; - ts =3D ktime_to_timespec64(xtstamp.sys_realtime); - precise_offset.sys_realtime.sec =3D ts.tv_sec; - precise_offset.sys_realtime.nsec =3D ts.tv_nsec; - ts =3D ktime_to_timespec64(xtstamp.sys_monoraw); - precise_offset.sys_monoraw.sec =3D ts.tv_sec; - precise_offset.sys_monoraw.nsec =3D ts.tv_nsec; - if (copy_to_user((void __user *)arg, &precise_offset, - sizeof(precise_offset))) - err =3D -EFAULT; - break; + return ptp_sys_offset_precise(ptp, argptr); =20 case PTP_SYS_OFFSET_EXTENDED: case PTP_SYS_OFFSET_EXTENDED2: From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 A54F928C857; Fri, 20 Jun 2025 13:24:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425881; cv=none; b=mdW5o/SsTDS1vxcWz/6DbitDFNZ85RQGGW4mjnvhcZXSbUQkE0wJQfEu/FpBZ9apuKQiwWVnzKnEfHUoarWKEp3zfAZcJMS2kjXMjDTCOJp8aLk67CpmqewTJ7s72y7vZAU34E6Sb0mCSjmq6sqEmQKN9T7gh0ZTRsw/5RlC0r4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425881; c=relaxed/simple; bh=Do/szley7fS/q5nwVmGhJRdXAL6MgHPbPBiQzYJOmOM=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=GDjIfYZq6qoenp8/9YAjVmNHejcgHDnqsA4SvxeEHqDqcwzYL8n3J+sfJ6tmW+lyxUdn0ou3FAr4O1tHWr3cHygx71FFE+nPb+Xt9YOvfG4j9eNOlXiOJtppfINnvuHhzp4jfVHDxIj0jLlGet0CFNcxpRnPaam3+8Yc4+j/ris= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qs+Wxhtj; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Ml1BaYsV; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qs+Wxhtj"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Ml1BaYsV" Message-ID: <20250620131944.092766931@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425877; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Ps+B3mXPi6bAdX/VWvyVREKmEuPvI5a2EtOj9giLmZs=; b=qs+WxhtjTToGMC1yaudWfiA+CoHvAf5VEp/bYOboRuLAVmpVYNfozXOWj3pK3AtXvaMjlm u4hdUymgAxFbfPPiqnFRjJir+9nT3RZdCjoSG1r3tCAaL81EvIec/sodkzwBiqZMrCz07M E3Pa8Vpa+RLmcC07Yp7GS5EsisfN/WvMnuJ8cEdhP49nA/jlrUtaT/T/jB7j1m055uyAxy Y+53lyWrC3VDVWvQ6/puYCDfqaNzHoW3w3iKKBk032F514AqPPa9fAB2ObtAZNvLPKsMkO oNioq4SapZZJEZcBb78nTzmtg3RzxITl7/ExJuq+tqEIul57g/cxs5h6YMOnBA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425877; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Ps+B3mXPi6bAdX/VWvyVREKmEuPvI5a2EtOj9giLmZs=; b=Ml1BaYsV8lnN1iphMdag2XASenaQEN4FOpvJWn1C9YZalkpZ1vSoShYUgFnEdmWnjaGKhC 2JlpOdDv/EXgOPCg== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 06/13] ptp: Split out PTP_SYS_OFFSET_EXTENDED ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:36 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_SYS_OFFSET_EXTENDED ioctl code into a helper function. Convert it to __free() to avoid gotos. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 75 +++++++++++++++++++++++------------------= ----- 1 file changed, 39 insertions(+), 36 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -319,16 +319,52 @@ static long ptp_sys_offset_precise(struc return copy_to_user(arg, &precise_offset, sizeof(precise_offset)) ? -EFAU= LT : 0; } =20 +static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *ar= g) +{ + struct ptp_sys_offset_extended *extoff __free(kfree) =3D NULL; + struct ptp_system_timestamp sts; + + if (!ptp->info->gettimex64) + return -EOPNOTSUPP; + + extoff =3D memdup_user(arg, sizeof(*extoff)); + if (IS_ERR(extoff)) + return PTR_ERR(extoff); + + if (extoff->n_samples > PTP_MAX_SAMPLES || + extoff->rsv[0] || extoff->rsv[1] || + (extoff->clockid !=3D CLOCK_REALTIME && + extoff->clockid !=3D CLOCK_MONOTONIC && + extoff->clockid !=3D CLOCK_MONOTONIC_RAW)) + return -EINVAL; + + sts.clockid =3D extoff->clockid; + for (unsigned int i =3D 0; i < extoff->n_samples; i++) { + struct timespec64 ts; + int err; + + err =3D ptp->info->gettimex64(ptp->info, &ts, &sts); + if (err) + return err; + extoff->ts[i][0].sec =3D sts.pre_ts.tv_sec; + extoff->ts[i][0].nsec =3D sts.pre_ts.tv_nsec; + extoff->ts[i][1].sec =3D ts.tv_sec; + extoff->ts[i][1].nsec =3D ts.tv_nsec; + extoff->ts[i][2].sec =3D sts.post_ts.tv_sec; + extoff->ts[i][2].nsec =3D sts.post_ts.tv_nsec; + } + + return copy_to_user(arg, extoff, sizeof(*extoff)) ? -EFAULT : 0; +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, clock); - struct ptp_sys_offset_extended *extoff =3D NULL; struct ptp_clock_info *ops =3D ptp->info; struct ptp_sys_offset *sysoff =3D NULL; struct timestamp_event_queue *tsevq; - struct ptp_system_timestamp sts; struct ptp_clock_time *pct; unsigned int i, pin_index; struct ptp_pin_desc pd; @@ -371,39 +407,7 @@ long ptp_ioctl(struct posix_clock_contex =20 case PTP_SYS_OFFSET_EXTENDED: case PTP_SYS_OFFSET_EXTENDED2: - if (!ptp->info->gettimex64) { - err =3D -EOPNOTSUPP; - break; - } - extoff =3D memdup_user((void __user *)arg, sizeof(*extoff)); - if (IS_ERR(extoff)) { - err =3D PTR_ERR(extoff); - extoff =3D NULL; - break; - } - if (extoff->n_samples > PTP_MAX_SAMPLES || - extoff->rsv[0] || extoff->rsv[1] || - (extoff->clockid !=3D CLOCK_REALTIME && - extoff->clockid !=3D CLOCK_MONOTONIC && - extoff->clockid !=3D CLOCK_MONOTONIC_RAW)) { - err =3D -EINVAL; - break; - } - sts.clockid =3D extoff->clockid; - for (i =3D 0; i < extoff->n_samples; i++) { - err =3D ptp->info->gettimex64(ptp->info, &ts, &sts); - if (err) - goto out; - extoff->ts[i][0].sec =3D sts.pre_ts.tv_sec; - extoff->ts[i][0].nsec =3D sts.pre_ts.tv_nsec; - extoff->ts[i][1].sec =3D ts.tv_sec; - extoff->ts[i][1].nsec =3D ts.tv_nsec; - extoff->ts[i][2].sec =3D sts.post_ts.tv_sec; - extoff->ts[i][2].nsec =3D sts.post_ts.tv_nsec; - } - if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff))) - err =3D -EFAULT; - break; + return ptp_sys_offset_extended(ptp, argptr); =20 case PTP_SYS_OFFSET: case PTP_SYS_OFFSET2: @@ -528,7 +532,6 @@ long ptp_ioctl(struct posix_clock_contex } =20 out: - kfree(extoff); kfree(sysoff); return err; } From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 47B0A29AB12; Fri, 20 Jun 2025 13:24:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425883; cv=none; b=JrzbetWkLhOZinEXy0uAOtTm0G3JA8zpXs5ruVvTeJDnkT8wQz+3d2ecgOjbgEsJKmv/qta49xso/vntxuwD46dXlavtAOYtgXrebHA9CE+l0I4p99l/SI9zgxtv9oloTGxSIhV/Gr1ohhaY3rZPTDgy9lRlDZrtnYlpZisuGU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425883; c=relaxed/simple; bh=Me9eENgU3jcYNbw/PY4B+DVhSWzqX+uaPlTRAhxu/AA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=t1k+TyiY+qX8O/ec7icUGrV1NbM5dJ+9viCZklpTa4ikPEQUrOtrD4may0IJDzjXF3yETQWGqk2kW201UYAoRkdNo++J0C3Heo922aZ2cy4/sr9gLKDlFrPpXwy/WxK66bBpcvEmHc+SPl3YqeB5OGL6uL5eQ3ar9SdSMUW1MRM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Rglsplcu; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=zD5xR9+G; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Rglsplcu"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="zD5xR9+G" Message-ID: <20250620131944.156514985@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=cSbxmWR8L1/Rl4GA/fXKJwWZqfPiyvAnjSZcYEHQJHs=; b=RglsplcuUMdO1xUyWmErmw25JiH0C03syk2dtCuLmhPd4sBeo7a6QlnI073BV0DJ3ntjPD a+TN71ZyTHkA5qf8maH6DgPZfOPpH9medhIzmuAOVBN9EAFP7/vqiV8YdOcAWvvL5rCEty jUaqVhIu2IWH5n5oeMpSM8L2hzTmen61kQZEKuFDNLpvHamKtYnCMTLpAgBRtpVUZ1ZvaO SqDLgEJuwOZ6rW7tabQsrSR6SjCA1EWJBycRHa9IHpahe9bxizE3WQAgpTW/i3tD9RUQDW Nt0bV6ebC7D7vr1CEPZUsDxroc0XsrfLuKtVLVMJ1f+AXlHe651Ji00g17yb7Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=cSbxmWR8L1/Rl4GA/fXKJwWZqfPiyvAnjSZcYEHQJHs=; b=zD5xR9+Gadp8ju7K+9EwCpzRJSrNDtxXyoTwc88gFKgk/tqhs5yxjAG7rqRpjnXthsUCqK 11vu8KQp1V+lf9Aw== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 07/13] ptp: Split out PTP_SYS_OFFSET ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:38 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_SYS_OFFSET ioctl code into a helper function. Convert it to __free() to avoid gotos. No functional change intended. Signed-off-by: Thomas Gleixner --- drivers/ptp/ptp_chardev.c | 78 +++++++++++++++++++++++------------------= ----- 1 file changed, 40 insertions(+), 38 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -357,18 +357,54 @@ static long ptp_sys_offset_extended(stru return copy_to_user(arg, extoff, sizeof(*extoff)) ? -EFAULT : 0; } =20 +static long ptp_sys_offset(struct ptp_clock *ptp, void __user *arg) +{ + struct ptp_sys_offset *sysoff __free(kfree) =3D NULL; + struct ptp_clock_time *pct; + struct timespec64 ts; + + sysoff =3D memdup_user(arg, sizeof(*sysoff)); + if (IS_ERR(sysoff)) + return PTR_ERR(sysoff); + + if (sysoff->n_samples > PTP_MAX_SAMPLES) + return -EINVAL; + + pct =3D &sysoff->ts[0]; + for (unsigned int i =3D 0; i < sysoff->n_samples; i++) { + struct ptp_clock_info *ops =3D ptp->info; + int err; + + ktime_get_real_ts64(&ts); + pct->sec =3D ts.tv_sec; + pct->nsec =3D ts.tv_nsec; + pct++; + if (ops->gettimex64) + err =3D ops->gettimex64(ops, &ts, NULL); + else + err =3D ops->gettime64(ops, &ts); + if (err) + return err; + pct->sec =3D ts.tv_sec; + pct->nsec =3D ts.tv_nsec; + pct++; + } + ktime_get_real_ts64(&ts); + pct->sec =3D ts.tv_sec; + pct->nsec =3D ts.tv_nsec; + + return copy_to_user(arg, sysoff, sizeof(*sysoff)) ? -EFAULT : 0; +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, clock); struct ptp_clock_info *ops =3D ptp->info; - struct ptp_sys_offset *sysoff =3D NULL; struct timestamp_event_queue *tsevq; - struct ptp_clock_time *pct; unsigned int i, pin_index; struct ptp_pin_desc pd; - struct timespec64 ts; void __user *argptr; int err =3D 0; =20 @@ -411,38 +447,7 @@ long ptp_ioctl(struct posix_clock_contex =20 case PTP_SYS_OFFSET: case PTP_SYS_OFFSET2: - sysoff =3D memdup_user((void __user *)arg, sizeof(*sysoff)); - if (IS_ERR(sysoff)) { - err =3D PTR_ERR(sysoff); - sysoff =3D NULL; - break; - } - if (sysoff->n_samples > PTP_MAX_SAMPLES) { - err =3D -EINVAL; - break; - } - pct =3D &sysoff->ts[0]; - for (i =3D 0; i < sysoff->n_samples; i++) { - ktime_get_real_ts64(&ts); - pct->sec =3D ts.tv_sec; - pct->nsec =3D ts.tv_nsec; - pct++; - if (ops->gettimex64) - err =3D ops->gettimex64(ops, &ts, NULL); - else - err =3D ops->gettime64(ops, &ts); - if (err) - goto out; - pct->sec =3D ts.tv_sec; - pct->nsec =3D ts.tv_nsec; - pct++; - } - ktime_get_real_ts64(&ts); - pct->sec =3D ts.tv_sec; - pct->nsec =3D ts.tv_nsec; - if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff))) - err =3D -EFAULT; - break; + return ptp_sys_offset(ptp, argptr); =20 case PTP_PIN_GETFUNC: case PTP_PIN_GETFUNC2: @@ -530,9 +535,6 @@ long ptp_ioctl(struct posix_clock_contex err =3D -ENOTTY; break; } - -out: - kfree(sysoff); return err; } From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 4A6EF29ACD7; Fri, 20 Jun 2025 13:24:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425884; cv=none; b=dwFO+r3E5RMl/fcEPayHxQSEpHxf53bTriDLdzJGJXRg1NK5nwWwjK+/NcsJiJQtw5cHjrA6b14RBJaLJ3K+XVm4/WMqbDhpJ9n0FT47pBForM66GXJZhsSKHwH3TFVGdy/1bJ7QWUICMsuiSEvshIukUsi5mQEMzTG4sQ0NVLk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425884; c=relaxed/simple; bh=pwHkcg0BdO/LmL5gPnGWlPVjKgswFctmj4N0zMzEQ10=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=pir33ekE/63EorVdSdcZ3rmE15OUG3Z/fAe93knHqNr32xedDjZEn8/H+jYIMiUPOySE+NYqfLhcmXiVHcvBpF/6ez8BfXKp7845xyJNDxIIAzNpv/tbeSQlvyL4Wze4bEZ9nHd+6lBdFJLtDTGYE8mOvZfOOqMSknNkGgntFbQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hzac/RqN; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hAkhMb17; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hzac/RqN"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hAkhMb17" Message-ID: <20250620131944.218487429@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425881; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=S7Rzox5+/xP/z1rX7Tz/4GkdNBghtSktOAWf0UIesXo=; b=hzac/RqNUxREhaCMsFzNBneuK0Cnh6/BeWRNFEkO5rLpPpnfOmNVt4iFO5t/VTHIAiTG5m yARpsCijYjktvagmB/ZG3OD33ZEnlEg8G2MYoKBrzoHsWNp1MphEAH1MJAA0EDSHkOUzE0 IPIKz5IpKZ4VgNkq51ka5KH5ygGqQzYMWnjxzaf7ZXymlcJBeDy4+kKc3qby//U4xCOqJB rmQb52PjTE+gsDkrA/9PSV9JkKqWouifYTzZe09Xsjcboj9S9fnznzW+zX3IyqqKn2a8zf fEuB2tqH97hZr633LAY2rsLdESjl4Z59R0G9aAu2LPTmA2djoqsYF+XEf69Uzg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425881; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=S7Rzox5+/xP/z1rX7Tz/4GkdNBghtSktOAWf0UIesXo=; b=hAkhMb17uKgJanPkN/jorDyOauVAFGvczg5ZFGHRTJIXeD5xpPLHaRU7hNSzyuh8CT19sC 8ACqK4YyhB0T2MCw== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 08/13] ptp: Split out PTP_PIN_GETFUNC ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:40 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_PIN_GETFUNC ioctl code into a helper function. Convert to lock guard while at it. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 52 ++++++++++++++++++++---------------------= ----- 1 file changed, 23 insertions(+), 29 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -396,6 +396,28 @@ static long ptp_sys_offset(struct ptp_cl return copy_to_user(arg, sysoff, sizeof(*sysoff)) ? -EFAULT : 0; } =20 +static long ptp_pin_getfunc(struct ptp_clock *ptp, unsigned int cmd, void = __user *arg) +{ + struct ptp_clock_info *ops =3D ptp->info; + struct ptp_pin_desc pd; + + if (copy_from_user(&pd, arg, sizeof(pd))) + return -EFAULT; + + if (cmd =3D=3D PTP_PIN_GETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv))) + return -EINVAL; + else + memset(pd.rsv, 0, sizeof(pd.rsv)); + + if (pd.index >=3D ops->n_pins) + return -EINVAL; + + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) + pd =3D ops->pin_config[array_index_nospec(pd.index, ops->n_pins)]; + + return copy_to_user(arg, &pd, sizeof(pd)) ? -EFAULT : 0; +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { @@ -451,35 +473,7 @@ long ptp_ioctl(struct posix_clock_contex =20 case PTP_PIN_GETFUNC: case PTP_PIN_GETFUNC2: - if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { - err =3D -EFAULT; - break; - } - if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] - || pd.rsv[3] || pd.rsv[4]) - && cmd =3D=3D PTP_PIN_GETFUNC2) { - err =3D -EINVAL; - break; - } else if (cmd =3D=3D PTP_PIN_GETFUNC) { - pd.rsv[0] =3D 0; - pd.rsv[1] =3D 0; - pd.rsv[2] =3D 0; - pd.rsv[3] =3D 0; - pd.rsv[4] =3D 0; - } - pin_index =3D pd.index; - if (pin_index >=3D ops->n_pins) { - err =3D -EINVAL; - break; - } - pin_index =3D array_index_nospec(pin_index, ops->n_pins); - if (mutex_lock_interruptible(&ptp->pincfg_mux)) - return -ERESTARTSYS; - pd =3D ops->pin_config[pin_index]; - mutex_unlock(&ptp->pincfg_mux); - if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd))) - err =3D -EFAULT; - break; + return ptp_pin_getfunc(ptp, cmd, argptr); =20 case PTP_PIN_SETFUNC: case PTP_PIN_SETFUNC2: From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 30D3B29C325; Fri, 20 Jun 2025 13:24:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425886; cv=none; b=lR4izOAevLhW/WVTQDgmeA0GjjQgtj4cyOdbtcYqMREf7HVgScjfvi840bIvXTAlPrS0TrwkzfZWBeu75pUjiS5QEWYPeQmecz19lyCoe66DkMm5MmPLK+G8mh//zF6UM0fLbUHozp2Nb/Hivlm88e7fxP/A8aS/IoM/eGqnUXs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425886; c=relaxed/simple; bh=tD8KcHkGGJjZu7lBwsNaUG+Ryvqhvm7u9NBLinKQndk=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=g0PBL3BhidVHwPVNbJsxiqeFp95K0bGgNu/W1fWJ8OMZvJ6kb38E4EaOUL/J0KHtTQcx58IznNFgttJvw1Yxr3HkHllKLTf/sGmYduEUbcZ1/YMkxzQ1suWI+HBMoOfbrq//njx/wmEQcDd3IR0G0dKgZVhyHmxZzTWU7bR/110= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lMM44mpP; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=LVfFRNuz; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lMM44mpP"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="LVfFRNuz" Message-ID: <20250620131944.281928614@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=o6RvzwrSSybDkGE9Gbix1ouiqfsEbC1375kHUnRbTI0=; b=lMM44mpPQTUfT0sFUE+x/lxisq6Sf4irMi5eIOpIXsbXjjSdrdSxXbFWvW6hODVYKiPZrS eo8F8W0OqwTg/ZSQ4QGOQXqWYUNFrwn81oGq3tCCp/lV/CyOPEjr/v2npH40qL9wu5i5GN wt8Gq/A6cqy4KXYuwrVRKCnm9vcKYVR06BeLV7k4qvUWDVLDIyJ1vA4mEw8S91OU/82LiF z52dqH7KRmui8FR4NUGd4do7T2S/H4wI7nqB55/Is7KYsNIbmmrgcR3Uex8PtFflPCTfXJ 4jDihPwUFQAwcIyI2fV3kIJEf+n/fH2BNRGITASVcfYAxYEqkoZmWrUK6lC6JQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425883; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=o6RvzwrSSybDkGE9Gbix1ouiqfsEbC1375kHUnRbTI0=; b=LVfFRNuzCZHs1Ntomr/0OMkg+rm9urFSm+39tsZqXCQ0EanuFu09Q/92K9VELeg15E85ew 50wQ+6TbNaE/Y6Cg== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 09/13] ptp: Split out PTP_PIN_SETFUNC ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:42 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_PIN_SETFUNC ioctl code into a helper function. Convert to lock guard while at it. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 60 +++++++++++++++++++----------------------= ----- 1 file changed, 26 insertions(+), 34 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -420,16 +420,36 @@ static long ptp_pin_getfunc(struct ptp_c return copy_to_user(arg, &pd, sizeof(pd)) ? -EFAULT : 0; } =20 +static long ptp_pin_setfunc(struct ptp_clock *ptp, unsigned int cmd, void = __user *arg) +{ + struct ptp_clock_info *ops =3D ptp->info; + struct ptp_pin_desc pd; + unsigned int pin_index; + + if (copy_from_user(&pd, arg, sizeof(pd))) + return -EFAULT; + + if (cmd =3D=3D PTP_PIN_SETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv))) + return -EINVAL; + else + memset(pd.rsv, 0, sizeof(pd.rsv)); + + if (pd.index >=3D ops->n_pins) + return -EINVAL; + + pin_index =3D array_index_nospec(pd.index, ops->n_pins); + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) + return ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, clock); - struct ptp_clock_info *ops =3D ptp->info; struct timestamp_event_queue *tsevq; - unsigned int i, pin_index; - struct ptp_pin_desc pd; void __user *argptr; + unsigned int i; int err =3D 0; =20 if (in_compat_syscall() && cmd !=3D PTP_ENABLE_PPS && cmd !=3D PTP_ENABLE= _PPS2) @@ -479,37 +499,9 @@ long ptp_ioctl(struct posix_clock_contex =20 case PTP_PIN_SETFUNC: case PTP_PIN_SETFUNC2: - if ((pccontext->fp->f_mode & FMODE_WRITE) =3D=3D 0) { - err =3D -EACCES; - break; - } - if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { - err =3D -EFAULT; - break; - } - if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] - || pd.rsv[3] || pd.rsv[4]) - && cmd =3D=3D PTP_PIN_SETFUNC2) { - err =3D -EINVAL; - break; - } else if (cmd =3D=3D PTP_PIN_SETFUNC) { - pd.rsv[0] =3D 0; - pd.rsv[1] =3D 0; - pd.rsv[2] =3D 0; - pd.rsv[3] =3D 0; - pd.rsv[4] =3D 0; - } - pin_index =3D pd.index; - if (pin_index >=3D ops->n_pins) { - err =3D -EINVAL; - break; - } - pin_index =3D array_index_nospec(pin_index, ops->n_pins); - if (mutex_lock_interruptible(&ptp->pincfg_mux)) - return -ERESTARTSYS; - err =3D ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); - mutex_unlock(&ptp->pincfg_mux); - break; + if ((pccontext->fp->f_mode & FMODE_WRITE) =3D=3D 0) + return -EACCES; + return ptp_pin_setfunc(ptp, cmd, argptr); =20 case PTP_MASK_CLEAR_ALL: bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS); From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 B6D6D29B23F; Fri, 20 Jun 2025 13:24:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425888; cv=none; b=IkaXDCKn4xo+o0Kutwo3BdAx5Hd51I6B94B2QJFUxKsddpEaKPgt8FDM33UrjgEM10eQ/WE7mBchmxb+JfLXfEZwIR0yQqpA3k/azimoI+LQlmeCSKsMF9xOyGnF8tIMRMiy8RzpQIUPmfBGVIg9dYgif2VAoQfiPeOIrtkcA5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425888; c=relaxed/simple; bh=aIu4PRiBcrsPPWLGSpmb6Xt9PwyMaFXJ5LYX9Cfj854=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=HZkBq4CvdkxR84G6VQudQ1pIgk64wvnXC+n43hD4hhkFXeXga2/MJqA3XLoPkP7terYt2KrZN5HPR7pTq7kGpd/FN67oS5HZBQoWirsnUdE9m01Nl+oeshBWgSL9uigjrXQgw2sS8Fey/4fbFYlRZr3b59vlsmKePhBskpkpLqk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=oTpxXXts; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kYUPSCEi; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="oTpxXXts"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kYUPSCEi" Message-ID: <20250620131944.344887489@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Gykaki7sViJDV0IpK1pJ16zNE8qYHNuR3Gz60NSphMQ=; b=oTpxXXtsSzqFDrjnhYulGVpRE7SOniui3ZqBZPAFsypVn4a4XbE+uFF7yYtyXxERJy37V7 +J/BOiHy4ZmRPQM8x1eAzhAlhw947TcplXC2/Q0iDjA3isKFTLnzCmwkvfpLiHt2KC7q0w lsxp2/oo4QHNxQCWnj+dCz/TsPLee+40Uci0IhhYc4uP78nPgavDJ+zLukTIQQvlQXshmq YnXfOpTAqfiWG3HpMsF7SBjxNBhy31+SDWTvVJQTn7K0HhkkNxRkW3Rjneosq7SX4z85lR HXWALD1Wy8ikKIE3xNwXjsJ0rv8e9ncO1l8yu1MsS3uJOtGLQdXBAnR+xpL61Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425885; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Gykaki7sViJDV0IpK1pJ16zNE8qYHNuR3Gz60NSphMQ=; b=kYUPSCEilRfFP0LM2eU1I0kKcO8+h9ke+IaW1sgQD1naGCiTg0nVMgDzpNKykpnEgubyMa QUoEN2dly8okekDA== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 10/13] ptp: Split out PTP_MASK_CLEAR_ALL ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:44 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Continue the ptp_ioctl() cleanup by splitting out the PTP_MASK_CLEAR_ALL io= ctl code into a helper function. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -442,6 +442,12 @@ static long ptp_pin_setfunc(struct ptp_c return ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); } =20 +static long ptp_mask_clear_all(struct timestamp_event_queue *tsevq) +{ + bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS); + return 0; +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { @@ -504,8 +510,7 @@ long ptp_ioctl(struct posix_clock_contex return ptp_pin_setfunc(ptp, cmd, argptr); =20 case PTP_MASK_CLEAR_ALL: - bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS); - break; + return ptp_mask_clear_all(pccontext->private_clkdata); =20 case PTP_MASK_EN_SINGLE: if (copy_from_user(&i, (void __user *)arg, sizeof(i))) { From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 30BA72BD59A; Fri, 20 Jun 2025 13:24:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425890; cv=none; b=NbpzdPagKAdWryX99ghgnjYFhwGylK+CN6GHbe0EpUUoTdgXghYPVTTJRxAFJZSBwZYKG5Pw2cU1miw2aCuiLBaemwc1eMUDlYl6N5xRwYbrTx2z0RaQmoq5MihHaQ7vPe2tEXknHhY7QfSKV+eurX36nrljXhPZip8/24BH8uE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425890; c=relaxed/simple; bh=uNfHKM57G76FFv6JzyBP4WrvZku4fleSoyUjJtqw/7Y=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=TBFdB6b0nUfJX0+HfVCZtU7LoVdi4H0ZBUkHdyguk4Z2LY9GQJeOympb8FlQ55GMLnKXZjQdUkWlYOxgIwuLHRnPsoblfF4ltnXpTKEqto/Mw1L9CPYF49BtwtD011V1OdJrt3lQJIxryLR3RcBDeInW1Of+yxY2pjGU9ZXb5Ec= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=H6d1Suar; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=58DIbyOu; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="H6d1Suar"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="58DIbyOu" Message-ID: <20250620131944.408020331@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425887; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=CZbllYMTwyg8r+RWTW/1nbw7yqcKYAx79dp9Mm+YOqw=; b=H6d1Suar3CdWK0it2f8eomzC+wzAJQP58AN7bCHkEWsD+AhpHGoTlEwFPy/bKFkuMriubi MIybbOKI+OLq+HW5ghw0GwKAb2rP1qzkiA8Kt9AtblmPpNqQEu0Q9mVqSQavI8YpVOF1zO W6/w8yd5kvuccv5NWDV7ZhH1GONIJkCtQRsDWnWMhsDobXRVSomi2gbOpwfRhQ4+GJX9qU eOxvnwJMVDrUNUtn1LfWr9uWaCeF5wk9pvTTyOkc/ZGzPc4Q0orA8EBluwG+fKOR96JwWz mIfVdS7CtPk75Oloz6/cDPtpF2uuj+fikykPP+TlWa30HDqXQZRnDXTMSd28lg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425887; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=CZbllYMTwyg8r+RWTW/1nbw7yqcKYAx79dp9Mm+YOqw=; b=58DIbyOuT2JBhCBrHOiEUsRtMfbIrO/XS03RAC2/XMf5IG1RkpwjU5mSWyrVZB2aBQ7j1k K9AgFs3zI0WBICDg== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 11/13] ptp: Split out PTP_MASK_EN_SINGLE ioctl code References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:46 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Finish the ptp_ioctl() cleanup by splitting out the PTP_MASK_EN_SINGLE ioctl code and removing the remaining local variables and return statements. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_chardev.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -448,22 +448,28 @@ static long ptp_mask_clear_all(struct ti return 0; } =20 +static long ptp_mask_en_single(struct timestamp_event_queue *tsevq, void _= _user *arg) +{ + unsigned int channel; + + if (copy_from_user(&channel, arg, sizeof(channel))) + return -EFAULT; + if (channel >=3D PTP_MAX_CHANNELS) + return -EFAULT; + set_bit(channel, tsevq->mask); + return 0; +} + long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, unsigned long arg) { - struct ptp_clock *ptp =3D - container_of(pccontext->clk, struct ptp_clock, clock); - struct timestamp_event_queue *tsevq; + struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, = clock); void __user *argptr; - unsigned int i; - int err =3D 0; =20 if (in_compat_syscall() && cmd !=3D PTP_ENABLE_PPS && cmd !=3D PTP_ENABLE= _PPS2) arg =3D (unsigned long)compat_ptr(arg); argptr =3D (void __force __user *)arg; =20 - tsevq =3D pccontext->private_clkdata; - switch (cmd) { case PTP_CLOCK_GETCAPS: case PTP_CLOCK_GETCAPS2: @@ -513,22 +519,11 @@ long ptp_ioctl(struct posix_clock_contex return ptp_mask_clear_all(pccontext->private_clkdata); =20 case PTP_MASK_EN_SINGLE: - if (copy_from_user(&i, (void __user *)arg, sizeof(i))) { - err =3D -EFAULT; - break; - } - if (i >=3D PTP_MAX_CHANNELS) { - err =3D -EFAULT; - break; - } - set_bit(i, tsevq->mask); - break; + return ptp_mask_en_single(pccontext->private_clkdata, argptr); =20 default: - err =3D -ENOTTY; - break; + return -ENOTTY; } - return err; } =20 __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp, From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 283BD29B23F; Fri, 20 Jun 2025 13:24:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425892; cv=none; b=NFf4mrzxJb37JXonr2w3F+BAWagzxMc/KNEN57OZJx930vZbOeDN5axElkjYdHO2ujTwi7KBLTqXluWbZzeuPinyBujPOnGS4Il+YV/BaieC4zPUdjELYJ7QWGyik8A5DCmPpMEUgQZ8q+w/Ilkqiv3t9nwz9hEFQP24hfZNWnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425892; c=relaxed/simple; bh=+yB62XNLPUTlaPeJXS/0wIXtUzrxaYOCvpDNFe2810k=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=tm6JXilpgWS1Kzw/YSHJZBIpknoypnvXBePnMXr2nW/aVbAAeD/j46ufCGGGRyfB6eSCwhpZ6Q2Gvt1I0h2xL8uht3r6L9kiaiOiObcuo6Wa/sAZXoxWRHu8Hc2D6lq0kVjiXSOzpKrI7w3Ot+36BcsI2Khn3wFVTJi1ETppxb0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=DX/FYDh5; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hBLHJVHf; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="DX/FYDh5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hBLHJVHf" Message-ID: <20250620131944.471011376@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425889; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=/7Rhk90+L+EEwpYNNWS9ADvx0DoiKvCcJBgpAPppGcY=; b=DX/FYDh5fRiTRrXqCxacw595bJNcW3Z5+UQwAoBU/w7fUwbbIxl+TAWUKMtUU3Nidwg+IR vfGPrKJpD2P/Nu6KqxHc3YJhSQDq9uaJT4Zhueockjuk6ls0eVSz9iS9jxvm0G9DXT9rUB 4aI5SQtTxBR+s3O6tcqVU1X2JMHz03Ccye5eBimwoAkv2SvHjiZXZo6e+D8uBf1E69Qcqv bERWjvM1UUsVECuyfJfU7YrKaSdia8jcnvH9nHOYZ6I5GVnRbqtDkqHM9NFp0BGNzH/oLF 6/i/9AjFgCBOnLxGBS1andoiYUkzylVPhNVoP5ESngmqHTZYriTUkaQpjuMbxw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425889; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=/7Rhk90+L+EEwpYNNWS9ADvx0DoiKvCcJBgpAPppGcY=; b=hBLHJVHfeQvRFn1UYglwq7Z8h2aJv+itlg0l1BWi1slp9j94941ainVNi88OPqzQCLJNQC ZsX9YhaUIWbD4oDg== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 12/13] ptp: Convert chardev code to lock guards References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:48 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert the various spin_lock_irqsave() protected critical regions to scoped guards. Use spinlock_irq instead of spinlock_irqsave as all the functions are invoked in thread context with interrupts enabled. No functional change intended. Signed-off-by: Thomas Gleixner --- drivers/ptp/ptp_chardev.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -110,7 +110,6 @@ int ptp_open(struct posix_clock_context container_of(pccontext->clk, struct ptp_clock, clock); struct timestamp_event_queue *queue; char debugfsname[32]; - unsigned long flags; =20 queue =3D kzalloc(sizeof(*queue), GFP_KERNEL); if (!queue) @@ -122,9 +121,8 @@ int ptp_open(struct posix_clock_context } bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS); spin_lock_init(&queue->lock); - spin_lock_irqsave(&ptp->tsevqs_lock, flags); - list_add_tail(&queue->qlist, &ptp->tsevqs); - spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); + scoped_guard(spinlock_irq, &ptp->tsevqs_lock) + list_add_tail(&queue->qlist, &ptp->tsevqs); pccontext->private_clkdata =3D queue; =20 /* Debugfs contents */ @@ -143,15 +141,13 @@ int ptp_open(struct posix_clock_context int ptp_release(struct posix_clock_context *pccontext) { struct timestamp_event_queue *queue =3D pccontext->private_clkdata; - unsigned long flags; struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, clock); =20 debugfs_remove(queue->debugfs_instance); pccontext->private_clkdata =3D NULL; - spin_lock_irqsave(&ptp->tsevqs_lock, flags); - list_del(&queue->qlist); - spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); + scoped_guard(spinlock_irq, &ptp->tsevqs_lock) + list_del(&queue->qlist); bitmap_free(queue->mask); kfree(queue); return 0; @@ -548,8 +544,6 @@ ssize_t ptp_read(struct posix_clock_cont container_of(pccontext->clk, struct ptp_clock, clock); struct timestamp_event_queue *queue; struct ptp_extts_event *event; - unsigned long flags; - size_t qcnt, i; int result; =20 queue =3D pccontext->private_clkdata; @@ -584,21 +578,19 @@ ssize_t ptp_read(struct posix_clock_cont goto exit; } =20 - spin_lock_irqsave(&queue->lock, flags); + scoped_guard(spinlock_irq, &queue->lock) { + size_t qcnt =3D queue_cnt(queue); =20 - qcnt =3D queue_cnt(queue); + if (cnt > qcnt) + cnt =3D qcnt; =20 - if (cnt > qcnt) - cnt =3D qcnt; - - for (i =3D 0; i < cnt; i++) { - event[i] =3D queue->buf[queue->head]; - /* Paired with READ_ONCE() in queue_cnt() */ - WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); + for (size_t i =3D 0; i < cnt; i++) { + event[i] =3D queue->buf[queue->head]; + /* Paired with READ_ONCE() in queue_cnt() */ + WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); + } } =20 - spin_unlock_irqrestore(&queue->lock, flags); - cnt =3D cnt * sizeof(struct ptp_extts_event); =20 result =3D cnt; From nobody Thu Oct 9 03:14:48 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 B25492BDC26; Fri, 20 Jun 2025 13:24:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425894; cv=none; b=uAe58dymYwiNsEV9ruQOzeCzZ5JZfxPMsVwTNDStotwvl02OqHL6vzsuHAov+RKkjh4CgQ6xcvDTCWJGIodpbCXIJz6NilgifRUSRuv6unhERq7pnS2MOK8zHa7vqdN5c1/XeptF548Hc+D8Y2LOiEGWjly7uTEuciZreb7drYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750425894; c=relaxed/simple; bh=+yQNQVtDUDnZEDOwjEyTfF300+/7hA3mOBRzxQBCcdg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=jihvObGZvhPnZ9qNRETGn4uI23/EIhVHPhUjHuE/Yd2ciYdyIFVOgH2YKYRXnr6Q1AXgc1wB+RoisEwTRTuQuXj6gXWTkMQqamqZ5rBXNQw8CnhgahKL+6syhypvW2BuOKKZ2Te4x6R/wHqyUYS/KZNSv4wzdqFsHIXSjkgG7ms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=r3+6T9Em; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=N1+bRPVS; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="r3+6T9Em"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="N1+bRPVS" Message-ID: <20250620131944.533741574@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1750425891; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=t5AUZu+5N4DYRhjssasKvybcKzjmriU2anO1KoUYxkA=; b=r3+6T9EmTdlMc7zHSKMT4YJNIkDrBT/QGeXroAsFqe9mzsUuvfGsibYbtGKmC+P0B6NxmW GCucNVS+DkIVUBpmdzMDLgl+U0igiis5qIw/Qh6jaLMrle80+tXHrRArwLR0czMvpzgXu/ 2G4ZUk/+EJWsIRxdDtsfmxGwtJ/L5LPp1NQHaaZrTsCcRnV9npi0MhKOwNGyVtg+iiIdWM Qg9Eo/R69W2FYzDDF6ja9J3kSZT9RJkaZRt3KpkBVK17MoTZTTMDZ8+uW/WkiQDbaFmg8v oumC10BtXaSINmBMHr8+q1D6+FUd99UP9h/x8zD9Uh6/raFxSuhxKm3tSAqYsA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1750425891; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=t5AUZu+5N4DYRhjssasKvybcKzjmriU2anO1KoUYxkA=; b=N1+bRPVSrrVLcFrgOp8lStb1p3QF+9b7T+aBMDPYr8aDTI+2smDURGfm8L0qCIFdxIiwWv mqM09XOrzBo+X5Bg== From: Thomas Gleixner To: LKML Cc: Richard Cochran , netdev@vger.kernel.org Subject: [patch 13/13] ptp: Convert ptp_open/read() to __free() References: <20250620130144.351492917@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Fri, 20 Jun 2025 15:24:50 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Get rid of the kfree() and goto maze and just return error codes directly. No functional change intended. Signed-off-by: Thomas Gleixner --- drivers/ptp/ptp_chardev.c | 70 ++++++++++++-----------------------------= ----- 1 file changed, 19 insertions(+), 51 deletions(-) --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -106,19 +106,17 @@ int ptp_set_pinfunc(struct ptp_clock *pt =20 int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode) { - struct ptp_clock *ptp =3D - container_of(pccontext->clk, struct ptp_clock, clock); - struct timestamp_event_queue *queue; + struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, = clock); + struct timestamp_event_queue *queue __free(kfree) =3D NULL; char debugfsname[32]; =20 queue =3D kzalloc(sizeof(*queue), GFP_KERNEL); if (!queue) return -EINVAL; queue->mask =3D bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL); - if (!queue->mask) { - kfree(queue); + if (!queue->mask) return -EINVAL; - } + bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS); spin_lock_init(&queue->lock); scoped_guard(spinlock_irq, &ptp->tsevqs_lock) @@ -134,7 +132,6 @@ int ptp_open(struct posix_clock_context DIV_ROUND_UP(PTP_MAX_CHANNELS, BITS_PER_BYTE * sizeof(u32)); debugfs_create_u32_array("mask", 0444, queue->debugfs_instance, &queue->dfs_bitmap); - return 0; } =20 @@ -540,67 +537,38 @@ long ptp_ioctl(struct posix_clock_contex ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags, char __user *buf, size_t cnt) { - struct ptp_clock *ptp =3D - container_of(pccontext->clk, struct ptp_clock, clock); - struct timestamp_event_queue *queue; - struct ptp_extts_event *event; - int result; + struct ptp_clock *ptp =3D container_of(pccontext->clk, struct ptp_clock, = clock); + struct timestamp_event_queue *queue =3D pccontext->private_clkdata; + struct ptp_extts_event *event __free(kfree) =3D NULL; =20 - queue =3D pccontext->private_clkdata; - if (!queue) { - result =3D -EINVAL; - goto exit; - } + if (!queue) + return -EINVAL; =20 - if (cnt % sizeof(struct ptp_extts_event) !=3D 0) { - result =3D -EINVAL; - goto exit; - } + if (cnt % sizeof(struct ptp_extts_event) !=3D 0) + return -EINVAL; =20 if (cnt > EXTTS_BUFSIZE) cnt =3D EXTTS_BUFSIZE; =20 - cnt =3D cnt / sizeof(struct ptp_extts_event); - - if (wait_event_interruptible(ptp->tsev_wq, - ptp->defunct || queue_cnt(queue))) { + if (wait_event_interruptible(ptp->tsev_wq, ptp->defunct || queue_cnt(queu= e))) return -ERESTARTSYS; - } =20 - if (ptp->defunct) { - result =3D -ENODEV; - goto exit; - } + if (ptp->defunct) + return -ENODEV; =20 event =3D kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); - if (!event) { - result =3D -ENOMEM; - goto exit; - } + if (!event) + return -ENOMEM; =20 scoped_guard(spinlock_irq, &queue->lock) { - size_t qcnt =3D queue_cnt(queue); - - if (cnt > qcnt) - cnt =3D qcnt; + size_t qcnt =3D min((size_t)queue_cnt(queue), cnt / sizeof(*event)); =20 - for (size_t i =3D 0; i < cnt; i++) { + for (size_t i =3D 0; i < qcnt; i++) { event[i] =3D queue->buf[queue->head]; /* Paired with READ_ONCE() in queue_cnt() */ WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); } } =20 - cnt =3D cnt * sizeof(struct ptp_extts_event); - - result =3D cnt; - if (copy_to_user(buf, event, cnt)) { - result =3D -EFAULT; - goto free_event; - } - -free_event: - kfree(event); -exit: - return result; + return copy_to_user(buf, event, cnt) ? -EFAULT : cnt; }