Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1762032AbcJ1WZB (ORCPT ); Fri, 28 Oct 2016 18:25:01 -0400 Received: from us-smtp-delivery-194.mimecast.com ([63.128.21.194]:45511 "EHLO us-smtp-delivery-194.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761960AbcJ1WZA (ORCPT ); Fri, 28 Oct 2016 18:25:00 -0400 From: Trond Myklebust To: Sebastian Andrzej Siewior CC: Schumaker Anna , List Linux NFS Mailing , List Linux Kernel Mailing , "tglx@linutronix.de" Subject: Re: [PATCH v2] NFSv4: replace seqcount_t with a seqlock_t Thread-Topic: [PATCH v2] NFSv4: replace seqcount_t with a seqlock_t Thread-Index: AQHSMV8ChsEAqeCJOEWHPT0r6Hlsi6C+cXSA Date: Fri, 28 Oct 2016 22:24:52 +0000 Message-ID: <747E2CCB-3D83-40FD-8B31-46AB5A5E8592@primarydata.com> References: <20161021164727.24485-1-bigeasy@linutronix.de> <20161028210511.k7hrsfxovti7gqtu@linutronix.de> In-Reply-To: <20161028210511.k7hrsfxovti7gqtu@linutronix.de> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [68.49.162.121] x-ms-office365-filtering-correlation-id: 1c8a3330-9da7-4585-98d0-08d3ff813d10 x-microsoft-exchange-diagnostics: 1;BN6PR11MB1571;7:6YcbpVbF65McQdvr1uw1UmJeFiqRnbWWv5BVjVstEMH14C4Jc6Fcg3JsEzyfv5wIqHRCIyNW5+4sjyguJnmZIfQlrSsDFGCCzs30q5J2gkuW0B+kceHtGTifiPFVLigYft6fLbYrfTaGTD/zgpfwvDqqNLR8mUWreBYvdRW/c7wD133nUXoZ2SVY3X+n5iexmYMA2gdAwIKGUYRxS9ape8FuxkLx1VlR/fH4aI9uh7ZHDlsQPzTwMrLLoxUnMsbdrz7FYsbkcUgcPQDnUxOl9KG7E8TrIFWOrOdj4qfHJ/kKwLFoLovqqkG/hpEQXFoZOz0V2EJFnUdn+xbYsEjVtiE1kdTzlN7SeXJFIN6QGb0=;20:EATtXL+SYy+PIzNMgWfZSjDNFI3bmIXf54n557e0OyZ3A5aDeru/J5PiBB0a45MFoMhnF2kRBq+bu3RWF39Du6A7iI0mV3ujP7qniOWtf9yrdWoKQNg5L2YbVNgXwmc4d+AOVAzNH97Y8cFiC5D0Oag2wAcyEc8kfZE2PXY4rdI= x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BN6PR11MB1571; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(158342451672863)(788757137089); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(6040176)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6042046)(6043046);SRVR:BN6PR11MB1571;BCL:0;PCL:0;RULEID:;SRVR:BN6PR11MB1571; x-forefront-prvs: 0109D382B0 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(6009001)(7916002)(199003)(24454002)(189002)(77096005)(3280700002)(8936002)(66066001)(54356999)(2900100001)(68736007)(50986999)(76176999)(575784001)(86362001)(36756003)(99286002)(101416001)(106356001)(5890100001)(105586002)(106116001)(81166006)(189998001)(4326007)(2950100002)(6916009)(81156014)(82746002)(2906002)(8676002)(102836003)(7736002)(7846002)(33656002)(122556002)(305945005)(5002640100001)(586003)(10400500002)(6116002)(19580405001)(19580395003)(92566002)(3660700001)(5660300001)(87936001)(97736004)(110136003)(83716003)(3846002)(21314002)(104396002);DIR:OUT;SFP:1102;SCL:1;SRVR:BN6PR11MB1571;H:BN6PR11MB1570.namprd11.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-ID: <7DE592D7CBA27942910C099430881F66@namprd11.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: primarydata.com X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Oct 2016 22:24:52.7371 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 03193ed6-8726-4bb3-a832-18ab0d28adb7 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR11MB1571 X-MC-Unique: OiCbsHJOOLax9TnhuPwoqg-1 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by mail.home.local id u9SMP8Ob018368 Content-Length: 7554 Lines: 174 > On Oct 28, 2016, at 17:05, Sebastian Andrzej Siewior wrote: > > The raw_write_seqcount_begin() in nfs4_reclaim_open_state() bugs me > because it maps to preempt_disable() in -RT which I can't have at this > point. So I took a look at the code. > It the lockdep part was removed in commit abbec2da13f0 ("NFS: Use > raw_write_seqcount_begin/end int nfs4_reclaim_open_state") because > lockdep complained. The whole seqcount thing was introduced in commit > c137afabe330 ("NFSv4: Allow the state manager to mark an open_owner as > being recovered"). > I don't understand how it is possible that we don't end up with two > writers for the same ressource because the `sp->so_lock' lock is dropped > is soon in the list_for_each_entry() loop. It might be the > test_and_clear_bit() check in nfs4_do_reclaim() but it might clear one > bit on each iteration so I *think* we could have two invocations of the > same struct nfs4_state_owner in nfs4_reclaim_open_state(). > So there is that. > > But back to the list_for_each_entry() macro. > It seems that this lock protects the ->so_states list among other > atomic_t & flags members. So at the begin of the loop we inc ->count > ensuring that this field is not removed while we use it. So we drop the > ->so_lock loc during the loop it seems. And after nfs4_reclaim_locks() > invocation we nfs4_put_open_state() and grab the ->so_lock again. So if > we were the last user of this struct and we remove it, then the > following list_next_entry() invocation is a use-after-free. Even if we > use list_for_each_entry_safe() there is no guarantee that the following > member is still valid because it might have been removed by another > writer, right? > So there is this. > > However to address my initial problem I have here a patch :) So it uses > a seqlock_t which ensures that there is only one writer at a time. So it > should be basically what is happening now plus a tiny tiny tiny lock > plus lockdep coverage. I tried to this myself but I don't manage to get > into this code path at all so I might be doing something wrong. > > Could you please check if this patch is working for you and whether my > list_for_each_entry() observation is correct or not? > > v1…v2: write_seqlock() disables preemption and some function need it > (thread_run(), non-GFP_ATOMIC memory alloction()). We don't want > preemption enabled because a preempted writer would stall the reader > spinning. This is a duct tape mutex. Maybe the seqlock should go. > > Signed-off-by: Sebastian Andrzej Siewior > --- > fs/nfs/delegation.c | 4 ++-- > fs/nfs/nfs4_fs.h | 3 ++- > fs/nfs/nfs4proc.c | 4 ++-- > fs/nfs/nfs4state.c | 23 +++++++++++++++++------ > 4 files changed, 23 insertions(+), 11 deletions(-) > > diff --git a/fs/nfs/delegation.c b/fs/nfs/delegation.c > index dff600ae0d74..d726d2e09353 100644 > --- a/fs/nfs/delegation.c > +++ b/fs/nfs/delegation.c > @@ -150,11 +150,11 @@ static int nfs_delegation_claim_opens(struct inode *inode, > sp = state->owner; > /* Block nfs4_proc_unlck */ > mutex_lock(&sp->so_delegreturn_mutex); > - seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); > + seq = read_seqbegin(&sp->so_reclaim_seqlock); > err = nfs4_open_delegation_recall(ctx, state, stateid, type); > if (!err) > err = nfs_delegation_claim_locks(ctx, state, stateid); > - if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) > + if (!err && read_seqretry(&sp->so_reclaim_seqlock, seq)) > err = -EAGAIN; > mutex_unlock(&sp->so_delegreturn_mutex); > put_nfs_open_context(ctx); > diff --git a/fs/nfs/nfs4_fs.h b/fs/nfs/nfs4_fs.h > index 9b3a82abab07..2fee1a2e8b57 100644 > --- a/fs/nfs/nfs4_fs.h > +++ b/fs/nfs/nfs4_fs.h > @@ -111,7 +111,8 @@ struct nfs4_state_owner { > unsigned long so_flags; > struct list_head so_states; > struct nfs_seqid_counter so_seqid; > - seqcount_t so_reclaim_seqcount; > + seqlock_t so_reclaim_seqlock; > + struct mutex so_reclaim_seqlock_mutex; > struct mutex so_delegreturn_mutex; > }; > > diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c > index 7897826d7c51..9b9d53cd85f9 100644 > --- a/fs/nfs/nfs4proc.c > +++ b/fs/nfs/nfs4proc.c > @@ -2685,7 +2685,7 @@ static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, > unsigned int seq; > int ret; > > - seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); > + seq = raw_seqcount_begin(&sp->so_reclaim_seqlock.seqcount); > > ret = _nfs4_proc_open(opendata); > if (ret != 0) > @@ -2723,7 +2723,7 @@ static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, > ctx->state = state; > if (d_inode(dentry) == state->inode) { > nfs_inode_attach_open_context(ctx); > - if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) > + if (read_seqretry(&sp->so_reclaim_seqlock, seq)) > nfs4_schedule_stateid_recovery(server, state); > } > out: > diff --git a/fs/nfs/nfs4state.c b/fs/nfs/nfs4state.c > index 5f4281ec5f72..a442d9867942 100644 > --- a/fs/nfs/nfs4state.c > +++ b/fs/nfs/nfs4state.c > @@ -488,7 +488,8 @@ nfs4_alloc_state_owner(struct nfs_server *server, > nfs4_init_seqid_counter(&sp->so_seqid); > atomic_set(&sp->so_count, 1); > INIT_LIST_HEAD(&sp->so_lru); > - seqcount_init(&sp->so_reclaim_seqcount); > + seqlock_init(&sp->so_reclaim_seqlock); > + mutex_init(&sp->so_reclaim_seqlock_mutex); > mutex_init(&sp->so_delegreturn_mutex); > return sp; > } > @@ -1497,8 +1498,18 @@ static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs > * recovering after a network partition or a reboot from a > * server that doesn't support a grace period. > */ > + /* > + * XXX > + * This mutex is wrong. It protects against multiple writer. However There is only 1 recovery thread per client/server pair, which is why we know there is only a single writer. No need for a mutex here. > + * write_seqlock() should have been used for this task. This would avoid > + * preemption while the seqlock is held which is good because the writer > + * shouldn't be preempted as it would let the reader spin for no good Recovery involves sending RPC calls to a server. There is no avoiding preemption if we have to recover state. The point of the sequence counter is to signal to processes that may have raced with this recovery thread that they may need to replay their locks. > + * reason. There are a few memory allocations and kthread_run() so we > + * have this mutex now. > + */ > + mutex_lock(&sp->so_reclaim_seqlock_mutex); > + write_seqcount_begin(&sp->so_reclaim_seqlock.seqcount); > spin_lock(&sp->so_lock); > - raw_write_seqcount_begin(&sp->so_reclaim_seqcount); > restart: > list_for_each_entry(state, &sp->so_states, open_states) { > if (!test_and_clear_bit(ops->state_flag_bit, &state->flags)) > @@ -1566,14 +1577,14 @@ static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs > spin_lock(&sp->so_lock); > goto restart; > } > - raw_write_seqcount_end(&sp->so_reclaim_seqcount); > spin_unlock(&sp->so_lock); > + write_seqcount_end(&sp->so_reclaim_seqlock.seqcount); This will reintroduce lockdep checking. We don’t need or want that... > + mutex_unlock(&sp->so_reclaim_seqlock_mutex); > return 0; > out_err: > nfs4_put_open_state(state); > - spin_lock(&sp->so_lock); > - raw_write_seqcount_end(&sp->so_reclaim_seqcount); > - spin_unlock(&sp->so_lock); > + write_seqcount_end(&sp->so_reclaim_seqlock.seqcount); > + mutex_unlock(&sp->so_reclaim_seqlock_mutex); > return status; > } > > -- > 2.10.1 >