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.

xarray: extract helper from __xa_{insert,cmpxchg}

Reduce code duplication by extracting a static inline function. This
function is identical to __xa_cmpxchg with the exception that it does not
coerce zero entries to null on the return path.

[tamird@gmail.com: fix __xa_erase()]
Link: https://lkml.kernel.org/r/CAJ-ks9kN_qddZ3Ne5d=cADu5POC1rHd4rQcbVSD_spnZOrLLZg@mail.gmail.com
Link: https://lkml.kernel.org/r/20241112-xarray-insert-cmpxchg-v1-2-dc2bdd8c4136@gmail.com
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Andreas Hindborg <a.hindborg@kernel.org>
Cc: Matthew Wilcox <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Tamir Duberstein and committed by
Andrew Morton
79ada2ae 74e2712b

+19 -20
+19 -20
lib/xarray.c
··· 1491 1491 { 1492 1492 if (xas_error(xas)) 1493 1493 curr = xas->xa_node; 1494 - return xa_zero_to_null(curr); 1494 + return curr; 1495 1495 } 1496 1496 1497 1497 /** ··· 1509 1509 void *__xa_erase(struct xarray *xa, unsigned long index) 1510 1510 { 1511 1511 XA_STATE(xas, xa, index); 1512 - return xas_result(&xas, xas_store(&xas, NULL)); 1512 + return xas_result(&xas, xa_zero_to_null(xas_store(&xas, NULL))); 1513 1513 } 1514 1514 EXPORT_SYMBOL(__xa_erase); 1515 1515 ··· 1568 1568 xas_clear_mark(&xas, XA_FREE_MARK); 1569 1569 } while (__xas_nomem(&xas, gfp)); 1570 1570 1571 - return xas_result(&xas, curr); 1571 + return xas_result(&xas, xa_zero_to_null(curr)); 1572 1572 } 1573 1573 EXPORT_SYMBOL(__xa_store); 1574 1574 ··· 1601 1601 } 1602 1602 EXPORT_SYMBOL(xa_store); 1603 1603 1604 + static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index, 1605 + void *old, void *entry, gfp_t gfp); 1606 + 1604 1607 /** 1605 1608 * __xa_cmpxchg() - Store this entry in the XArray. 1606 1609 * @xa: XArray. ··· 1623 1620 void *__xa_cmpxchg(struct xarray *xa, unsigned long index, 1624 1621 void *old, void *entry, gfp_t gfp) 1625 1622 { 1623 + return xa_zero_to_null(__xa_cmpxchg_raw(xa, index, old, entry, gfp)); 1624 + } 1625 + EXPORT_SYMBOL(__xa_cmpxchg); 1626 + 1627 + static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index, 1628 + void *old, void *entry, gfp_t gfp) 1629 + { 1626 1630 XA_STATE(xas, xa, index); 1627 1631 void *curr; 1628 1632 ··· 1647 1637 1648 1638 return xas_result(&xas, curr); 1649 1639 } 1650 - EXPORT_SYMBOL(__xa_cmpxchg); 1651 1640 1652 1641 /** 1653 1642 * __xa_insert() - Store this entry in the XArray if no entry is present. ··· 1666 1657 */ 1667 1658 int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp) 1668 1659 { 1669 - XA_STATE(xas, xa, index); 1670 1660 void *curr; 1661 + int errno; 1671 1662 1672 - if (WARN_ON_ONCE(xa_is_advanced(entry))) 1673 - return -EINVAL; 1674 1663 if (!entry) 1675 1664 entry = XA_ZERO_ENTRY; 1676 - 1677 - do { 1678 - curr = xas_load(&xas); 1679 - if (!curr) { 1680 - xas_store(&xas, entry); 1681 - if (xa_track_free(xa)) 1682 - xas_clear_mark(&xas, XA_FREE_MARK); 1683 - } else { 1684 - xas_set_err(&xas, -EBUSY); 1685 - } 1686 - } while (__xas_nomem(&xas, gfp)); 1687 - 1688 - return xas_error(&xas); 1665 + curr = __xa_cmpxchg_raw(xa, index, NULL, entry, gfp); 1666 + errno = xa_err(curr); 1667 + if (errno) 1668 + return errno; 1669 + return (curr != NULL) ? -EBUSY : 0; 1689 1670 } 1690 1671 EXPORT_SYMBOL(__xa_insert); 1691 1672