From patchwork Thu Apr 14 22:28:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 562197 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 5BD12C433EF for ; Thu, 14 Apr 2022 22:29:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232190AbiDNWcI (ORCPT ); Thu, 14 Apr 2022 18:32:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347047AbiDNWcH (ORCPT ); Thu, 14 Apr 2022 18:32:07 -0400 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 305391EC6F; Thu, 14 Apr 2022 15:29:41 -0700 (PDT) Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 23EJ07dl018439; Thu, 14 Apr 2022 22:29:39 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=IEndfXy9jiOzWuwTkMw4OXx/3CbSemHQeUkLPmGuOhc=; b=nYGjQgaKEbVGI7PYBS0b8Si+Y6hA9wC7fIbt1C7iAWheUq08kNv/5fbyXf0vmxlCjvw5 3lMNtwr73Xx8qfld8ZDvvE0D0LRQygimicgCSxjKRgW/r1n01n7I5Znet2ycOHad2sqd Fx19vTU6VN4u8yEJCsq18jfb0PfcJyMYSbDKv42jPnsR8bf3IsMFPcIryWblgxfPMiSW YC4PaiD9wjMh4VZqzfW81DpqIyImNN900/6fVxAITaUENKyNipiewvMxnpaLa5mFt65L 8vkts1uAdXRrS7fxAfSrU5F2FkWoUgCQd0WqgqAWMnoXBimN0ObXaD4p5oZ9ZRcR4Q/+ EA== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com with ESMTP id 3fb0r1p62f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Apr 2022 22:29:39 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 23EMHhD1014987; Thu, 14 Apr 2022 22:29:38 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2100.outbound.protection.outlook.com [104.47.55.100]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3fb0k5uc7w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Apr 2022 22:29:38 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cNaZikEaXtz9t0L7MWwbWFth8ccFCGlLDMz7rwiiPCmuPrFrFsL5WxHWi/v7JCfc0t7WT6Nd6d/8BoNFvul2KvcxP+iSXm/5ln/Ol4AOKQN/TMu+r0Ts5UmHQJnx69Q8GljNSjUt9mtcfExpG3F16ormlNuXFPWpnhcswZRYeDVLQGMMJYUftc4nCYfYCBwqlg5GYaD/y0H0sB+bk7g6AMb/F+iF/0hBYUstSEBVcjv/L7ld4XLEuxgUkejzDaIkqWI1K9LQdNdBwhfUvVQJP6TxLyLnK5wRC+Aq98ueD2e81syD++JrzclHnMcDGAA8k3Vovwq0lJwZR1kxcEYuAw== 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=IEndfXy9jiOzWuwTkMw4OXx/3CbSemHQeUkLPmGuOhc=; b=BX7VFed/DqNn6sfyJb7kPYi+83GyNVaztvIsRVIHZHEfBxwPXY/UqigcR0+fEiZFecEcD73RHIKupvMmDjDSfs1vbfJv9rv583LNOhZlFlA7Yb42DZ4vE+onUJv9BlioL02G3swuCkAS+2NsLLTft7ccoldLV5H5tJI3TCdswAZd0Xr38Ept/zWprJL+BIBuO4KoNVN85nnfbEThH/r2PqseIoVyjvwa6npT74rA21IbeuM655M1jPlVm0ysqOIVxDbhghhJzcqghVORn1n6bp9hIyURHrKxxDym+mnbYXTpKzodYQCagLiyu8+zDALO1Npg2SA/96eJIYU25+YN6g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IEndfXy9jiOzWuwTkMw4OXx/3CbSemHQeUkLPmGuOhc=; b=MoihSEWJ2RBGJdzcBMOfYbluCMJAIeck0zs5H5TvlVnlk4e/qkjcBlplPqxwCC8SFSjdRHdJlhGCLByZpNGu62Qy+SI5n6xF10e+MCI8G0/y5Edm04iUj6vyy72F1dZk9Wve0gz9K1Cy4+vEvfnStdftKkmfCRuzFgqjijWXO7M= Received: from PH0PR10MB5706.namprd10.prod.outlook.com (2603:10b6:510:148::10) by MWHPR1001MB2094.namprd10.prod.outlook.com (2603:10b6:301:2b::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5164.18; Thu, 14 Apr 2022 22:29:36 +0000 Received: from PH0PR10MB5706.namprd10.prod.outlook.com ([fe80::d414:7654:e1b8:4306]) by PH0PR10MB5706.namprd10.prod.outlook.com ([fe80::d414:7654:e1b8:4306%7]) with mapi id 15.20.5144.029; Thu, 14 Apr 2022 22:29:36 +0000 From: Anand Jain To: stable@vger.kernel.org Cc: linux-btrfs@vger.kernel.org, Andreas Gruenbacher , Anand Jain Subject: [PATCH v3 05/18 stable-5.15.y] gfs2: Clean up function may_grant Date: Fri, 15 Apr 2022 06:28:43 +0800 Message-Id: <16061e1d0b15ee024905913510b9569e0c5011b4.1649951733.git.anand.jain@oracle.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: References: X-ClientProxiedBy: TYAPR01CA0095.jpnprd01.prod.outlook.com (2603:1096:404:2c::35) To PH0PR10MB5706.namprd10.prod.outlook.com (2603:10b6:510:148::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 68555fc4-40eb-45a7-8a7a-08da1e664199 X-MS-TrafficTypeDiagnostic: MWHPR1001MB2094:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 19PnDl7cKhOJ9HieWz2wCjVemewqfdB5cFEF6f84Zfzm8jYHYOAmUIi1nhjNFhX7cfh05kZWmlPc9i3TaDLUuCx2LyNF+uyPZCTVwtvLy6N+rxThaDbeX8yELnGGxUVH87+FAYpW+VcM3OlVERjGig1+FUaJMkirrhYRbUv5xYD+3w+kCP+M1V6kzyeWouqwRwCGwPJuzEG0xxmMlHCMpa9hcfn1ANA8F648bns1yF7ktxjcb4bEdmuRU1thx9cYFQlhOyqgSXMcKIWCUsLDzicovOVjAUkBBzGVS35+ivuzgJHyunnxWUtwbN1Z/IeimRDSw6rlhZaIZFzYUlDMDGv8TfYFudmdKBy4dZ4iO0+5VEg2CeOj0WcvqrXuspK81MZl6HVInz4zdjKicVZHQXSZb+VPBOh/UFL3ePsv5oXe7zCnmPC905k2HhwxiOMTtHYbrlvadbkRlyLsONCOm+KpkroORsNIxjKnS54N4jPJ62unErEbNlRdxXAbdu3vvCCh/gnxd2J2ykM/RFbo+ilRRgXVbiE/Klyte5ZfSHMp1c/gGkADYNp12YKP3gfm+danjgNWjmgbVmzw9QnSK0tBrirdNDLVmtCtb2oGZoVotUz4a3xvACdXvs2FppVradxmAqXAGZxocpZNFq8MBQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PH0PR10MB5706.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(36756003)(6486002)(2906002)(86362001)(44832011)(508600001)(6666004)(6506007)(6512007)(5660300002)(8936002)(54906003)(316002)(186003)(6916009)(66476007)(83380400001)(66946007)(66556008)(4326008)(38100700002)(2616005)(107886003)(8676002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TG83qMpooXyixME9o7dFQTMby5Bz8rPAX0Spwmo4uodFa2DkKV/1Y4KhZ5KdcBCPqwmOXTqbFSsTY8xFTsFbzLW4MhJsrPswSO4kJLy6orF55jFR/bhczZxJLPQDvRdgtObnkNEqM6TelWitmvdQ58sLt8lfbX9DHbzduAnGdJvShST7PRuE7kJQTfLxnQ4duViYsBHAjIQVjjIX0GTQVxhKLC4qoKvfXPI4NL3qOFCa8cJYhY9Q3TcatlAl9LEqhvHFf3mNsxwXZ0DGYBMeBXOBGeaAMS26r9NzNRpFvjHxrtC/I9VfIRNUak9RTZa72NzMEk6OaBhc86tABVcmJy0rNMAuy1MNLVpBPqa4QFdYPqdDpiW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zyzfuRcPGJeaP40Fdc6kfhZezBcbJGSD14cOmmMECiBFRXkCn9b8eJ5+ry9A7DiJsOmn7nm52fCptidZ5cRmHcrGZYfIAaK+FyFjjCriIg1tG239fAKSPmEzOcpOIDwCTiTtzQ0DwdugFSEUQqMOAroZHNP6ZmhfM9D8KZIFNtQV+quxTnYZyZua8T/d9+nrGVVLoNDEOKmknVK9n6gfTKLiN4WzPzzLTzEVlET2b9T5YJQT/BzkM30NpXa79xiNT6p92gXVfmAZ5Zx4UyFvrDfZZbyuySNq+vFnzF66/aHbgFpNgAGoCfu62olWRChqTRisI6Ld/DeS2Qn1L X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 68555fc4-40eb-45a7-8a7a-08da1e664199 X-MS-Exchange-CrossTenant-AuthSource: PH0PR10MB5706.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Apr 2022 22:29:36.4300 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9nvW1PXqRBeF5lbxOLma+a0F8M3wOqJ6cveJltK9VxGVhOfFWQqH6TSokCEvrYpenHxLxejWGaY40OKo/J8D9Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR1001MB2094 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486, 18.0.858 definitions=2022-04-14_07:2022-04-14, 2022-04-14 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 adultscore=0 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204140116 X-Proofpoint-GUID: kJIJatnOFWpiZiyPm2kIGt_XqVulqQQ4 X-Proofpoint-ORIG-GUID: kJIJatnOFWpiZiyPm2kIGt_XqVulqQQ4 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Andreas Gruenbacher commit 6144464937fe1e6135b13a30502a339d549bf093 upstream Pass the first current glock holder into function may_grant and deobfuscate the logic there. While at it, switch from BUG_ON to GLOCK_BUG_ON in may_grant. To make that build cleanly, de-constify the may_grant arguments. We're now using function find_first_holder in do_promote, so move the function's definition above do_promote. Signed-off-by: Andreas Gruenbacher Signed-off-by: Anand Jain --- fs/gfs2/glock.c | 119 ++++++++++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 50 deletions(-) diff --git a/fs/gfs2/glock.c b/fs/gfs2/glock.c index 02cd0ae98208..8f30ad956270 100644 --- a/fs/gfs2/glock.c +++ b/fs/gfs2/glock.c @@ -301,46 +301,59 @@ void gfs2_glock_put(struct gfs2_glock *gl) } /** - * may_grant - check if its ok to grant a new lock + * may_grant - check if it's ok to grant a new lock * @gl: The glock + * @current_gh: One of the current holders of @gl * @gh: The lock request which we wish to grant * - * Returns: true if its ok to grant the lock + * With our current compatibility rules, if a glock has one or more active + * holders (HIF_HOLDER flag set), any of those holders can be passed in as + * @current_gh; they are all the same as far as compatibility with the new @gh + * goes. + * + * Returns true if it's ok to grant the lock. */ -static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh) -{ - const struct gfs2_holder *gh_head = list_first_entry(&gl->gl_holders, const struct gfs2_holder, gh_list); +static inline bool may_grant(struct gfs2_glock *gl, + struct gfs2_holder *current_gh, + struct gfs2_holder *gh) +{ + if (current_gh) { + GLOCK_BUG_ON(gl, !test_bit(HIF_HOLDER, ¤t_gh->gh_iflags)); + + switch(current_gh->gh_state) { + case LM_ST_EXCLUSIVE: + /* + * Here we make a special exception to grant holders + * who agree to share the EX lock with other holders + * who also have the bit set. If the original holder + * has the LM_FLAG_NODE_SCOPE bit set, we grant more + * holders with the bit set. + */ + return gh->gh_state == LM_ST_EXCLUSIVE && + (current_gh->gh_flags & LM_FLAG_NODE_SCOPE) && + (gh->gh_flags & LM_FLAG_NODE_SCOPE); - if (gh != gh_head) { - /** - * Here we make a special exception to grant holders who agree - * to share the EX lock with other holders who also have the - * bit set. If the original holder has the LM_FLAG_NODE_SCOPE bit - * is set, we grant more holders with the bit set. - */ - if (gh_head->gh_state == LM_ST_EXCLUSIVE && - (gh_head->gh_flags & LM_FLAG_NODE_SCOPE) && - gh->gh_state == LM_ST_EXCLUSIVE && - (gh->gh_flags & LM_FLAG_NODE_SCOPE)) - return 1; - if ((gh->gh_state == LM_ST_EXCLUSIVE || - gh_head->gh_state == LM_ST_EXCLUSIVE)) - return 0; + case LM_ST_SHARED: + case LM_ST_DEFERRED: + return gh->gh_state == current_gh->gh_state; + + default: + return false; + } } + if (gl->gl_state == gh->gh_state) - return 1; + return true; if (gh->gh_flags & GL_EXACT) - return 0; + return false; if (gl->gl_state == LM_ST_EXCLUSIVE) { - if (gh->gh_state == LM_ST_SHARED && gh_head->gh_state == LM_ST_SHARED) - return 1; - if (gh->gh_state == LM_ST_DEFERRED && gh_head->gh_state == LM_ST_DEFERRED) - return 1; + return gh->gh_state == LM_ST_SHARED || + gh->gh_state == LM_ST_DEFERRED; } - if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY)) - return 1; - return 0; + if (gh->gh_flags & LM_FLAG_ANY) + return gl->gl_state != LM_ST_UNLOCKED; + return false; } static void gfs2_holder_wake(struct gfs2_holder *gh) @@ -380,6 +393,24 @@ static void do_error(struct gfs2_glock *gl, const int ret) } } +/** + * find_first_holder - find the first "holder" gh + * @gl: the glock + */ + +static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl) +{ + struct gfs2_holder *gh; + + if (!list_empty(&gl->gl_holders)) { + gh = list_first_entry(&gl->gl_holders, struct gfs2_holder, + gh_list); + if (test_bit(HIF_HOLDER, &gh->gh_iflags)) + return gh; + } + return NULL; +} + /** * do_promote - promote as many requests as possible on the current queue * @gl: The glock @@ -393,14 +424,15 @@ __releases(&gl->gl_lockref.lock) __acquires(&gl->gl_lockref.lock) { const struct gfs2_glock_operations *glops = gl->gl_ops; - struct gfs2_holder *gh, *tmp; + struct gfs2_holder *gh, *tmp, *first_gh; int ret; restart: + first_gh = find_first_holder(gl); list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) { if (test_bit(HIF_HOLDER, &gh->gh_iflags)) continue; - if (may_grant(gl, gh)) { + if (may_grant(gl, first_gh, gh)) { if (gh->gh_list.prev == &gl->gl_holders && glops->go_lock) { spin_unlock(&gl->gl_lockref.lock); @@ -722,23 +754,6 @@ __acquires(&gl->gl_lockref.lock) spin_lock(&gl->gl_lockref.lock); } -/** - * find_first_holder - find the first "holder" gh - * @gl: the glock - */ - -static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl) -{ - struct gfs2_holder *gh; - - if (!list_empty(&gl->gl_holders)) { - gh = list_first_entry(&gl->gl_holders, struct gfs2_holder, gh_list); - if (test_bit(HIF_HOLDER, &gh->gh_iflags)) - return gh; - } - return NULL; -} - /** * run_queue - do all outstanding tasks related to a glock * @gl: The glock in question @@ -1354,8 +1369,12 @@ __acquires(&gl->gl_lockref.lock) GLOCK_BUG_ON(gl, true); if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) { - if (test_bit(GLF_LOCK, &gl->gl_flags)) - try_futile = !may_grant(gl, gh); + if (test_bit(GLF_LOCK, &gl->gl_flags)) { + struct gfs2_holder *first_gh; + + first_gh = find_first_holder(gl); + try_futile = !may_grant(gl, first_gh, gh); + } if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) goto fail; }