From patchwork Fri Dec 17 06:49:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vasily Averin X-Patchwork-Id: 12683811 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6606C433F5 for ; Fri, 17 Dec 2021 06:49:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232239AbhLQGto (ORCPT ); Fri, 17 Dec 2021 01:49:44 -0500 Received: from mail-vi1eur05on2139.outbound.protection.outlook.com ([40.107.21.139]:10720 "EHLO EUR05-VI1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S229757AbhLQGtn (ORCPT ); Fri, 17 Dec 2021 01:49:43 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fdxlREcOmjzEQDBwOynC4tC60RlhBdRSNd3lVvJmZDiPmZgepG3tGnR39ZA4Ws3A0cuobsRi7t2TM4NYE9EJ726ZGgdARgRC4MhzYUDQx1/VusDsvWFIXWeos5teuB9ubxwMnMQIf6KagpNf02d9BcfaTtlmVUz/s3tN0tjCeL4zQPa76HD43N7Gf9XaATUnWTE8S/nvCmLinCdreMtuKGPjRj8F+uMNyaO4llgYfeDNPcmHxoBvzh9SwNuysnctIb+8ofVYJ+1hvx0+66mG9p3DO7qBhjuImAJZOPcXiUBsDSTnt4Zs9hyzejFk13g8s9BvPhnWiD7PPi+dkK9+Ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=LcQfSMXMXaNGvv84bfkYV9JzK7qfWrQQAwsVRZEwsaU=; b=KaST1VShAdOiHtqARTFnyL+9xx3akWhlqlAjDCv5gOEOWlVs0pzaiRUhq5BJ6g//KdPz1fWyHHyb9SqlYfLgNuJqy25qpmnFpuqQuQEWCo7iqpTec8fWaEqovvRJG7CsRkltRgSDspJl9/XPxZvMXEucQQG77wgUNrY6SaKuwRon9qmjJ6Dia2l+Ws/+epQ3tvvPf5oaeyS6nP+pbrwQa9GK9INHJX0oYD4ZE7cbfgJrHE4aywYTHMdXfabppGfNEQBrUu1lAE/+3nQh1038UyB2kpK1C6iC4wSftiS5YiURJuDveKDe/2JewRr8cIhk37dZJCCOZMsKKlOwQr//AQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=virtuozzo.com; dmarc=pass action=none header.from=virtuozzo.com; dkim=pass header.d=virtuozzo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LcQfSMXMXaNGvv84bfkYV9JzK7qfWrQQAwsVRZEwsaU=; b=BhoV0EabcIzh293MzQtaKq/SWm+RWjUFucYgXln+ozP6LWJDpKMhTHiONKywy1dt2X5JUS1FJsJ9elOo+sEbtYcdOSOeka3DFHH+mRCPBfYTkHrkVVgx3XZti/OnSFSWMmsnILxp8Gm1ukJC0J1hs0lTflHUz2AL7GBbnm8UoWE= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=virtuozzo.com; Received: from DB9PR08MB6619.eurprd08.prod.outlook.com (2603:10a6:10:257::21) by DBBPR08MB6108.eurprd08.prod.outlook.com (2603:10a6:10:1f4::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.12; Fri, 17 Dec 2021 06:49:41 +0000 Received: from DB9PR08MB6619.eurprd08.prod.outlook.com ([fe80::347f:d385:ec53:75aa]) by DB9PR08MB6619.eurprd08.prod.outlook.com ([fe80::347f:d385:ec53:75aa%7]) with mapi id 15.20.4801.015; Fri, 17 Dec 2021 06:49:41 +0000 From: Vasily Averin Subject: [PATCH] nfsd4: add refcount for nfsd4_blocked_lock To: Bruce Fields , Chuck Lever Cc: Konstantin Khorenko , "Denis V. Lunev" , Cyrill Gorcunov , Jeff Layton , kernel@openvz.org, linux-nfs@vger.kernel.org References: Message-ID: <943cfe47-d48a-4a55-3738-e93370160508@virtuozzo.com> Date: Fri, 17 Dec 2021 09:49:39 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 In-Reply-To: Content-Language: en-US X-ClientProxiedBy: AS9PR06CA0231.eurprd06.prod.outlook.com (2603:10a6:20b:45e::28) To DB9PR08MB6619.eurprd08.prod.outlook.com (2603:10a6:10:257::21) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ba5d04e5-4a5f-41c8-f967-08d9c1296690 X-MS-TrafficTypeDiagnostic: DBBPR08MB6108:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ihTEA3HFBGHWOwz2LcKsvSf2KFGKmuWpsntxigQY3M/e7v4oBdK/OMqEBpH0i65woUV0kVYWrrvyy3RlOauRkcYNEFXs8ZreIReH/p73uhCtNU/ZPTmcIngrFFwwF9Gsb1vB7XTOGzaAwIHTCSGNegWnPh4mqNmxNfXlNP7qm+nI+T30oRpg3BVsooUWpq/PnagFIWgTTg3GGkJFaVq89Z0MfUbLFpjxLXWWX2zdyAPtWP9uvWcAw3rhP+nePA4dv5lsfkOvMO/SUcJb4YxjaGEDQErM9gqsnsET+n2D5vsnHNSsZuu04M8yW0L9CArEZp1qtFZQkrNEVtvwsCLX2+FjgW0MpbvJewZAgucOUDw5VswyndbHxY+wtk+gGWuJND2RFcw4ecZkqxeq8u8JCOzH9Z+54lLxxWfOL2OM2Qsvy7wDagWrGEzsuTooag9/6UR3hxJwP4/CzW4eLH/eAQMrsfDv4hWV6VOtAfCFRdDeekSN1G45CoMVGjDB7isKMqHOYewbSRbC72Tmm8Q8tAeTkmlkbQMIKtEyvdZP8LZ0GOCMuEvbub3vdrCgVZfFkx/0b0r0u1elbiiaxB3hwgBTaJfIHBTAg6c1Lba9VyQ8yFqAhJCS4spfoQImbLPLJpNEDYtn85+38QOCIVWrxbbnkBNjhEOLN1HaGvWnP7/fy9t9SUU12Nc2U7o7sGtB9uMC7bg5Mc7UVO2+kplvUkaGrVZBFe1B0GDq1zo72SvbksN8x8s6FU/fhFCY+T5XMCv3PGYP0XzWq2Ap9lR6YA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DB9PR08MB6619.eurprd08.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(5660300002)(6486002)(6512007)(2616005)(316002)(66946007)(66476007)(66556008)(38100700002)(8936002)(38350700002)(31696002)(4326008)(8676002)(2906002)(508600001)(83380400001)(31686004)(86362001)(36756003)(26005)(52116002)(110136005)(186003)(6506007)(54906003)(43740500002)(45980500001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?+SfthC2Q+diSn6LvQEDEHjUs7IfM?= =?utf-8?q?IMC21JaS+fsW7DLqJoZM6qJwT6U0PZ5m8Db0gOXswc5TwqoFC7Pgf1ivoLErPZS8P?= =?utf-8?q?XN9+O2ZDrLKgL19XcxitxpMqhRdu0T+5Ly11nL+Fq8kc1LT+y4ik1P5EZDR52xDc9?= =?utf-8?q?N71/1kE0+nY1TxA4T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/KTlE0aQSndqU826xhKNa8WVQ+8fFxeipRH/OJyCulT6rr2k?= =?utf-8?q?LLYsJjrrDYf1UkxeuXXXAtkW0OPilVNJp4Nu0ahS1wNWNm5pIBRUKAamOosKtA2A9?= =?utf-8?q?WagobRGys0+FRAsbGEyDibXF4x350/u2hyU7z4z9T+YIpZDJv1DxWwBuNZo9z1teJ?= =?utf-8?q?lTNDzPyyIAzF8nbA2euJgsj+f2Q3HLZo7W4nKFbLRf0ZZUH2g4ArMnsKP0RBhpEkT?= =?utf-8?q?e58hbtj+NpzNtwT0hOMuvF+27/J2HwAetQVLOnqK1iL9EEysV4I+2UOXLrZbZGNYS?= =?utf-8?q?d10W+iQrNxirqkNv6IXdCdglEeD+WehiEasNN2UTGP0ai1ZPhYG5tVgzshe5tEnhW?= =?utf-8?q?bWpYQfVhG+nV71hipGcpZMLmmaVjlXwOp4P6uRnxJGy/mluKQYOM90tIedWgmbBkZ?= =?utf-8?q?45pp4XFXcPtWkvIrDn3RlATbTEMaegBBt42dqrjCMqB7/G2srdVz/naozbLqwT4Q7?= =?utf-8?q?e7vN+V1b9pnbHi+3hk5d7rv0PGOoZsWLU3y54hwZpomLNSf2L96LhUk=3D?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: ba5d04e5-4a5f-41c8-f967-08d9c1296690 X-MS-Exchange-CrossTenant-AuthSource: DB9PR08MB6619.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2021 06:49:40.9188 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: cz8SVZO99iMVczn5C4/Dg/sO5nK3TATjqDevY3xmwez73kBsY3UiEQFKYFnts/uy1i3K3rUCc9qA4Sb4iAiL0g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBBPR08MB6108 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org nbl allocated in nfsd4_lock can be released by a several ways: directly in nfsd4_lock(), via nfs4_laundromat(), via another nfs command RELEASE_LOCKOWNER or via nfsd4_callback. This structure should be refcounted to be used and released correctly in all these cases. Refcount is initialized to 1 during allocation and is incremented when nbl is added into nbl_list/nbl_lru lists. Usually nbl is linked into both lists together, so only one refcount is used for both lists. However nfsd4_lock() should keep in mind that nbl can be present in one of lists only. This can happen if nbl was handled already by nfs4_laundromat/nfsd4_callback/etc. Refcount is decremented if vfs_lock_file() returns FILE_LOCK_DEFERRED, because nbl can be handled already by nfs4_laundromat/nfsd4_callback/etc. Refcount is not changed in find_blocked_lock() because of it reuses counter released after removing nbl from lists. Signed-off-by: Vasily Averin Reviewed-by: Jeff Layton --- fs/nfsd/nfs4state.c | 25 ++++++++++++++++++++++--- fs/nfsd/state.h | 1 + 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index d75e1235c4f5..b74f36e9901c 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -246,6 +246,7 @@ find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { if (fh_match(fh, &cur->nbl_fh)) { list_del_init(&cur->nbl_list); + WARN_ON(list_empty(&cur->nbl_lru)); list_del_init(&cur->nbl_lru); found = cur; break; @@ -271,6 +272,7 @@ find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, INIT_LIST_HEAD(&nbl->nbl_lru); fh_copy_shallow(&nbl->nbl_fh, fh); locks_init_lock(&nbl->nbl_lock); + kref_init(&nbl->nbl_kref); nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, &nfsd4_cb_notify_lock_ops, NFSPROC4_CLNT_CB_NOTIFY_LOCK); @@ -279,12 +281,21 @@ find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, return nbl; } +static void +free_nbl(struct kref *kref) +{ + struct nfsd4_blocked_lock *nbl; + + nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref); + kfree(nbl); +} + static void free_blocked_lock(struct nfsd4_blocked_lock *nbl) { locks_delete_block(&nbl->nbl_lock); locks_release_private(&nbl->nbl_lock); - kfree(nbl); + kref_put(&nbl->nbl_kref, free_nbl); } static void @@ -302,6 +313,7 @@ remove_blocked_locks(struct nfs4_lockowner *lo) struct nfsd4_blocked_lock, nbl_list); list_del_init(&nbl->nbl_list); + WARN_ON(list_empty(&nbl->nbl_lru)); list_move(&nbl->nbl_lru, &reaplist); } spin_unlock(&nn->blocked_locks_lock); @@ -6976,6 +6988,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, spin_lock(&nn->blocked_locks_lock); list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); + kref_get(&nbl->nbl_kref); spin_unlock(&nn->blocked_locks_lock); } @@ -6988,6 +7001,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, nn->somebody_reclaimed = true; break; case FILE_LOCK_DEFERRED: + kref_put(&nbl->nbl_kref, free_nbl); nbl = NULL; fallthrough; case -EAGAIN: /* conflock holds conflicting lock */ @@ -7008,8 +7022,13 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, /* dequeue it if we queued it before */ if (fl_flags & FL_SLEEP) { spin_lock(&nn->blocked_locks_lock); - list_del_init(&nbl->nbl_list); - list_del_init(&nbl->nbl_lru); + if (!list_empty(&nbl->nbl_list) && + !list_empty(&nbl->nbl_lru)) { + list_del_init(&nbl->nbl_list); + list_del_init(&nbl->nbl_lru); + kref_put(&nbl->nbl_kref, free_nbl); + } + /* nbl can use one of lists to be linked to reaplist */ spin_unlock(&nn->blocked_locks_lock); } free_blocked_lock(nbl); diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h index e73bdbb1634a..ab61dc102300 100644 --- a/fs/nfsd/state.h +++ b/fs/nfsd/state.h @@ -629,6 +629,7 @@ struct nfsd4_blocked_lock { struct file_lock nbl_lock; struct knfsd_fh nbl_fh; struct nfsd4_callback nbl_cb; + struct kref nbl_kref; }; struct nfsd4_compound_state;