Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'ptp-belated-spring-cleaning-of-the-chardev-driver'

Thomas Gleixner says:

====================
ptp: Belated spring cleaning of the chardev driver

When looking into supporting auxiliary clocks in the PTP ioctl, the
inpenetrable ptp_ioctl() letter soup bothered me enough to clean it up.

The code (~400 lines!) is really hard to follow due to a gazillion of
local variables, which are only used in certain case scopes, and a
mixture of gotos, breaks and direct error return paths.

Clean it up by splitting out the IOCTL functionality into seperate
functions, which contain only the required local variables and are trivial
to follow. Complete the cleanup by converting the code to lock guards and
get rid of all gotos.

That reduces the code size by 48 lines and also the binary text size is
80 bytes smaller than the current maze.

The series is split up into one patch per IOCTL command group for easy
review.

v1: https://lore.kernel.org/20250620130144.351492917@linutronix.de
====================

Link: https://patch.msgid.link/20250625114404.102196103@linutronix.de
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+346 -398
+346 -398
drivers/ptp/ptp_chardev.c
··· 106 106 107 107 int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode) 108 108 { 109 - struct ptp_clock *ptp = 110 - container_of(pccontext->clk, struct ptp_clock, clock); 109 + struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock); 111 110 struct timestamp_event_queue *queue; 112 111 char debugfsname[32]; 113 - unsigned long flags; 114 112 115 113 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 116 114 if (!queue) ··· 120 122 } 121 123 bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS); 122 124 spin_lock_init(&queue->lock); 123 - spin_lock_irqsave(&ptp->tsevqs_lock, flags); 124 - list_add_tail(&queue->qlist, &ptp->tsevqs); 125 - spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); 125 + scoped_guard(spinlock_irq, &ptp->tsevqs_lock) 126 + list_add_tail(&queue->qlist, &ptp->tsevqs); 126 127 pccontext->private_clkdata = queue; 127 128 128 129 /* Debugfs contents */ ··· 140 143 int ptp_release(struct posix_clock_context *pccontext) 141 144 { 142 145 struct timestamp_event_queue *queue = pccontext->private_clkdata; 143 - unsigned long flags; 144 146 struct ptp_clock *ptp = 145 147 container_of(pccontext->clk, struct ptp_clock, clock); 146 148 147 149 debugfs_remove(queue->debugfs_instance); 148 150 pccontext->private_clkdata = NULL; 149 - spin_lock_irqsave(&ptp->tsevqs_lock, flags); 150 - list_del(&queue->qlist); 151 - spin_unlock_irqrestore(&ptp->tsevqs_lock, flags); 151 + scoped_guard(spinlock_irq, &ptp->tsevqs_lock) 152 + list_del(&queue->qlist); 152 153 bitmap_free(queue->mask); 153 154 kfree(queue); 155 + return 0; 156 + } 157 + 158 + static long ptp_clock_getcaps(struct ptp_clock *ptp, void __user *arg) 159 + { 160 + struct ptp_clock_caps caps = { 161 + .max_adj = ptp->info->max_adj, 162 + .n_alarm = ptp->info->n_alarm, 163 + .n_ext_ts = ptp->info->n_ext_ts, 164 + .n_per_out = ptp->info->n_per_out, 165 + .pps = ptp->info->pps, 166 + .n_pins = ptp->info->n_pins, 167 + .cross_timestamping = ptp->info->getcrosststamp != NULL, 168 + .adjust_phase = ptp->info->adjphase != NULL && 169 + ptp->info->getmaxphase != NULL, 170 + }; 171 + 172 + if (caps.adjust_phase) 173 + caps.max_phase_adj = ptp->info->getmaxphase(ptp->info); 174 + 175 + return copy_to_user(arg, &caps, sizeof(caps)) ? -EFAULT : 0; 176 + } 177 + 178 + static long ptp_extts_request(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 179 + { 180 + struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS }; 181 + struct ptp_clock_info *ops = ptp->info; 182 + unsigned int supported_extts_flags; 183 + 184 + if (copy_from_user(&req.extts, arg, sizeof(req.extts))) 185 + return -EFAULT; 186 + 187 + if (cmd == PTP_EXTTS_REQUEST2) { 188 + /* Tell the drivers to check the flags carefully. */ 189 + req.extts.flags |= PTP_STRICT_FLAGS; 190 + /* Make sure no reserved bit is set. */ 191 + if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) || 192 + req.extts.rsv[0] || req.extts.rsv[1]) 193 + return -EINVAL; 194 + 195 + /* Ensure one of the rising/falling edge bits is set. */ 196 + if ((req.extts.flags & PTP_ENABLE_FEATURE) && 197 + (req.extts.flags & PTP_EXTTS_EDGES) == 0) 198 + return -EINVAL; 199 + } else { 200 + req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS; 201 + memset(req.extts.rsv, 0, sizeof(req.extts.rsv)); 202 + } 203 + 204 + if (req.extts.index >= ops->n_ext_ts) 205 + return -EINVAL; 206 + 207 + supported_extts_flags = ptp->info->supported_extts_flags; 208 + /* The PTP_ENABLE_FEATURE flag is always supported. */ 209 + supported_extts_flags |= PTP_ENABLE_FEATURE; 210 + /* If the driver does not support strictly checking flags, the 211 + * PTP_RISING_EDGE and PTP_FALLING_EDGE flags are merely hints 212 + * which are not enforced. 213 + */ 214 + if (!(supported_extts_flags & PTP_STRICT_FLAGS)) 215 + supported_extts_flags |= PTP_EXTTS_EDGES; 216 + /* Reject unsupported flags */ 217 + if (req.extts.flags & ~supported_extts_flags) 218 + return -EOPNOTSUPP; 219 + 220 + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 221 + return ops->enable(ops, &req, req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0); 222 + } 223 + 224 + static long ptp_perout_request(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 225 + { 226 + struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT }; 227 + struct ptp_perout_request *perout = &req.perout; 228 + struct ptp_clock_info *ops = ptp->info; 229 + 230 + if (copy_from_user(perout, arg, sizeof(*perout))) 231 + return -EFAULT; 232 + 233 + if (cmd == PTP_PEROUT_REQUEST2) { 234 + if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) 235 + return -EINVAL; 236 + 237 + /* 238 + * The "on" field has undefined meaning if 239 + * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat it 240 + * as reserved, which must be set to zero. 241 + */ 242 + if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) && 243 + !mem_is_zero(perout->rsv, sizeof(perout->rsv))) 244 + return -EINVAL; 245 + 246 + if (perout->flags & PTP_PEROUT_DUTY_CYCLE) { 247 + /* The duty cycle must be subunitary. */ 248 + if (perout->on.sec > perout->period.sec || 249 + (perout->on.sec == perout->period.sec && 250 + perout->on.nsec > perout->period.nsec)) 251 + return -ERANGE; 252 + } 253 + 254 + if (perout->flags & PTP_PEROUT_PHASE) { 255 + /* 256 + * The phase should be specified modulo the period, 257 + * therefore anything equal or larger than 1 period 258 + * is invalid. 259 + */ 260 + if (perout->phase.sec > perout->period.sec || 261 + (perout->phase.sec == perout->period.sec && 262 + perout->phase.nsec >= perout->period.nsec)) 263 + return -ERANGE; 264 + } 265 + } else { 266 + perout->flags &= PTP_PEROUT_V1_VALID_FLAGS; 267 + memset(perout->rsv, 0, sizeof(perout->rsv)); 268 + } 269 + 270 + if (perout->index >= ops->n_per_out) 271 + return -EINVAL; 272 + if (perout->flags & ~ops->supported_perout_flags) 273 + return -EOPNOTSUPP; 274 + 275 + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 276 + return ops->enable(ops, &req, perout->period.sec || perout->period.nsec); 277 + } 278 + 279 + static long ptp_enable_pps(struct ptp_clock *ptp, bool enable) 280 + { 281 + struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS }; 282 + struct ptp_clock_info *ops = ptp->info; 283 + 284 + if (!capable(CAP_SYS_TIME)) 285 + return -EPERM; 286 + 287 + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 288 + return ops->enable(ops, &req, enable); 289 + } 290 + 291 + static long ptp_sys_offset_precise(struct ptp_clock *ptp, void __user *arg) 292 + { 293 + struct ptp_sys_offset_precise precise_offset; 294 + struct system_device_crosststamp xtstamp; 295 + struct timespec64 ts; 296 + int err; 297 + 298 + if (!ptp->info->getcrosststamp) 299 + return -EOPNOTSUPP; 300 + 301 + err = ptp->info->getcrosststamp(ptp->info, &xtstamp); 302 + if (err) 303 + return err; 304 + 305 + memset(&precise_offset, 0, sizeof(precise_offset)); 306 + ts = ktime_to_timespec64(xtstamp.device); 307 + precise_offset.device.sec = ts.tv_sec; 308 + precise_offset.device.nsec = ts.tv_nsec; 309 + ts = ktime_to_timespec64(xtstamp.sys_realtime); 310 + precise_offset.sys_realtime.sec = ts.tv_sec; 311 + precise_offset.sys_realtime.nsec = ts.tv_nsec; 312 + ts = ktime_to_timespec64(xtstamp.sys_monoraw); 313 + precise_offset.sys_monoraw.sec = ts.tv_sec; 314 + precise_offset.sys_monoraw.nsec = ts.tv_nsec; 315 + 316 + return copy_to_user(arg, &precise_offset, sizeof(precise_offset)) ? -EFAULT : 0; 317 + } 318 + 319 + static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg) 320 + { 321 + struct ptp_sys_offset_extended *extoff __free(kfree) = NULL; 322 + struct ptp_system_timestamp sts; 323 + 324 + if (!ptp->info->gettimex64) 325 + return -EOPNOTSUPP; 326 + 327 + extoff = memdup_user(arg, sizeof(*extoff)); 328 + if (IS_ERR(extoff)) 329 + return PTR_ERR(extoff); 330 + 331 + if (extoff->n_samples > PTP_MAX_SAMPLES || 332 + extoff->rsv[0] || extoff->rsv[1] || 333 + (extoff->clockid != CLOCK_REALTIME && 334 + extoff->clockid != CLOCK_MONOTONIC && 335 + extoff->clockid != CLOCK_MONOTONIC_RAW)) 336 + return -EINVAL; 337 + 338 + sts.clockid = extoff->clockid; 339 + for (unsigned int i = 0; i < extoff->n_samples; i++) { 340 + struct timespec64 ts; 341 + int err; 342 + 343 + err = ptp->info->gettimex64(ptp->info, &ts, &sts); 344 + if (err) 345 + return err; 346 + extoff->ts[i][0].sec = sts.pre_ts.tv_sec; 347 + extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec; 348 + extoff->ts[i][1].sec = ts.tv_sec; 349 + extoff->ts[i][1].nsec = ts.tv_nsec; 350 + extoff->ts[i][2].sec = sts.post_ts.tv_sec; 351 + extoff->ts[i][2].nsec = sts.post_ts.tv_nsec; 352 + } 353 + 354 + return copy_to_user(arg, extoff, sizeof(*extoff)) ? -EFAULT : 0; 355 + } 356 + 357 + static long ptp_sys_offset(struct ptp_clock *ptp, void __user *arg) 358 + { 359 + struct ptp_sys_offset *sysoff __free(kfree) = NULL; 360 + struct ptp_clock_time *pct; 361 + struct timespec64 ts; 362 + 363 + sysoff = memdup_user(arg, sizeof(*sysoff)); 364 + if (IS_ERR(sysoff)) 365 + return PTR_ERR(sysoff); 366 + 367 + if (sysoff->n_samples > PTP_MAX_SAMPLES) 368 + return -EINVAL; 369 + 370 + pct = &sysoff->ts[0]; 371 + for (unsigned int i = 0; i < sysoff->n_samples; i++) { 372 + struct ptp_clock_info *ops = ptp->info; 373 + int err; 374 + 375 + ktime_get_real_ts64(&ts); 376 + pct->sec = ts.tv_sec; 377 + pct->nsec = ts.tv_nsec; 378 + pct++; 379 + if (ops->gettimex64) 380 + err = ops->gettimex64(ops, &ts, NULL); 381 + else 382 + err = ops->gettime64(ops, &ts); 383 + if (err) 384 + return err; 385 + pct->sec = ts.tv_sec; 386 + pct->nsec = ts.tv_nsec; 387 + pct++; 388 + } 389 + ktime_get_real_ts64(&ts); 390 + pct->sec = ts.tv_sec; 391 + pct->nsec = ts.tv_nsec; 392 + 393 + return copy_to_user(arg, sysoff, sizeof(*sysoff)) ? -EFAULT : 0; 394 + } 395 + 396 + static long ptp_pin_getfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 397 + { 398 + struct ptp_clock_info *ops = ptp->info; 399 + struct ptp_pin_desc pd; 400 + 401 + if (copy_from_user(&pd, arg, sizeof(pd))) 402 + return -EFAULT; 403 + 404 + if (cmd == PTP_PIN_GETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv))) 405 + return -EINVAL; 406 + 407 + if (pd.index >= ops->n_pins) 408 + return -EINVAL; 409 + 410 + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 411 + pd = ops->pin_config[array_index_nospec(pd.index, ops->n_pins)]; 412 + 413 + return copy_to_user(arg, &pd, sizeof(pd)) ? -EFAULT : 0; 414 + } 415 + 416 + static long ptp_pin_setfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 417 + { 418 + struct ptp_clock_info *ops = ptp->info; 419 + struct ptp_pin_desc pd; 420 + unsigned int pin_index; 421 + 422 + if (copy_from_user(&pd, arg, sizeof(pd))) 423 + return -EFAULT; 424 + 425 + if (cmd == PTP_PIN_SETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv))) 426 + return -EINVAL; 427 + 428 + if (pd.index >= ops->n_pins) 429 + return -EINVAL; 430 + 431 + pin_index = array_index_nospec(pd.index, ops->n_pins); 432 + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 433 + return ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); 434 + } 435 + 436 + static long ptp_mask_clear_all(struct timestamp_event_queue *tsevq) 437 + { 438 + bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS); 439 + return 0; 440 + } 441 + 442 + static long ptp_mask_en_single(struct timestamp_event_queue *tsevq, void __user *arg) 443 + { 444 + unsigned int channel; 445 + 446 + if (copy_from_user(&channel, arg, sizeof(channel))) 447 + return -EFAULT; 448 + if (channel >= PTP_MAX_CHANNELS) 449 + return -EFAULT; 450 + set_bit(channel, tsevq->mask); 154 451 return 0; 155 452 } 156 453 157 454 long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, 158 455 unsigned long arg) 159 456 { 160 - struct ptp_clock *ptp = 161 - container_of(pccontext->clk, struct ptp_clock, clock); 162 - unsigned int i, pin_index, supported_extts_flags; 163 - struct ptp_sys_offset_extended *extoff = NULL; 164 - struct ptp_sys_offset_precise precise_offset; 165 - struct system_device_crosststamp xtstamp; 166 - struct ptp_clock_info *ops = ptp->info; 167 - struct ptp_sys_offset *sysoff = NULL; 168 - struct timestamp_event_queue *tsevq; 169 - struct ptp_system_timestamp sts; 170 - struct ptp_clock_request req; 171 - struct ptp_clock_caps caps; 172 - struct ptp_clock_time *pct; 173 - struct ptp_pin_desc pd; 174 - struct timespec64 ts; 175 - int enable, err = 0; 457 + struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock); 458 + void __user *argptr; 176 459 177 460 if (in_compat_syscall() && cmd != PTP_ENABLE_PPS && cmd != PTP_ENABLE_PPS2) 178 461 arg = (unsigned long)compat_ptr(arg); 179 - 180 - tsevq = pccontext->private_clkdata; 462 + argptr = (void __force __user *)arg; 181 463 182 464 switch (cmd) { 183 - 184 465 case PTP_CLOCK_GETCAPS: 185 466 case PTP_CLOCK_GETCAPS2: 186 - memset(&caps, 0, sizeof(caps)); 187 - 188 - caps.max_adj = ptp->info->max_adj; 189 - caps.n_alarm = ptp->info->n_alarm; 190 - caps.n_ext_ts = ptp->info->n_ext_ts; 191 - caps.n_per_out = ptp->info->n_per_out; 192 - caps.pps = ptp->info->pps; 193 - caps.n_pins = ptp->info->n_pins; 194 - caps.cross_timestamping = ptp->info->getcrosststamp != NULL; 195 - caps.adjust_phase = ptp->info->adjphase != NULL && 196 - ptp->info->getmaxphase != NULL; 197 - if (caps.adjust_phase) 198 - caps.max_phase_adj = ptp->info->getmaxphase(ptp->info); 199 - if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) 200 - err = -EFAULT; 201 - break; 467 + return ptp_clock_getcaps(ptp, argptr); 202 468 203 469 case PTP_EXTTS_REQUEST: 204 470 case PTP_EXTTS_REQUEST2: 205 - if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) { 206 - err = -EACCES; 207 - break; 208 - } 209 - memset(&req, 0, sizeof(req)); 210 - 211 - if (copy_from_user(&req.extts, (void __user *)arg, 212 - sizeof(req.extts))) { 213 - err = -EFAULT; 214 - break; 215 - } 216 - if (cmd == PTP_EXTTS_REQUEST2) { 217 - /* Tell the drivers to check the flags carefully. */ 218 - req.extts.flags |= PTP_STRICT_FLAGS; 219 - /* Make sure no reserved bit is set. */ 220 - if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) || 221 - req.extts.rsv[0] || req.extts.rsv[1]) { 222 - err = -EINVAL; 223 - break; 224 - } 225 - /* Ensure one of the rising/falling edge bits is set. */ 226 - if ((req.extts.flags & PTP_ENABLE_FEATURE) && 227 - (req.extts.flags & PTP_EXTTS_EDGES) == 0) { 228 - err = -EINVAL; 229 - break; 230 - } 231 - } else if (cmd == PTP_EXTTS_REQUEST) { 232 - req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS; 233 - req.extts.rsv[0] = 0; 234 - req.extts.rsv[1] = 0; 235 - } 236 - if (req.extts.index >= ops->n_ext_ts) { 237 - err = -EINVAL; 238 - break; 239 - } 240 - supported_extts_flags = ptp->info->supported_extts_flags; 241 - /* The PTP_ENABLE_FEATURE flag is always supported. */ 242 - supported_extts_flags |= PTP_ENABLE_FEATURE; 243 - /* If the driver does not support strictly checking flags, the 244 - * PTP_RISING_EDGE and PTP_FALLING_EDGE flags are merely 245 - * hints which are not enforced. 246 - */ 247 - if (!(supported_extts_flags & PTP_STRICT_FLAGS)) 248 - supported_extts_flags |= PTP_EXTTS_EDGES; 249 - /* Reject unsupported flags */ 250 - if (req.extts.flags & ~supported_extts_flags) 251 - return -EOPNOTSUPP; 252 - req.type = PTP_CLK_REQ_EXTTS; 253 - enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; 254 - if (mutex_lock_interruptible(&ptp->pincfg_mux)) 255 - return -ERESTARTSYS; 256 - err = ops->enable(ops, &req, enable); 257 - mutex_unlock(&ptp->pincfg_mux); 258 - break; 471 + if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 472 + return -EACCES; 473 + return ptp_extts_request(ptp, cmd, argptr); 259 474 260 475 case PTP_PEROUT_REQUEST: 261 476 case PTP_PEROUT_REQUEST2: 262 - if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) { 263 - err = -EACCES; 264 - break; 265 - } 266 - memset(&req, 0, sizeof(req)); 267 - 268 - if (copy_from_user(&req.perout, (void __user *)arg, 269 - sizeof(req.perout))) { 270 - err = -EFAULT; 271 - break; 272 - } 273 - if (cmd == PTP_PEROUT_REQUEST2) { 274 - struct ptp_perout_request *perout = &req.perout; 275 - 276 - if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) { 277 - err = -EINVAL; 278 - break; 279 - } 280 - /* 281 - * The "on" field has undefined meaning if 282 - * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat 283 - * it as reserved, which must be set to zero. 284 - */ 285 - if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) && 286 - (perout->rsv[0] || perout->rsv[1] || 287 - perout->rsv[2] || perout->rsv[3])) { 288 - err = -EINVAL; 289 - break; 290 - } 291 - if (perout->flags & PTP_PEROUT_DUTY_CYCLE) { 292 - /* The duty cycle must be subunitary. */ 293 - if (perout->on.sec > perout->period.sec || 294 - (perout->on.sec == perout->period.sec && 295 - perout->on.nsec > perout->period.nsec)) { 296 - err = -ERANGE; 297 - break; 298 - } 299 - } 300 - if (perout->flags & PTP_PEROUT_PHASE) { 301 - /* 302 - * The phase should be specified modulo the 303 - * period, therefore anything equal or larger 304 - * than 1 period is invalid. 305 - */ 306 - if (perout->phase.sec > perout->period.sec || 307 - (perout->phase.sec == perout->period.sec && 308 - perout->phase.nsec >= perout->period.nsec)) { 309 - err = -ERANGE; 310 - break; 311 - } 312 - } 313 - } else if (cmd == PTP_PEROUT_REQUEST) { 314 - req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS; 315 - req.perout.rsv[0] = 0; 316 - req.perout.rsv[1] = 0; 317 - req.perout.rsv[2] = 0; 318 - req.perout.rsv[3] = 0; 319 - } 320 - if (req.perout.index >= ops->n_per_out) { 321 - err = -EINVAL; 322 - break; 323 - } 324 - if (req.perout.flags & ~ptp->info->supported_perout_flags) 325 - return -EOPNOTSUPP; 326 - req.type = PTP_CLK_REQ_PEROUT; 327 - enable = req.perout.period.sec || req.perout.period.nsec; 328 - if (mutex_lock_interruptible(&ptp->pincfg_mux)) 329 - return -ERESTARTSYS; 330 - err = ops->enable(ops, &req, enable); 331 - mutex_unlock(&ptp->pincfg_mux); 332 - break; 477 + if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 478 + return -EACCES; 479 + return ptp_perout_request(ptp, cmd, argptr); 333 480 334 481 case PTP_ENABLE_PPS: 335 482 case PTP_ENABLE_PPS2: 336 - if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) { 337 - err = -EACCES; 338 - break; 339 - } 340 - memset(&req, 0, sizeof(req)); 341 - 342 - if (!capable(CAP_SYS_TIME)) 343 - return -EPERM; 344 - req.type = PTP_CLK_REQ_PPS; 345 - enable = arg ? 1 : 0; 346 - if (mutex_lock_interruptible(&ptp->pincfg_mux)) 347 - return -ERESTARTSYS; 348 - err = ops->enable(ops, &req, enable); 349 - mutex_unlock(&ptp->pincfg_mux); 350 - break; 483 + if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 484 + return -EACCES; 485 + return ptp_enable_pps(ptp, !!arg); 351 486 352 487 case PTP_SYS_OFFSET_PRECISE: 353 488 case PTP_SYS_OFFSET_PRECISE2: 354 - if (!ptp->info->getcrosststamp) { 355 - err = -EOPNOTSUPP; 356 - break; 357 - } 358 - err = ptp->info->getcrosststamp(ptp->info, &xtstamp); 359 - if (err) 360 - break; 361 - 362 - memset(&precise_offset, 0, sizeof(precise_offset)); 363 - ts = ktime_to_timespec64(xtstamp.device); 364 - precise_offset.device.sec = ts.tv_sec; 365 - precise_offset.device.nsec = ts.tv_nsec; 366 - ts = ktime_to_timespec64(xtstamp.sys_realtime); 367 - precise_offset.sys_realtime.sec = ts.tv_sec; 368 - precise_offset.sys_realtime.nsec = ts.tv_nsec; 369 - ts = ktime_to_timespec64(xtstamp.sys_monoraw); 370 - precise_offset.sys_monoraw.sec = ts.tv_sec; 371 - precise_offset.sys_monoraw.nsec = ts.tv_nsec; 372 - if (copy_to_user((void __user *)arg, &precise_offset, 373 - sizeof(precise_offset))) 374 - err = -EFAULT; 375 - break; 489 + return ptp_sys_offset_precise(ptp, argptr); 376 490 377 491 case PTP_SYS_OFFSET_EXTENDED: 378 492 case PTP_SYS_OFFSET_EXTENDED2: 379 - if (!ptp->info->gettimex64) { 380 - err = -EOPNOTSUPP; 381 - break; 382 - } 383 - extoff = memdup_user((void __user *)arg, sizeof(*extoff)); 384 - if (IS_ERR(extoff)) { 385 - err = PTR_ERR(extoff); 386 - extoff = NULL; 387 - break; 388 - } 389 - if (extoff->n_samples > PTP_MAX_SAMPLES || 390 - extoff->rsv[0] || extoff->rsv[1] || 391 - (extoff->clockid != CLOCK_REALTIME && 392 - extoff->clockid != CLOCK_MONOTONIC && 393 - extoff->clockid != CLOCK_MONOTONIC_RAW)) { 394 - err = -EINVAL; 395 - break; 396 - } 397 - sts.clockid = extoff->clockid; 398 - for (i = 0; i < extoff->n_samples; i++) { 399 - err = ptp->info->gettimex64(ptp->info, &ts, &sts); 400 - if (err) 401 - goto out; 402 - extoff->ts[i][0].sec = sts.pre_ts.tv_sec; 403 - extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec; 404 - extoff->ts[i][1].sec = ts.tv_sec; 405 - extoff->ts[i][1].nsec = ts.tv_nsec; 406 - extoff->ts[i][2].sec = sts.post_ts.tv_sec; 407 - extoff->ts[i][2].nsec = sts.post_ts.tv_nsec; 408 - } 409 - if (copy_to_user((void __user *)arg, extoff, sizeof(*extoff))) 410 - err = -EFAULT; 411 - break; 493 + return ptp_sys_offset_extended(ptp, argptr); 412 494 413 495 case PTP_SYS_OFFSET: 414 496 case PTP_SYS_OFFSET2: 415 - sysoff = memdup_user((void __user *)arg, sizeof(*sysoff)); 416 - if (IS_ERR(sysoff)) { 417 - err = PTR_ERR(sysoff); 418 - sysoff = NULL; 419 - break; 420 - } 421 - if (sysoff->n_samples > PTP_MAX_SAMPLES) { 422 - err = -EINVAL; 423 - break; 424 - } 425 - pct = &sysoff->ts[0]; 426 - for (i = 0; i < sysoff->n_samples; i++) { 427 - ktime_get_real_ts64(&ts); 428 - pct->sec = ts.tv_sec; 429 - pct->nsec = ts.tv_nsec; 430 - pct++; 431 - if (ops->gettimex64) 432 - err = ops->gettimex64(ops, &ts, NULL); 433 - else 434 - err = ops->gettime64(ops, &ts); 435 - if (err) 436 - goto out; 437 - pct->sec = ts.tv_sec; 438 - pct->nsec = ts.tv_nsec; 439 - pct++; 440 - } 441 - ktime_get_real_ts64(&ts); 442 - pct->sec = ts.tv_sec; 443 - pct->nsec = ts.tv_nsec; 444 - if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff))) 445 - err = -EFAULT; 446 - break; 497 + return ptp_sys_offset(ptp, argptr); 447 498 448 499 case PTP_PIN_GETFUNC: 449 500 case PTP_PIN_GETFUNC2: 450 - if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { 451 - err = -EFAULT; 452 - break; 453 - } 454 - if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] 455 - || pd.rsv[3] || pd.rsv[4]) 456 - && cmd == PTP_PIN_GETFUNC2) { 457 - err = -EINVAL; 458 - break; 459 - } else if (cmd == PTP_PIN_GETFUNC) { 460 - pd.rsv[0] = 0; 461 - pd.rsv[1] = 0; 462 - pd.rsv[2] = 0; 463 - pd.rsv[3] = 0; 464 - pd.rsv[4] = 0; 465 - } 466 - pin_index = pd.index; 467 - if (pin_index >= ops->n_pins) { 468 - err = -EINVAL; 469 - break; 470 - } 471 - pin_index = array_index_nospec(pin_index, ops->n_pins); 472 - if (mutex_lock_interruptible(&ptp->pincfg_mux)) 473 - return -ERESTARTSYS; 474 - pd = ops->pin_config[pin_index]; 475 - mutex_unlock(&ptp->pincfg_mux); 476 - if (!err && copy_to_user((void __user *)arg, &pd, sizeof(pd))) 477 - err = -EFAULT; 478 - break; 501 + return ptp_pin_getfunc(ptp, cmd, argptr); 479 502 480 503 case PTP_PIN_SETFUNC: 481 504 case PTP_PIN_SETFUNC2: 482 - if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) { 483 - err = -EACCES; 484 - break; 485 - } 486 - if (copy_from_user(&pd, (void __user *)arg, sizeof(pd))) { 487 - err = -EFAULT; 488 - break; 489 - } 490 - if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2] 491 - || pd.rsv[3] || pd.rsv[4]) 492 - && cmd == PTP_PIN_SETFUNC2) { 493 - err = -EINVAL; 494 - break; 495 - } else if (cmd == PTP_PIN_SETFUNC) { 496 - pd.rsv[0] = 0; 497 - pd.rsv[1] = 0; 498 - pd.rsv[2] = 0; 499 - pd.rsv[3] = 0; 500 - pd.rsv[4] = 0; 501 - } 502 - pin_index = pd.index; 503 - if (pin_index >= ops->n_pins) { 504 - err = -EINVAL; 505 - break; 506 - } 507 - pin_index = array_index_nospec(pin_index, ops->n_pins); 508 - if (mutex_lock_interruptible(&ptp->pincfg_mux)) 509 - return -ERESTARTSYS; 510 - err = ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); 511 - mutex_unlock(&ptp->pincfg_mux); 512 - break; 505 + if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 506 + return -EACCES; 507 + return ptp_pin_setfunc(ptp, cmd, argptr); 513 508 514 509 case PTP_MASK_CLEAR_ALL: 515 - bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS); 516 - break; 510 + return ptp_mask_clear_all(pccontext->private_clkdata); 517 511 518 512 case PTP_MASK_EN_SINGLE: 519 - if (copy_from_user(&i, (void __user *)arg, sizeof(i))) { 520 - err = -EFAULT; 521 - break; 522 - } 523 - if (i >= PTP_MAX_CHANNELS) { 524 - err = -EFAULT; 525 - break; 526 - } 527 - set_bit(i, tsevq->mask); 528 - break; 513 + return ptp_mask_en_single(pccontext->private_clkdata, argptr); 529 514 530 515 default: 531 - err = -ENOTTY; 532 - break; 516 + return -ENOTTY; 533 517 } 534 - 535 - out: 536 - kfree(extoff); 537 - kfree(sysoff); 538 - return err; 539 518 } 540 519 541 520 __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp, ··· 535 562 ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags, 536 563 char __user *buf, size_t cnt) 537 564 { 538 - struct ptp_clock *ptp = 539 - container_of(pccontext->clk, struct ptp_clock, clock); 565 + struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock); 540 566 struct timestamp_event_queue *queue; 541 567 struct ptp_extts_event *event; 542 - unsigned long flags; 543 - size_t qcnt, i; 544 - int result; 568 + ssize_t result; 545 569 546 570 queue = pccontext->private_clkdata; 547 - if (!queue) { 548 - result = -EINVAL; 549 - goto exit; 550 - } 571 + if (!queue) 572 + return -EINVAL; 551 573 552 - if (cnt % sizeof(struct ptp_extts_event) != 0) { 553 - result = -EINVAL; 554 - goto exit; 555 - } 574 + if (cnt % sizeof(*event) != 0) 575 + return -EINVAL; 556 576 557 577 if (cnt > EXTTS_BUFSIZE) 558 578 cnt = EXTTS_BUFSIZE; 559 579 560 - cnt = cnt / sizeof(struct ptp_extts_event); 561 - 562 - if (wait_event_interruptible(ptp->tsev_wq, 563 - ptp->defunct || queue_cnt(queue))) { 580 + if (wait_event_interruptible(ptp->tsev_wq, ptp->defunct || queue_cnt(queue))) 564 581 return -ERESTARTSYS; 565 - } 566 582 567 - if (ptp->defunct) { 568 - result = -ENODEV; 569 - goto exit; 570 - } 583 + if (ptp->defunct) 584 + return -ENODEV; 571 585 572 586 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); 573 - if (!event) { 574 - result = -ENOMEM; 575 - goto exit; 587 + if (!event) 588 + return -ENOMEM; 589 + 590 + scoped_guard(spinlock_irq, &queue->lock) { 591 + size_t qcnt = min((size_t)queue_cnt(queue), cnt / sizeof(*event)); 592 + 593 + for (size_t i = 0; i < qcnt; i++) { 594 + event[i] = queue->buf[queue->head]; 595 + /* Paired with READ_ONCE() in queue_cnt() */ 596 + WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); 597 + } 598 + cnt = qcnt * sizeof(*event); 576 599 } 577 - 578 - spin_lock_irqsave(&queue->lock, flags); 579 - 580 - qcnt = queue_cnt(queue); 581 - 582 - if (cnt > qcnt) 583 - cnt = qcnt; 584 - 585 - for (i = 0; i < cnt; i++) { 586 - event[i] = queue->buf[queue->head]; 587 - /* Paired with READ_ONCE() in queue_cnt() */ 588 - WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); 589 - } 590 - 591 - spin_unlock_irqrestore(&queue->lock, flags); 592 - 593 - cnt = cnt * sizeof(struct ptp_extts_event); 594 600 595 601 result = cnt; 596 - if (copy_to_user(buf, event, cnt)) { 602 + if (copy_to_user(buf, event, cnt)) 597 603 result = -EFAULT; 598 - goto free_event; 599 - } 600 604 601 - free_event: 602 605 kfree(event); 603 - exit: 604 606 return result; 605 607 }