Skip to contentSkip to conversation

nfsd: add +1 to reference counting scheme for struct nfsd4_session

RFC PATCH In review David Windsor 7 participants 14 messages Feb 9, 2017 – Feb 15, 2017 6 messages not in the archive

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 nfsd: add +1 to reference counting scheme for struct nfsd4_session

In furtherance of the KSPP effort to add overflow protection to kernel reference counters, a new type (refcount_t) and API have been created. Part of the refcount_t API is refcount_inc(), which will not increment a refcount_t variable if its value is 0 (as this would indicate a possible use-after-free condition).

In auditing the kernel for refcounting corner cases, we've come across the case of struct nfsd4_session.

Quoted 1 line

From fs/nfsd/state.h:

/*
* Representation of a v4.1+ session. These are refcounted in a similar
* fashion to the nfs4_client. References are only taken when the server
* is actively working on the object (primarily during the processing of
* compounds).
*/

struct nfsd4_session {
    atomic_t se_ref;
    ...
};
Quoted 1 line

From fs/nfsd/nfs4state.c:

static void init_session(..., struct nfsd4_session *new, ...)

{
    ...
    atomic_set(&new->se_ref, 0);
    ...
}

Since nfsd4_session objects are initialized with refcount = 0, subsequent increments will fail using the new refcount_t API.

Being largely unfamiliar with this subsystem's garbage collection mechanism, I'm unsure how to best fix this. Attached is a patch that performs a logical +1 on struct nfsd4_session's reference counting scheme.

If this is the correct route to take, I will resubmit this patch with updated comments for how struct nfsd4_session is refcounted (see the above comment from fs/nsfd/state.h). This is in preparation for the previously mentioned refcount_t API series.

Thanks,
David Windsor

---
 fs/nfsd/nfs4state.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +3 -3
index a0dee8a..b0f3010 100644
@@ -196,7 +196,7 @@ static void nfsd4_put_session_locked(struct nfsd4_session *ses)
 
 	lockdep_assert_held(&nn->client_lock);
 
-	if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
+	if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_des(ses))
 		free_session(ses);
 	put_client_renew_locked(clp);
 }
@@ -1645,7 +1645,7 @@ static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, stru
 	new->se_flags = cses->flags;
 	new->se_cb_prog = cses->callback_prog;
 	new->se_cb_sec = cses->cb_sec;
-	atomic_set(&new->se_ref, 0);
+	atomic_set(&new->se_ref, 1);
 	idx = hash_sessionid(&new->se_sessionid);
 	list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
 	spin_lock(&clp->cl_lock);
@@ -1792,7 +1792,7 @@ free_client(struct nfs4_client *clp)
 		ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
 				se_perclnt);
 		list_del(&ses->se_perclnt);
-		WARN_ON_ONCE(atomic_read(&ses->se_ref));
+		WARN_ON_ONCE((atomic_read(&ses->se_ref) > 1));
 		free_session(ses);
 	}
 	rpc_destroy_wait_queue(&clp->cl_cb_waitq);
Signature · 1 line
2.7.4
#1 lore raw

<snip>

Quoted 10 lines
---
 fs/nfsd/nfs4state.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index a0dee8a..b0f3010 100644

fs/nfsd/nfs4state.c static void nfsd4_put_session_locked(struct nfsd4_session *ses) · line 199

Quoted 5 lines
fs/nfsd/nfs4state.c +1 -1
@@ -196,7 +196,7 @@ static void nfsd4_put_session_locked(struct nfsd4_session *ses)
 
        lockdep_assert_held(&nn->client_lock);
 
-       if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
+       if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_des(ses))

This should read:
if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_dead(ses))

Quoted 24 lines
fs/nfsd/nfs4state.c +2 -3
                free_session(ses);
        put_client_renew_locked(clp);
 }
@@ -1645,7 +1645,7 @@ static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, stru
        new->se_flags = cses->flags;
        new->se_cb_prog = cses->callback_prog;
        new->se_cb_sec = cses->cb_sec;
-       atomic_set(&new->se_ref, 0);
+       atomic_set(&new->se_ref, 1);
        idx = hash_sessionid(&new->se_sessionid);
        list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
        spin_lock(&clp->cl_lock);
@@ -1792,7 +1792,7 @@ free_client(struct nfs4_client *clp)
                ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
                                se_perclnt);
                list_del(&ses->se_perclnt);
