Re: git: e6bc24b0385a - main - kref: switch internal type to atomic_t and bring back const to kref_read

From: Mateusz Guzik <mjguzik_at_gmail.com>
Date: Wed, 14 Dec 2022 08:53:04 UTC
On 12/14/22, Gleb Smirnoff <glebius@freebsd.org> wrote:
>   One u_int32_t sneaked in between multiple uint32_t in this change.
>

Does not matter, it all is going to be replaced with refcount_t
soon(tm) -- there are numerous other violations all over the codebase
before I can do it though.

> On Tue, Dec 13, 2022 at 08:47:09PM +0000, Mateusz Guzik wrote:
> M> The branch main has been updated by mjg:
> M>
> M> URL:
> https://cgit.FreeBSD.org/src/commit/?id=e6bc24b0385a166526d20b2eb0cbb6a116350075
> M>
> M> commit e6bc24b0385a166526d20b2eb0cbb6a116350075
> M> Author:     Mateusz Guzik <mjg@FreeBSD.org>
> M> AuthorDate: 2022-12-13 20:42:32 +0000
> M> Commit:     Mateusz Guzik <mjg@FreeBSD.org>
> M> CommitDate: 2022-12-13 20:46:58 +0000
> M>
> M>     kref: switch internal type to atomic_t and bring back const to
> kref_read
> M>
> M>     This unbreak drm-kmod build.
> M>
> M>     the const is part of Linux API
> M>
> M>     Unfortunately drm-kmod uses hand-rolled refcount* calls on a kref
> M>     object. For now go the easy route of keeping it operational by
> casting
> M>     stuff internally.
> M>
> M>     The general goal here is to make FreeBSD refcount API use an opaque
> M>     type, hence the ongoing removal of hand-rolled accesses.
> M>
> M>     Reported by:    emaste
> M> ---
> M>  sys/compat/linuxkpi/common/include/linux/kref.h | 22
> +++++++++++-----------
> M>  1 file changed, 11 insertions(+), 11 deletions(-)
> M>
> M> diff --git a/sys/compat/linuxkpi/common/include/linux/kref.h
> b/sys/compat/linuxkpi/common/include/linux/kref.h
> M> index 9a6814175223..5a1fd834a58d 100644
> M> --- a/sys/compat/linuxkpi/common/include/linux/kref.h
> M> +++ b/sys/compat/linuxkpi/common/include/linux/kref.h
> M> @@ -44,35 +44,35 @@
> M>
> M>  struct kref {
> M>  	/* XXX In Linux this is a refcount_t */
> M> -	volatile u_int32_t refcount;
> M> +	atomic_t refcount;
> M>  };
> M>
> M>  static inline void
> M>  kref_init(struct kref *kref)
> M>  {
> M>
> M> -	refcount_init(&kref->refcount, 1);
> M> +	refcount_init((uint32_t *)&kref->refcount, 1);
> M>  }
> M>
> M>  static inline unsigned int
> M> -kref_read(struct kref *kref)
> M> +kref_read(const struct kref *kref)
> M>  {
> M>
> M> -	return (refcount_load(&kref->refcount));
> M> +	return (refcount_load(__DECONST(u_int32_t *, &kref->refcount)));
> M>  }
> M>
> M>  static inline void
> M>  kref_get(struct kref *kref)
> M>  {
> M>
> M> -	refcount_acquire(&kref->refcount);
> M> +	refcount_acquire((uint32_t *)&kref->refcount);
> M>  }
> M>
> M>  static inline int
> M>  kref_put(struct kref *kref, void (*rel)(struct kref *kref))
> M>  {
> M>
> M> -	if (refcount_release(&kref->refcount)) {
> M> +	if (refcount_release((uint32_t *)&kref->refcount)) {
> M>  		rel(kref);
> M>  		return 1;
> M>  	}
> M> @@ -84,7 +84,7 @@ kref_put_lock(struct kref *kref, void (*rel)(struct
> kref *kref),
> M>      spinlock_t *lock)
> M>  {
> M>
> M> -	if (refcount_release(&kref->refcount)) {
> M> +	if (refcount_release((uint32_t *)&kref->refcount)) {
> M>  		spin_lock(lock);
> M>  		rel(kref);
> M>  		return (1);
> M> @@ -98,7 +98,7 @@ kref_sub(struct kref *kref, unsigned int count,
> M>  {
> M>
> M>  	while (count--) {
> M> -		if (refcount_release(&kref->refcount)) {
> M> +		if (refcount_release((uint32_t *)&kref->refcount)) {
> M>  			rel(kref);
> M>  			return 1;
> M>  		}
> M> @@ -110,16 +110,16 @@ static inline int __must_check
> M>  kref_get_unless_zero(struct kref *kref)
> M>  {
> M>
> M> -	return refcount_acquire_if_not_zero(&kref->refcount);
> M> +	return refcount_acquire_if_not_zero((uint32_t *)&kref->refcount);
> M>  }
> M>
> M>  static inline int kref_put_mutex(struct kref *kref,
> M>      void (*release)(struct kref *kref), struct mutex *lock)
> M>  {
> M>  	WARN_ON(release == NULL);
> M> -	if (unlikely(!refcount_release_if_not_last(&kref->refcount))) {
> M> +	if (unlikely(!refcount_release_if_not_last((uint32_t
> *)&kref->refcount))) {
> M>  		mutex_lock(lock);
> M> -		if (unlikely(!refcount_release(&kref->refcount))) {
> M> +		if (unlikely(!refcount_release((uint32_t *)&kref->refcount))) {
> M>  			mutex_unlock(lock);
> M>  			return 0;
> M>  		}
>
> --
> Gleb Smirnoff
>


-- 
Mateusz Guzik <mjguzik gmail.com>