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.

iio: buffer-dma: Use the cleanup.h API

Make use of the cleanup.h API for locks and memory allocation in order
to simplify some code paths.

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Nuno Sá and committed by
Jonathan Cameron
07d6dc17 09ccc1b6

+62 -93
+62 -93
drivers/iio/buffer/industrialio-buffer-dma.c
··· 136 136 struct iio_dma_buffer_block *block, *_block; 137 137 LIST_HEAD(block_list); 138 138 139 - spin_lock_irq(&iio_dma_buffer_dead_blocks_lock); 140 - list_splice_tail_init(&iio_dma_buffer_dead_blocks, &block_list); 141 - spin_unlock_irq(&iio_dma_buffer_dead_blocks_lock); 139 + scoped_guard(spinlock_irq, &iio_dma_buffer_dead_blocks_lock) 140 + list_splice_tail_init(&iio_dma_buffer_dead_blocks, &block_list); 142 141 143 142 list_for_each_entry_safe(block, _block, &block_list, head) 144 143 iio_buffer_block_release(&block->kref); ··· 147 148 static void iio_buffer_block_release_atomic(struct kref *kref) 148 149 { 149 150 struct iio_dma_buffer_block *block; 150 - unsigned long flags; 151 151 152 152 block = container_of(kref, struct iio_dma_buffer_block, kref); 153 153 154 - spin_lock_irqsave(&iio_dma_buffer_dead_blocks_lock, flags); 155 - list_add_tail(&block->head, &iio_dma_buffer_dead_blocks); 156 - spin_unlock_irqrestore(&iio_dma_buffer_dead_blocks_lock, flags); 154 + scoped_guard(spinlock_irqsave, &iio_dma_buffer_dead_blocks_lock) 155 + list_add_tail(&block->head, &iio_dma_buffer_dead_blocks); 157 156 158 157 schedule_work(&iio_dma_buffer_cleanup_work); 159 158 } ··· 172 175 static struct iio_dma_buffer_block *iio_dma_buffer_alloc_block( 173 176 struct iio_dma_buffer_queue *queue, size_t size, bool fileio) 174 177 { 175 - struct iio_dma_buffer_block *block; 176 - 177 - block = kzalloc(sizeof(*block), GFP_KERNEL); 178 + struct iio_dma_buffer_block *block __free(kfree) = 179 + kzalloc(sizeof(*block), GFP_KERNEL); 178 180 if (!block) 179 181 return NULL; 180 182 181 183 if (fileio) { 182 184 block->vaddr = dma_alloc_coherent(queue->dev, PAGE_ALIGN(size), 183 185 &block->phys_addr, GFP_KERNEL); 184 - if (!block->vaddr) { 185 - kfree(block); 186 + if (!block->vaddr) 186 187 return NULL; 187 - } 188 188 } 189 189 190 190 block->fileio = fileio; ··· 196 202 if (!fileio) 197 203 atomic_inc(&queue->num_dmabufs); 198 204 199 - return block; 205 + return_ptr(block); 200 206 } 201 207 202 208 static void _iio_dma_buffer_block_done(struct iio_dma_buffer_block *block) ··· 227 233 void iio_dma_buffer_block_done(struct iio_dma_buffer_block *block) 228 234 { 229 235 struct iio_dma_buffer_queue *queue = block->queue; 230 - unsigned long flags; 231 236 bool cookie; 232 237 233 238 cookie = dma_fence_begin_signalling(); 234 239 235 - spin_lock_irqsave(&queue->list_lock, flags); 236 - _iio_dma_buffer_block_done(block); 237 - spin_unlock_irqrestore(&queue->list_lock, flags); 240 + scoped_guard(spinlock_irqsave, &queue->list_lock) 241 + _iio_dma_buffer_block_done(block); 238 242 239 243 if (!block->fileio) 240 244 iio_buffer_signal_dmabuf_done(block->fence, 0); ··· 257 265 struct list_head *list) 258 266 { 259 267 struct iio_dma_buffer_block *block, *_block; 260 - unsigned long flags; 261 268 bool cookie; 262 269 263 270 cookie = dma_fence_begin_signalling(); 264 271 265 - spin_lock_irqsave(&queue->list_lock, flags); 266 - list_for_each_entry_safe(block, _block, list, head) { 267 - list_del(&block->head); 268 - block->bytes_used = 0; 269 - _iio_dma_buffer_block_done(block); 272 + scoped_guard(spinlock_irqsave, &queue->list_lock) { 273 + list_for_each_entry_safe(block, _block, list, head) { 274 + list_del(&block->head); 275 + block->bytes_used = 0; 276 + _iio_dma_buffer_block_done(block); 270 277 271 - if (!block->fileio) 272 - iio_buffer_signal_dmabuf_done(block->fence, -EINTR); 273 - iio_buffer_block_put_atomic(block); 278 + if (!block->fileio) 279 + iio_buffer_signal_dmabuf_done(block->fence, 280 + -EINTR); 281 + iio_buffer_block_put_atomic(block); 282 + } 274 283 } 275 - spin_unlock_irqrestore(&queue->list_lock, flags); 276 284 277 285 if (queue->fileio.enabled) 278 286 queue->fileio.enabled = false; ··· 321 329 struct iio_dma_buffer_block *block; 322 330 bool try_reuse = false; 323 331 size_t size; 324 - int ret = 0; 325 332 int i; 326 333 327 334 /* ··· 331 340 size = DIV_ROUND_UP(queue->buffer.bytes_per_datum * 332 341 queue->buffer.length, 2); 333 342 334 - mutex_lock(&queue->lock); 343 + guard(mutex)(&queue->lock); 335 344 336 345 queue->fileio.enabled = iio_dma_buffer_can_use_fileio(queue); 337 346 338 347 /* If DMABUFs were created, disable fileio interface */ 339 348 if (!queue->fileio.enabled) 340 - goto out_unlock; 349 + return 0; 341 350 342 351 /* Allocations are page aligned */ 343 352 if (PAGE_ALIGN(queue->fileio.block_size) == PAGE_ALIGN(size)) ··· 346 355 queue->fileio.block_size = size; 347 356 queue->fileio.active_block = NULL; 348 357 349 - spin_lock_irq(&queue->list_lock); 350 - for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { 351 - block = queue->fileio.blocks[i]; 358 + scoped_guard(spinlock_irq, &queue->list_lock) { 359 + for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { 360 + block = queue->fileio.blocks[i]; 352 361 353 - /* If we can't re-use it free it */ 354 - if (block && (!iio_dma_block_reusable(block) || !try_reuse)) 355 - block->state = IIO_BLOCK_STATE_DEAD; 362 + /* If we can't re-use it free it */ 363 + if (block && (!iio_dma_block_reusable(block) || !try_reuse)) 364 + block->state = IIO_BLOCK_STATE_DEAD; 365 + } 366 + 367 + /* 368 + * At this point all blocks are either owned by the core or 369 + * marked as dead. This means we can reset the lists without 370 + * having to fear corruption. 371 + */ 356 372 } 357 - 358 - /* 359 - * At this point all blocks are either owned by the core or marked as 360 - * dead. This means we can reset the lists without having to fear 361 - * corrution. 362 - */ 363 - spin_unlock_irq(&queue->list_lock); 364 373 365 374 INIT_LIST_HEAD(&queue->incoming); 366 375 ··· 380 389 381 390 if (!block) { 382 391 block = iio_dma_buffer_alloc_block(queue, size, true); 383 - if (!block) { 384 - ret = -ENOMEM; 385 - goto out_unlock; 386 - } 392 + if (!block) 393 + return -ENOMEM; 394 + 387 395 queue->fileio.blocks[i] = block; 388 396 } 389 397 ··· 406 416 } 407 417 } 408 418 409 - out_unlock: 410 - mutex_unlock(&queue->lock); 411 - 412 - return ret; 419 + return 0; 413 420 } 414 421 EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_request_update, "IIO_DMA_BUFFER"); 415 422 ··· 414 427 { 415 428 unsigned int i; 416 429 417 - spin_lock_irq(&queue->list_lock); 418 - for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { 419 - if (!queue->fileio.blocks[i]) 420 - continue; 421 - queue->fileio.blocks[i]->state = IIO_BLOCK_STATE_DEAD; 430 + scoped_guard(spinlock_irq, &queue->list_lock) { 431 + for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { 432 + if (!queue->fileio.blocks[i]) 433 + continue; 434 + queue->fileio.blocks[i]->state = IIO_BLOCK_STATE_DEAD; 435 + } 422 436 } 423 - spin_unlock_irq(&queue->list_lock); 424 437 425 438 INIT_LIST_HEAD(&queue->incoming); 426 439 ··· 484 497 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer); 485 498 struct iio_dma_buffer_block *block, *_block; 486 499 487 - mutex_lock(&queue->lock); 500 + guard(mutex)(&queue->lock); 488 501 queue->active = true; 489 502 list_for_each_entry_safe(block, _block, &queue->incoming, head) { 490 503 list_del(&block->head); 491 504 iio_dma_buffer_submit_block(queue, block); 492 505 } 493 - mutex_unlock(&queue->lock); 494 506 495 507 return 0; 496 508 } ··· 508 522 { 509 523 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer); 510 524 511 - mutex_lock(&queue->lock); 525 + guard(mutex)(&queue->lock); 512 526 queue->active = false; 513 527 514 528 if (queue->ops && queue->ops->abort) 515 529 queue->ops->abort(queue); 516 - mutex_unlock(&queue->lock); 517 530 518 531 return 0; 519 532 } ··· 537 552 struct iio_dma_buffer_block *block; 538 553 unsigned int idx; 539 554 540 - spin_lock_irq(&queue->list_lock); 555 + guard(spinlock_irq)(&queue->list_lock); 541 556 542 557 idx = queue->fileio.next_dequeue; 543 558 block = queue->fileio.blocks[idx]; 544 559 545 - if (block->state == IIO_BLOCK_STATE_DONE) { 546 - idx = (idx + 1) % ARRAY_SIZE(queue->fileio.blocks); 547 - queue->fileio.next_dequeue = idx; 548 - } else { 549 - block = NULL; 550 - } 560 + if (block->state != IIO_BLOCK_STATE_DONE) 561 + return NULL; 551 562 552 - spin_unlock_irq(&queue->list_lock); 563 + idx = (idx + 1) % ARRAY_SIZE(queue->fileio.blocks); 564 + queue->fileio.next_dequeue = idx; 553 565 554 566 return block; 555 567 } ··· 562 580 if (n < buffer->bytes_per_datum) 563 581 return -EINVAL; 564 582 565 - mutex_lock(&queue->lock); 583 + guard(mutex)(&queue->lock); 566 584 567 585 if (!queue->fileio.active_block) { 568 586 block = iio_dma_buffer_dequeue(queue); 569 - if (block == NULL) { 570 - ret = 0; 571 - goto out_unlock; 572 - } 587 + if (!block) 588 + return 0; 589 + 573 590 queue->fileio.pos = 0; 574 591 queue->fileio.active_block = block; 575 592 } else { ··· 584 603 ret = copy_from_user(addr, user_buffer, n); 585 604 else 586 605 ret = copy_to_user(user_buffer, addr, n); 587 - if (ret) { 588 - ret = -EFAULT; 589 - goto out_unlock; 590 - } 606 + if (ret) 607 + return -EFAULT; 591 608 592 609 queue->fileio.pos += n; 593 610 ··· 594 615 iio_dma_buffer_enqueue(queue, block); 595 616 } 596 617 597 - ret = n; 598 - 599 - out_unlock: 600 - mutex_unlock(&queue->lock); 601 - 602 - return ret; 618 + return n; 603 619 } 604 620 605 621 /** ··· 652 678 * but won't increase since all blocks are in use. 653 679 */ 654 680 655 - mutex_lock(&queue->lock); 681 + guard(mutex)(&queue->lock); 656 682 if (queue->fileio.active_block) 657 683 data_available += queue->fileio.active_block->size; 658 684 659 - spin_lock_irq(&queue->list_lock); 685 + guard(spinlock_irq)(&queue->list_lock); 660 686 661 687 for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { 662 688 block = queue->fileio.blocks[i]; ··· 665 691 && block->state == IIO_BLOCK_STATE_DONE) 666 692 data_available += block->size; 667 693 } 668 - 669 - spin_unlock_irq(&queue->list_lock); 670 - mutex_unlock(&queue->lock); 671 694 672 695 return data_available; 673 696 } ··· 853 882 */ 854 883 void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue) 855 884 { 856 - mutex_lock(&queue->lock); 885 + guard(mutex)(&queue->lock); 857 886 858 887 iio_dma_buffer_fileio_free(queue); 859 888 queue->ops = NULL; 860 - 861 - mutex_unlock(&queue->lock); 862 889 } 863 890 EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_exit, "IIO_DMA_BUFFER"); 864 891