-               WARN_ON_ONCE(atomic_read(&ses->se_ref));
+               WARN_ON_ONCE((atomic_read(&ses->se_ref) > 1));
                free_session(ses);
        }
        rpc_destroy_wait_queue(&clp->cl_cb_waitq);
--

2.7.4

A message that is not in the archive (sent off-list or not yet received): CAEXv5_jUa8Av4JABoKSAueiLHSLzibMvaE-8DrVcxZHFceckMg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#3 lore raw
Quoted 37 lines · David Windsor

<snip>

Quoted 10 lines
---
 fs/nfsd/nfs4state.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index a0dee8a..b0f3010 100644

fs/nfsd/nfs4state.c static void nfsd4_put_session_locked(struct nfsd4_session *ses) · line 199

Quoted 5 lines
fs/nfsd/nfs4state.c +1 -1
@@ -196,7 +196,7 @@ static void nfsd4_put_session_locked(struct nfsd4_session *ses)
 
        lockdep_assert_held(&nn->client_lock);
 
-       if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
+       if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_des(ses))

This should read:
if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_dead(ses))

Quoted 16 lines
fs/nfsd/nfs4state.c +1 -1
                free_session(ses);
        put_client_renew_locked(clp);
 }
@@ -1645,7 +1645,7 @@ static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, stru
        new->se_flags = cses->flags;
        new->se_cb_prog = cses->callback_prog;
        new->se_cb_sec = cses->cb_sec;
-       atomic_set(&new->se_ref, 0);
+       atomic_set(&new->se_ref, 1);
        idx = hash_sessionid(&new->se_sessionid);
        list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
        spin_lock(&clp->cl_lock);
@@ -1792,7 +1792,7 @@ free_client(struct nfs4_client *clp)
                ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
                                se_perclnt);
                list_del(&ses->se_perclnt);
Quoted 8 lines
Quoted 8 lines
fs/nfsd/nfs4state.c +1 -2
-               WARN_ON_ONCE(atomic_read(&ses->se_ref));
+               WARN_ON_ONCE((atomic_read(&ses->se_ref) > 1));
                free_session(ses);
        }
        rpc_destroy_wait_queue(&clp->cl_cb_waitq);
--

2.7.4

The basic idea here is that nfsv4 sessions have a "resting state" of 0. We want to keep them around, but if they go "dead" then we we'll tear them down if they aren't actively in use at the time. So, we still free the thing when the refcount goes to zero, but we have an extra condition before we free it on the put -- that the session is also "dead" (meaning that the client asked us to destroy it).

Your patch doesn't look like it'll break anything, but I personally find
it harder to follow that way. The freeable reference state will be 1
instead of the normal 0.

--
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 54 lines · Jeff Layton
Quoted 37 lines · David Windsor

<snip>

Quoted 10 lines
---
 fs/nfsd/nfs4state.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index a0dee8a..b0f3010 100644

fs/nfsd/nfs4state.c static void nfsd4_put_session_locked(struct nfsd4_session *ses) · line 199

Quoted 5 lines
fs/nfsd/nfs4state.c +1 -1
@@ -196,7 +196,7 @@ static void nfsd4_put_session_locked(struct nfsd4_session *ses)
 
        lockdep_assert_held(&nn->client_lock);
 
-       if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
+       if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_des(ses))

This should read:
if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_dead(ses))

Quoted 16 lines
fs/nfsd/nfs4state.c +1 -1
                free_session(ses);
        put_client_renew_locked(clp);
 }
@@ -1645,7 +1645,7 @@ static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, stru
        new->se_flags = cses->flags;
        new->se_cb_prog = cses->callback_prog;
        new->se_cb_sec = cses->cb_sec;
-       atomic_set(&new->se_ref, 0);
+       atomic_set(&new->se_ref, 1);
        idx = hash_sessionid(&new->se_sessionid);
        list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
        spin_lock(&clp->cl_lock);
@@ -1792,7 +1792,7 @@ free_client(struct nfs4_client *clp)
                ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
                                se_perclnt);
                list_del(&ses->se_perclnt);
Quoted 8 lines
Quoted 8 lines
fs/nfsd/nfs4state.c +1 -2
-               WARN_ON_ONCE(atomic_read(&ses->se_ref));
+               WARN_ON_ONCE((atomic_read(&ses->se_ref) > 1));
                free_session(ses);
        }
        rpc_destroy_wait_queue(&clp->cl_cb_waitq);
