Linux Audio

Check our new training course

Loading...
v4.6
 
  1/******************************************************************************
  2*******************************************************************************
  3**
  4**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
  5**  Copyright (C) 2004-2010 Red Hat, Inc.  All rights reserved.
  6**
  7**  This copyrighted material is made available to anyone wishing to use,
  8**  modify, copy, or redistribute it subject to the terms and conditions
  9**  of the GNU General Public License v.2.
 10**
 11*******************************************************************************
 12******************************************************************************/
 13
 
 
 14#include "dlm_internal.h"
 
 
 15#include "lock.h"
 16#include "user.h"
 17#include "ast.h"
 18
 19static uint64_t dlm_cb_seq;
 20static DEFINE_SPINLOCK(dlm_cb_seq_spin);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 21
 22static void dlm_dump_lkb_callbacks(struct dlm_lkb *lkb)
 23{
 24	int i;
 25
 26	log_print("last_bast %x %llu flags %x mode %d sb %d %x",
 27		  lkb->lkb_id,
 28		  (unsigned long long)lkb->lkb_last_bast.seq,
 29		  lkb->lkb_last_bast.flags,
 30		  lkb->lkb_last_bast.mode,
 31		  lkb->lkb_last_bast.sb_status,
 32		  lkb->lkb_last_bast.sb_flags);
 33
 34	log_print("last_cast %x %llu flags %x mode %d sb %d %x",
 35		  lkb->lkb_id,
 36		  (unsigned long long)lkb->lkb_last_cast.seq,
 37		  lkb->lkb_last_cast.flags,
 38		  lkb->lkb_last_cast.mode,
 39		  lkb->lkb_last_cast.sb_status,
 40		  lkb->lkb_last_cast.sb_flags);
 41
 42	for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
 43		log_print("cb %x %llu flags %x mode %d sb %d %x",
 44			  lkb->lkb_id,
 45			  (unsigned long long)lkb->lkb_callbacks[i].seq,
 46			  lkb->lkb_callbacks[i].flags,
 47			  lkb->lkb_callbacks[i].mode,
 48			  lkb->lkb_callbacks[i].sb_status,
 49			  lkb->lkb_callbacks[i].sb_flags);
 50	}
 51}
 52
 53int dlm_add_lkb_callback(struct dlm_lkb *lkb, uint32_t flags, int mode,
 54			 int status, uint32_t sbflags, uint64_t seq)
 55{
 56	struct dlm_ls *ls = lkb->lkb_resource->res_ls;
 57	uint64_t prev_seq;
 58	int prev_mode;
 59	int i, rv;
 60
 61	for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
 62		if (lkb->lkb_callbacks[i].seq)
 63			continue;
 
 
 
 
 
 
 
 
 
 
 
 
 64
 65		/*
 66		 * Suppress some redundant basts here, do more on removal.
 67		 * Don't even add a bast if the callback just before it
 68		 * is a bast for the same mode or a more restrictive mode.
 69		 * (the addional > PR check is needed for PR/CW inversion)
 70		 */
 71
 72		if ((i > 0) && (flags & DLM_CB_BAST) &&
 73		    (lkb->lkb_callbacks[i-1].flags & DLM_CB_BAST)) {
 74
 75			prev_seq = lkb->lkb_callbacks[i-1].seq;
 76			prev_mode = lkb->lkb_callbacks[i-1].mode;
 77
 78			if ((prev_mode == mode) ||
 79			    (prev_mode > mode && prev_mode > DLM_LOCK_PR)) {
 
 
 
 
 
 80
 81				log_debug(ls, "skip %x add bast %llu mode %d "
 82					  "for bast %llu mode %d",
 83					  lkb->lkb_id,
 84					  (unsigned long long)seq,
 85					  mode,
 86					  (unsigned long long)prev_seq,
 87					  prev_mode);
 88				rv = 0;
 89				goto out;
 
 90			}
 91		}
 92
 93		lkb->lkb_callbacks[i].seq = seq;
 94		lkb->lkb_callbacks[i].flags = flags;
 95		lkb->lkb_callbacks[i].mode = mode;
 96		lkb->lkb_callbacks[i].sb_status = status;
 97		lkb->lkb_callbacks[i].sb_flags = (sbflags & 0x000000FF);
 98		rv = 0;
 99		break;
100	}
101
102	if (i == DLM_CALLBACKS_SIZE) {
103		log_error(ls, "no callbacks %x %llu flags %x mode %d sb %d %x",
104			  lkb->lkb_id, (unsigned long long)seq,
105			  flags, mode, status, sbflags);
106		dlm_dump_lkb_callbacks(lkb);
107		rv = -1;
108		goto out;
109	}
110 out:
111	return rv;
112}
113
114int dlm_rem_lkb_callback(struct dlm_ls *ls, struct dlm_lkb *lkb,
115			 struct dlm_callback *cb, int *resid)
 
