From: Kinglong Mee <kinglongmee-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Al Viro <viro-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org>
Cc: "J. Bruce Fields"
<bfields-uC3wQj2KruNg9hUCZPvPmw@public.gmane.org>,
"linux-nfs-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
<linux-nfs-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
NeilBrown <neilb-l3A5Bk7waGM@public.gmane.org>,
Trond Myklebust
<trond.myklebust-7I+n7zu2hftEKMMhf/gKZA@public.gmane.org>,
kinglongmee-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Subject: Re: [PATCH 10/10 v6] nfsd: Allows user un-mounting filesystem where nfsd exports base on
Date: Thu, 02 Jul 2015 23:17:56 +0800 [thread overview]
Message-ID: <55955624.80503@gmail.com> (raw)
In-Reply-To: <20150701054751.GB17109-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org>
On 7/1/2015 13:47, Al Viro wrote:
> On Thu, Jun 25, 2015 at 10:37:14PM +0800, Kinglong Mee wrote:
>> +static void expkey_validate(struct cache_head *h)
>> +{
>> + struct svc_expkey *key = container_of(h, struct svc_expkey, h);
>> +
>> + if (!test_bit(CACHE_VALID, &key->h.flags) ||
>> + test_bit(CACHE_NEGATIVE, &key->h.flags))
>> + return;
>> +
>> + if (atomic_read(&h->ref.refcount) == 1) {
>> + mutex_lock(&key->ek_mutex);
>
> ... followed by kref_get(&h->ref) in caller
Got it.
>
>> + if (atomic_read(&h->ref.refcount) == 2) {
>> + mutex_lock(&key->ek_mutex);
>
> ... followed by kref_put() in caller.
No, must before kref_put.
If kref_put() to zero will free the structure.
>
> Suppose two threads call cache_get() at the same time. Refcount is 1.
> Depending on the timing you get either one or both grabbing vfsmount
> references. Whichever variant matches the one you want, there is no way
> to tell one from another afterwards and they *do* differ in the resulting
> vfsmount refcount changes.
>
> Similar to that, suppose the refcount is 3 and two threads call cache_put()
> at the same time. If one of them gets through the entire thing (including
> kref_put()) before the other gets to atomic_read(), you get the second
> see refcount 2 and do that mntput(). If not, _nobody_ will ever see refcount
> 2 and mntput() is not done.
>
> How can that code possibly be correct? This kind of splitting atomic_read
> from increment/decrement (and slapping a sleeping operation in between,
> no less) is basically never right. Not unless you have everything serialized
> on the outside and do not need the atomic in the first place, which doesn't
> seem to be the case here.
For protect the reference, maybe I will implements a couple of get_ref/put_ref
as kref_get/kref_put.
+static void expkey_get_ref(struct cache_head *h)
+{
+ struct svc_expkey *key = container_of(h, struct svc_expkey, h);
+
+ mutex_lock(&key->ref_mutex);
+ kref_get(&h->ref);
+
+ if (!test_bit(CACHE_VALID, &key->h.flags) ||
+ test_bit(CACHE_NEGATIVE, &key->h.flags))
+ goto out;
+
+ if (atomic_read(&h->ref.refcount) == 2) {
+ if (legitimize_mntget(key->ek_path.mnt) == NULL) {
+ printk(KERN_WARNING "%s: Get mnt for %pd2 failed!\n",
+ __func__, key->ek_path.dentry);
+ set_bit(CACHE_NEGATIVE, &h->flags);
+ } else
+ key->ek_mnt_ref = true;
+ }
+out:
+ mutex_unlock(&key->ref_mutex);
+}
+
+static void expkey_put_ref(struct cache_head *h)
+{
+ struct svc_expkey *key = container_of(h, struct svc_expkey, h);
+
+ mutex_lock(&key->ref_mutex);
+ if (key->ek_mnt_ref && (atomic_read(&h->ref.refcount) == 2)) {
+ mntput(key->ek_path.mnt);
+ key->ek_mnt_ref = false;
+ }
+
+ if (unlikely(!atomic_dec_and_test(&h->ref.refcount))) {
+ mutex_unlock(&key->ref_mutex);
+ return ;
+ }
+
+ expkey_put(&h->ref);
+}
+
Code for nfsd exports cache is similar as expkey.
thanks,
Kinglong Mee
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
prev parent reply other threads:[~2015-07-02 15:17 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-25 14:17 [PATCH 00/10 v6] NFSD: Pin to vfsmount for nfsd exports cache Kinglong Mee
[not found] ` <558C0D6A.9050104-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-06-25 14:18 ` [PATCH 01/10 v6] fs_pin: Initialize value for fs_pin explicitly Kinglong Mee
2015-06-25 14:25 ` [PATCH 05/10 v6] sunrpc: Store cache_detail in seq_file's private directly Kinglong Mee
2015-06-25 14:34 ` [PATCH 08/10] sunrpc: New helper cache_delete_entry for deleting cache_head directly Kinglong Mee
2015-06-25 14:36 ` [PATCH 09/10 v6] sunrpc: Support validate/invalidate for reference change in cache_detail Kinglong Mee
2015-06-25 14:19 ` [PATCH 02/10 v6] fs_pin: Export functions for specific filesystem Kinglong Mee
2015-06-25 14:19 ` [PATCH 03/10 v6] path: New helpers path_get_pin/path_put_unpin for path pin Kinglong Mee
2015-06-25 14:21 ` [PATCH 04/10 v6] fs: New helper legitimize_mntget() for getting a legitimize mnt Kinglong Mee
2015-06-25 14:27 ` [PATCH 06/10 v6] sunrpc/nfsd: Remove redundant code by exports seq_operations functions Kinglong Mee
2015-06-25 14:29 ` [PATCH 07/10 v6] sunrpc: Switch to using list_head instead single list Kinglong Mee
2015-06-25 14:37 ` [PATCH 10/10 v6] nfsd: Allows user un-mounting filesystem where nfsd exports base on Kinglong Mee
2015-07-01 5:47 ` Al Viro
[not found] ` <20150701054751.GB17109-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org>
2015-07-02 15:17 ` Kinglong Mee [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=55955624.80503@gmail.com \
--to=kinglongmee-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
--cc=bfields-uC3wQj2KruNg9hUCZPvPmw@public.gmane.org \
--cc=linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-nfs-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=neilb-l3A5Bk7waGM@public.gmane.org \
--cc=trond.myklebust-7I+n7zu2hftEKMMhf/gKZA@public.gmane.org \
--cc=viro-3bDd1+5oDREiFSDQTTA3OLVCufUGDwFn@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).