--

2.7.4

The basic idea here is that nfsv4 sessions have a "resting state" of 0. We want to keep them around, but if they go "dead" then we we'll tear them down if they aren't actively in use at the time. So, we still free the thing when the refcount goes to zero, but we have an extra condition before we free it on the put -- that the session is also "dead" (meaning that the client asked us to destroy it).

Quoted 4 lines

Your patch doesn't look like it'll break anything, but I personally find
it harder to follow that way. The freeable reference state will be 1
instead of the normal 0.

I'm not sure there's another way to accomplish what we need (initializing struct nfsd4_session objects with refcount=1) without also modifying the freeable reference state. After migrating to the refcount_t API, if we leave init_session() as is, the first call to nfsd4_get_session_locked() will fail:

static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)

{
         __be32 status;

         if (is_session_dead(ses))
                 return nfserr_badsession;
         status = get_client_locked(ses->se_client);
         if (status)
                 return status;
         refcount_inc(&ses->se_ref);    /* This fails and WARNS when
ses->se_ref is 0. */
         return nfs_ok;
}

The refcount_t API patches aren't yet merged, so this discussion is a bit limited in that respect, but refcount_inc() WARNS when called with a refcount_t object whose value is 0, as this may represent a use-after-free attempt.

Given this, I'm unsure how it's possible to achieve initialization of struct nfsd4_session objects with refcount=1 while still maintaining these objects' "rest state" at refcount=0.

Quoted 2 lines

--
Jeff Layton <[email protected]>

#6 lore raw
Quoted 81 lines · David Windsor
Quoted 54 lines · Jeff Layton
Quoted 37 lines · David Windsor

<snip>

Quoted 10 lines
---
 fs/nfsd/nfs4state.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index a0dee8a..b0f3010 100644

fs/nfsd/nfs4state.c static void nfsd4_put_session_locked(struct nfsd4_session *ses) · line 199

Quoted 5 lines
fs/nfsd/nfs4state.c +1 -1
@@ -196,7 +196,7 @@ static void nfsd4_put_session_locked(struct nfsd4_session *ses)
 
        lockdep_assert_held(&nn->client_lock);
 
-       if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
+       if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_des(ses))

This should read:
if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_dead(ses))

Quoted 16 lines
fs/nfsd/nfs4state.c +1 -1
                free_session(ses);
        put_client_renew_locked(clp);
 }
@@ -1645,7 +1645,7 @@ static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, stru
        new->se_flags = cses->flags;
        new->se_cb_prog = cses->callback_prog;
        new->se_cb_sec = cses->cb_sec;
-       atomic_set(&new->se_ref, 0);
+       atomic_set(&new->se_ref, 1);
        idx = hash_sessionid(&new->se_sessionid);
        list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
        spin_lock(&clp->cl_lock);
@@ -1792,7 +1792,7 @@ free_client(struct nfs4_client *clp)
                ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
                                se_perclnt);
                list_del(&ses->se_perclnt);
Quoted 8 lines
Quoted 8 lines
fs/nfsd/nfs4state.c +1 -2
-               WARN_ON_ONCE(atomic_read(&ses->se_ref));
+               WARN_ON_ONCE((atomic_read(&ses->se_ref) > 1));
                free_session(ses);
        }
        rpc_destroy_wait_queue(&clp->cl_cb_waitq);
--

2.7.4

The basic idea here is that nfsv4 sessions have a "resting state" of 0. We want to keep them around, but if they go "dead" then we we'll tear them down if they aren't actively in use at the time. So, we still free the thing when the refcount goes to zero, but we have an extra condition before we free it on the put -- that the session is also "dead" (meaning that the client asked us to destroy it).

Quoted 4 lines

Your patch doesn't look like it'll break anything, but I personally find
it harder to follow that way. The freeable reference state will be 1
instead of the normal 0.

I'm not sure there's another way to accomplish what we need (initializing struct nfsd4_session objects with refcount=1) without also modifying the freeable reference state. After migrating to the refcount_t API, if we leave init_session() as is, the first call to nfsd4_get_session_locked() will fail:

static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)