116{
117	int i, rv;
118
119	*resid = 0;
120
121	if (!lkb->lkb_callbacks[0].seq) {
122		rv = -ENOENT;
123		goto out;
124	}
125
126	/* oldest undelivered cb is callbacks[0] */
127
128	memcpy(cb, &lkb->lkb_callbacks[0], sizeof(struct dlm_callback));
129	memset(&lkb->lkb_callbacks[0], 0, sizeof(struct dlm_callback));
130
131	/* shift others down */
132
133	for (i = 1; i < DLM_CALLBACKS_SIZE; i++) {
134		if (!lkb->lkb_callbacks[i].seq)
135			break;
136		memcpy(&lkb->lkb_callbacks[i-1], &lkb->lkb_callbacks[i],
137		       sizeof(struct dlm_callback));
138		memset(&lkb->lkb_callbacks[i], 0, sizeof(struct dlm_callback));
139		(*resid)++;
140	}
 
 
 
141
142	/* if cb is a bast, it should be skipped if the blocking mode is
143	   compatible with the last granted mode */
144
145	if ((cb->flags & DLM_CB_BAST) && lkb->lkb_last_cast.seq) {
146		if (dlm_modes_compat(cb->mode, lkb->lkb_last_cast.mode)) {
147			cb->flags |= DLM_CB_SKIP;
148
149			log_debug(ls, "skip %x bast %llu mode %d "
150				  "for cast %llu mode %d",
151				  lkb->lkb_id,
152				  (unsigned long long)cb->seq,
153				  cb->mode,
154				  (unsigned long long)lkb->lkb_last_cast.seq,
155				  lkb->lkb_last_cast.mode);
156			rv = 0;
157			goto out;
158		}
159	}
160
161	if (cb->flags & DLM_CB_CAST) {
162		memcpy(&lkb->lkb_last_cast, cb, sizeof(struct dlm_callback));
163		lkb->lkb_last_cast_time = ktime_get();
164	}
 
 
 
 
165
166	if (cb->flags & DLM_CB_BAST) {
167		memcpy(&lkb->lkb_last_bast, cb, sizeof(struct dlm_callback));
168		lkb->lkb_last_bast_time = ktime_get();
169	}
170	rv = 0;
171 out:
172	return rv;
173}
174
175void dlm_add_cb(struct dlm_lkb *lkb, uint32_t flags, int mode, int status,
176		uint32_t sbflags)
177{
178	struct dlm_ls *ls = lkb->lkb_resource->res_ls;
179	uint64_t new_seq, prev_seq;
 
180	int rv;
181
182	spin_lock(&dlm_cb_seq_spin);
183	new_seq = ++dlm_cb_seq;
184	spin_unlock(&dlm_cb_seq_spin);
185
186	if (lkb->lkb_flags & DLM_IFL_USER) {
187		dlm_user_add_ast(lkb, flags, mode, status, sbflags, new_seq);
188		return;
189	}
190
191	mutex_lock(&lkb->lkb_cb_mutex);
192	prev_seq = lkb->lkb_callbacks[0].seq;
193
194	rv = dlm_add_lkb_callback(lkb, flags, mode, status, sbflags, new_seq);
195	if (rv < 0)
196		goto out;
197
198	if (!prev_seq) {
199		kref_get(&lkb->lkb_ref);
200
201		if (test_bit(LSFL_CB_DELAY, &ls->ls_flags)) {
202			mutex_lock(&ls->ls_cb_mutex);
203			list_add(&lkb->lkb_cb_list, &ls->ls_cb_delay);
204			mutex_unlock(&ls->ls_cb_mutex);
 
205		} else {
206			queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
207		}
208	}
209 out:
210	mutex_unlock(&lkb->lkb_cb_mutex);
211}
212
213void dlm_callback_work(struct work_struct *work)
214{
215	struct dlm_lkb *lkb = container_of(work, struct dlm_lkb, lkb_cb_work);
216	struct dlm_ls *ls = lkb->lkb_resource->res_ls;
217	void (*castfn) (void *astparam);
218	void (*bastfn) (void *astparam, int mode);
219	struct dlm_callback callbacks[DLM_CALLBACKS_SIZE];
220	int i, rv, resid;
221
222	memset(&callbacks, 0, sizeof(callbacks));
223
224	mutex_lock(&lkb->lkb_cb_mutex);
225	if (!lkb->lkb_callbacks[0].seq) {
226		/* no callback work exists, shouldn't happen */
227		log_error(ls, "dlm_callback_work %x no work", lkb->lkb_id);
228		dlm_print_lkb(lkb);
229		dlm_dump_lkb_callbacks(lkb);
230	}
231
232	for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
233		rv = dlm_rem_lkb_callback(ls, lkb, &callbacks[i], &resid);
234		if (rv < 0)
235			break;
236	}
237
238	if (resid) {
239		/* cbs remain, loop should have removed all, shouldn't happen */
240		log_error(ls, "dlm_callback_work %x resid %d", lkb->lkb_id,
241			  resid);
242		dlm_print_lkb(lkb);
243		dlm_dump_lkb_callbacks(lkb);
244	}
245	mutex_unlock(&lkb->lkb_cb_mutex);
246
247	castfn = lkb->lkb_astfn;
248	bastfn = lkb->lkb_bastfn;
249
250	for (i = 0; i < DLM_CALLBACKS_SIZE; i++) {
251		if (!callbacks[i].seq)
252			break;
253		if (callbacks[i].flags & DLM_CB_SKIP) {
254			continue;
255		} else if (callbacks[i].flags & DLM_CB_BAST) {
256			bastfn(lkb->lkb_astparam, callbacks[i].mode);
257		} else if (callbacks[i].flags & DLM_CB_CAST) {
258			lkb->lkb_lksb->sb_status = callbacks[i].sb_status;
259			lkb->lkb_lksb->sb_flags = callbacks[i].sb_flags;
260			castfn(lkb->lkb_astparam);
261		}
262	}
263
264	/* undo kref_get from dlm_add_callback, may cause lkb to be freed */
265	dlm_put_lkb(lkb);
266}
267
268int dlm_callback_start(struct dlm_ls *ls)
269{
270	ls->ls_callback_wq = alloc_workqueue("dlm_callback",
271					     WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
 
 
 
 
272	if (!ls->ls_callback_wq) {
273		log_print("can't start dlm_callback workqueue");
274		return -ENOMEM;
275	}
276	return 0;
277}
278
279void dlm_callback_stop(struct dlm_ls *ls)
280{
281	if (ls->ls_callback_wq)
282		destroy_workqueue(ls->ls_callback_wq);
283}
284
285void dlm_callback_suspend(struct dlm_ls *ls)
286{
 
 
 
 
287	set_bit(LSFL_CB_DELAY, &ls->ls_flags);
 
288
289	if (ls->ls_callback_wq)
290		flush_workqueue(ls->ls_callback_wq);
291}
292
 
 
293void dlm_callback_resume(struct dlm_ls *ls)
294{
295	struct dlm_lkb *lkb, *safe;
296	int count = 0;
 
297
298	clear_bit(LSFL_CB_DELAY, &ls->ls_flags);
299
300	if (!ls->ls_callback_wq)
301		return;
302
303	mutex_lock(&ls->ls_cb_mutex);
304	list_for_each_entry_safe(lkb, safe, &ls->ls_cb_delay, lkb_cb_list) {
305		list_del_init(&lkb->lkb_cb_list);
306		queue_work(ls->ls_callback_wq, &lkb->lkb_cb_work);
 
 
 
 
 
307		count++;
 
 
 
 
 
 
 
 
 
 
 
 
 
308	}
309	mutex_unlock(&ls->ls_cb_mutex);
310
311	if (count)
312		log_rinfo(ls, "dlm_callback_resume %d", count);
313}
314
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/******************************************************************************
  3*******************************************************************************
  4**
  5**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
  6**  Copyright (C) 2004-2010 Red Hat, Inc.  All rights reserved.
  7**
 
 
 
  8**
  9*******************************************************************************
 10******************************************************************************/
 11
 12#include <trace/events/dlm.h>
 13
 14#include "dlm_internal.h"
 15#include "lvb_table.h"
 16#include "memory.h"
 17#include "lock.h"
 18#include "user.h"
 19#include "ast.h"
 20
 21static void dlm_run_callback(uint32_t ls_id, uint32_t lkb_id, int8_t mode,
 22			     uint32_t flags, uint8_t sb_flags, int sb_status,
 23			     struct dlm_lksb *lksb,
 24			     void (*astfn)(void *astparam),
 25			     void (*bastfn)(void *astparam, int mode),
 26			     void *astparam, const char *res_name,
 27			     size_t res_length)
 28{
 29	if (flags & DLM_CB_BAST) {
 30		trace_dlm_bast(ls_id, lkb_id, mode, res_name, res_length);
 31		bastfn(astparam, mode);
 32	} else if (flags & DLM_CB_CAST) {
 33		trace_dlm_ast(ls_id, lkb_id, sb_flags, sb_status, res_name,
 34			      res_length);
 35		lksb->sb_status = sb_status;
 36		lksb->sb_flags = sb_flags;
 37		astfn(astparam);
 38	}
 39}
 40
 41static void dlm_do_callback(struct dlm_callback *cb)
 42{
 43	dlm_run_callback(cb->ls_id, cb->lkb_id, cb->mode, cb->flags,
 44			 cb->sb_flags, cb->sb_status, cb->lkb_lksb,
 45			 cb->astfn, cb->bastfn, cb->astparam,
 46			 cb->res_name, cb->res_length);
 47	dlm_free_cb(cb);
 48}
 49
 50static void dlm_callback_work(struct work_struct *work)
 51{
 52	struct dlm_callback *cb = container_of(work, struct dlm_callback, work);
 53
 54	dlm_do_callback(cb);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 55}
 56
 57bool dlm_may_skip_callback(struct dlm_lkb *lkb, uint32_t flags, int mode,
 58			   int status, uint32_t sbflags, int *copy_lvb)
 59{
 60	struct dlm_rsb *rsb = lkb->lkb_resource;
 61	struct dlm_ls *ls = rsb->res_ls;
 62	int prev_mode;
 
 63
 64	if (copy_lvb)
 65		*copy_lvb = 0;
 66
 67	if (flags & DLM_CB_BAST) {
 68		/* if cb is a bast, it should be skipped if the blocking mode is
 69		 * compatible with the last granted mode
 70		 */
 71		if (lkb->lkb_last_cast_cb_mode != -1) {
 72			if (dlm_modes_compat(mode, lkb->lkb_last_cast_cb_mode)) {
 73				log_debug(ls, "skip %x bast mode %d for cast mode %d",
 74					  lkb->lkb_id, mode,
 75					  lkb->lkb_last_cast_cb_mode);
 76				return true;
 77			}
 78		}
 79
 80		/*
 81		 * Suppress some redundant basts here, do more on removal.
 82		 * Don't even add a bast if the callback just before it
 83		 * is a bast for the same mode or a more restrictive mode.
 84		 * (the addional > PR check is needed for PR/CW inversion)
 85		 */
 86		if (lkb->lkb_last_cb_mode != -1 &&
 87		    lkb->lkb_last_cb_flags & DLM_CB_BAST) {
 88			prev_mode = lkb->lkb_last_cb_mode;
 
 
 
 89
 90			if ((prev_mode == mode) ||
 91			    (prev_mode > mode && prev_mode > DLM_LOCK_PR)) {
 92				log_debug(ls, "skip %x add bast mode %d for bast mode %d",
 93					  lkb->lkb_id, mode, prev_mode);
 94				return true;
 95			}
 96		}
 97
 98		lkb->lkb_last_bast_time = ktime_get();
 99		lkb->lkb_last_bast_cb_mode = mode;
100	} else if (flags & DLM_CB_CAST) {
101		if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
102			prev_mode = lkb->lkb_last_cast_cb_mode;
103
104			if (!status && lkb->lkb_lksb->sb_lvbptr &&
105			    dlm_lvb_operations[prev_mode + 1][mode + 1]) {
106				if (copy_lvb)
107					*copy_lvb = 1;
108			}
109		}
110
111		lkb->lkb_last_cast_cb_mode = mode;
112		lkb->lkb_last_cast_time = ktime_get();
 
 
 
 
 
113	}
114
115	lkb->lkb_last_cb_mode = mode;
116	lkb->lkb_last_cb_flags = flags;
117
118	return false;
 
 
 
 
 
 
119}
120
121int dlm_get_cb(struct dlm_lkb *lkb, uint32_t flags, int mode,
122	       int status, uint32_t sbflags,
123	       struct dlm_callback **cb)
124{
125	struct dlm_rsb *rsb = lkb->lkb_resource;
126	struct dlm_ls *ls = rsb->res_ls;
 
 
 
 
 
 
 
 
127
128	*cb = dlm_allocate_cb();
129	if (WARN_ON_ONCE(!*cb))
130		return -ENOMEM;
 
131
132	/* for tracing */
133	(*cb)->lkb_id = lkb->lkb_id;
134	(*cb)->ls_id = ls->ls_global_id;
135	memcpy((*cb)->res_name, rsb->res_name, rsb->res_length);
136	(*cb)->res_length = rsb->res_length;
137
138	(*cb)->flags = flags;
139	(*cb)->mode = mode;
140	(*cb)->sb_status = status;
141	(*cb)->sb_flags = (sbflags & 0x000000FF);
142	(*cb)->lkb_lksb = lkb->lkb_lksb;
143
144	return 0;
145}
146
147static int dlm_get_queue_cb(struct dlm_lkb *lkb, uint32_t flags, int mode,
148			    int status, uint32_t sbflags,
149			    struct dlm_callback **cb)
150{
151	int rv;
 
 
 
 
 
 
 
 
 
 
152
153	rv = dlm_get_cb(lkb, flags, mode, status, sbflags, cb);
154	if (rv)
155		return rv;
156
157	(*cb)->astfn = lkb->lkb_astfn;
158	(*cb)->bastfn = lkb->lkb_bastfn;
159	(*cb)->astparam = lkb->lkb_astparam;
160	INIT_WORK(&(*cb)->work, dlm_callback_work);
161
162	return 0;
 
 
 
 
 
 
163}
164
165void dlm_add_cb(struct dlm_lkb *lkb, uint32_t flags, int mode, int status,
166		uint32_t sbflags)
167{
168	struct dlm_rsb *rsb = lkb->lkb_resource;
169	struct dlm_ls *ls = rsb->res_ls;
170	struct dlm_callback *cb;
171	int rv;
172
173	if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
174		dlm_user_add_ast(lkb, flags, mode, status, sbflags);
 
 
 
 
175		return;
176	}
177
178	if (dlm_may_skip_callback(lkb, flags, mode, status, sbflags, NULL))
179		return;
180
181	spin_lock_bh(&ls->ls_cb_lock);
182	if (test_bit(LSFL_CB_DELAY, &ls->ls_flags)) {
183		rv = dlm_get_queue_cb(lkb, flags, mode, status, sbflags, &cb);
184		if (!rv)
185			list_add(&cb->list, &ls->ls_cb_delay);
186	} else {
187		if (test_bit(LSFL_SOFTIRQ, &ls->ls_flags)) {
188			dlm_run_callback(ls->ls_global_id, lkb->lkb_id, mode, flags,
189					 sbflags, status, lkb->lkb_lksb,
190					 lkb->lkb_astfn, lkb->lkb_bastfn,
191					 lkb->lkb_astparam, rsb->res_name,
192					 rsb->res_length);
193		} else {
194			rv = dlm_get_queue_cb(lkb, flags, mode, status, sbflags, &cb);
195			if (!rv)
196				queue_work(ls->ls_callback_wq, &cb->work);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197		}
198	}
199	spin_unlock_bh(&ls->ls_cb_lock);
 
 
200}
201
202int dlm_callback_start(struct dlm_ls *ls)
203{
204	if (!test_bit(LSFL_FS, &ls->ls_flags) ||
205	    test_bit(LSFL_SOFTIRQ, &ls->ls_flags))
206		return 0;
207
208	ls->ls_callback_wq = alloc_ordered_workqueue("dlm_callback",
209						     WQ_HIGHPRI | WQ_MEM_RECLAIM);
210	if (!ls->ls_callback_wq) {
211		log_print("can't start dlm_callback workqueue");
212		return -ENOMEM;
213	}
214	return 0;
215}
216
217void dlm_callback_stop(struct dlm_ls *ls)
218{
219	if (ls->ls_callback_wq)
220		destroy_workqueue(ls->ls_callback_wq);
221}
222
223void dlm_callback_suspend(struct dlm_ls *ls)
224{
225	if (!test_bit(LSFL_FS, &ls->ls_flags))
226		return;
227
228	spin_lock_bh(&ls->ls_cb_lock);
229	set_bit(LSFL_CB_DELAY, &ls->ls_flags);
230	spin_unlock_bh(&ls->ls_cb_lock);
231
232	if (ls->ls_callback_wq)
233		flush_workqueue(ls->ls_callback_wq);
234}
235
236#define MAX_CB_QUEUE 25
237
238void dlm_callback_resume(struct dlm_ls *ls)
239{
240	struct dlm_callback *cb, *safe;
241	int count = 0, sum = 0;
242	bool empty;
243
244	if (!test_bit(LSFL_FS, &ls->ls_flags))
 
 
245		return;
246
247more:
248	spin_lock_bh(&ls->ls_cb_lock);
249	list_for_each_entry_safe(cb, safe, &ls->ls_cb_delay, list) {
250		list_del(&cb->list);
251		if (test_bit(LSFL_SOFTIRQ, &ls->ls_flags))
252			dlm_do_callback(cb);
253		else
254			queue_work(ls->ls_callback_wq, &cb->work);
255
256		count++;
257		if (count == MAX_CB_QUEUE)
258			break;
259	}
260	empty = list_empty(&ls->ls_cb_delay);
261	if (empty)
262		clear_bit(LSFL_CB_DELAY, &ls->ls_flags);
263	spin_unlock_bh(&ls->ls_cb_lock);
264
265	sum += count;
266	if (!empty) {
267		count = 0;
268		cond_resched();
269		goto more;
270	}
 
271
272	if (sum)
273		log_rinfo(ls, "%s %d", __func__, sum);
274}
275