blob: 1f2f70a1b824eb3dcc6ae91e4cc1d41c2643b560 [file] [log] [blame]
Thomas Gleixner2522fe42019-05-28 09:57:20 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglande7fd4172006-01-18 09:30:29 +00002/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
David Teigland7fe2b312010-02-24 11:08:18 -06006** Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
David Teiglande7fd4172006-01-18 09:30:29 +00007**
David Teiglande7fd4172006-01-18 09:30:29 +00008**
9*******************************************************************************
10******************************************************************************/
11
Alexander Aringf1d3b8f2021-11-02 15:17:15 -040012#include <trace/events/dlm.h>
13
David Teiglande7fd4172006-01-18 09:30:29 +000014#include "dlm_internal.h"
Alexander Aring61bed0b2022-10-27 16:45:21 -040015#include "memory.h"
David Teiglande7fd4172006-01-18 09:30:29 +000016#include "lock.h"
David Teigland597d0ca2006-07-12 16:44:04 -050017#include "user.h"
Rashika Kheria95058572014-02-09 18:19:17 +053018#include "ast.h"
David Teiglande7fd4172006-01-18 09:30:29 +000019
Alexander Aring61bed0b2022-10-27 16:45:21 -040020void dlm_release_callback(struct kref *ref)
David Teigland8304d6f2011-02-21 14:58:21 -060021{
Alexander Aring61bed0b2022-10-27 16:45:21 -040022 struct dlm_callback *cb = container_of(ref, struct dlm_callback, ref);
David Teigland8304d6f2011-02-21 14:58:21 -060023
Alexander Aring61bed0b2022-10-27 16:45:21 -040024 dlm_free_cb(cb);
David Teigland8304d6f2011-02-21 14:58:21 -060025}
26
Alexander Aring61bed0b2022-10-27 16:45:21 -040027void dlm_callback_set_last_ptr(struct dlm_callback **from,
28 struct dlm_callback *to)
29{
30 if (*from)
31 kref_put(&(*from)->ref, dlm_release_callback);
32
33 if (to)
34 kref_get(&to->ref);
35
36 *from = to;
37}
38
Alexander Aring61bed0b2022-10-27 16:45:21 -040039int dlm_enqueue_lkb_callback(struct dlm_lkb *lkb, uint32_t flags, int mode,
40 int status, uint32_t sbflags)
David Teiglande7fd4172006-01-18 09:30:29 +000041{
David Teigland8304d6f2011-02-21 14:58:21 -060042 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
Alexander Aring61bed0b2022-10-27 16:45:21 -040043 int rv = DLM_ENQUEUE_CALLBACK_SUCCESS;
44 struct dlm_callback *cb;
David Teigland8304d6f2011-02-21 14:58:21 -060045 int prev_mode;
David Teigland8304d6f2011-02-21 14:58:21 -060046
Alexander Aring61bed0b2022-10-27 16:45:21 -040047 if (flags & DLM_CB_BAST) {
48 /* if cb is a bast, it should be skipped if the blocking mode is
49 * compatible with the last granted mode
50 */
51 if (lkb->lkb_last_cast) {
52 if (dlm_modes_compat(mode, lkb->lkb_last_cast->mode)) {
53 log_debug(ls, "skip %x bast mode %d for cast mode %d",
54 lkb->lkb_id, mode,
55 lkb->lkb_last_cast->mode);
56 goto out;
57 }
58 }
David Teigland8304d6f2011-02-21 14:58:21 -060059
60 /*
61 * Suppress some redundant basts here, do more on removal.
62 * Don't even add a bast if the callback just before it
63 * is a bast for the same mode or a more restrictive mode.
64 * (the addional > PR check is needed for PR/CW inversion)
65 */
Alexander Aring61bed0b2022-10-27 16:45:21 -040066 if (lkb->lkb_last_cb && lkb->lkb_last_cb->flags & DLM_CB_BAST) {
67 prev_mode = lkb->lkb_last_cb->mode;
David Teigland8304d6f2011-02-21 14:58:21 -060068
69 if ((prev_mode == mode) ||
70 (prev_mode > mode && prev_mode > DLM_LOCK_PR)) {
Alexander Aring61bed0b2022-10-27 16:45:21 -040071 log_debug(ls, "skip %x add bast mode %d for bast mode %d",
72 lkb->lkb_id, mode, prev_mode);
David Teigland23e8e1a2011-04-05 13:16:24 -050073 goto out;
David Teigland8304d6f2011-02-21 14:58:21 -060074 }
75 }
David Teigland8304d6f2011-02-21 14:58:21 -060076 }
77
Alexander Aring61bed0b2022-10-27 16:45:21 -040078 cb = dlm_allocate_cb();
79 if (!cb) {
80 rv = DLM_ENQUEUE_CALLBACK_FAILURE;
David Teigland23e8e1a2011-04-05 13:16:24 -050081 goto out;
David Teigland8304d6f2011-02-21 14:58:21 -060082 }
Alexander Aring61bed0b2022-10-27 16:45:21 -040083
84 cb->flags = flags;
85 cb->mode = mode;
86 cb->sb_status = status;
87 cb->sb_flags = (sbflags & 0x000000FF);
88 kref_init(&cb->ref);
Alexander Aringa034c132023-03-06 15:48:08 -050089 if (!test_and_set_bit(DLM_IFL_CB_PENDING_BIT, &lkb->lkb_iflags))
Alexander Aring61bed0b2022-10-27 16:45:21 -040090 rv = DLM_ENQUEUE_CALLBACK_NEED_SCHED;
Alexander Aringa034c132023-03-06 15:48:08 -050091
Alexander Aring61bed0b2022-10-27 16:45:21 -040092 list_add_tail(&cb->list, &lkb->lkb_callbacks);
93
94 if (flags & DLM_CB_CAST)
95 dlm_callback_set_last_ptr(&lkb->lkb_last_cast, cb);
96
97 dlm_callback_set_last_ptr(&lkb->lkb_last_cb, cb);
98
David Teigland23e8e1a2011-04-05 13:16:24 -050099 out:
100 return rv;
David Teigland8304d6f2011-02-21 14:58:21 -0600101}
102
Alexander Aring61bed0b2022-10-27 16:45:21 -0400103int dlm_dequeue_lkb_callback(struct dlm_lkb *lkb, struct dlm_callback **cb)
David Teigland8304d6f2011-02-21 14:58:21 -0600104{
Alexander Aring61bed0b2022-10-27 16:45:21 -0400105 /* oldest undelivered cb is callbacks first entry */
106 *cb = list_first_entry_or_null(&lkb->lkb_callbacks,
107 struct dlm_callback, list);
108 if (!*cb)
109 return DLM_DEQUEUE_CALLBACK_EMPTY;
David Teigland8304d6f2011-02-21 14:58:21 -0600110
Alexander Aring61bed0b2022-10-27 16:45:21 -0400111 /* remove it from callbacks so shift others down */
112 list_del(&(*cb)->list);
113 if (list_empty(&lkb->lkb_callbacks))
114 return DLM_DEQUEUE_CALLBACK_LAST;
David Teigland8304d6f2011-02-21 14:58:21 -0600115
Alexander Aring61bed0b2022-10-27 16:45:21 -0400116 return DLM_DEQUEUE_CALLBACK_SUCCESS;
David Teigland8304d6f2011-02-21 14:58:21 -0600117}
118
David Teigland23e8e1a2011-04-05 13:16:24 -0500119void dlm_add_cb(struct dlm_lkb *lkb, uint32_t flags, int mode, int status,
120 uint32_t sbflags)
David Teigland8304d6f2011-02-21 14:58:21 -0600121{
David Teigland23e8e1a2011-04-05 13:16:24 -0500122 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
David Teigland8304d6f2011-02-21 14:58:21 -0600123 int rv;
124
Alexander Aring8a39dcd2023-03-06 15:48:15 -0500125 if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
Alexander Aring61bed0b2022-10-27 16:45:21 -0400126 dlm_user_add_ast(lkb, flags, mode, status, sbflags);
David Teigland597d0ca2006-07-12 16:44:04 -0500127 return;
128 }
129
Alexander Aring92e95732022-10-27 16:45:19 -0400130 spin_lock(&lkb->lkb_cb_lock);
Alexander Aring61bed0b2022-10-27 16:45:21 -0400131 rv = dlm_enqueue_lkb_callback(lkb, flags, mode, status, sbflags);
132 switch (rv) {
133 case DLM_ENQUEUE_CALLBACK_NEED_SCHED:
David Teigland8304d6f2011-02-21 14:58:21 -0600134 kref_get(&lkb->lkb_ref);
David Teiglande7fd4172006-01-18 09:30:29 +0000135
Alexander Aringa4c03522022-10-27 16:45:18 -0400136 spin_lock(&ls->ls_cb_lock);
David Teigland23e8e1a2011-04-05 13:16:24 -0500137 if (test_bit(LSFL_CB_DELAY, &ls->ls_flags)) {
David Teigland23e8e1a2011-04-05 13:16:24 -0500138 list_add(&lkb->lkb_cb_list, &ls->ls_cb_delay);
David Teigland23e8e1a2011-04-05 13:16:24 -0500139 } else {
140 queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
141 }
Alexander Aringa4c03522022-10-27 16:45:18 -0400142 spin_unlock(&ls->ls_cb_lock);
Alexander Aring61bed0b2022-10-27 16:45:21 -0400143 break;
144 case DLM_ENQUEUE_CALLBACK_FAILURE:
Alexander Aring740bb8f2022-11-17 17:11:42 -0500145 WARN_ON_ONCE(1);
Alexander Aring61bed0b2022-10-27 16:45:21 -0400146 break;
147 case DLM_ENQUEUE_CALLBACK_SUCCESS:
148 break;
149 default:
Alexander Aring740bb8f2022-11-17 17:11:42 -0500150 WARN_ON_ONCE(1);
Alexander Aring61bed0b2022-10-27 16:45:21 -0400151 break;
David Teigland23e8e1a2011-04-05 13:16:24 -0500152 }
Alexander Aring92e95732022-10-27 16:45:19 -0400153 spin_unlock(&lkb->lkb_cb_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000154}
155
David Teigland23e8e1a2011-04-05 13:16:24 -0500156void dlm_callback_work(struct work_struct *work)
David Teiglande7fd4172006-01-18 09:30:29 +0000157{
David Teigland23e8e1a2011-04-05 13:16:24 -0500158 struct dlm_lkb *lkb = container_of(work, struct dlm_lkb, lkb_cb_work);
159 struct dlm_ls *ls = lkb->lkb_resource->res_ls;
David Teigland7fe2b312010-02-24 11:08:18 -0600160 void (*castfn) (void *astparam);
161 void (*bastfn) (void *astparam, int mode);
Alexander Aring61bed0b2022-10-27 16:45:21 -0400162 struct dlm_callback *cb;
163 int rv;
David Teiglande7fd4172006-01-18 09:30:29 +0000164
Alexander Aring92e95732022-10-27 16:45:19 -0400165 spin_lock(&lkb->lkb_cb_lock);
Alexander Aring61bed0b2022-10-27 16:45:21 -0400166 rv = dlm_dequeue_lkb_callback(lkb, &cb);
Alexander Aring7a931472023-05-29 17:44:30 -0400167 if (WARN_ON_ONCE(rv == DLM_DEQUEUE_CALLBACK_EMPTY)) {
168 clear_bit(DLM_IFL_CB_PENDING_BIT, &lkb->lkb_iflags);
169 spin_unlock(&lkb->lkb_cb_lock);
Alexander Aring9267c852022-11-17 17:11:41 -0500170 goto out;
Alexander Aring7a931472023-05-29 17:44:30 -0400171 }
172 spin_unlock(&lkb->lkb_cb_lock);
David Teigland23e8e1a2011-04-05 13:16:24 -0500173
Alexander Aring61bed0b2022-10-27 16:45:21 -0400174 for (;;) {
175 castfn = lkb->lkb_astfn;
176 bastfn = lkb->lkb_bastfn;
177
178 if (cb->flags & DLM_CB_BAST) {
179 trace_dlm_bast(ls, lkb, cb->mode);
Alexander Aring27d39942022-10-27 16:45:20 -0400180 lkb->lkb_last_bast_time = ktime_get();
Alexander Aring61bed0b2022-10-27 16:45:21 -0400181 lkb->lkb_last_bast_mode = cb->mode;
182 bastfn(lkb->lkb_astparam, cb->mode);
183 } else if (cb->flags & DLM_CB_CAST) {
184 lkb->lkb_lksb->sb_status = cb->sb_status;
185 lkb->lkb_lksb->sb_flags = cb->sb_flags;
Alexander Aring0c4c5162022-06-22 14:45:11 -0400186 trace_dlm_ast(ls, lkb);
Alexander Aring27d39942022-10-27 16:45:20 -0400187 lkb->lkb_last_cast_time = ktime_get();
Alexander Aringcd1e8ca2022-06-22 14:45:10 -0400188 castfn(lkb->lkb_astparam);
David Teigland23e8e1a2011-04-05 13:16:24 -0500189 }
Alexander Aring61bed0b2022-10-27 16:45:21 -0400190
191 kref_put(&cb->ref, dlm_release_callback);
192
193 spin_lock(&lkb->lkb_cb_lock);
194 rv = dlm_dequeue_lkb_callback(lkb, &cb);
195 if (rv == DLM_DEQUEUE_CALLBACK_EMPTY) {
Alexander Aringa034c132023-03-06 15:48:08 -0500196 clear_bit(DLM_IFL_CB_PENDING_BIT, &lkb->lkb_iflags);
Alexander Aring61bed0b2022-10-27 16:45:21 -0400197 spin_unlock(&lkb->lkb_cb_lock);
198 break;
199 }
200 spin_unlock(&lkb->lkb_cb_lock);
David Teigland23e8e1a2011-04-05 13:16:24 -0500201 }
202
Alexander Aring9267c852022-11-17 17:11:41 -0500203out:
David Teigland23e8e1a2011-04-05 13:16:24 -0500204 /* undo kref_get from dlm_add_callback, may cause lkb to be freed */
205 dlm_put_lkb(lkb);
David Teiglande7fd4172006-01-18 09:30:29 +0000206}
207
David Teigland23e8e1a2011-04-05 13:16:24 -0500208int dlm_callback_start(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000209{
David Teigland23e8e1a2011-04-05 13:16:24 -0500210 ls->ls_callback_wq = alloc_workqueue("dlm_callback",
Bob Petersonaa9f1012016-10-19 11:34:54 -0400211 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
David Teigland23e8e1a2011-04-05 13:16:24 -0500212 if (!ls->ls_callback_wq) {
213 log_print("can't start dlm_callback workqueue");
214 return -ENOMEM;
David Teiglande7fd4172006-01-18 09:30:29 +0000215 }
216 return 0;
217}
218
David Teigland23e8e1a2011-04-05 13:16:24 -0500219void dlm_callback_stop(struct dlm_ls *ls)
David Teiglande7fd4172006-01-18 09:30:29 +0000220{
David Teigland23e8e1a2011-04-05 13:16:24 -0500221 if (ls->ls_callback_wq)
222 destroy_workqueue(ls->ls_callback_wq);
223}
224
225void dlm_callback_suspend(struct dlm_ls *ls)
226{
Alexander Aring9cb16d42022-08-15 15:43:26 -0400227 if (ls->ls_callback_wq) {
Alexander Aringa4c03522022-10-27 16:45:18 -0400228 spin_lock(&ls->ls_cb_lock);
Alexander Aring9cb16d42022-08-15 15:43:26 -0400229 set_bit(LSFL_CB_DELAY, &ls->ls_flags);
Alexander Aringa4c03522022-10-27 16:45:18 -0400230 spin_unlock(&ls->ls_cb_lock);
David Teigland23e8e1a2011-04-05 13:16:24 -0500231
David Teigland23e8e1a2011-04-05 13:16:24 -0500232 flush_workqueue(ls->ls_callback_wq);
Alexander Aring9cb16d42022-08-15 15:43:26 -0400233 }
David Teigland23e8e1a2011-04-05 13:16:24 -0500234}
235
Bob Peterson216f0ef2018-11-08 14:04:50 -0500236#define MAX_CB_QUEUE 25
237
David Teigland23e8e1a2011-04-05 13:16:24 -0500238void dlm_callback_resume(struct dlm_ls *ls)
239{
240 struct dlm_lkb *lkb, *safe;
Alexander Aring2f05ec42021-11-02 15:17:14 -0400241 int count = 0, sum = 0;
Alexander Aringf70813d2021-11-30 14:47:14 -0500242 bool empty;
David Teigland23e8e1a2011-04-05 13:16:24 -0500243
David Teigland23e8e1a2011-04-05 13:16:24 -0500244 if (!ls->ls_callback_wq)
245 return;
246
Bob Peterson216f0ef2018-11-08 14:04:50 -0500247more:
Alexander Aringa4c03522022-10-27 16:45:18 -0400248 spin_lock(&ls->ls_cb_lock);
David Teigland23e8e1a2011-04-05 13:16:24 -0500249 list_for_each_entry_safe(lkb, safe, &ls->ls_cb_delay, lkb_cb_list) {
250 list_del_init(&lkb->lkb_cb_list);
251 queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
252 count++;
Bob Peterson216f0ef2018-11-08 14:04:50 -0500253 if (count == MAX_CB_QUEUE)
254 break;
David Teiglande7fd4172006-01-18 09:30:29 +0000255 }
Alexander Aringf70813d2021-11-30 14:47:14 -0500256 empty = list_empty(&ls->ls_cb_delay);
Alexander Aring85839f22022-10-27 16:45:16 -0400257 if (empty)
258 clear_bit(LSFL_CB_DELAY, &ls->ls_flags);
Alexander Aringa4c03522022-10-27 16:45:18 -0400259 spin_unlock(&ls->ls_cb_lock);
David Teiglande7fd4172006-01-18 09:30:29 +0000260
Alexander Aring2f05ec42021-11-02 15:17:14 -0400261 sum += count;
Alexander Aringf70813d2021-11-30 14:47:14 -0500262 if (!empty) {
Bob Peterson216f0ef2018-11-08 14:04:50 -0500263 count = 0;
264 cond_resched();
265 goto more;
266 }
Alexander Aring2f05ec42021-11-02 15:17:14 -0400267
268 if (sum)
269 log_rinfo(ls, "%s %d", __func__, sum);
David Teiglande7fd4172006-01-18 09:30:29 +0000270}
271