{
         __be32 status;

         if (is_session_dead(ses))
                 return nfserr_badsession;
         status = get_client_locked(ses->se_client);
         if (status)
                 return status;
         refcount_inc(&ses->se_ref);    /* This fails and WARNS when
ses->se_ref is 0. */
         return nfs_ok;
}
Quoted 9 lines

The refcount_t API patches aren't yet merged, so this discussion is a bit limited in that respect, but refcount_inc() WARNS when called with a refcount_t object whose value is 0, as this may represent a use-after-free attempt.

Given this, I'm unsure how it's possible to achieve initialization of struct nfsd4_session objects with refcount=1 while still maintaining these objects' "rest state" at refcount=0.

One idea might be to take an extra reference on the thing when creating it, and then drop that reference when the thing is marked DEAD. The extra reference would be superfluous, but it might make it look a little more natural.

Signature · 1 line
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): CAEXv5_gd7F-eaazzU1BWPzH4huhEcO1Y-FWov5UP9T+6R+fv-A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#8 lore raw
Quoted 5 lines · David Windsor

I'm not sure there's another way to accomplish what we need (initializing struct nfsd4_session objects with refcount=1) without also modifying the freeable reference state. After migrating to the refcount_t API, if we leave init_session() as is, the first call to nfsd4_get_session_locked() will fail:

Which is a pretty clear indicator that this code should simply not migrate to the recount_t API. Why was it even considered if the conversion is obviously broken?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · David Windsor

I'm not sure there's another way to accomplish what we need (initializing struct nfsd4_session objects with refcount=1) without also modifying the freeable reference state. After migrating to the refcount_t API, if we leave init_session() as is, the first call to nfsd4_get_session_locked() will fail:

Which is a pretty clear indicator that this code should simply not migrate to the recount_t API. Why was it even considered if the conversion is obviously broken?

I'm not sure this is a sound argument for not converting to refcount_t. In other locations in which refcounting schemes are "unnatural," i.e. freeing refcounted objects when their refcount is -1 (rather than 0), conversion to refcount_t is accomplished by performing a logical +1 to the overall refcounting scheme. We're auditing all refcounting corner cases, such as these, to see if similar solutions can be found.

Thanks,
David

A message that is not in the archive (sent off-list or not yet received): CAEXv5_g=DS4wk0mgZuw-doVCqountb-CxZki1LOoQH-P7W1U4A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#12 lore raw
Quoted 2 lines · David Windsor

I'm not sure this is a sound argument for not converting to refcount_t.

It's an argument again the way how this patch was sent. Please take care of all the trivial conversions first, and then do anything non-trivial on a case by case basis.

#13 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · David Windsor

I'm not sure this is a sound argument for not converting to refcount_t.

It's an argument again the way how this patch was sent. Please take care of all the trivial conversions first, and then do anything non-trivial on a case by case basis.

This is actually what we're currently doing.

The vast majority of atomic_t to refcount_t conversions are trivial, as they most always follow the pattern:

1.  Initialize the atomic_t refcounter to 1.
2.  Perform shared object get/put operations.
3.  Free the shared object when its refcount becomes 0.

We've identified a handful of instances in which steps 1 and 3 are different from above (i.e. initializing refcounts to 0, freeing objects when refcount is non-zero, etc.). The above patch addresses one of these instances in NFS.

Thanks,
David

#14 lore raw
In reply to a message not in the archive
Quoted 10 lines · Jeff Layton

The basic idea here is that nfsv4 sessions have a "resting state" of 0. We want to keep them around, but if they go "dead" then we we'll tear them down if they aren't actively in use at the time. So, we still free the thing when the refcount goes to zero, but we have an extra condition before we free it on the put -- that the session is also "dead" (meaning that the client asked us to destroy it).

Your patch doesn't look like it'll break anything, but I personally find
it harder to follow that way. The freeable reference state will be 1
instead of the normal 0.

Alas, I don't have any examples in mind, but doesn't this pattern happen all over?

You have objects that live in some data structure. They're freed only when they're removed from the data structure. You want removal to fail whenever they're in use.

So it's natural to use an atomic counter to track the number of external users and some other lock to serialize lookup and destruction.

--b.

#15 lore raw
Quoted 8 lines · Bruce Fields
Quoted 7 lines · Jeff Layton

