lockd: refcount NLM_SHARE access/deny modes
Subsystems (2)
PATCH 1/1 lockd: refcount NLM_SHARE access/deny modes
When an NFSv3/NLM client issues multiple NLM_SHARE calls from a single host for the same (file, owner) tuple, the current implementation overwrites the recorded access and deny modes with the latest pair. A subsequent NLM_UNSHARE then drops the entire entry, even if other grants were implicitly subsumed by the most recent SHARE. This is particularly visible to Windows-style clients that map each open of a file to a distinct NLM_SHARE, all carrying the same NLM owner handle. For example:
1. SHARE(access=RW, deny=W) -> entry [RW, deny W]
2. SHARE(access=R, deny=N) -> entry [R, deny N] (RW/W overwritten)
3. UNSHARE(access=R, deny=N) -> entry freed 4. UNSHARE(access=RW, deny=W) -> nothing to release
Track each of the four valid fsh_access and fsh_mode values with a small per-value refcount. On SHARE the appropriate buckets are incremented; on UNSHARE they are decremented. s_access / s_mode are recomputed in both paths as the union of all positive buckets, and the entry is freed only when every bucket has reached zero.
NLM_UNSHARE gains the access and deny modes as arguments so the correct buckets can be decremented. The two callers in svcproc.c and svc4proc.c are updated to forward the decoded values.
The per-file f_mutex held by callers in fs/lockd/svcsubs.c continues to serialise share manipulation, so the new arrays do not require their own locking.
--- fs/lockd/share.h | 6 +++++- fs/lockd/svc4proc.c | 4 +++- fs/lockd/svcproc.c | 4 +++- fs/lockd/svcshare.c | 40 +++++++++++++++++++++++++++++++++++----- 4 files changed, 46 insertions(+), 8 deletions(-)
fs/lockd/share.h +5 -1
@@ -11,6 +11,8 @@/* Synthetic svid for lockowner lookup during share operations */ #define LOCKD_SHARE_SVID (~(u32)0) +#define LOCKD_FSH_NR 4/* fsh_access / fsh_mode are in {0..3} */ +/* * DOS share for a specific file */
@@ -21,12 +23,14 @@ struct lockd_share {struct xdr_netobj s_owner;/* owner handle */ u32 s_access;/* access mode */ u32 s_mode;/* deny mode */ +u32 s_access_counts[LOCKD_FSH_NR ]; +u32 s_mode_counts[LOCKD_FSH_NR ]; };__be32 nlmsvc_share_file (struct nlm_host *host,struct nlm_file *file,struct xdr_netobj *oh,u32 access,u32 mode);__be32 nlmsvc_unshare_file (struct nlm_host *host,struct nlm_file *file, -struct xdr_netobj *oh); +struct xdr_netobj *oh,u32 access,u32 mode);void nlmsvc_traverse_shares (struct nlm_host *,struct nlm_file *,nlm_host_match_fn_t );
fs/lockd/svc4proc.c +3 -1
@@ -1078,7 +1078,9 @@ static __be32 nlm4svc_proc_unshare(struct svc_rqst *rqstp)if (resp->xdrgen.stat) goto out; - resp->xdrgen.stat =nlmsvc_unshare_file (host, file, &lock->oh); + resp->xdrgen.stat =nlmsvc_unshare_file (host, file, &lock->oh, + argp->xdrgen.share.access, + argp->xdrgen.share.mode);nlmsvc_release_lockowner (lock);
fs/lockd/svcproc.c +3 -1
@@ -1097,7 +1097,9 @@ static __be32 nlmsvc_proc_unshare(struct svc_rqst *rqstp)if (resp->xdrgen.stat) goto out; - resp->xdrgen.stat =nlmsvc_unshare_file (host, file, &lock->oh); + resp->xdrgen.stat =nlmsvc_unshare_file (host, file, &lock->oh, + argp->xdrgen.share.access, + argp->xdrgen.share.mode);nlmsvc_release_lockowner (lock);
fs/lockd/svcshare.c +35 -5
@@ -25,6 +25,24 @@ nlm_cmp_owner(struct lockd_share *share, struct xdr_netobj *oh) && !memcmp (share->s_owner.data, oh->data, oh->len); } +/* +* Recompute s_access / s_mode as the union of all positive refcount +* buckets. Caller must hold the per-file f_mutex. +*/ +static void nlm_recompute_share (struct lockd_share *share) +{ +u32 new_access =0 , new_mode =0 , v; + +for (v =1 ; v <LOCKD_FSH_NR ; v++) { +if (share->s_access_counts[v]) + new_access |= v; +if (share->s_mode_counts[v]) + new_mode |= v; + } + share->s_access = new_access; + share->s_mode = new_mode; +} +/** * nlmsvc_share_file - create a share * @host: Network client peer
@@ -64,12 +82,15 @@ nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file, share->s_host = host; share->s_owner.data = ohdata; share->s_owner.len = oh->len; +memset (share->s_access_counts,0 ,sizeof (share->s_access_counts)); +memset (share->s_mode_counts,0 ,sizeof (share->s_mode_counts)); share->s_next = file->f_shares; file->f_shares = share; update: - share->s_access = access; - share->s_mode = mode; + share->s_access_counts[access]++; + share->s_mode_counts[mode]++; +nlm_recompute_share (share);return nlm_granted; }
@@ -78,12 +99,14 @@ nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file,* @host: Network client peer * @file: File to be unshared * @oh: Share owner handle +* @access: Access mode of the SHARE being released +* @mode: Deny mode of the SHARE being released * * Returns an NLM status code. */ __be32 nlmsvc_unshare_file (struct nlm_host *host,struct nlm_file *file, -struct xdr_netobj *oh) +struct xdr_netobj *oh,u32 access,u32 mode) {struct lockd_share *share, **shpp;
@@ -93,8 +116,15 @@ nlmsvc_unshare_file(struct nlm_host *host, struct nlm_file *file,for (shpp = &file->f_shares; (share = *shpp) !=NULL ; shpp = &share->s_next) {if (share->s_host == host &&nlm_cmp_owner (share, oh)) { - *shpp = share->s_next; -kfree (share); +if (share->s_access_counts[access]) + share->s_access_counts[access]--; +if (share->s_mode_counts[mode]) + share->s_mode_counts[mode]--; +nlm_recompute_share (share); +if (!share->s_access && !share->s_mode) { + *shpp = share->s_next; +kfree (share); + }return nlm_granted; } }
Signature · 4 lines
2.34.1 Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
Chuck Lever
maintainerQuoted 14 lines · Oscar Ou
When an NFSv3/NLM client issues multiple
NLM_SHAREcalls from a single host for the same (file, owner) tuple, the current implementation overwrites the recorded access and deny modes with the latest pair. A subsequentNLM_UNSHAREthen drops the entire entry, even if other grants were implicitly subsumed by the most recent SHARE. This is particularly visible to Windows-style clients that map each open of a file to a distinctNLM_SHARE, all carrying the same NLM owner handle.fs/lockd/svcshare.c +0
-0
fs/lockd/svcshare.c nlm_cmp_owner(struct lockd_share *share, struct · line 32
Quoted 8 lines
fs/lockd/svcshare.c +4
-0@@ -25,6 +25,24 @@ nlm_cmp_owner(struct lockd_share *share, struct xdr_netobj *oh) && !memcmp(share->s_owner.data, oh->data, oh->len); } +/* +* Recompute s_access / s_mode as the union of all positive refcount +* buckets. Caller must hold the per-file f_mutex. +*/
Is the f_mutex statement here accurate, and the matching sentence in the commit message:
The per-file f_mutex held by callers in fs/lockd/svcsubs.c continues to serialise share manipulation, so the new arrays do not require their own locking.
nlm_lookup_file() in fs/lockd/svcsubs.c takes file->f_mutex only around nlm_do_fopen() and drops it before returning:
fs/lockd/svcsubs.c:nlm_lookup_file() {
...
mutex_lock(&file->f_mutex);
nfserr = nlm_do_fopen(rqstp, file, mode);
mutex_unlock(&file->f_mutex);
...
}By the time nlmsvc_proc_share() / nlmsvc_proc_unshare() call into nlmsvc_share_file() / nlmsvc_unshare_file(), f_mutex is no longer held. What actually serialises these requests is that lockd runs a single service thread (svc_set_num_threads(serv, 0, 1) in fs/lockd/svc.c), so the conclusion that the arrays need no extra locking holds. Would it be worth pointing the comment and commit message at the single-threaded service rather than f_mutex?
The loop starts at v = 1, so s_access_counts[0] and s_mode_counts[0] are incremented and decremented but never read. That is fine for computing the union, since index 0 (fsa_NONE / fsm_DN) contributes no bits. It does mean the free decision keys on the recomputed s_access / s_mode rather than on every bucket, so the commit message wording
the entry is freed only when every bucket has reached zero.
is slightly stronger than the code: an entry can be freed while s_access_counts[0] or s_mode_counts[0] is still non-zero. Harmless given NONE/DN enforce nothing, but the stated invariant and the bucket-0 storage do not quite line up.
Quoted 26 lines
fs/lockd/svcshare.c +14
-0+static void nlm_recompute_share (struct lockd_share *share) +{ +u32 new_access =0 , new_mode =0 , v; + +for (v =1 ; v <LOCKD_FSH_NR ; v++) { +if (share->s_access_counts[v]) + new_access |= v; +if (share->s_mode_counts[v]) + new_mode |= v; + } + share->s_access = new_access; + share->s_mode = new_mode; +} +/** * nlmsvc_share_file - create a share * @host: Network client peer
fs/lockd/svcshare.c +2
-0@@ -64,12 +82,15 @@ nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file, share->s_host = host; share->s_owner.data = ohdata; share->s_owner.len = oh->len; +memset (share->s_access_counts,0 ,sizeof (share->s_access_counts)); +memset (share->s_mode_counts,0 ,sizeof (share->s_mode_counts)); share->s_next = file->f_shares;
fs/lockd/svcshare.c nlmsvc_share_file(struct nlm_host *host, struct · line 96
Quoted 10 lines
fs/lockd/svcshare.c +3
-2file->f_shares = share; update: - share->s_access = access; - share->s_mode = mode; + share->s_access_counts[access]++; + share->s_mode_counts[mode]++; +nlm_recompute_share (share);return nlm_granted; }
A remote NLMv4 client can drive access or mode out of {0..3} here and write past s_access_counts[] / s_mode_counts[]. There are one or two other spots with the same issue.
Thus your patch can't be applied as written.
However, today is your lucky day: I have a patch that regenerates the XDR decoders to prevent exactly this issue, which I will post forthwith.
I'm wondering about a Fixes tag. This issue has been around since 1da177e4c3f4 ("Linux-2.6.12-rc2"), but the fix depends mechanically on the xdrgen conversion, so it certainly will not apply cleanly to kernels earlier than v7.1, and a proper fix will need both the regeneration patch and this one to be applied.
Signature · 1 line
Chuck Lever
PATCH v2 1/1 lockd: refcount NLM_SHARE access/deny modes
In reply to Oscar OuWhen an NFSv3/NLM client issues multiple NLM_SHARE calls from a single host for the same (file, owner) tuple, the current implementation overwrites the recorded access and deny modes with the latest pair. A subsequent NLM_UNSHARE then drops the entire entry, even if other grants were implicitly subsumed by the most recent SHARE. This is particularly visible to Windows-style clients that map each open of a file to a distinct NLM_SHARE, all carrying the same NLM owner handle. For example:
1. SHARE(access=RW, deny=W) -> entry [RW, deny W]
2. SHARE(access=R, deny=N) -> entry [R, deny N] (RW/W overwritten)
3. UNSHARE(access=R, deny=N) -> entry freed 4. UNSHARE(access=RW, deny=W) -> nothing to release
Track each of the four valid fsh_access and fsh_mode values with a small per-value refcount. On SHARE the appropriate buckets are incremented; on UNSHARE they are decremented. s_access / s_mode are recomputed in both paths as the union of all positive buckets with a non-NONE / non-DN value (indices 1..3), and the entry is freed once s_access and s_mode are both zero.
NLM_UNSHARE gains the access and deny modes as arguments so the correct buckets can be decremented. The two callers in svcproc.c and svc4proc.c are updated to forward the decoded values.
---
Applies on top of "lockd: Regenerate NLMv4 XDR code" by Chuck Lever.
No Fixes: tag. The fix depends mechanically on the xdrgen regeneration, so a Fixes: tag would mislead -stable backporters. Happy to add one plus an informal Depends-on: line if preferred.
Changes since v1:
- Reworded the "buckets" sentence in the commit message: the entry is freed once s_access and s_mode are both zero, not when every bucket has reached zero. Index 0 (fsa_NONE / fsm_DN) contributes no bits to the union and does not gate freeing.
- Dropped the incorrect claim that per-file f_mutex serialises the new arrays, from both the commit message and the comment on nlm_recompute_share(). f_mutex is released by nlm_lookup_file() before the share helpers run.
fs/lockd/share.h | 6 +++++- fs/lockd/svc4proc.c | 4 +++- fs/lockd/svcproc.c | 4 +++- fs/lockd/svcshare.c | 40 +++++++++++++++++++++++++++++++++++----- 4 files changed, 46 insertions(+), 8 deletions(-)
fs/lockd/share.h +5 -1
@@ -11,6 +11,8 @@/* Synthetic svid for lockowner lookup during share operations */ #define LOCKD_SHARE_SVID (~(u32)0) +#define LOCKD_FSH_NR 4/* fsh_access / fsh_mode are in {0..3} */ +/* * DOS share for a specific file */
@@ -21,12 +23,14 @@ struct lockd_share {struct xdr_netobj s_owner;/* owner handle */ u32 s_access;/* access mode */ u32 s_mode;/* deny mode */ +u32 s_access_counts[LOCKD_FSH_NR ]; +u32 s_mode_counts[LOCKD_FSH_NR ]; };__be32 nlmsvc_share_file (struct nlm_host *host,struct nlm_file *file,struct xdr_netobj *oh,u32 access,u32 mode);__be32 nlmsvc_unshare_file (struct nlm_host *host,struct nlm_file *file, -struct xdr_netobj *oh); +struct xdr_netobj *oh,u32 access,u32 mode);void nlmsvc_traverse_shares (struct nlm_host *,struct nlm_file *,nlm_host_match_fn_t );
fs/lockd/svc4proc.c +3 -1
@@ -1078,7 +1078,9 @@ static __be32 nlm4svc_proc_unshare(struct svc_rqst *rqstp)if (resp->xdrgen.stat) goto out; - resp->xdrgen.stat =nlmsvc_unshare_file (host, file, &lock->oh); + resp->xdrgen.stat =nlmsvc_unshare_file (host, file, &lock->oh, + argp->xdrgen.share.access, + argp->xdrgen.share.mode);nlmsvc_release_lockowner (lock);
fs/lockd/svcproc.c +3 -1
@@ -1097,7 +1097,9 @@ static __be32 nlmsvc_proc_unshare(struct svc_rqst *rqstp)if (resp->xdrgen.stat) goto out; - resp->xdrgen.stat =nlmsvc_unshare_file (host, file, &lock->oh); + resp->xdrgen.stat =nlmsvc_unshare_file (host, file, &lock->oh, + argp->xdrgen.share.access, + argp->xdrgen.share.mode);nlmsvc_release_lockowner (lock);
fs/lockd/svcshare.c +35 -5
@@ -25,6 +25,24 @@ nlm_cmp_owner(struct lockd_share *share, struct xdr_netobj *oh) && !memcmp (share->s_owner.data, oh->data, oh->len); } +/* +* Recompute s_access / s_mode as the union of all positive refcount +* buckets. +*/ +static void nlm_recompute_share (struct lockd_share *share) +{ +u32 new_access =0 , new_mode =0 , v; + +for (v =1 ; v <LOCKD_FSH_NR ; v++) { +if (share->s_access_counts[v]) + new_access |= v; +if (share->s_mode_counts[v]) + new_mode |= v; + } + share->s_access = new_access; + share->s_mode = new_mode; +} +/** * nlmsvc_share_file - create a share * @host: Network client peer
@@ -64,12 +82,15 @@ nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file, share->s_host = host; share->s_owner.data = ohdata; share->s_owner.len = oh->len; +memset (share->s_access_counts,0 ,sizeof (share->s_access_counts)); +memset (share->s_mode_counts,0 ,sizeof (share->s_mode_counts)); share->s_next = file->f_shares; file->f_shares = share; update: - share->s_access = access; - share->s_mode = mode; + share->s_access_counts[access]++; + share->s_mode_counts[mode]++; +nlm_recompute_share (share);return nlm_granted; }
@@ -78,12 +99,14 @@ nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file,* @host: Network client peer * @file: File to be unshared * @oh: Share owner handle +* @access: Access mode of the SHARE being released +* @mode: Deny mode of the SHARE being released * * Returns an NLM status code. */ __be32 nlmsvc_unshare_file (struct nlm_host *host,struct nlm_file *file, -struct xdr_netobj *oh) +struct xdr_netobj *oh,u32 access,u32 mode) {struct lockd_share *share, **shpp;
@@ -93,8 +116,15 @@ nlmsvc_unshare_file(struct nlm_host *host, struct nlm_file *file,for (shpp = &file->f_shares; (share = *shpp) !=NULL ; shpp = &share->s_next) {if (share->s_host == host &&nlm_cmp_owner (share, oh)) { - *shpp = share->s_next; -kfree (share); +if (share->s_access_counts[access]) + share->s_access_counts[access]--; +if (share->s_mode_counts[mode]) + share->s_mode_counts[mode]--; +nlm_recompute_share (share); +if (!share->s_access && !share->s_mode) { + *shpp = share->s_next; +kfree (share); + }return nlm_granted; } }
Signature · 4 lines
2.34.1 Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
Chuck Lever
maintainerQuoted 43 lines · Oscar Ou
When an NFSv3/NLM client issues multiple
NLM_SHAREcalls from a single host for the same (file, owner) tuple, the current implementation overwrites the recorded access and deny modes with the latest pair. A subsequentNLM_UNSHAREthen drops the entire entry, even if other grants were implicitly subsumed by the most recent SHARE. This is particularly visible to Windows-style clients that map each open of a file to a distinctNLM_SHARE, all carrying the same NLM owner handle. For example:1. SHARE(access=RW, deny=W) -> entry [RW, deny W] 2. SHARE(access=R, deny=N) -> entry [R, deny N] (RW/W overwritten) 3. UNSHARE(access=R, deny=N) -> entry freed4.
UNSHARE(access=RW, deny=W)-> nothing to releaseTrack each of the four valid
fsh_accessandfsh_modevalues with a small per-value refcount. On SHARE the appropriate buckets are incremented; on UNSHARE they are decremented.s_access/s_modeare recomputed in both paths as the union of all positive buckets with a non-NONE / non-DN value (indices 1..3), and the entry is freed onces_accessands_modeare both zero.
NLM_UNSHAREgains the access and deny modes as arguments so the correct buckets can be decremented. The two callers insvcproc.candsvc4proc.care updated to forward the decoded values.Signed-off-by Oscar Ou <[email protected]>---
Applies on top of "lockd: Regenerate NLMv4 XDR code" by Chuck Lever.No Fixes: tag. The fix depends mechanically on the xdrgen regeneration, so a Fixes: tag would mislead -stable backporters. Happy to add one plus an informal Depends-on: line if preferred.
Changes since v1:
- Reworded the "buckets" sentence in the commit message: the entry is freed onces_accessands_modeare both zero, not when every bucket has reached zero. Index 0 (fsa_NONE/fsm_DN) contributes no bits to the union and does not gate freeing.
- Dropped the incorrect claim that per-filef_mutexserialises the new arrays, from both the commit message and the comment onnlm_recompute_share().f_mutexis released bynlm_lookup_file()before the share helpers run.
Quoted 5 lines
fs/lockd/share.h | 6 +++++- fs/lockd/svc4proc.c | 4 +++- fs/lockd/svcproc.c | 4 +++- fs/lockd/svcshare.c | 40 +++++++++++++++++++++++++++++++++++----- 4 files changed, 46 insertions(+), 8 deletions(-)
Hi Oscar -
First, a "process" note, then notes on your patch
follow below that.
Thanks for the "Applies on" and the change log. You've addressed the concerns from my review of v1.
I want to highlight a couple of subtleties when sending updated patch versions.
1. New thread per revision — no In-Reply-To: back to the old version.
Post v2 as its own top-level thread; don't reply-to the v1 cover or any v1 patch. Per the doc (lines 845–850): "for a multi-patch series, it is generally best to avoid using
In-Reply-To: to link to older versions of the series … so multiple versions don't become an unmanageable forest of references."
This matters for our tooling: b4 and patchwork group a thread by its reference chain. If v2 is threaded under v1, b4 sees one giant thread and can pick up stale patches when someone runs b4 am; maintainers who "apply the latest" can grab the wrong blobs. A clean top-level thread per version keeps b4 am/patchwork resolving to exactly the vN the contributor intended.
2. Version the subject tag, not RESEND. [PATCH v2], [PATCH v2 01/27] (lines 716–719, 726–731). RESEND is only for re-sending an /unchanged/ series that got no response (lines 379–380) — a modified series is never RESEND.
Quoted 13 lines
fs/lockd/svcshare.c +2
-0@@ -64,12 +82,15 @@ nlmsvc_share_file(struct nlm_host *host, struct nlm_file *file, share->s_host = host; share->s_owner.data = ohdata; share->s_owner.len = oh->len; +memset (share->s_access_counts,0 ,sizeof (share->s_access_counts)); +memset (share->s_mode_counts,0 ,sizeof (share->s_mode_counts)); share->s_next = file->f_shares;
fs/lockd/svcshare.c nlmsvc_share_file(struct nlm_host *host, struct · line 96
Quoted 10 lines
fs/lockd/svcshare.c +3
-2file->f_shares = share; update: - share->s_access = access; - share->s_mode = mode; + share->s_access_counts[access]++; + share->s_mode_counts[mode]++; +nlm_recompute_share (share);return nlm_granted; }
NLM has no duplicate reply cache, so it depends on its request handlers being idempotent: nlmsvc_dispatch() runs the procedure directly for every request that arrives, with nothing to absorb a retransmit.
fs/lockd/svc.c:nlmsvc_dispatch() {
...
*statp = procp->pc_func(rqstp);
...
}The rest of the NLM procedures are built around that invariant. LOCK reuses an existing block and lets vfs_lock_file(F_SETLK) re-apply a held lock as a no-op, UNLOCK and CANCEL act on state that a replay simply finds already gone, and FREE_ALL / SM_NOTIFY discard everything for a host, so a replayed request lands on the same state. Before this change SHARE fit the same pattern: a repeated SHARE for an already-recorded (host, owner) just reassigned s_access and s_mode.
With the per-value refcount a replayed SHARE instead increments the bucket a second time. Over UDP a lost reply followed by a client retransmit reaches nlmsvc_share_file() twice for one open, and if the client then sends one matching UNSHARE:
SHARE(access=RW, deny=W) -> s_access_counts[3] = 1 SHARE(access=RW, deny=W) (retransmit) -> s_access_counts[3] = 2 UNSHARE(access=RW, deny=W) -> s_access_counts[3] = 1
nlm_recompute_share() still sees s_access_counts[3] > 0, so s_access stays non-zero and the entry is never freed. Can this leave a stale share reservation that denies later conflicting opens until the host is torn down?
Quoted 18 lines
fs/lockd/svcshare.c +9
-2@@ -93,8 +116,15 @@ nlmsvc_unshare_file(struct nlm_host *host, struct nlm_file *file, for (shpp = &file->f_shares; (share = *shpp) != NULL; shpp = &share->s_next) {if (share->s_host == host &&nlm_cmp_owner (share, oh)) { - *shpp = share->s_next; -kfree (share); +if (share->s_access_counts[access]) + share->s_access_counts[access]--; +if (share->s_mode_counts[mode]) + share->s_mode_counts[mode]--; +nlm_recompute_share (share); +if (!share->s_access && !share->s_mode) { + *shpp = share->s_next; +kfree (share); + }return nlm_granted; }
Quoted 1 line
}
The decrement side has the mirror problem. The old UNSHARE dropped the whole entry on the first matching call, so a replay found nothing and returned granted, idempotent. The refcount version decrements a bucket that may still be positive, so a replayed UNSHARE keeps going:
open #1: SHARE(RW, deny=W) -> s_access_counts[3] = 1 open #2: SHARE(RW, deny=W) -> s_access_counts[3] = 2 close #1: UNSHARE(RW, deny=W) -> s_access_counts[3] = 1 retransmit of that UNSHARE -> s_access_counts[3] = 0 (entry freed)
Here the client still holds open #2, but its reservation is gone, so a conflicting open from another owner would now be granted. Can a retransmitted UNSHARE release a grant the client still holds?
Taken together, is there a way to keep SHARE and UNSHARE idempotent under retransmission, some per-open identity to match on rather than a bare count, so a duplicated request lands on the same state the way the other NLM procedures do?
One direction would be to track the set of held (access, deny) combinations rather than a count of each. There are only sixteen possible pairs, so a single u16 held-mask with one bit per (access << 2 | deny) covers it:
SHARE(a, d): held_mask |= BIT((a << 2) | d); UNSHARE(a, d): held_mask &= ~BIT((a << 2) | d);
s_access and s_mode are then the union of (i >> 2) and (i & 3) over the set bits, and the entry is freed once held_mask is zero. Setting or clearing a bit is idempotent, so a replayed SHARE or UNSHARE lands on the same mask, while distinct (access, deny) opens from one owner are still tracked separately.
The tradeoff is that two opens with the identical (access, deny) pair collapse to one bit, so a count of duplicate identical opens is lost. RFC 1813 defines SHARE and UNSHARE but does not require that repeated identical SHAREs from one owner survive an equal number of UNSHAREs, and its duplicate-request discussion (section 4.5) treats a non-idempotent request as something a reply cache has to guard, which lockd does not run for NLM. Tracking each distinct (access, deny) combination therefore stays within the protocol while keeping both operations idempotent. Does that match your reading?
The bottom line is that after more consideration, IMO the reference count approach is not going to work, in particular because NLM still operates on unreliable network transports like UDP. Consider the above direction not as a mandate but as a possible approach that might address the issue you've reported while avoiding introducing non-idempotency.
Signature · 1 line
Chuck Lever
Quoted 8 lines · Chuck Lever
1. New thread per revision — no In-Reply-To: back to the old version.
[...]
2. Version the subject tag, not RESEND. [PATCH v2], [PATCH v2 01/27] (lines 716–719, 726–731). RESEND is only for re-sending an /unchanged/ series that got no response (lines 379–380) — a modified series is never RESEND.
Understood, thanks -- v3 will go out as its own top-level thread with a [PATCH v3] subject.
Quoted 5 lines
With the per-value refcount a replayed SHARE instead increments the bucket a second time. Over UDP a lost reply followed by a client retransmit reaches
nlmsvc_share_file()twice for one open, and if the client then sends one matching UNSHARE:
Quoted 8 lines
SHARE(access=RW, deny=W) -> s_access_counts[3] = 1 SHARE(access=RW, deny=W) (retransmit) -> s_access_counts[3] = 2 UNSHARE(access=RW, deny=W) -> s_access_counts[3] = 1
nlm_recompute_share()still sees s_access_counts[3] > 0, sos_accessstays non-zero and the entry is never freed. Can this leave a stale share reservation that denies later conflicting opens until the host is torn down?
Yes. The inflated bucket only drains via the client's own UNSHAREs or a FREE_ALL / SM_NOTIFY teardown, so until then conflicting SHAREs from other owners see a spurious NLM_LCK_DENIED.
Quoted 3 lines
Here the client still holds open #2, but its reservation is gone, so a conflicting open from another owner would now be granted. Can a retransmitted UNSHARE release a grant the client still holds?
Agreed -- the UNSHARE retransmit should have been a no-op, but v2 decrements a second time and releases a share the client still holds.
Quoted 11 lines
Taken together, is there a way to keep SHARE and UNSHARE idempotent under retransmission, some per-open identity to match on rather than a bare count, so a duplicated request lands on the same state the way the other NLM procedures do?
One direction would be to track the set of held (access, deny) combinations rather than a count of each.
[...]
Setting or clearing a bit is idempotent, so a replayed SHARE or UNSHARE lands on the same mask, while distinct (access, deny) opens from one owner are still tracked separately.
Agreed -- that lines up with the invariant the other NLM handlers rely on, and it's the shape I'll use for v3. Concretely:
#define LOCKD_FSH_BIT(a, d) BIT(((a) << 2) | (d))
SHARE: share->s_held_mask |= LOCKD_FSH_BIT(access, mode);
UNSHARE: share->s_held_mask &= ~LOCKD_FSH_BIT(access, mode);s_access / s_mode are recomputed as the union of (i >> 2) / (i & 3) over the set bits, and the entry is freed once s_held_mask is zero. Both ops become an idempotent bit set/clear, so retransmits land on the same state.
Quoted 6 lines
The tradeoff is that two opens with the identical (access, deny) pair collapse to one bit, so a count of duplicate identical opens is lost. RFC 1813 defines SHARE and UNSHARE but does not require that repeated identical SHAREs from one owner survive an equal number of UNSHAREs [...]
Does that match your reading?
Yes. RFC 1813 is silent on repeat semantics for SHARE / UNSHARE, and section 4.5 explicitly treats non-idempotent requests as something a reply cache has to protect, which lockd doesn't run for NLM. So collapsing identical (access, deny) opens from one owner into a single bit stays inside the protocol, and it buys back the idempotency the other NLM handlers already assume.
Quoted 6 lines
The bottom line is that after more consideration, IMO the reference count approach is not going to work, in particular because NLM still operates on unreliable network transports like UDP. Consider the above direction not as a mandate but as a possible approach that might address the issue you've reported while avoiding introducing non-idempotency.
Adopting the held-mask direction. v3 shortly, as a new top-level thread.
Thanks,
Oscar
Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.