The basic idea here is that nfsv4 sessions have a "resting state" of 0. We want to keep them around, but if they go "dead" then we we'll tear them down if they aren't actively in use at the time. So, we still free the thing when the refcount goes to zero, but we have an extra condition before we free it on the put -- that the session is also "dead" (meaning that the client asked us to destroy it).

Quoted 7 lines
Quoted 3 lines

Your patch doesn't look like it'll break anything, but I personally find
it harder to follow that way. The freeable reference state will be 1
instead of the normal 0.

Alas, I don't have any examples in mind, but doesn't this pattern happen all over?

The majority of refcounted objects are allocated with refcount=1: the very fact that they're being allocated means that they already have a user.

The issue with struct nfsd4_session is that, like struct nfs4_client, references to it are only taken when the server is actively working on it. Its default "resting state" is with refcount=0.

I would like to make its default resting state with refcount=1. In other cases similar to this, we've gotten around it by doing a semantic +1 to the object's overall refcounting scheme.

Jeff suggested taking an additional reference in init_session(), and dropping it in is_session_dead(), after determining, in fact, that the object is DEAD.

Quoted 4 lines

You have objects that live in some data structure. They're freed only when they're removed from the data structure. You want removal to fail whenever they're in use.

When they're in use, these objects' refcount should be > 0.

Quoted 3 lines

So it's natural to use an atomic counter to track the number of external users and some other lock to serialize lookup and destruction.

When considering refcounted objects, the most "natural" interpretation of refcount=0 means that the object no longer has any users and can be freed. Increments on objects with refcount=0 shouldn't be allowed, as this may indicate a use-after-free condition.

This discussion is difficult because the refcount_t API hasn't yet been introduced. The purpose of that API is to eliminate use-after-free bugs.

Quoted 1 line

--b.

#16 lore raw
In reply to a message not in the archive
Quoted 13 lines · David Windsor

<snip>

Quoted 11 lines
---
 fs/nfsd/nfs4state.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index a0dee8a..b0f3010 100644
@@ -196,7 +196,7 @@ static void nfsd4_put_session_locked(struct nfsd4_session *ses)
 
Quoted 9 lines

fs/nfsd/nfs4state.c static void nfsd4_put_session_locked(struct nfsd4_session *ses) · line 199

Quoted 4 lines
fs/nfsd/nfs4state.c +1 -1
        lockdep_assert_held(&nn->client_lock);
 
-       if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
+       if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_des(ses))

This should read:
if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_dead(ses))

Quoted 1 line
fs/nfsd/nfs4state.c +0 -0
                free_session(ses);

Hi,

I'm not sure if I have this correctly; But both before and after the patch free_session gets called when se_ref count was 1, shouldn't this have changed with the +1 scheme?

Also, since the !atomic_add_unless doesn't actually decrement when at 1, doesn't this leave the se_ref as 1 when it's destroyed? The function seems to always be locked, so perhaps this doesn't matter, but still seems a bit risky.

Thanks,
-hans

Quoted 23 lines
Quoted 23 lines
fs/nfsd/nfs4state.c +2 -3
        put_client_renew_locked(clp);
 }
@@ -1645,7 +1645,7 @@ static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, stru
        new->se_flags = cses->flags;
        new->se_cb_prog = cses->callback_prog;
        new->se_cb_sec = cses->cb_sec;
-       atomic_set(&new->se_ref, 0);
+       atomic_set(&new->se_ref, 1);
        idx = hash_sessionid(&new->se_sessionid);
        list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
        spin_lock(&clp->cl_lock);
@@ -1792,7 +1792,7 @@ free_client(struct nfs4_client *clp)
                ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
                                se_perclnt);
                list_del(&ses->se_perclnt);
-               WARN_ON_ONCE(atomic_read(&ses->se_ref));
+               WARN_ON_ONCE((atomic_read(&ses->se_ref) > 1));
                free_session(ses);
        }
        rpc_destroy_wait_queue(&clp->cl_cb_waitq);
--

2.7.4

#17 lore raw
Quoted 34 lines · Hans Liljestrand
Quoted 17 lines · David Windsor

<snip>

Quoted 14 lines
---
 fs/nfsd/nfs4state.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index a0dee8a..b0f3010 100644
@@ -196,7 +196,7 @@ static void nfsd4_put_session_locked(struct
 nfsd4_session *ses)
 
        lockdep_assert_held(&nn->client_lock);
 
Quoted 9 lines

fs/nfsd/nfs4state.c static void nfsd4_put_session_locked(struct · line 201

Quoted 3 lines
fs/nfsd/nfs4state.c +1 -1
-       if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
+       if (!atomic_add_unless(&ses->se_ref, -1, 1) &&
 is_session_des(ses))

This should read:
if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_dead(ses))

Quoted 1 line
fs/nfsd/nfs4state.c +0 -0
                free_session(ses);

Hi,
I'm not sure if I have this correctly; But both before and after the patch free_session gets called when se_ref count was 1, shouldn't this have changed with the +1 scheme?

Quoted 5 lines

Also, since the !atomic_add_unless doesn't actually decrement when at 1, doesn't this leave the se_ref as 1 when it's destroyed? The function seems to always be locked, so perhaps this doesn't matter, but still seems a bit risky.

Yes; I forgot the additional call to atomic_dec_and_test() before free_session(). Thanks!

I'll resubmit this after seeing how the rest of this discussion goes. We may end up abandoning this refcounting case.

Quoted 30 lines

Thanks,
-hans

Quoted 25 lines
Quoted 25 lines
        put_client_renew_locked(clp);

}

fs/nfsd/nfs4state.c +0 -0
@@ -1645,7 +1645,7 @@ static void init_session(struct svc_rqst *rqstp,

struct nfsd4_session *new, stru

        new->se_flags = cses->flags;
        new->se_cb_prog = cses->callback_prog;
        new->se_cb_sec = cses->cb_sec;
fs/nfsd/nfs4state.c +2 -2
-       atomic_set(&new->se_ref, 0);
+       atomic_set(&new->se_ref, 1);
        idx = hash_sessionid(&new->se_sessionid);
        list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
        spin_lock(&clp->cl_lock);
@@ -1792,7 +1792,7 @@ free_client(struct nfs4_client *clp)
                ses = list_entry(clp->cl_sessions.next, struct
 nfsd4_session,
                                se_perclnt);
                list_del(&ses->se_perclnt);
-               WARN_ON_ONCE(atomic_read(&ses->se_ref));
+               WARN_ON_ONCE((atomic_read(&ses->se_ref) > 1));
                free_session(ses);
        }
        rpc_destroy_wait_queue(&clp->cl_cb_waitq);

--
2.7.4

A message that is not in the archive (sent off-list or not yet received): CAEXv5_hP39k7HSLP-G_khx7MMQHnk=8Z5caa+U5n3bYvUTE1gQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#19 lore raw
Quoted 41 lines · David Windsor
Quoted 34 lines · Hans Liljestrand
Quoted 17 lines · David Windsor

<snip>

Quoted 14 lines
---
 fs/nfsd/nfs4state.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +0 -0
index a0dee8a..b0f3010 100644
@@ -196,7 +196,7 @@ static void nfsd4_put_session_locked(struct
 nfsd4_session *ses)
 
        lockdep_assert_held(&nn->client_lock);
 
Quoted 9 lines

fs/nfsd/nfs4state.c static void nfsd4_put_session_locked(struct · line 201

Quoted 3 lines
fs/nfsd/nfs4state.c +1 -1
-       if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
+       if (!atomic_add_unless(&ses->se_ref, -1, 1) &&
 is_session_des(ses))

This should read:
if (!atomic_add_unless(&ses->se_ref, -1, 1) && is_session_dead(ses))

Quoted 1 line
fs/nfsd/nfs4state.c +0 -0
                free_session(ses);

Hi,
I'm not sure if I have this correctly; But both before and after the patch free_session gets called when se_ref count was 1, shouldn't this have changed with the +1 scheme?

Quoted 5 lines

Also, since the !atomic_add_unless doesn't actually decrement when at 1, doesn't this leave the se_ref as 1 when it's destroyed? The function seems to always be locked, so perhaps this doesn't matter, but still seems a bit risky.

Quoted 5 lines

Yes; I forgot the additional call to atomic_dec_and_test() before free_session(). Thanks!

I'll resubmit this after seeing how the rest of this discussion goes. We may end up abandoning this refcounting case.

I could live with it.

My knee jerk reaction is like Jeff's--it just seems more natural to me for reference count 0 to mean "not in use, OK to free" in cases like this--but maybe I just need to get used to the idea.

It'd be interesting to see what the final result looks like after conversion to refcount_t.

--b.