xfs: consolidated log and optimisation changes
Subsystems (3)
Re: [PATCH 29/45] xfs:_introduce xlog_write_partial()
In reply to Dave ChinnerQuoted 44 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Handle writing of a logvec chain into an iclog that doesn't have enough space to fit it all. The iclog has already been changed to
WANT_SYNCbyxlog_get_iclog_space(), so the entire remaining space in the iclog is exclusively owned by this logvec chain.The difference between the single and partial cases is that we end up with partial iovec writes in the iclog and have to split a log vec regions across two iclogs. The state handling for this is currently awful and so we're building up the pieces needed to handle this more cleanly one at a time.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log.c | 525 ++++++++++++++++++++++------------------------- 1 file changed, 251 insertions(+), 274 deletions(-)fs/xfs/xfs_log.c +2
-15@@ -2099,166 +2099,250 @@ xlog_print_trans( } } -static xlog_op_header_t * -xlog_write_setup_ophdr ( -struct xlog_op_header *ophdr, -struct xlog_ticket *ticket) -{ - ophdr->oh_clientid =XFS_TRANSACTION ; - ophdr->oh_res2 =0 ; - ophdr->oh_flags =0 ; -return ophdr; -} -/* -* Set up the parameters of the region copy into the log. This has -* to handle region write split across multiple log buffers - this -* state is kept external to this function so that this code can -* be written in an obvious, self documenting manner. +* Write whole log vectors into a single iclog which is guaranteed to have +* either sufficient space for the entire log vector chain to be written or
fs/xfs/xfs_log.c xlog_print_trans( · line 2112
Quoted 10 lines
fs/xfs/xfs_log.c +7
-2+* exclusive access to the remaining space in the iclog. +* +* Return the number of iovecs and data written into the iclog, as well as +* a pointer to the logvec that doesn't fit in the log (or NULL if we hit the +* end of the chain. */ -static int -xlog_write_setup_copy( +static struct xfs_log_vec * +xlog_write_single(
Ouch. Could you fix the previous patch to move this new function a little higher in the file (like above xlog_write_setup_ophdr) so that it doesn't get shredded like this?
Sooo... I /think/ this looks all right, but this is a pretty long reorganization. I might revisit this in the morning. :/
(Skip to the second-to-last hunk, that's where the next comment is...)
Quoted 555 lines
fs/xfs/xfs_log.c +237
-253+struct xfs_log_vec *log_vector, struct xlog_ticket *ticket, - struct xlog_op_header *ophdr, - int space_available, - int space_required, - int *copy_off, - int *copy_len, - int *last_was_partial_copy, - int *bytes_consumed) -{ - int still_to_copy; - - still_to_copy = space_required - *bytes_consumed; - *copy_off = *bytes_consumed; - -if (still_to_copy <= space_available) { -/* write of region completes here */ - *copy_len = still_to_copy; - ophdr->oh_len =cpu_to_be32 (*copy_len); -if (*last_was_partial_copy) - ophdr->oh_flags |= (XLOG_END_TRANS |XLOG_WAS_CONT_TRANS ); - *last_was_partial_copy =0 ; - *bytes_consumed =0 ; -return 0 ; - } - -/* partial write of region, needs extra log op header reservation */ - *copy_len = space_available; - ophdr->oh_len =cpu_to_be32 (*copy_len); - ophdr->oh_flags |=XLOG_CONTINUE_TRANS ; -if (*last_was_partial_copy) - ophdr->oh_flags |=XLOG_WAS_CONT_TRANS ; - *bytes_consumed += *copy_len; - (*last_was_partial_copy)++; - -/* account for new log op header */ - ticket->t_curr_res -=sizeof (struct xlog_op_header ); - -return sizeof (struct xlog_op_header ); -} - -static int -xlog_write_copy_finish ( -struct xlog *log, struct xlog_in_core *iclog, -uint flags, -int *record_cnt, -int *data_cnt, -int *partial_copy, -int *partial_copy_len, -int log_offset, -struct xlog_in_core **commit_iclog) + uint32_t *log_offset, + uint32_t *len, + uint32_t *record_cnt, + uint32_t *data_cnt) { -int error; + struct xfs_log_vec *lv = log_vector; +void *ptr; +int index; -if (*partial_copy) { +ASSERT (*log_offset + *len <= iclog->ic_size || + iclog->ic_state ==XLOG_STATE_WANT_SYNC ); + + ptr = iclog->ic_datap + *log_offset; +for (lv = log_vector; lv; lv = lv->lv_next) {/* -* This iclog has already been marked WANT_SYNC by -* xlog_state_get_iclog_space. +* If the entire log vec does not fit in the iclog, punt it to +* the partial copy loop which can handle this case. */ -spin_lock (&log->l_icloglock); -xlog_state_finish_copy (log, iclog, *record_cnt, *data_cnt); - *record_cnt =0 ; - *data_cnt =0 ; - goto release_iclog; - } +if (lv->lv_niovecs && + lv->lv_bytes > iclog->ic_size - *log_offset) +break ; - *partial_copy =0 ; - *partial_copy_len =0 ; +/* +* Ordered log vectors have no regions to write so this +* loop will naturally skip them. +*/ +for (index =0 ; index < lv->lv_niovecs; index++) { +struct xfs_log_iovec *reg = &lv->lv_iovecp[index]; +struct xlog_op_header *ophdr = reg->i_addr; -if (iclog->ic_size - log_offset <=sizeof (xlog_op_header_t)) { -/* no more space in this iclog - push it. */ -spin_lock (&log->l_icloglock); -xlog_state_finish_copy (log, iclog, *record_cnt, *data_cnt); - *record_cnt =0 ; - *data_cnt =0 ; +ASSERT (reg->i_len %sizeof (int32_t ) ==0 ); +ASSERT ((unsigned long )ptr %sizeof (int32_t ) ==0 ); -if (iclog->ic_state ==XLOG_STATE_ACTIVE ) -xlog_state_switch_iclogs (log, iclog,0 ); -else -ASSERT (iclog->ic_state ==XLOG_STATE_WANT_SYNC || - iclog->ic_state ==XLOG_STATE_IOERROR ); -if (!commit_iclog) - goto release_iclog; -spin_unlock (&log->l_icloglock); -ASSERT (flags &XLOG_COMMIT_TRANS ); - *commit_iclog = iclog; + ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); + ophdr->oh_len =cpu_to_be32 (reg->i_len - +sizeof (struct xlog_op_header )); +memcpy (ptr, reg->i_addr, reg->i_len); +xlog_write_adv_cnt (&ptr, len, log_offset, reg->i_len); + (*record_cnt)++; + *data_cnt += reg->i_len; + } } +ASSERT (*len ==0 || lv); +return lv; +} -return 0 ; +static int +xlog_write_get_more_iclog_space ( +struct xlog *log, +struct xlog_ticket *ticket, +struct xlog_in_core **iclogp, +uint32_t *log_offset, +uint32_t len, +uint32_t *record_cnt, +uint32_t *data_cnt, +int *contwr) +{ +struct xlog_in_core *iclog = *iclogp; +int error; -release_iclog: +spin_lock (&log->l_icloglock); +xlog_state_finish_copy (log, iclog, *record_cnt, *data_cnt); +ASSERT (iclog->ic_state ==XLOG_STATE_WANT_SYNC || + iclog->ic_state ==XLOG_STATE_IOERROR ); error =xlog_state_release_iclog (log, iclog);spin_unlock (&log->l_icloglock); -return error; +if (error) +return error; + + error =xlog_state_get_iclog_space (log, len, &iclog, + ticket, contwr, log_offset); +if (error) +return error; + *record_cnt =0 ; + *data_cnt =0 ; + *iclogp = iclog; +return 0 ; }/* -* Write log vectors into a single iclog which is guaranteed by the caller -* to have enough space to write the entire log vector into. Return the number -* of log vectors written into the iclog. +* Write log vectors into a single iclog which is smaller than the current chain +* length. We write until we cannot fit a full record into the remaining space +* and then stop. We return the log vector that is to be written that cannot +* wholly fit in the iclog. */ -static int -xlog_write_single ( +static struct xfs_log_vec * +xlog_write_partial ( +struct xlog *log,struct xfs_log_vec *log_vector,struct xlog_ticket *ticket, -struct xlog_in_core *iclog, -uint32_t log_offset, -uint32_t len) +struct xlog_in_core **iclogp, +uint32_t *log_offset, +uint32_t *len, +uint32_t *record_cnt, +uint32_t *data_cnt, +int *contwr) { +struct xlog_in_core *iclog = *iclogp;struct xfs_log_vec *lv = log_vector; +struct xfs_log_iovec *reg; +struct xlog_op_header *ophdr;void *ptr;int index =0 ; -int record_cnt =0 ; +uint32_t rlen; +int error; -ASSERT (log_offset + len <= iclog->ic_size); +/* walk the logvec, copying until we run out of space in the iclog */ + ptr = iclog->ic_datap + *log_offset; +for (index =0 ; index < lv->lv_niovecs; index++) { +uint32_t reg_offset =0 ; + + reg = &lv->lv_iovecp[index]; +ASSERT (reg->i_len %sizeof (int32_t ) ==0 ); - ptr = iclog->ic_datap + log_offset; -for (lv = log_vector; lv; lv = lv->lv_next) {/* -* Ordered log vectors have no regions to write so this -* loop will naturally skip them. +* The first region of a continuation must have a non-zero +* length otherwise log recovery will just skip over it and +* start recovering from the next opheader it finds. Because we +* mark the next opheader as a continuation, recovery will then +* incorrectly add the continuation to the previous region and +* that breaks stuff. +* +* Hence if there isn't space for region data after the +* opheader, then we need to start afresh with a new iclog. */ -for (index =0 ; index < lv->lv_niovecs; index++) { -struct xfs_log_iovec *reg = &lv->lv_iovecp[index]; -struct xlog_op_header *ophdr = reg->i_addr; +if (iclog->ic_size - *log_offset <= +sizeof (struct xlog_op_header )) { + error =xlog_write_get_more_iclog_space (log, ticket, + &iclog, log_offset, *len, record_cnt, + data_cnt, contwr); +if (error) +return ERR_PTR (error); + ptr = iclog->ic_datap + *log_offset; + } -ASSERT (reg->i_len %sizeof (int32_t ) ==0 ); -ASSERT ((unsigned long )ptr %sizeof (int32_t ) ==0 ); + ophdr = reg->i_addr; + rlen =min_t (uint32_t, reg->i_len, iclog->ic_size - *log_offset); + + ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); + ophdr->oh_len =cpu_to_be32 (rlen -sizeof (struct xlog_op_header )); +if (rlen != reg->i_len) + ophdr->oh_flags |=XLOG_CONTINUE_TRANS ; +ASSERT ((unsigned long )ptr %sizeof (int32_t ) ==0 ); +xlog_verify_dest_ptr (log, ptr); +memcpy (ptr, reg->i_addr, rlen); +xlog_write_adv_cnt (&ptr, len, log_offset, rlen); + (*record_cnt)++; + *data_cnt += rlen; + +if (rlen == reg->i_len) +continue ; + +/* +* We now have a partially written iovec, but it can span +* multiple iclogs so we loop here. First we release the iclog +* we currently have, then we get a new iclog and add a new +* opheader. Then we continue copying from where we were until +* we either complete the iovec or fill the iclog. If we +* complete the iovec, then we increment the index and go right +* back to the top of the outer loop. if we fill the iclog, we +* run the inner loop again. +* +* This is complicated by the tail of a region using all the +* space in an iclog and hence requiring us to release the iclog +* and get a new one before returning to the outer loop. We must +* always guarantee that we exit this inner loop with at least +* space for log transaction opheaders left in the current +* iclog, hence we cannot just terminate the loop at the end +* of the of the continuation. So we loop while there is no +* space left in the current iclog, and check for the end of the +* continuation after getting a new iclog. +*/ +do { +/* +* Account for the continuation opheader before we get +* a new iclog. This is necessary so that we reserve +* space in the iclog for it. +*/ +if (ophdr->oh_flags &XLOG_CONTINUE_TRANS ) { + *len +=sizeof (struct xlog_op_header ); + ticket->t_curr_res -=sizeof (struct xlog_op_header ); + } + error =xlog_write_get_more_iclog_space (log, ticket, + &iclog, log_offset, *len, record_cnt, + data_cnt, contwr); +if (error) +return ERR_PTR (error); + ptr = iclog->ic_datap + *log_offset; + + ophdr = ptr; ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); - ophdr->oh_len =cpu_to_be32 (reg->i_len - + ophdr->oh_clientid =XFS_TRANSACTION ; + ophdr->oh_res2 =0 ; + ophdr->oh_flags =XLOG_WAS_CONT_TRANS ; + +xlog_write_adv_cnt (&ptr, len, log_offset,sizeof (struct xlog_op_header )); -memcpy (ptr, reg->i_addr, reg->i_len); -xlog_write_adv_cnt (&ptr, &len, &log_offset, reg->i_len); - record_cnt++; - } + *data_cnt +=sizeof (struct xlog_op_header ); + +/* +* If rlen fits in the iclog, then end the region +* continuation. Otherwise we're going around again. +*/ + reg_offset += rlen; + rlen = reg->i_len - reg_offset; +if (rlen <= iclog->ic_size - *log_offset) + ophdr->oh_flags |=XLOG_END_TRANS ; +else + ophdr->oh_flags |=XLOG_CONTINUE_TRANS ; + + rlen =min_t (uint32_t, rlen, iclog->ic_size - *log_offset); + ophdr->oh_len =cpu_to_be32 (rlen); + +xlog_verify_dest_ptr (log, ptr); +memcpy (ptr, reg->i_addr + reg_offset, rlen); +xlog_write_adv_cnt (&ptr, len, log_offset, rlen); + (*record_cnt)++; + *data_cnt += rlen; + + }while (ophdr->oh_flags &XLOG_CONTINUE_TRANS ); } -ASSERT (len ==0 ); -return record_cnt; -} +/* +* No more iovecs remain in this logvec so return the next log vec to +* the caller so it can go back to fast path copying. +*/ + *iclogp = iclog; +return lv->lv_next; +}/* * Write some region out to in-core log @@ -2312,14 +2396,11 @@ xlog_write( {struct xlog_in_core *iclog =NULL ;struct xfs_log_vec *lv = log_vector; -struct xfs_log_iovec *vecp = lv->lv_iovecp; -int index =0 ; -int partial_copy =0 ; -int partial_copy_len =0 ;int contwr =0 ;int record_cnt =0 ;int data_cnt =0 ;int error =0 ; +int log_offset;if (ticket->t_curr_res <0 ) { xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,@@ -2328,157 +2409,52 @@ xlog_write(xfs_force_shutdown (log->l_mp,SHUTDOWN_LOG_IO_ERROR ); } -if (start_lsn) - *start_lsn =0 ; -while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) { -void *ptr; -int log_offset; - - error =xlog_state_get_iclog_space (log, len, &iclog, ticket, - &contwr, &log_offset); -if (error) -return error; - -ASSERT (log_offset <= iclog->ic_size -1 ); + error =xlog_state_get_iclog_space (log, len, &iclog, ticket, + &contwr, &log_offset); +if (error) +return error; -/* Start_lsn is the first lsn written to. */ -if (start_lsn && !*start_lsn) - *start_lsn =be64_to_cpu (iclog->ic_header.h_lsn); +/* start_lsn is the LSN of the first iclog written to. */ +if (start_lsn) + *start_lsn =be64_to_cpu (iclog->ic_header.h_lsn); -/* -* iclogs containing commit records or unmount records need -* to issue ordering cache flushes and commit immediately -* to stable storage to guarantee journal vs metadata ordering -* is correctly maintained in the storage media. -*/ -if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) { - iclog->ic_flags |= (XLOG_ICL_NEED_FLUSH | -XLOG_ICL_NEED_FUA ); - } +/* +* iclogs containing commit records or unmount records need +* to issue ordering cache flushes and commit immediately +* to stable storage to guarantee journal vs metadata ordering +* is correctly maintained in the storage media. This will always +* fit in the iclog we have been already been passed. +*/ +if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) { + iclog->ic_flags |= (XLOG_ICL_NEED_FLUSH |XLOG_ICL_NEED_FUA ); +ASSERT (!contwr); + } -/* If this is a single iclog write, go fast... */ -if (!contwr && lv == log_vector) { - record_cnt =xlog_write_single (lv, ticket, iclog, - log_offset, len); - len =0 ; - data_cnt = len; +while (lv) { + lv =xlog_write_single (lv, ticket, iclog, &log_offset, + &len, &record_cnt, &data_cnt); +if (!lv)break ; - } - -/* -* This loop writes out as many regions as can fit in the amount -* of space which was allocated by xlog_state_get_iclog_space(). -*/ - ptr = iclog->ic_datap + log_offset; -while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) { -struct xfs_log_iovec *reg; -struct xlog_op_header *ophdr; -int copy_len; -int copy_off; -bool ordered = false; -bool added_ophdr = false; - -/* ordered log vectors have no regions to write */ -if (lv->lv_buf_len ==XFS_LOG_VEC_ORDERED ) { -ASSERT (lv->lv_niovecs ==0 ); - ordered = true; - goto next_lv; - } - - reg = &vecp[index]; -ASSERT (reg->i_len %sizeof (int32_t ) ==0 ); -ASSERT ((unsigned long )ptr %sizeof (int32_t ) ==0 ); - -/* -* Regions always have their ophdr at the start of the -* region, except for: -* - a transaction start which has a start record ophdr -* before the first region ophdr; and -* - the previous region didn't fully fit into an iclog -* so needs a continuation ophdr to prepend the region -* in this new iclog. -*/ - ophdr = reg->i_addr; -if (optype && index) { - optype &= ~XLOG_START_TRANS ; - }else if (partial_copy) { - ophdr =xlog_write_setup_ophdr (ptr, ticket); -xlog_write_adv_cnt (&ptr, &len, &log_offset, -sizeof (struct xlog_op_header )); - added_ophdr = true; - } - ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); - - len +=xlog_write_setup_copy (ticket, ophdr, - iclog->ic_size-log_offset, - reg->i_len, - ©_off, ©_len, - &partial_copy, - &partial_copy_len); -xlog_verify_dest_ptr (log, ptr); - -/* -* Wart: need to update length in embedded ophdr not -* to include it's own length. -*/ -if (!added_ophdr) { - ophdr->oh_len =cpu_to_be32 (copy_len - -sizeof (struct xlog_op_header )); - } - -ASSERT (copy_len >0 ); -memcpy (ptr, reg->i_addr + copy_off, copy_len); -xlog_write_adv_cnt (&ptr, &len, &log_offset, copy_len); - -if (added_ophdr) - copy_len +=sizeof (struct xlog_op_header ); - record_cnt++; - data_cnt += contwr ? copy_len :0 ; - - error =xlog_write_copy_finish (log, iclog, optype, - &record_cnt, &data_cnt, - &partial_copy, - &partial_copy_len, - log_offset, - commit_iclog); -if (error) -return error; - -/* -* if we had a partial copy, we need to get more iclog -* space but we don't want to increment the region -* index because there is still more is this region to -* write. -* -* If we completed writing this region, and we flushed -* the iclog (indicated by resetting of the record -* count), then we also need to get more log space. If -* this was the last record, though, we are done and -* can just return. -*/ -if (partial_copy) -break ; - -if (++index == lv->lv_niovecs) { -next_lv: - lv = lv->lv_next; - index =0 ; -if (lv) - vecp = lv->lv_iovecp; - } -if (record_cnt ==0 && !ordered) { -if (!lv) -return 0 ; -break ; - } +ASSERT (!(optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS ))); + lv =xlog_write_partial (log, lv, ticket, &iclog, &log_offset, + &len, &record_cnt, &data_cnt, &contwr); +if (IS_ERR_OR_NULL (lv)) { + error =PTR_ERR_OR_ZERO (lv); +break ; } } +ASSERT ((len ==0 && !lv) || error); -ASSERT (len ==0 ); - +/* +* We've already been guaranteed that the last writes will fit inside +* the current iclog, and hence it will already have the space used by +* those writes accounted to it. Hence we do not need to update the +* iclog with the number of bytes written here. +*/ +ASSERT (!contwr ||XLOG_FORCED_SHUTDOWN (log));spin_lock (&log->l_icloglock); -xlog_state_finish_copy (log, iclog, record_cnt, data_cnt); +xlog_state_finish_copy (log, iclog, record_cnt,0 );if (commit_iclog) {ASSERT (optype &XLOG_COMMIT_TRANS ); *commit_iclog = iclog;
fs/xfs/xfs_log.c xlog_state_get_iclog_space( · line 2909
Quoted 5 lines
fs/xfs/xfs_log.c +1
-1@@ -2930,7 +2906,7 @@ xlog_state_get_iclog_space(* xlog_write() algorithm assumes that at least 2 xlog_op_header_t's * can fit into remaining data section. */ -if (iclog->ic_size - iclog->ic_offset <2 *sizeof (xlog_op_header_t)) { +if (iclog->ic_size - iclog->ic_offset <3 *sizeof (xlog_op_header_t)) {
Why does this change to 3? Does the comment need amending?
--D
Quoted 22 lines
fs/xfs/xfs_log.c +4
-3int error =0 ;xlog_state_switch_iclogs (log, iclog, iclog->ic_size);@@ -3633,11 +3609,12 @@ xlog_verify_iclog( iclog->ic_header.h_cycle_data[idx]); } } -if (clientid !=XFS_TRANSACTION && clientid !=XFS_LOG ) +if (clientid !=XFS_TRANSACTION && clientid !=XFS_LOG ) {xfs_warn (log->l_mp, -"%s: invalid clientid %d op " PTR_FMT " offset 0x%lx" , - __func__, clientid, ophead, +"%s: op %d invalid clientid %d op " PTR_FMT " offset 0x%lx" , + __func__, i, clientid, ophead, (unsigned long )field_offset); + }/* check length */ p = &ophead->oh_len;Signature · 1 line
2.28.0
Re: [PATCH 29/45] xfs:_introduce xlog_write_partial()
Quoted 51 lines · Darrick J. Wong
Quoted 50 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Handle writing of a logvec chain into an iclog that doesn't have enough space to fit it all. The iclog has already been changed to
WANT_SYNCbyxlog_get_iclog_space(), so the entire remaining space in the iclog is exclusively owned by this logvec chain.The difference between the single and partial cases is that we end up with partial iovec writes in the iclog and have to split a log vec regions across two iclogs. The state handling for this is currently awful and so we're building up the pieces needed to handle this more cleanly one at a time.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log.c | 525 ++++++++++++++++++++++------------------------- 1 file changed, 251 insertions(+), 274 deletions(-)fs/xfs/xfs_log.c +7
-15@@ -2099,166 +2099,250 @@ xlog_print_trans( } } -static xlog_op_header_t * -xlog_write_setup_ophdr ( -struct xlog_op_header *ophdr, -struct xlog_ticket *ticket) -{ - ophdr->oh_clientid =XFS_TRANSACTION ; - ophdr->oh_res2 =0 ; - ophdr->oh_flags =0 ; -return ophdr; -} -/* -* Set up the parameters of the region copy into the log. This has -* to handle region write split across multiple log buffers - this -* state is kept external to this function so that this code can -* be written in an obvious, self documenting manner. +* Write whole log vectors into a single iclog which is guaranteed to have +* either sufficient space for the entire log vector chain to be written or +* exclusive access to the remaining space in the iclog. +* +* Return the number of iovecs and data written into the iclog, as well as +* a pointer to the logvec that doesn't fit in the log (or NULL if we hit the +* end of the chain. */
Quoted 8 lines
fs/xfs/xfs_log.c xlog_print_trans( · line 2112
Quoted 4 lines
fs/xfs/xfs_log.c +2
-2-static int -xlog_write_setup_copy( +static struct xfs_log_vec * +xlog_write_single(Ouch. Could you fix the previous patch to move this new function a little higher in the file (like above
xlog_write_setup_ophdr) so that it doesn't get shredded like this?
Not possible because xlog_write_setup_ophdr() is removed by this patch. I can't help it if the diffs are unreadable - I can't really control what git is doing here...
Quoted 8 lines
fs/xfs/xfs_log.c xlog_state_get_iclog_space( · line 2909
Quoted 6 lines
fs/xfs/xfs_log.c +1
-1@@ -2930,7 +2906,7 @@ xlog_state_get_iclog_space(* xlog_write() algorithm assumes that at least 2 xlog_op_header_t's * can fit into remaining data section. */ -if (iclog->ic_size - iclog->ic_offset <2 *sizeof (xlog_op_header_t)) { +if (iclog->ic_size - iclog->ic_offset <3 *sizeof (xlog_op_header_t)) {Why does this change to 3? Does the comment need amending?
Ah, that was to do with the avoiding the need to split the start record/transaction header of across two iclogs. That was because the partial copy loop didn't have special handling for start records and so that log vector had to be wholly handled by the xlog_write_single() loop to set the iclog flush flags.
However, with all the changes since then that have added explicit pre-flushes before the start record is formatted and the lifting of the iclog flush flags to the callers, we've removed all the special optype handling in xlog_write(). Hence we no longer need to guarantee the start record is handled by the single path, it now can be handled by this partial path just fine. So I can revert this hunk.
Did I mention that this code was full of all sorts of subtle corner cases? :/
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 29/45] xfs:_introduce xlog_write_partial()
In reply to Dave ChinnerQuoted 7 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Handle writing of a logvec chain into an iclog that doesn't have enough space to fit it all. The iclog has already been changed to
WANT_SYNCbyxlog_get_iclog_space(), so the entire remaining space in the iclog is exclusively owned by this logvec chain.
Quoted 8 lines
The difference between the single and partial cases is that we end up with partial iovec writes in the iclog and have to split a log vec regions across two iclogs. The state handling for this is currently awful and so we're building up the pieces needed to handle this more cleanly one at a time.
Signed-off-by Dave Chinner <[email protected]>---
FWIW, git --patience mode generates a more readable diff for this patch than what it generates by default. I'm referring to that locally and will try to leave feedback in the appropriate points here.
Quoted 30 lines
fs/xfs/xfs_log.c | 525 ++++++++++++++++++++++------------------------- 1 file changed, 251 insertions(+), 274 deletions(-)fs/xfs/xfs_log.c +3
-15@@ -2099,166 +2099,250 @@ xlog_print_trans( } } -static xlog_op_header_t * -xlog_write_setup_ophdr ( -struct xlog_op_header *ophdr, -struct xlog_ticket *ticket) -{ - ophdr->oh_clientid =XFS_TRANSACTION ; - ophdr->oh_res2 =0 ; - ophdr->oh_flags =0 ; -return ophdr; -} -/* -* Set up the parameters of the region copy into the log. This has -* to handle region write split across multiple log buffers - this -* state is kept external to this function so that this code can -* be written in an obvious, self documenting manner. +* Write whole log vectors into a single iclog which is guaranteed to have +* either sufficient space for the entire log vector chain to be written or +* exclusive access to the remaining space in the iclog.
fs/xfs/xfs_log.c xlog_print_trans( · line 2113
Quoted 10 lines
fs/xfs/xfs_log.c +7
-2+* +* Return the number of iovecs and data written into the iclog, as well as +* a pointer to the logvec that doesn't fit in the log (or NULL if we hit the +* end of the chain. */ -static int -xlog_write_setup_copy( +static struct xfs_log_vec * +xlog_write_single( + struct xfs_log_vec *log_vector,
So xlog_write_single() was initially for single CIL xlog_write() calls and now it appears to be slightly different in that it writes as many full log vectors that fit in the current iclog and cycles through xlog_write_partial() (and back) to process log vectors that span iclogs differently from those that don't.
Quoted 179 lines
fs/xfs/xfs_log.c +72
-87struct xlog_ticket *ticket, - struct xlog_op_header *ophdr, - int space_available, - int space_required, - int *copy_off, - int *copy_len, - int *last_was_partial_copy, - int *bytes_consumed) -{ - int still_to_copy; - - still_to_copy = space_required - *bytes_consumed; - *copy_off = *bytes_consumed; - -if (still_to_copy <= space_available) { -/* write of region completes here */ - *copy_len = still_to_copy; - ophdr->oh_len =cpu_to_be32 (*copy_len); -if (*last_was_partial_copy) - ophdr->oh_flags |= (XLOG_END_TRANS |XLOG_WAS_CONT_TRANS ); - *last_was_partial_copy =0 ; - *bytes_consumed =0 ; -return 0 ; - } - -/* partial write of region, needs extra log op header reservation */ - *copy_len = space_available; - ophdr->oh_len =cpu_to_be32 (*copy_len); - ophdr->oh_flags |=XLOG_CONTINUE_TRANS ; -if (*last_was_partial_copy) - ophdr->oh_flags |=XLOG_WAS_CONT_TRANS ; - *bytes_consumed += *copy_len; - (*last_was_partial_copy)++; - -/* account for new log op header */ - ticket->t_curr_res -=sizeof (struct xlog_op_header ); - -return sizeof (struct xlog_op_header ); -} - -static int -xlog_write_copy_finish ( -struct xlog *log, struct xlog_in_core *iclog, -uint flags, -int *record_cnt, -int *data_cnt, -int *partial_copy, -int *partial_copy_len, -int log_offset, -struct xlog_in_core **commit_iclog) + uint32_t *log_offset, + uint32_t *len, + uint32_t *record_cnt, + uint32_t *data_cnt) { -int error; + struct xfs_log_vec *lv = log_vector; +void *ptr; +int index; -if (*partial_copy) { +ASSERT (*log_offset + *len <= iclog->ic_size || + iclog->ic_state ==XLOG_STATE_WANT_SYNC ); + + ptr = iclog->ic_datap + *log_offset; +for (lv = log_vector; lv; lv = lv->lv_next) {/* -* This iclog has already been marked WANT_SYNC by -* xlog_state_get_iclog_space. +* If the entire log vec does not fit in the iclog, punt it to +* the partial copy loop which can handle this case. */ -spin_lock (&log->l_icloglock); -xlog_state_finish_copy (log, iclog, *record_cnt, *data_cnt); - *record_cnt =0 ; - *data_cnt =0 ; - goto release_iclog; - } +if (lv->lv_niovecs && + lv->lv_bytes > iclog->ic_size - *log_offset) +break ; - *partial_copy =0 ; - *partial_copy_len =0 ; +/* +* Ordered log vectors have no regions to write so this +* loop will naturally skip them. +*/ +for (index =0 ; index < lv->lv_niovecs; index++) { +struct xfs_log_iovec *reg = &lv->lv_iovecp[index]; +struct xlog_op_header *ophdr = reg->i_addr; -if (iclog->ic_size - log_offset <=sizeof (xlog_op_header_t)) { -/* no more space in this iclog - push it. */ -spin_lock (&log->l_icloglock); -xlog_state_finish_copy (log, iclog, *record_cnt, *data_cnt); - *record_cnt =0 ; - *data_cnt =0 ; +ASSERT (reg->i_len %sizeof (int32_t ) ==0 ); +ASSERT ((unsigned long )ptr %sizeof (int32_t ) ==0 ); -if (iclog->ic_state ==XLOG_STATE_ACTIVE ) -xlog_state_switch_iclogs (log, iclog,0 ); -else -ASSERT (iclog->ic_state ==XLOG_STATE_WANT_SYNC || - iclog->ic_state ==XLOG_STATE_IOERROR ); -if (!commit_iclog) - goto release_iclog; -spin_unlock (&log->l_icloglock); -ASSERT (flags &XLOG_COMMIT_TRANS ); - *commit_iclog = iclog; + ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); + ophdr->oh_len =cpu_to_be32 (reg->i_len - +sizeof (struct xlog_op_header )); +memcpy (ptr, reg->i_addr, reg->i_len); +xlog_write_adv_cnt (&ptr, len, log_offset, reg->i_len); + (*record_cnt)++; + *data_cnt += reg->i_len; + } } +ASSERT (*len ==0 || lv); +return lv; +} -return 0 ; +static int +xlog_write_get_more_iclog_space ( +struct xlog *log, +struct xlog_ticket *ticket, +struct xlog_in_core **iclogp, +uint32_t *log_offset, +uint32_t len, +uint32_t *record_cnt, +uint32_t *data_cnt, +int *contwr) +{ +struct xlog_in_core *iclog = *iclogp; +int error; -release_iclog: +spin_lock (&log->l_icloglock); +xlog_state_finish_copy (log, iclog, *record_cnt, *data_cnt); +ASSERT (iclog->ic_state ==XLOG_STATE_WANT_SYNC || + iclog->ic_state ==XLOG_STATE_IOERROR ); error =xlog_state_release_iclog (log, iclog);spin_unlock (&log->l_icloglock); -return error; +if (error) +return error; + + error =xlog_state_get_iclog_space (log, len, &iclog, + ticket, contwr, log_offset); +if (error) +return error; + *record_cnt =0 ; + *data_cnt =0 ; + *iclogp = iclog; +return 0 ; }/* -* Write log vectors into a single iclog which is guaranteed by the caller -* to have enough space to write the entire log vector into. Return the number -* of log vectors written into the iclog. +* Write log vectors into a single iclog which is smaller than the current chain +* length. We write until we cannot fit a full record into the remaining space +* and then stop. We return the log vector that is to be written that cannot +* wholly fit in the iclog. */ -static int -xlog_write_single( +static struct xfs_log_vec * +xlog_write_partial( + struct xlog *log, struct xfs_log_vec *log_vector, struct xlog_ticket *ticket, - struct xlog_in_core *iclog, - uint32_t log_offset,
fs/xfs/xfs_log.c xlog_print_trans( · line 2214
Quoted 10 lines
fs/xfs/xfs_log.c +7
-1- uint32_t len) +struct xlog_in_core **iclogp, + uint32_t *log_offset, + uint32_t *len, + uint32_t *record_cnt, + uint32_t *data_cnt, + int *contwr) { + struct xlog_in_core *iclog = *iclogp;struct xfs_log_vec *lv = log_vector;
The log_vector -> lv assignment seems spurious at this point since this function only processes lv and returns the next.
Quoted 52 lines
fs/xfs/xfs_log.c +34
-11+struct xfs_log_iovec *reg; +struct xlog_op_header *ophdr;void *ptr;int index =0 ; -int record_cnt =0 ; +uint32_t rlen; +int error; -ASSERT (log_offset + len <= iclog->ic_size); +/* walk the logvec, copying until we run out of space in the iclog */ + ptr = iclog->ic_datap + *log_offset; +for (index =0 ; index < lv->lv_niovecs; index++) { +uint32_t reg_offset =0 ; + + reg = &lv->lv_iovecp[index]; +ASSERT (reg->i_len %sizeof (int32_t ) ==0 ); - ptr = iclog->ic_datap + log_offset; -for (lv = log_vector; lv; lv = lv->lv_next) {/* -* Ordered log vectors have no regions to write so this -* loop will naturally skip them. +* The first region of a continuation must have a non-zero +* length otherwise log recovery will just skip over it and +* start recovering from the next opheader it finds. Because we +* mark the next opheader as a continuation, recovery will then +* incorrectly add the continuation to the previous region and +* that breaks stuff. +* +* Hence if there isn't space for region data after the +* opheader, then we need to start afresh with a new iclog. */ -for (index =0 ; index < lv->lv_niovecs; index++) { -struct xfs_log_iovec *reg = &lv->lv_iovecp[index]; -struct xlog_op_header *ophdr = reg->i_addr; +if (iclog->ic_size - *log_offset <= +sizeof (struct xlog_op_header )) { + error =xlog_write_get_more_iclog_space (log, ticket, + &iclog, log_offset, *len, record_cnt, + data_cnt, contwr); +if (error) +return ERR_PTR (error); + ptr = iclog->ic_datap + *log_offset; + } -ASSERT (reg->i_len %sizeof (int32_t ) ==0 ); -ASSERT ((unsigned long )ptr %sizeof (int32_t ) ==0 ); + ophdr = reg->i_addr; + rlen =min_t (uint32_t, reg->i_len, iclog->ic_size - *log_offset); + + ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); + ophdr->oh_len =cpu_to_be32 (rlen -sizeof (struct xlog_op_header ));
fs/xfs/xfs_log.c xlog_print_trans( · line 2265
Quoted 10 lines
fs/xfs/xfs_log.c +9
-0+if (rlen != reg->i_len) + ophdr->oh_flags |=XLOG_CONTINUE_TRANS ; +ASSERT ((unsigned long )ptr %sizeof (int32_t ) ==0 ); +xlog_verify_dest_ptr (log, ptr); +memcpy (ptr, reg->i_addr, rlen); +xlog_write_adv_cnt (&ptr, len, log_offset, rlen); + (*record_cnt)++; + *data_cnt += rlen; +
/* if we fit the full region, jump to the next */
Quoted 13 lines
fs/xfs/xfs_log.c +13
-0+if (rlen == reg->i_len) +continue ; + +/* +* We now have a partially written iovec, but it can span +* multiple iclogs so we loop here. First we release the iclog +* we currently have, then we get a new iclog and add a new +* opheader. Then we continue copying from where we were until +* we either complete the iovec or fill the iclog. If we +* complete the iovec, then we increment the index and go right +* back to the top of the outer loop. if we fill the iclog, we +* run the inner loop again. +*
fs/xfs/xfs_log.c xlog_print_trans( · line 2288
Quoted 10 lines
fs/xfs/xfs_log.c +10
-0+* This is complicated by the tail of a region using all the +* space in an iclog and hence requiring us to release the iclog +* and get a new one before returning to the outer loop. We must +* always guarantee that we exit this inner loop with at least +* space for log transaction opheaders left in the current +* iclog, hence we cannot just terminate the loop at the end +* of the of the continuation. So we loop while there is no +* space left in the current iclog, and check for the end of the +* continuation after getting a new iclog. +*/
Ok, so we land in this function if an lv spans an iclog boundary. The upper loop writes full vectors until we hit said iclog boundary, then we fall into the inner loop...
fs/xfs/xfs_log.c xlog_print_trans( · line 2295
Quoted 7 lines
fs/xfs/xfs_log.c +7
-0+ do { + /* + * Account for the continuation opheader before we get + * a new iclog. This is necessary so that we reserve + * space in the iclog for it. + */ + if (ophdr->oh_flags & XLOG_CONTINUE_TRANS) {
(Is this ever not true here?)
Quoted 15 lines
fs/xfs/xfs_log.c +13
-1+ *len +=sizeof (struct xlog_op_header ); + ticket->t_curr_res -=sizeof (struct xlog_op_header ); + } + error =xlog_write_get_more_iclog_space (log, ticket, + &iclog, log_offset, *len, record_cnt, + data_cnt, contwr); +if (error) +return ERR_PTR (error); + ptr = iclog->ic_datap + *log_offset; + + ophdr = ptr; ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); - ophdr->oh_len = cpu_to_be32(reg->i_len - + ophdr->oh_clientid =XFS_TRANSACTION ; + ophdr->oh_res2 =0 ;
fs/xfs/xfs_log.c xlog_print_trans( · line 2315
Quoted 10 lines
fs/xfs/xfs_log.c +5
-4+ ophdr->oh_flags =XLOG_WAS_CONT_TRANS ; + +xlog_write_adv_cnt (&ptr, len, log_offset,sizeof (struct xlog_op_header )); -memcpy (ptr, reg->i_addr, reg->i_len); -xlog_write_adv_cnt (&ptr, &len, &log_offset, reg->i_len); - record_cnt++; - } + *data_cnt +=sizeof (struct xlog_op_header ); +
... which switches to the next iclog, writes the continuation header...
Quoted 11 lines
fs/xfs/xfs_log.c +11
-0+/* +* If rlen fits in the iclog, then end the region +* continuation. Otherwise we're going around again. +*/ + reg_offset += rlen; + rlen = reg->i_len - reg_offset; +if (rlen <= iclog->ic_size - *log_offset) + ophdr->oh_flags |=XLOG_END_TRANS ; +else + ophdr->oh_flags |=XLOG_CONTINUE_TRANS ; +
fs/xfs/xfs_log.c xlog_print_trans( · line 2336
Quoted 10 lines
fs/xfs/xfs_log.c +10
-0+ rlen =min_t (uint32_t, rlen, iclog->ic_size - *log_offset); + ophdr->oh_len =cpu_to_be32 (rlen); + +xlog_verify_dest_ptr (log, ptr); +memcpy (ptr, reg->i_addr + reg_offset, rlen); +xlog_write_adv_cnt (&ptr, len, log_offset, rlen); + (*record_cnt)++; + *data_cnt += rlen; + + }while (ophdr->oh_flags &XLOG_CONTINUE_TRANS );
... writes more of the region (iclog space permitting), and then determines whether we need further continuations (and partial writes of the same region) or can move onto the next region, until we're done with the lv.
I think I follow the high level flow and it seems reasonable from a functional standpoint, but this also seems like quite a bit of churn for not much reduction in overall complexity. The higher level loop is much more simple and I think the per lv/vector iteration is an improvement, but we also seem to have duplicate functionality throughout the updated code and have introduced new forms of complexity around the state expectations for the transitions between the different write modes and between each write mode and the higher level loop.
I.e., xlog_write_single() implements a straighforward loop to write out full log vectors. That seems fine, but the outer loop of xlog_write_partial() reimplements nearly the same per-region functionality with some added flexibility to handle op header flags and the special iclog processing associated with the continuation case. The inner loop factors out the continuation iclog management bits and op header injection, which I think is an improvement, but then duplicates region copying (yet again) pretty much only to implement partial copies, which really just involves offset management (i.e., fairly trivial relative to the broader complexity of the function).
I dunno. I'd certainly need to stare more at this to cover all of the details, but given the amount of swizzling going on in a single patch I'm kind of wondering if/why we couldn't land on a single iterator in the spirit of xlog_write_partial() in that it primarily iterates on regions and factors out the grotty reservation and continuation management bits, but doesn't unroll as much and leave so much duplicate functionality around.
For example, it looks to me that xlog_write_partial() almost nearly already supports a high level algorithm along the lines of the following (pseudocode):
xlog_write(len)
{
get_iclog_space(len)
for_each_lv() {
for_each_reg() {
reg_offset = 0;
cont_write:
/* write as much as will fit in the iclog, return count,
* and set ophdr cont flag based on write result */
reg_offset += write_region(reg, &len, ®_offset, ophdr, ...);
/* handle continuation writes */
if (reg_offset != reg->i_len) {
get_more_iclog_space(len);
/* stamp a WAS_CONT op hdr, set END if rlen fits
* into new space, then continue with the same region */
stamp_cont_op_hdr();
goto cont_write;
}
if (need_more_iclog_space(len))
get_more_iclog_space(len);
}
}
}That puts the whole thing back into a single high level walk and thus reintroduces the need for some of the continuation vs. non-continuation tracking wrt to the op header and iclog, but ISTM that complexity can be managed by the continuation abstraction you've already started to introduce (as opposed to the current scheme of conditionally accumulating data_cnt). It might even be fine to dump some of the requisite state into a context struct to carry between iclog reservation and copy finish processing rather than pass around so many independent and poorly named variables like the current upstream implementation does, but that's probably getting too deep into the weeds.
FWIW, I can also see an approach of moving from the implementation in this patch toward something like the above, but I'm not sure I'd want to subject to the upstream code to that process...
Brian
Quoted 254 lines
fs/xfs/xfs_log.c +50
-153} -ASSERT (len ==0 ); -return record_cnt; -} +/* +* No more iovecs remain in this logvec so return the next log vec to +* the caller so it can go back to fast path copying. +*/ + *iclogp = iclog; +return lv->lv_next; +}/* * Write some region out to in-core log @@ -2312,14 +2396,11 @@ xlog_write( {struct xlog_in_core *iclog =NULL ;struct xfs_log_vec *lv = log_vector; -struct xfs_log_iovec *vecp = lv->lv_iovecp; -int index =0 ; -int partial_copy =0 ; -int partial_copy_len =0 ;int contwr =0 ;int record_cnt =0 ;int data_cnt =0 ;int error =0 ; +int log_offset;if (ticket->t_curr_res <0 ) { xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES,@@ -2328,157 +2409,52 @@ xlog_write(xfs_force_shutdown (log->l_mp,SHUTDOWN_LOG_IO_ERROR ); } -if (start_lsn) - *start_lsn =0 ; -while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) { -void *ptr; -int log_offset; - - error =xlog_state_get_iclog_space (log, len, &iclog, ticket, - &contwr, &log_offset); -if (error) -return error; - -ASSERT (log_offset <= iclog->ic_size -1 ); + error =xlog_state_get_iclog_space (log, len, &iclog, ticket, + &contwr, &log_offset); +if (error) +return error; -/* Start_lsn is the first lsn written to. */ -if (start_lsn && !*start_lsn) - *start_lsn =be64_to_cpu (iclog->ic_header.h_lsn); +/* start_lsn is the LSN of the first iclog written to. */ +if (start_lsn) + *start_lsn =be64_to_cpu (iclog->ic_header.h_lsn); -/* -* iclogs containing commit records or unmount records need -* to issue ordering cache flushes and commit immediately -* to stable storage to guarantee journal vs metadata ordering -* is correctly maintained in the storage media. -*/ -if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) { - iclog->ic_flags |= (XLOG_ICL_NEED_FLUSH | -XLOG_ICL_NEED_FUA ); - } +/* +* iclogs containing commit records or unmount records need +* to issue ordering cache flushes and commit immediately +* to stable storage to guarantee journal vs metadata ordering +* is correctly maintained in the storage media. This will always +* fit in the iclog we have been already been passed. +*/ +if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) { + iclog->ic_flags |= (XLOG_ICL_NEED_FLUSH |XLOG_ICL_NEED_FUA ); +ASSERT (!contwr); + } -/* If this is a single iclog write, go fast... */ -if (!contwr && lv == log_vector) { - record_cnt =xlog_write_single (lv, ticket, iclog, - log_offset, len); - len =0 ; - data_cnt = len; +while (lv) { + lv =xlog_write_single (lv, ticket, iclog, &log_offset, + &len, &record_cnt, &data_cnt); +if (!lv)break ; - } - -/* -* This loop writes out as many regions as can fit in the amount -* of space which was allocated by xlog_state_get_iclog_space(). -*/ - ptr = iclog->ic_datap + log_offset; -while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) { -struct xfs_log_iovec *reg; -struct xlog_op_header *ophdr; -int copy_len; -int copy_off; -bool ordered = false; -bool added_ophdr = false; - -/* ordered log vectors have no regions to write */ -if (lv->lv_buf_len ==XFS_LOG_VEC_ORDERED ) { -ASSERT (lv->lv_niovecs ==0 ); - ordered = true; - goto next_lv; - } - - reg = &vecp[index]; -ASSERT (reg->i_len %sizeof (int32_t ) ==0 ); -ASSERT ((unsigned long )ptr %sizeof (int32_t ) ==0 ); - -/* -* Regions always have their ophdr at the start of the -* region, except for: -* - a transaction start which has a start record ophdr -* before the first region ophdr; and -* - the previous region didn't fully fit into an iclog -* so needs a continuation ophdr to prepend the region -* in this new iclog. -*/ - ophdr = reg->i_addr; -if (optype && index) { - optype &= ~XLOG_START_TRANS ; - }else if (partial_copy) { - ophdr =xlog_write_setup_ophdr (ptr, ticket); -xlog_write_adv_cnt (&ptr, &len, &log_offset, -sizeof (struct xlog_op_header )); - added_ophdr = true; - } - ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); - - len +=xlog_write_setup_copy (ticket, ophdr, - iclog->ic_size-log_offset, - reg->i_len, - ©_off, ©_len, - &partial_copy, - &partial_copy_len); -xlog_verify_dest_ptr (log, ptr); - -/* -* Wart: need to update length in embedded ophdr not -* to include it's own length. -*/ -if (!added_ophdr) { - ophdr->oh_len =cpu_to_be32 (copy_len - -sizeof (struct xlog_op_header )); - } - -ASSERT (copy_len >0 ); -memcpy (ptr, reg->i_addr + copy_off, copy_len); -xlog_write_adv_cnt (&ptr, &len, &log_offset, copy_len); - -if (added_ophdr) - copy_len +=sizeof (struct xlog_op_header ); - record_cnt++; - data_cnt += contwr ? copy_len :0 ; - - error =xlog_write_copy_finish (log, iclog, optype, - &record_cnt, &data_cnt, - &partial_copy, - &partial_copy_len, - log_offset, - commit_iclog); -if (error) -return error; - -/* -* if we had a partial copy, we need to get more iclog -* space but we don't want to increment the region -* index because there is still more is this region to -* write. -* -* If we completed writing this region, and we flushed -* the iclog (indicated by resetting of the record -* count), then we also need to get more log space. If -* this was the last record, though, we are done and -* can just return. -*/ -if (partial_copy) -break ; - -if (++index == lv->lv_niovecs) { -next_lv: - lv = lv->lv_next; - index =0 ; -if (lv) - vecp = lv->lv_iovecp; - } -if (record_cnt ==0 && !ordered) { -if (!lv) -return 0 ; -break ; - } +ASSERT (!(optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS ))); + lv =xlog_write_partial (log, lv, ticket, &iclog, &log_offset, + &len, &record_cnt, &data_cnt, &contwr); +if (IS_ERR_OR_NULL (lv)) { + error =PTR_ERR_OR_ZERO (lv); +break ; } } +ASSERT ((len ==0 && !lv) || error); -ASSERT (len ==0 ); - +/* +* We've already been guaranteed that the last writes will fit inside +* the current iclog, and hence it will already have the space used by +* those writes accounted to it. Hence we do not need to update the +* iclog with the number of bytes written here. +*/ +ASSERT (!contwr ||XLOG_FORCED_SHUTDOWN (log));spin_lock (&log->l_icloglock); -xlog_state_finish_copy (log, iclog, record_cnt, data_cnt); +xlog_state_finish_copy (log, iclog, record_cnt,0 );if (commit_iclog) {ASSERT (optype &XLOG_COMMIT_TRANS ); *commit_iclog = iclog;@@ -2930,7 +2906,7 @@ xlog_state_get_iclog_space(* xlog_write() algorithm assumes that at least 2 xlog_op_header_t's * can fit into remaining data section. */ -if (iclog->ic_size - iclog->ic_offset <2 *sizeof (xlog_op_header_t)) { +if (iclog->ic_size - iclog->ic_offset <3 *sizeof (xlog_op_header_t)) {int error =0 ;xlog_state_switch_iclogs (log, iclog, iclog->ic_size);@@ -3633,11 +3609,12 @@ xlog_verify_iclog( iclog->ic_header.h_cycle_data[idx]); } } -if (clientid !=XFS_TRANSACTION && clientid !=XFS_LOG ) +if (clientid !=XFS_TRANSACTION && clientid !=XFS_LOG ) {xfs_warn (log->l_mp, -"%s: invalid clientid %d op " PTR_FMT " offset 0x%lx" , - __func__, clientid, ophead, +"%s: op %d invalid clientid %d op " PTR_FMT " offset 0x%lx" , + __func__, i, clientid, ophead, (unsigned long )field_offset); + }/* check length */ p = &ophead->oh_len;Signature · 1 line
2.28.0
Re: [PATCH 29/45] xfs:_introduce xlog_write_partial()
Quoted 59 lines · Brian Foster
Quoted 7 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Handle writing of a logvec chain into an iclog that doesn't have enough space to fit it all. The iclog has already been changed to
WANT_SYNCbyxlog_get_iclog_space(), so the entire remaining space in the iclog is exclusively owned by this logvec chain.Quoted 8 lines
The difference between the single and partial cases is that we end up with partial iovec writes in the iclog and have to split a log vec regions across two iclogs. The state handling for this is currently awful and so we're building up the pieces needed to handle this more cleanly one at a time.
Signed-off-by Dave Chinner <[email protected]>---
FWIW, git --patience mode generates a more readable diff for this patch than what it generates by default. I'm referring to that locally and will try to leave feedback in the appropriate points here.
Quoted 38 lines
fs/xfs/xfs_log.c | 525 ++++++++++++++++++++++------------------------- 1 file changed, 251 insertions(+), 274 deletions(-)fs/xfs/xfs_log.c +8
-17@@ -2099,166 +2099,250 @@ xlog_print_trans( } } -static xlog_op_header_t * -xlog_write_setup_ophdr ( -struct xlog_op_header *ophdr, -struct xlog_ticket *ticket) -{ - ophdr->oh_clientid =XFS_TRANSACTION ; - ophdr->oh_res2 =0 ; - ophdr->oh_flags =0 ; -return ophdr; -} -/* -* Set up the parameters of the region copy into the log. This has -* to handle region write split across multiple log buffers - this -* state is kept external to this function so that this code can -* be written in an obvious, self documenting manner. +* Write whole log vectors into a single iclog which is guaranteed to have +* either sufficient space for the entire log vector chain to be written or +* exclusive access to the remaining space in the iclog. +* +* Return the number of iovecs and data written into the iclog, as well as +* a pointer to the logvec that doesn't fit in the log (or NULL if we hit the +* end of the chain. */ -static int -xlog_write_setup_copy( +static struct xfs_log_vec *
Quoted 8 lines
fs/xfs/xfs_log.c xlog_print_trans( · line 2113
Quoted 2 lines
fs/xfs/xfs_log.c +2
-0+xlog_write_single ( + struct xfs_log_vec *log_vector,So
xlog_write_single()was initially for single CILxlog_write()calls and now it appears to be slightly different in that it writes as many full log vectors that fit in the current iclog and cycles throughxlog_write_partial()(and back) to process log vectors that span iclogs differently from those that don't.
Yes, that is what it does, but no, you've got the process and meaning backwards. I wrote xlog_write_single() it as it appears in this patch first, then split it out backwards to ease review. IOWs, "single" means "write everything that fits within this single iclog", not "only call this function if the entire lv chain fits inside a single iclog".
The latter is what I split out to make it simpler to review, but it was not the reason it was called xlog_write_single()....
Quoted 9 lines
fs/xfs/xfs_log.c xlog_print_trans( · line 2295
Quoted 7 lines
fs/xfs/xfs_log.c +7
-0+ do { + /* + * Account for the continuation opheader before we get + * a new iclog. This is necessary so that we reserve + * space in the iclog for it. + */ + if (ophdr->oh_flags & XLOG_CONTINUE_TRANS) {(Is this ever not true here?)
It is now, wasn't always. Fixed.
Quoted 47 lines
Quoted 16 lines
fs/xfs/xfs_log.c +13
-1+ *len +=sizeof (struct xlog_op_header ); + ticket->t_curr_res -=sizeof (struct xlog_op_header ); + } + error =xlog_write_get_more_iclog_space (log, ticket, + &iclog, log_offset, *len, record_cnt, + data_cnt, contwr); +if (error) +return ERR_PTR (error); + ptr = iclog->ic_datap + *log_offset; + + ophdr = ptr; ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); - ophdr->oh_len = cpu_to_be32(reg->i_len - + ophdr->oh_clientid =XFS_TRANSACTION ; + ophdr->oh_res2 =0 ;fs/xfs/xfs_log.c xlog_print_trans( · line 2315
Quoted 10 lines
fs/xfs/xfs_log.c +5
-4+ ophdr->oh_flags =XLOG_WAS_CONT_TRANS ; + +xlog_write_adv_cnt (&ptr, len, log_offset,sizeof (struct xlog_op_header )); -memcpy (ptr, reg->i_addr, reg->i_len); -xlog_write_adv_cnt (&ptr, &len, &log_offset, reg->i_len); - record_cnt++; - } + *data_cnt +=sizeof (struct xlog_op_header ); +... which switches to the next iclog, writes the continuation header...
Quoted 18 lines
fs/xfs/xfs_log.c +18
-0+/* +* If rlen fits in the iclog, then end the region +* continuation. Otherwise we're going around again. +*/ + reg_offset += rlen; + rlen = reg->i_len - reg_offset; +if (rlen <= iclog->ic_size - *log_offset) + ophdr->oh_flags |=XLOG_END_TRANS ; +else + ophdr->oh_flags |=XLOG_CONTINUE_TRANS ; + + rlen =min_t (uint32_t, rlen, iclog->ic_size - *log_offset); + ophdr->oh_len =cpu_to_be32 (rlen); + +xlog_verify_dest_ptr (log, ptr); +memcpy (ptr, reg->i_addr + reg_offset, rlen); +xlog_write_adv_cnt (&ptr, len, log_offset, rlen); + (*record_cnt)++;
Quoted 8 lines
fs/xfs/xfs_log.c xlog_print_trans( · line 2336
Quoted 3 lines
fs/xfs/xfs_log.c +3
-0+ *data_cnt += rlen; + + }while (ophdr->oh_flags &XLOG_CONTINUE_TRANS );... writes more of the region (iclog space permitting), and then determines whether we need further continuations (and partial writes of the same region) or can move onto the next region, until we're done with the lv.
Yup.
Quoted 8 lines
I think I follow the high level flow and it seems reasonable from a functional standpoint, but this also seems like quite a bit of churn for not much reduction in overall complexity. The higher level loop is much more simple and I think the per lv/vector iteration is an improvement, but we also seem to have duplicate functionality throughout the updated code and have introduced new forms of complexity around the state expectations for the transitions between the different write modes and between each write mode and the higher level loop.
Just getting untangling the code to get it to this point has been hard enough. I've held off doing more factoring and changing this code so I can actaully test it and find the bugs I might have left in it.
Yes, it can be further improved by factoring the region copying stuff, but that's secondary to the major work of refactoring this code in the first place. The fact that you actually understood this fairly easily indicates just how much better this code already is compared to what is currently upstream....
Quoted 41 lines
I.e.,
xlog_write_single()implements a straighforward loop to write out full log vectors. That seems fine, but the outer loop ofxlog_write_partial()reimplements nearly the same per-region functionality with some added flexibility to handle op header flags and the special iclog processing associated with the continuation case. The inner loop factors out the continuation iclog management bits and op header injection, which I think is an improvement, but then duplicates region copying (yet again) pretty much only to implement partial copies, which really just involves offset management (i.e., fairly trivial relative to the broader complexity of the function).I dunno. I'd certainly need to stare more at this to cover all of the details, but given the amount of swizzling going on in a single patch I'm kind of wondering if/why we couldn't land on a single iterator in the spirit of
xlog_write_partial()in that it primarily iterates on regions and factors out the grotty reservation and continuation management bits, but doesn't unroll as much and leave so much duplicate functionality around.For example, it looks to me that
xlog_write_partial()almost nearly already supports a high level algorithm along the lines of the following (pseudocode):
xlog_write(len){ get_iclog_space(len) for_each_lv() { for_each_reg() { reg_offset = 0; cont_write: /* write as much as will fit in the iclog, return count, * and set ophdr cont flag based on write result */ reg_offset += write_region(reg, &len, ®_offset, ophdr, ...); /* handle continuation writes */ if (reg_offset != reg->i_len) { get_more_iclog_space(len); /* stamp a WAS_CONT op hdr, set END if rlen fits * into new space, then continue with the same region */ stamp_cont_op_hdr();
Quoted 8 lines
goto cont_write; } if (need_more_iclog_space(len)) get_more_iclog_space(len); } } }
Yeah, na. That is exactly the mess that I've just untangled.
I don't want to rewrite this code again, and I don't want it more tightly tied to iclogs than it already is - I'm trying to move the code towards a common, simple fast path that knows nothing about iclogs and a slow path that handles the partial regions and obtaining a new buffer to write into. I want the two cases completely separate logic, because that makes both cases simpler to modify and reason about.
Indeed, I want xlog_write to move away from iclogs because I want to use this code with direct mapped pmem regions, not just fixed memory buffers held in iclogs.
IOWs, the code as it stands is a beginning, not an end. And even as a beginning, it works, is much better and faster than the current code, has been tested for some time now, can be further factored to make it simpler, easier to understand and provide infrastructure for new features.
Quoted 11 lines
That puts the whole thing back into a single high level walk and thus reintroduces the need for some of the continuation vs. non-continuation tracking wrt to the op header and iclog, but ISTM that complexity can be managed by the continuation abstraction you've already started to introduce (as opposed to the current scheme of conditionally accumulating
data_cnt). It might even be fine to dump some of the requisite state into a context struct to carry between iclog reservation and copy finish processing rather than pass around so many independent and poorly named variables like the current upstream implementation does, but that's probably getting too deep into the weeds.
Quoted 3 lines
FWIW, I can also see an approach of moving from the implementation in this patch toward something like the above, but I'm not sure I'd want to subject to the upstream code to that process...
This is exactly what upstream is for - iterative improvement via small steps. This is the first step of many, and what you propose takes the code in the wrong direction for the steps I've already taken and are planning to take.
Perfect is the enemy of good, and if upstream is not the place to make iterative improvements like this that build towards a bigger picture goal, then where the hell are we supposed to do them?
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 29/45] xfs:_introduce xlog_write_partial()
Quoted 163 lines · Dave Chinner
Quoted 59 lines · Brian Foster
Quoted 7 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Handle writing of a logvec chain into an iclog that doesn't have enough space to fit it all. The iclog has already been changed to
WANT_SYNCbyxlog_get_iclog_space(), so the entire remaining space in the iclog is exclusively owned by this logvec chain.Quoted 8 lines
The difference between the single and partial cases is that we end up with partial iovec writes in the iclog and have to split a log vec regions across two iclogs. The state handling for this is currently awful and so we're building up the pieces needed to handle this more cleanly one at a time.
Signed-off-by Dave Chinner <[email protected]>---
FWIW, git --patience mode generates a more readable diff for this patch than what it generates by default. I'm referring to that locally and will try to leave feedback in the appropriate points here.
Quoted 38 lines
fs/xfs/xfs_log.c | 525 ++++++++++++++++++++++------------------------- 1 file changed, 251 insertions(+), 274 deletions(-)fs/xfs/xfs_log.c +8
-17@@ -2099,166 +2099,250 @@ xlog_print_trans( } } -static xlog_op_header_t * -xlog_write_setup_ophdr ( -struct xlog_op_header *ophdr, -struct xlog_ticket *ticket) -{ - ophdr->oh_clientid =XFS_TRANSACTION ; - ophdr->oh_res2 =0 ; - ophdr->oh_flags =0 ; -return ophdr; -} -/* -* Set up the parameters of the region copy into the log. This has -* to handle region write split across multiple log buffers - this -* state is kept external to this function so that this code can -* be written in an obvious, self documenting manner. +* Write whole log vectors into a single iclog which is guaranteed to have +* either sufficient space for the entire log vector chain to be written or +* exclusive access to the remaining space in the iclog. +* +* Return the number of iovecs and data written into the iclog, as well as +* a pointer to the logvec that doesn't fit in the log (or NULL if we hit the +* end of the chain. */ -static int -xlog_write_setup_copy( +static struct xfs_log_vec *Quoted 8 lines
fs/xfs/xfs_log.c xlog_print_trans( · line 2113
Quoted 2 lines
fs/xfs/xfs_log.c +2
-0+xlog_write_single ( + struct xfs_log_vec *log_vector,So
xlog_write_single()was initially for single CILxlog_write()calls and now it appears to be slightly different in that it writes as many full log vectors that fit in the current iclog and cycles throughxlog_write_partial()(and back) to process log vectors that span iclogs differently from those that don't.Yes, that is what it does, but no, you've got the process and meaning backwards. I wrote
xlog_write_single()it as it appears in this patch first, then split it out backwards to ease review. IOWs, "single" means "write everything that fits within this single iclog", not "only call this function if the entire lv chain fits inside a single iclog".The latter is what I split out to make it simpler to review, but it was not the reason it was called
xlog_write_single()....Quoted 9 lines
fs/xfs/xfs_log.c xlog_print_trans( · line 2295
Quoted 7 lines
fs/xfs/xfs_log.c +7
-0+ do { + /* + * Account for the continuation opheader before we get + * a new iclog. This is necessary so that we reserve + * space in the iclog for it. + */ + if (ophdr->oh_flags & XLOG_CONTINUE_TRANS) {(Is this ever not true here?)
It is now, wasn't always. Fixed.
Quoted 47 lines
Quoted 16 lines
fs/xfs/xfs_log.c +13
-1+ *len +=sizeof (struct xlog_op_header ); + ticket->t_curr_res -=sizeof (struct xlog_op_header ); + } + error =xlog_write_get_more_iclog_space (log, ticket, + &iclog, log_offset, *len, record_cnt, + data_cnt, contwr); +if (error) +return ERR_PTR (error); + ptr = iclog->ic_datap + *log_offset; + + ophdr = ptr; ophdr->oh_tid =cpu_to_be32 (ticket->t_tid); - ophdr->oh_len = cpu_to_be32(reg->i_len - + ophdr->oh_clientid =XFS_TRANSACTION ; + ophdr->oh_res2 =0 ;fs/xfs/xfs_log.c xlog_print_trans( · line 2315
Quoted 10 lines
fs/xfs/xfs_log.c +5
-4+ ophdr->oh_flags =XLOG_WAS_CONT_TRANS ; + +xlog_write_adv_cnt (&ptr, len, log_offset,sizeof (struct xlog_op_header )); -memcpy (ptr, reg->i_addr, reg->i_len); -xlog_write_adv_cnt (&ptr, &len, &log_offset, reg->i_len); - record_cnt++; - } + *data_cnt +=sizeof (struct xlog_op_header ); +... which switches to the next iclog, writes the continuation header...
Quoted 18 lines
fs/xfs/xfs_log.c +18
-0+/* +* If rlen fits in the iclog, then end the region +* continuation. Otherwise we're going around again. +*/ + reg_offset += rlen; + rlen = reg->i_len - reg_offset; +if (rlen <= iclog->ic_size - *log_offset) + ophdr->oh_flags |=XLOG_END_TRANS ; +else + ophdr->oh_flags |=XLOG_CONTINUE_TRANS ; + + rlen =min_t (uint32_t, rlen, iclog->ic_size - *log_offset); + ophdr->oh_len =cpu_to_be32 (rlen); + +xlog_verify_dest_ptr (log, ptr); +memcpy (ptr, reg->i_addr + reg_offset, rlen); +xlog_write_adv_cnt (&ptr, len, log_offset, rlen); + (*record_cnt)++;Quoted 8 lines
fs/xfs/xfs_log.c xlog_print_trans( · line 2336
Quoted 3 lines
fs/xfs/xfs_log.c +3
-0+ *data_cnt += rlen; + + }while (ophdr->oh_flags &XLOG_CONTINUE_TRANS );... writes more of the region (iclog space permitting), and then determines whether we need further continuations (and partial writes of the same region) or can move onto the next region, until we're done with the lv.
Yup.
Quoted 8 lines
I think I follow the high level flow and it seems reasonable from a functional standpoint, but this also seems like quite a bit of churn for not much reduction in overall complexity. The higher level loop is much more simple and I think the per lv/vector iteration is an improvement, but we also seem to have duplicate functionality throughout the updated code and have introduced new forms of complexity around the state expectations for the transitions between the different write modes and between each write mode and the higher level loop.
Just getting untangling the code to get it to this point has been hard enough. I've held off doing more factoring and changing this code so I can actaully test it and find the bugs I might have left in it.
Quoted 6 lines
Yes, it can be further improved by factoring the region copying stuff, but that's secondary to the major work of refactoring this code in the first place. The fact that you actually understood this fairly easily indicates just how much better this code already is compared to what is currently upstream....
Heh. "You understood the patch, so it must be better!" :P
I've paged much of this out in the 2 months or so since this review was posted, but my recollection is quite different. I use the existing code as a baseline to confirm behavior and assess readability of the updated code.
Quoted 52 lines
Quoted 41 lines
I.e.,
xlog_write_single()implements a straighforward loop to write out full log vectors. That seems fine, but the outer loop ofxlog_write_partial()reimplements nearly the same per-region functionality with some added flexibility to handle op header flags and the special iclog processing associated with the continuation case. The inner loop factors out the continuation iclog management bits and op header injection, which I think is an improvement, but then duplicates region copying (yet again) pretty much only to implement partial copies, which really just involves offset management (i.e., fairly trivial relative to the broader complexity of the function).I dunno. I'd certainly need to stare more at this to cover all of the details, but given the amount of swizzling going on in a single patch I'm kind of wondering if/why we couldn't land on a single iterator in the spirit of
xlog_write_partial()in that it primarily iterates on regions and factors out the grotty reservation and continuation management bits, but doesn't unroll as much and leave so much duplicate functionality around.For example, it looks to me that
xlog_write_partial()almost nearly already supports a high level algorithm along the lines of the following (pseudocode):
xlog_write(len){ get_iclog_space(len) for_each_lv() { for_each_reg() { reg_offset = 0; cont_write: /* write as much as will fit in the iclog, return count, * and set ophdr cont flag based on write result */ reg_offset += write_region(reg, &len, ®_offset, ophdr, ...); /* handle continuation writes */ if (reg_offset != reg->i_len) { get_more_iclog_space(len); /* stamp a WAS_CONT op hdr, set END if rlen fits * into new space, then continue with the same region */ stamp_cont_op_hdr();Quoted 8 lines
goto cont_write; } if (need_more_iclog_space(len)) get_more_iclog_space(len); } } }Yeah, na. That is exactly the mess that I've just untangled.
Quoted 8 lines
I don't want to rewrite this code again, and I don't want it more tightly tied to iclogs than it already is - I'm trying to move the code towards a common, simple fast path that knows nothing about iclogs and a slow path that handles the partial regions and obtaining a new buffer to write into. I want the two cases completely separate logic, because that makes both cases simpler to modify and reason about.
Well, this review has been on the list for more than a couple months now. Given the response seems to have appeared after the next version of the series, I'm not sure it's worth digging my head back into the details to try and make a more detailed argument. Suffice it to say that I recall what I proposed as intended to be a fairly reasonable incremental step from what you ended up at to replace the large amount of resulting duplication with a single implementation that otherwise preserves the majority of the other cleanups. Not a rewrite or anything of the sort..
In any event, no single one of us is ultimately the authority on "better" or "simple." I'm just providing feedback that I didn't find the resulting factoring as a clear improvement, find it a bit annoying to have to dig through duplicate implementations to locate the subtle and unnecessary differences, and provided a suggestion on how to address that concern (that doesn't involve rewriting the thing) with specific details on how and why I think it improves readability. *shrug* Perhaps others will look at this, disagree with that assessment and find the separate functions more straightforward.
Quoted 4 lines
Indeed, I want
xlog_writeto move away from iclogs because I want to use this code with direct mapped pmem regions, not just fixed memory buffers held in iclogs.
That context and how that relates the proposed structure is not clear to me. That said, I _thought_ I looked through far enough into this series to grok how intertwined the resulting structure might have been with subsequent patches in order to provide thoughtful feedback, but I could be mistaken.
Quoted 22 lines
IOWs, the code as it stands is a beginning, not an end. And even as a beginning, it works, is much better and faster than the current code, has been tested for some time now, can be further factored to make it simpler, easier to understand and provide infrastructure for new features.
Quoted 11 lines
That puts the whole thing back into a single high level walk and thus reintroduces the need for some of the continuation vs. non-continuation tracking wrt to the op header and iclog, but ISTM that complexity can be managed by the continuation abstraction you've already started to introduce (as opposed to the current scheme of conditionally accumulating
data_cnt). It might even be fine to dump some of the requisite state into a context struct to carry between iclog reservation and copy finish processing rather than pass around so many independent and poorly named variables like the current upstream implementation does, but that's probably getting too deep into the weeds.Quoted 3 lines
FWIW, I can also see an approach of moving from the implementation in this patch toward something like the above, but I'm not sure I'd want to subject to the upstream code to that process...
Quoted 9 lines
This is exactly what upstream is for - iterative improvement via small steps. This is the first step of many, and what you propose takes the code in the wrong direction for the steps I've already taken and are planning to take.
Perfect is the enemy of good, and if upstream is not the place to make iterative improvements like this that build towards a bigger picture goal, then where the hell are we supposed to do them?
Not every incremental development step is necessarily a suitable point for an upstream release. My comment above is basically to say that I think this refactoring is nearly to that point, but should go a bit further to reduce the duplication. If the argument against that step is dependence on future work, then propose the factoring close enough to that work such that sufficient context is available to review.
Brian
Quoted 5 lines
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 29/45] xfs:_introduce xlog_write_partial()
On Thu, May 20, 2021 at 08:33:04AM -0400, Brian Foster wrote:
<snipping the earlier comments out because I want only to respond to the discussion pertaining to handling of large patchsets>
Quoted 99 lines
Quoted 14 lines
Quoted 8 lines
I think I follow the high level flow and it seems reasonable from a functional standpoint, but this also seems like quite a bit of churn for not much reduction in overall complexity. The higher level loop is much more simple and I think the per lv/vector iteration is an improvement, but we also seem to have duplicate functionality throughout the updated code and have introduced new forms of complexity around the state expectations for the transitions between the different write modes and between each write mode and the higher level loop.
Just getting untangling the code to get it to this point has been hard enough. I've held off doing more factoring and changing this code so I can actaully test it and find the bugs I might have left in it.
Quoted 6 lines
Yes, it can be further improved by factoring the region copying stuff, but that's secondary to the major work of refactoring this code in the first place. The fact that you actually understood this fairly easily indicates just how much better this code already is compared to what is currently upstream....
Heh. "You understood the patch, so it must be better!" :P
I've paged much of this out in the 2 months or so since this review was posted, but my recollection is quite different. I use the existing code as a baseline to confirm behavior and assess readability of the updated code.
Quoted 52 lines
Quoted 41 lines
I.e.,
xlog_write_single()implements a straighforward loop to write out full log vectors. That seems fine, but the outer loop ofxlog_write_partial()reimplements nearly the same per-region functionality with some added flexibility to handle op header flags and the special iclog processing associated with the continuation case. The inner loop factors out the continuation iclog management bits and op header injection, which I think is an improvement, but then duplicates region copying (yet again) pretty much only to implement partial copies, which really just involves offset management (i.e., fairly trivial relative to the broader complexity of the function).I dunno. I'd certainly need to stare more at this to cover all of the details, but given the amount of swizzling going on in a single patch I'm kind of wondering if/why we couldn't land on a single iterator in the spirit of
xlog_write_partial()in that it primarily iterates on regions and factors out the grotty reservation and continuation management bits, but doesn't unroll as much and leave so much duplicate functionality around.For example, it looks to me that
xlog_write_partial()almost nearly already supports a high level algorithm along the lines of the following (pseudocode):
xlog_write(len){ get_iclog_space(len) for_each_lv() { for_each_reg() { reg_offset = 0; cont_write: /* write as much as will fit in the iclog, return count, * and set ophdr cont flag based on write result */ reg_offset += write_region(reg, &len, ®_offset, ophdr, ...); /* handle continuation writes */ if (reg_offset != reg->i_len) { get_more_iclog_space(len); /* stamp a WAS_CONT op hdr, set END if rlen fits * into new space, then continue with the same region */ stamp_cont_op_hdr();Quoted 8 lines
goto cont_write; } if (need_more_iclog_space(len)) get_more_iclog_space(len); } } }Yeah, na. That is exactly the mess that I've just untangled.
Quoted 8 lines
I don't want to rewrite this code again, and I don't want it more tightly tied to iclogs than it already is - I'm trying to move the code towards a common, simple fast path that knows nothing about iclogs and a slow path that handles the partial regions and obtaining a new buffer to write into. I want the two cases completely separate logic, because that makes both cases simpler to modify and reason about.
Well, this review has been on the list for more than a couple months now. Given the response seems to have appeared after the next version of the series, I'm not sure it's worth digging my head back into the details to try and make a more detailed argument. Suffice it to say that I recall what I proposed as intended to be a fairly reasonable incremental step from what you ended up at to replace the large amount of resulting duplication with a single implementation that otherwise preserves the majority of the other cleanups. Not a rewrite or anything of the sort..
Quoted 9 lines
In any event, no single one of us is ultimately the authority on "better" or "simple." I'm just providing feedback that I didn't find the resulting factoring as a clear improvement, find it a bit annoying to have to dig through duplicate implementations to locate the subtle and unnecessary differences, and provided a suggestion on how to address that concern (that doesn't involve rewriting the thing) with specific details on how and why I think it improves readability. *shrug* Perhaps others will look at this, disagree with that assessment and find the separate functions more straightforward.
Admittedly I did look at the:
xlog_verify_dest_ptr(log, ptr); memcpy(ptr, reg->i_addr + reg_offset, rlen); xlog_write_adv_cnt(&ptr, len, log_offset, rlen); (*record_cnt)++; *data_cnt += rlen;
sprinkled in three places and wondered why that couldn't have been a single function. Eh, well.
Leaving the ophdr manipulations as separate clauses actually helps me to figure out /why/ they're different.
Quoted 44 lines
Quoted 5 lines
Indeed, I want
xlog_writeto move away from iclogs because I want to use this code with direct mapped pmem regions, not just fixed memory buffers held in iclogs.That context and how that relates the proposed structure is not clear to me. That said, I _thought_ I looked through far enough into this series to grok how intertwined the resulting structure might have been with subsequent patches in order to provide thoughtful feedback, but I could be mistaken.
Quoted 22 lines
IOWs, the code as it stands is a beginning, not an end. And even as a beginning, it works, is much better and faster than the current code, has been tested for some time now, can be further factored to make it simpler, easier to understand and provide infrastructure for new features.
Quoted 11 lines
That puts the whole thing back into a single high level walk and thus reintroduces the need for some of the continuation vs. non-continuation tracking wrt to the op header and iclog, but ISTM that complexity can be managed by the continuation abstraction you've already started to introduce (as opposed to the current scheme of conditionally accumulating
data_cnt). It might even be fine to dump some of the requisite state into a context struct to carry between iclog reservation and copy finish processing rather than pass around so many independent and poorly named variables like the current upstream implementation does, but that's probably getting too deep into the weeds.Quoted 3 lines
FWIW, I can also see an approach of moving from the implementation in this patch toward something like the above, but I'm not sure I'd want to subject to the upstream code to that process...
Quoted 9 lines
This is exactly what upstream is for - iterative improvement via small steps. This is the first step of many, and what you propose takes the code in the wrong direction for the steps I've already taken and are planning to take.
Perfect is the enemy of good, and if upstream is not the place to make iterative improvements like this that build towards a bigger picture goal, then where the hell are we supposed to do them?
Quoted 6 lines
Not every incremental development step is necessarily a suitable point for an upstream release. My comment above is basically to say that I think this refactoring is nearly to that point, but should go a bit further to reduce the duplication. If the argument against that step is dependence on future work, then propose the factoring close enough to that work such that sufficient context is available to review.
For a short patchset I agree, but I don't think dumping the /next/ forty patches on the list as an RFC is going to help much. We're keyed to the kernel release cycle, which means (to me anyway) that the criteria is a little different for Gigantic Patchsets that are never going to land in a single cycle.
Whereas for small patchsets I think it's reasonable to ask that all the weird warts get fixed by the end of review, for bigger things I think it's ok to lower that standard to "Can we understand it in case the author disappears; and does it not introduce obvious regressions"?
I've applied the same principle to this really long story arc of adding parent pointers to the filesystem -- yes, the delayed xattrs series has some strange things in it structurally, but I was ok with only asking for obvious cleanups (like fixing the naming inconsistencies) so that we can get to the next series, which justifies all the slicing and dicing by turning the xattr state machine into a deferred log item.
Posting the full set as a git branch somewhere so at least we can pull it and see the even bigger picture might, though. It's helped immensely for reviewing the delayed xattrs series and throwing some early feedback to Allison w.r.t. deferred xattrs.
All right, back to the latest posting.
--D
Quoted 9 lines
Brian
Quoted 5 lines
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 30/45 xfs: xlog_write() no longer needs contwr state
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The rework of xlog_write() no longer requires xlog_get_iclog_state() to tell it about internal iclog space reservation state to direct it on what to do. Remove this parameter.
$ size fs/xfs/xfs_log.o.* text data bss dec hex filename 26520 560 8 27088 69d0 fs/xfs/xfs_log.o.orig 26384 560 8 26952 6948 fs/xfs/xfs_log.o.patched
--- fs/xfs/xfs_log.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-)
fs/xfs/xfs_log.c +11 -22
@@ -47,7 +47,6 @@ xlog_state_get_iclog_space(int len, struct xlog_in_core **iclog, struct xlog_ticket *ticket, - int *continued_write, int *logoffsetp); STATIC void xlog_grant_push_ail(
@@ -2167,8 +2166,7 @@ xlog_write_get_more_iclog_space(uint32_t *log_offset, uint32_t len, uint32_t *record_cnt, - uint32_t *data_cnt, - int *contwr) + uint32_t *data_cnt) { struct xlog_in_core *iclog = *iclogp;int error;
@@ -2182,8 +2180,8 @@ xlog_write_get_more_iclog_space(if (error)return error; - error =xlog_state_get_iclog_space (log, len, &iclog, - ticket, contwr, log_offset); + error =xlog_state_get_iclog_space (log, len, &iclog, ticket, + log_offset);if (error)return error; *record_cnt =0 ;
@@ -2207,8 +2205,7 @@ xlog_write_partial(uint32_t *log_offset, uint32_t *len, uint32_t *record_cnt, - uint32_t *data_cnt, - int *contwr) + uint32_t *data_cnt) { struct xlog_in_core *iclog = *iclogp;struct xfs_log_vec *lv = log_vector;
@@ -2242,7 +2239,7 @@ xlog_write_partial( sizeof(struct xlog_op_header)) { error =xlog_write_get_more_iclog_space (log, ticket, &iclog, log_offset, *len, record_cnt, - data_cnt, contwr); + data_cnt);if (error)return ERR_PTR (error); ptr = iclog->ic_datap + *log_offset;
@@ -2298,7 +2295,7 @@ xlog_write_partial( } error =xlog_write_get_more_iclog_space (log, ticket, &iclog, log_offset, *len, record_cnt, - data_cnt, contwr); + data_cnt);if (error)return ERR_PTR (error); ptr = iclog->ic_datap + *log_offset;
@@ -2396,7 +2393,6 @@ xlog_write( {struct xlog_in_core *iclog =NULL ;struct xfs_log_vec *lv = log_vector; -int contwr =0 ;int record_cnt =0 ;int data_cnt =0 ;int error =0 ;
@@ -2410,7 +2406,7 @@ xlog_write( } error =xlog_state_get_iclog_space (log, len, &iclog, ticket, - &contwr, &log_offset); + &log_offset);if (error)return error;
@@ -2425,10 +2421,8 @@ xlog_write(* is correctly maintained in the storage media. This will always * fit in the iclog we have been already been passed. */ -if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) { +if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) iclog->ic_flags |= (XLOG_ICL_NEED_FLUSH |XLOG_ICL_NEED_FUA ); -ASSERT (!contwr); - }while (lv) { lv = xlog_write_single(lv, ticket, iclog, &log_offset,
@@ -2438,7 +2432,7 @@ xlog_write(ASSERT (!(optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS ))); lv =xlog_write_partial (log, lv, ticket, &iclog, &log_offset, - &len, &record_cnt, &data_cnt, &contwr); + &len, &record_cnt, &data_cnt);if (IS_ERR_OR_NULL (lv)) { error =PTR_ERR_OR_ZERO (lv);break ;
@@ -2452,7 +2446,6 @@ xlog_write(* those writes accounted to it. Hence we do not need to update the * iclog with the number of bytes written here. */ -ASSERT (!contwr ||XLOG_FORCED_SHUTDOWN (log));spin_lock (&log->l_icloglock);xlog_state_finish_copy (log, iclog, record_cnt,0 );if (commit_iclog) {
@@ -2856,7 +2849,6 @@ xlog_state_get_iclog_space(int len, struct xlog_in_core **iclogp, struct xlog_ticket *ticket, - int *continued_write, int *logoffsetp) { int log_offset;
@@ -2932,13 +2924,10 @@ xlog_state_get_iclog_space(* iclogs (to mark it taken), this particular iclog will release/sync * to disk in xlog_write(). */ -if (len <= iclog->ic_size - iclog->ic_offset) { - *continued_write =0 ; +if (len <= iclog->ic_size - iclog->ic_offset) iclog->ic_offset += len; - }else { - *continued_write =1 ; +else xlog_state_switch_iclogs (log, iclog, iclog->ic_size); - } *iclogp = iclog;ASSERT (iclog->ic_offset <= iclog->ic_size);
Signature · 1 line
2.28.0
Re: [PATCH 30/45] xfs: xlog_write() no longer needs contwr state
Quoted 6 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The rework of
xlog_write()no longer requiresxlog_get_iclog_state()to tell it about internal iclog space reservation state to direct it on what to do. Remove this parameter.
Quoted 6 lines
$ size fs/xfs/xfs_log.o.* text data bss dec hex filename 26520 560 8 27088 69d0 fs/xfs/xfs_log.o.orig 26384 560 8 26952 6948 fs/xfs/xfs_log.o.patchedSigned-off-by Dave Chinner <[email protected]>
This seems pretty straightforward,
--D
Quoted 138 lines
--- fs/xfs/xfs_log.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-)fs/xfs/xfs_log.c +11
-22@@ -47,7 +47,6 @@ xlog_state_get_iclog_space(int len, struct xlog_in_core **iclog, struct xlog_ticket *ticket, - int *continued_write, int *logoffsetp); STATIC void xlog_grant_push_ail(@@ -2167,8 +2166,7 @@ xlog_write_get_more_iclog_space(uint32_t *log_offset, uint32_t len, uint32_t *record_cnt, - uint32_t *data_cnt, - int *contwr) + uint32_t *data_cnt) { struct xlog_in_core *iclog = *iclogp;int error;@@ -2182,8 +2180,8 @@ xlog_write_get_more_iclog_space(if (error)return error; - error =xlog_state_get_iclog_space (log, len, &iclog, - ticket, contwr, log_offset); + error =xlog_state_get_iclog_space (log, len, &iclog, ticket, + log_offset);if (error)return error; *record_cnt =0 ;@@ -2207,8 +2205,7 @@ xlog_write_partial(uint32_t *log_offset, uint32_t *len, uint32_t *record_cnt, - uint32_t *data_cnt, - int *contwr) + uint32_t *data_cnt) { struct xlog_in_core *iclog = *iclogp;struct xfs_log_vec *lv = log_vector;@@ -2242,7 +2239,7 @@ xlog_write_partial( sizeof(struct xlog_op_header)) { error =xlog_write_get_more_iclog_space (log, ticket, &iclog, log_offset, *len, record_cnt, - data_cnt, contwr); + data_cnt);if (error)return ERR_PTR (error); ptr = iclog->ic_datap + *log_offset;@@ -2298,7 +2295,7 @@ xlog_write_partial( } error =xlog_write_get_more_iclog_space (log, ticket, &iclog, log_offset, *len, record_cnt, - data_cnt, contwr); + data_cnt);if (error)return ERR_PTR (error); ptr = iclog->ic_datap + *log_offset;@@ -2396,7 +2393,6 @@ xlog_write( {struct xlog_in_core *iclog =NULL ;struct xfs_log_vec *lv = log_vector; -int contwr =0 ;int record_cnt =0 ;int data_cnt =0 ;int error =0 ;@@ -2410,7 +2406,7 @@ xlog_write( } error =xlog_state_get_iclog_space (log, len, &iclog, ticket, - &contwr, &log_offset); + &log_offset);if (error)return error;@@ -2425,10 +2421,8 @@ xlog_write(* is correctly maintained in the storage media. This will always * fit in the iclog we have been already been passed. */ -if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) { +if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) iclog->ic_flags |= (XLOG_ICL_NEED_FLUSH |XLOG_ICL_NEED_FUA ); -ASSERT (!contwr); - }while (lv) { lv = xlog_write_single(lv, ticket, iclog, &log_offset,@@ -2438,7 +2432,7 @@ xlog_write(ASSERT (!(optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS ))); lv =xlog_write_partial (log, lv, ticket, &iclog, &log_offset, - &len, &record_cnt, &data_cnt, &contwr); + &len, &record_cnt, &data_cnt);if (IS_ERR_OR_NULL (lv)) { error =PTR_ERR_OR_ZERO (lv);break ;@@ -2452,7 +2446,6 @@ xlog_write(* those writes accounted to it. Hence we do not need to update the * iclog with the number of bytes written here. */ -ASSERT (!contwr ||XLOG_FORCED_SHUTDOWN (log));spin_lock (&log->l_icloglock);xlog_state_finish_copy (log, iclog, record_cnt,0 );if (commit_iclog) {@@ -2856,7 +2849,6 @@ xlog_state_get_iclog_space(int len, struct xlog_in_core **iclogp, struct xlog_ticket *ticket, - int *continued_write, int *logoffsetp) { int log_offset;@@ -2932,13 +2924,10 @@ xlog_state_get_iclog_space(* iclogs (to mark it taken), this particular iclog will release/sync * to disk in xlog_write(). */ -if (len <= iclog->ic_size - iclog->ic_offset) { - *continued_write =0 ; +if (len <= iclog->ic_size - iclog->ic_offset) iclog->ic_offset += len; - }else { - *continued_write =1 ; +else xlog_state_switch_iclogs (log, iclog, iclog->ic_size); - } *iclogp = iclog;ASSERT (iclog->ic_offset <= iclog->ic_size);Signature · 1 line
2.28.0
PATCH v3 31/45 xfs: CIL context doesn't need to count iovecs
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Now that we account for log opheaders in the log item formatting code, we don't actually use the aggregated count of log iovecs in the CIL for anything. Remove it and the tracking code that calculates it.
--- fs/xfs/xfs_log_cil.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-)
fs/xfs/xfs_log_cil.c +6 -16
@@ -252,22 +252,18 @@ xlog_cil_alloc_shadow_bufs(/* * Prepare the log item for insertion into the CIL. Calculate the difference in -* log space and vectors it will consume, and if it is a new item pin it as -* well. +* log space it will consume, and if it is a new item pin it as well. */ STATIC voidxfs_cil_prepare_item (struct xlog *log,struct xfs_log_vec *lv,struct xfs_log_vec *old_lv, -int *diff_len, -int *diff_iovecs) +int *diff_len) {/* Account for the new LV being passed in */ -if (lv->lv_buf_len !=XFS_LOG_VEC_ORDERED ) { +if (lv->lv_buf_len !=XFS_LOG_VEC_ORDERED ) *diff_len += lv->lv_bytes; - *diff_iovecs += lv->lv_niovecs; - }/* * If there is no old LV, this is the first time we've seen the item in
@@ -284,7 +280,6 @@ xfs_cil_prepare_item(ASSERT (lv->lv_buf_len !=XFS_LOG_VEC_ORDERED ); *diff_len -= old_lv->lv_bytes; - *diff_iovecs -= old_lv->lv_niovecs; lv->lv_item->li_lv_shadow = old_lv; }
@@ -333,12 +328,10 @@ static voidxlog_cil_insert_format_items ( struct xlog *log, struct xfs_trans *tp, - int *diff_len, - int *diff_iovecs) + int *diff_len) { struct xfs_log_item *lip; -/* Bail out if we didn't find a log item. */ if (list_empty (&tp->t_items)) {ASSERT (0 );
@@ -381,7 +374,6 @@ xlog_cil_insert_format_items(* set the item up as though it is a new insertion so * that the space reservation accounting is correct. */ - *diff_iovecs -= lv->lv_niovecs; *diff_len -= lv->lv_bytes;/* Ensure the lv is set up according to ->iop_size */
@@ -406,7 +398,7 @@ xlog_cil_insert_format_items(ASSERT (IS_ALIGNED ((unsigned long )lv->lv_buf,sizeof (uint64_t ))); lip->li_ops->iop_format (lip, lv); insert: -xfs_cil_prepare_item (log, lv, old_lv, diff_len, diff_iovecs); +xfs_cil_prepare_item (log, lv, old_lv, diff_len); } }
@@ -426,7 +418,6 @@ xlog_cil_insert_items(struct xfs_cil_ctx *ctx = cil->xc_ctx;struct xfs_log_item *lip;int len =0 ; -int diff_iovecs =0 ;int iclog_space;int iovhdr_res =0 , split_res =0 , ctx_res =0 ;
@@ -436,7 +427,7 @@ xlog_cil_insert_items(* We can do this safely because the context can't checkpoint until we * are done so it doesn't matter exactly how we update the CIL. */ -xlog_cil_insert_format_items (log, tp, &len, &diff_iovecs); +xlog_cil_insert_format_items (log, tp, &len);spin_lock (&cil->xc_cil_lock);
@@ -471,7 +462,6 @@ xlog_cil_insert_items( } tp->t_ticket->t_curr_res -= len; ctx->space_used += len; - ctx->nvecs += diff_iovecs;/* * If we've overrun the reservation, dump the tx details before we move
Signature · 1 line
2.28.0
Re: [PATCH 31/45] xfs: CIL context doesn't need to count iovecs
Quoted 99 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Now that we account for log opheaders in the log item formatting code, we don't actually use the aggregated count of log iovecs in the CIL for anything. Remove it and the tracking code that calculates it.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-)fs/xfs/xfs_log_cil.c +6
-15@@ -252,22 +252,18 @@ xlog_cil_alloc_shadow_bufs(/* * Prepare the log item for insertion into the CIL. Calculate the difference in -* log space and vectors it will consume, and if it is a new item pin it as -* well. +* log space it will consume, and if it is a new item pin it as well. */ STATIC voidxfs_cil_prepare_item (struct xlog *log,struct xfs_log_vec *lv,struct xfs_log_vec *old_lv, -int *diff_len, -int *diff_iovecs) +int *diff_len) {/* Account for the new LV being passed in */ -if (lv->lv_buf_len !=XFS_LOG_VEC_ORDERED ) { +if (lv->lv_buf_len !=XFS_LOG_VEC_ORDERED ) *diff_len += lv->lv_bytes; - *diff_iovecs += lv->lv_niovecs; - }/* * If there is no old LV, this is the first time we've seen the item in @@ -284,7 +280,6 @@ xfs_cil_prepare_item(ASSERT (lv->lv_buf_len !=XFS_LOG_VEC_ORDERED ); *diff_len -= old_lv->lv_bytes; - *diff_iovecs -= old_lv->lv_niovecs; lv->lv_item->li_lv_shadow = old_lv; }@@ -333,12 +328,10 @@ static voidxlog_cil_insert_format_items ( struct xlog *log, struct xfs_trans *tp, - int *diff_len, - int *diff_iovecs) + int *diff_len) { struct xfs_log_item *lip; -/* Bail out if we didn't find a log item. */ if (list_empty (&tp->t_items)) {ASSERT (0 );@@ -381,7 +374,6 @@ xlog_cil_insert_format_items(* set the item up as though it is a new insertion so * that the space reservation accounting is correct. */ - *diff_iovecs -= lv->lv_niovecs; *diff_len -= lv->lv_bytes;/* Ensure the lv is set up according to ->iop_size */ @@ -406,7 +398,7 @@ xlog_cil_insert_format_items(ASSERT (IS_ALIGNED ((unsigned long )lv->lv_buf,sizeof (uint64_t ))); lip->li_ops->iop_format (lip, lv); insert: -xfs_cil_prepare_item (log, lv, old_lv, diff_len, diff_iovecs); +xfs_cil_prepare_item (log, lv, old_lv, diff_len); } }@@ -426,7 +418,6 @@ xlog_cil_insert_items(struct xfs_cil_ctx *ctx = cil->xc_ctx;struct xfs_log_item *lip;int len =0 ; -int diff_iovecs =0 ;int iclog_space;int iovhdr_res =0 , split_res =0 , ctx_res =0 ;@@ -436,7 +427,7 @@ xlog_cil_insert_items(* We can do this safely because the context can't checkpoint until we * are done so it doesn't matter exactly how we update the CIL. */ -xlog_cil_insert_format_items (log, tp, &len, &diff_iovecs); +xlog_cil_insert_format_items (log, tp, &len);spin_lock (&cil->xc_cil_lock);
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 474
Quoted 4 lines
fs/xfs/xfs_log_cil.c +0
-1@@ -471,7 +462,6 @@ xlog_cil_insert_items( } tp->t_ticket->t_curr_res -= len; ctx->space_used += len; - ctx->nvecs += diff_iovecs;
If the tracking variable isn't necessary any more, should the field go away from xfs_cil_ctx?
--D
Quoted 6 lines
fs/xfs/xfs_log_cil.c +0
-0/* * If we've overrun the reservation, dump the tx details before we move Signature · 1 line
2.28.0
Re: [PATCH 31/45] xfs: CIL context doesn't need to count iovecs
Quoted 9 lines · Darrick J. Wong
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 474
Quoted 5 lines · Dave Chinner
fs/xfs/xfs_log_cil.c +0
-1@@ -471,7 +462,6 @@ xlog_cil_insert_items( } tp->t_ticket->t_curr_res -= len; ctx->space_used += len; - ctx->nvecs += diff_iovecs;If the tracking variable isn't necessary any more, should the field go away from
xfs_cil_ctx?
Yes. I thought I cleaned that up. Fixed.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 32/45 xfs: use the CIL space used counter for emptiness checks
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
In the next patches we are going to make the CIL list itself per-cpu, and so we cannot use list_empty() to check is the list is empty. Replace the list_empty() checks with a flag in the CIL to indicate we have committed at least one transaction to the CIL and hence the CIL is not empty.
We need this flag to be an atomic so that we can clear it without holding any locks in the commit fast path, but we also need to be careful to avoid atomic operations in the fast path. Hence we use the fact that test_bit() is not an atomic op to first check if the flag is set and then run the atomic test_and_clear_bit() operation to clear it and steal the initial unit reservation for the CIL context checkpoint.
When we are switching to a new context in a push, we place the setting of the XLOG_CIL_EMPTY flag under the xc_push_lock. THis allows all the other places that need to check whether the CIL is empty to use test_bit() and still be serialised correctly with the CIL context swaps that set the bit.
--- fs/xfs/xfs_log_cil.c | 49 +++++++++++++++++++++++-------------------- fs/xfs/xfs_log_priv.h | 4 ++++ 2 files changed, 30 insertions(+), 23 deletions(-)
fs/xfs/xfs_log_cil.c +26 -23
@@ -70,6 +70,7 @@ xlog_cil_ctx_switch(struct xfs_cil *cil, struct xfs_cil_ctx *ctx) { + set_bit(XLOG_CIL_EMPTY, &cil->xc_flags); ctx->sequence = ++cil->xc_current_sequence; ctx->cil = cil; cil->xc_ctx = ctx;
@@ -436,13 +437,12 @@ xlog_cil_insert_items(list_splice_init (&tp->t_busy, &ctx->busy_extents);/* -* Now transfer enough transaction reservation to the context ticket -* for the checkpoint. The context ticket is special - the unit -* reservation has to grow as well as the current reservation as we -* steal from tickets so we can correctly determine the space used -* during the transaction commit. +* We need to take the CIL checkpoint unit reservation on the first +* commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't +* unnecessarily do an atomic op in the fast path here. */ -if (ctx->ticket->t_curr_res ==0 ) { +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags) && +test_and_clear_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) { ctx_res = ctx->ticket->t_unit_res; ctx->ticket->t_curr_res = ctx_res; tp->t_ticket->t_curr_res -= ctx_res;
@@ -771,7 +771,7 @@ xlog_cil_push_work(* move on to a new sequence number and so we have to be able to push * this sequence again later. */ -if (list_empty (&cil->xc_cil)) { +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) { cil->xc_push_seq =0 ;spin_unlock (&cil->xc_push_lock); goto out_skip;
@@ -1019,9 +1019,10 @@ xlog_cil_push_background(/* * The cil won't be empty because we are called while holding the -* context lock so whatever we added to the CIL will still be there +* context lock so whatever we added to the CIL will still be there. */ ASSERT (!list_empty (&cil->xc_cil)); +ASSERT (!test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));/* * Don't do a background push if we haven't used up all the
@@ -1108,7 +1109,8 @@ xlog_cil_push_now(* there's no work we need to do. */ spin_lock (&cil->xc_push_lock); -if (list_empty (&cil->xc_cil) || push_seq <= cil->xc_push_seq) { +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags) || + push_seq <= cil->xc_push_seq) {spin_unlock (&cil->xc_push_lock);return ; }
@@ -1128,7 +1130,7 @@ xlog_cil_empty(bool empty = false;spin_lock (&cil->xc_push_lock); -if (list_empty (&cil->xc_cil)) +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) empty = true;spin_unlock (&cil->xc_push_lock);return empty;
@@ -1289,7 +1291,7 @@ xlog_cil_force_seq(* we would have found the context on the committing list. */ if (sequence == cil->xc_current_sequence && - !list_empty (&cil->xc_cil)) { + !test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) {spin_unlock (&cil->xc_push_lock); goto restart; }
@@ -1320,21 +1322,19 @@ xlog_cil_force_seq(*/ bool xfs_log_item_in_current_chkpt ( -struct xfs_log_item *lip) +struct xfs_log_item *lip) { -struct xfs_cil_ctx *ctx; +struct xfs_cil *cil = lip->li_mountp->m_log->l_cilp; -if (list_empty (&lip->li_cil)) +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags))return false; - ctx = lip->li_mountp->m_log->l_cilp->xc_ctx; -/* * li_seq is written on the first commit of a log item to record the * first checkpoint it is written to. Hence if it is different to the * current sequence, we're in a new checkpoint. */ -if (XFS_LSN_CMP (lip->li_seq, ctx->sequence) !=0 ) +if (XFS_LSN_CMP (lip->li_seq, cil->xc_ctx->sequence) !=0 )return false;return true; }
@@ -1373,13 +1373,16 @@ voidxlog_cil_destroy ( struct xlog *log) { -if (log->l_cilp->xc_ctx) { -if (log->l_cilp->xc_ctx->ticket) -xfs_log_ticket_put (log->l_cilp->xc_ctx->ticket); -kmem_free (log->l_cilp->xc_ctx); +struct xfs_cil *cil = log->l_cilp; + +if (cil->xc_ctx) { +if (cil->xc_ctx->ticket) +xfs_log_ticket_put (cil->xc_ctx->ticket); +kmem_free (cil->xc_ctx); } -ASSERT (list_empty (&log->l_cilp->xc_cil)); -kmem_free (log->l_cilp); +ASSERT (list_empty (&cil->xc_cil)); +ASSERT (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)); +kmem_free (cil); }
fs/xfs/xfs_log_priv.h +4 -0
@@ -248,6 +248,7 @@ struct xfs_cil_ctx {*/ struct xfs_cil {struct xlog *xc_log; +unsigned long xc_flags;struct list_head xc_cil;spinlock_t xc_cil_lock;
@@ -263,6 +264,9 @@ struct xfs_cil {wait_queue_head_t xc_push_wait;/* background push throttle */ }____cacheline_aligned_in_smp ; +/* xc_flags bit values */ +#define XLOG_CIL_EMPTY 1 +/* * The amount of log space we allow the CIL to aggregate is difficult to size. * Whatever we choose, we have to make sure we can get a reservation for the
Signature · 1 line
2.28.0
Re: [PATCH 32/45] xfs: use the CIL space used counter for emptiness checks
Quoted 46 lines · Dave Chinner
From: Dave Chinner <[email protected]>
In the next patches we are going to make the CIL list itself per-cpu, and so we cannot use
list_empty()to check is the list is empty. Replace thelist_empty()checks with a flag in the CIL to indicate we have committed at least one transaction to the CIL and hence the CIL is not empty.We need this flag to be an atomic so that we can clear it without holding any locks in the commit fast path, but we also need to be careful to avoid atomic operations in the fast path. Hence we use the fact that
test_bit()is not an atomic op to first check if the flag is set and then run the atomictest_and_clear_bit()operation to clear it and steal the initial unit reservation for the CIL context checkpoint.When we are switching to a new context in a push, we place the setting of the
XLOG_CIL_EMPTYflag under thexc_push_lock. THis allows all the other places that need to check whether the CIL is empty to usetest_bit()and still be serialised correctly with the CIL context swaps that set the bit.Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 49 +++++++++++++++++++++++-------------------- fs/xfs/xfs_log_priv.h | 4 ++++ 2 files changed, 30 insertions(+), 23 deletions(-)fs/xfs/xfs_log_cil.c +1
-2@@ -70,6 +70,7 @@ xlog_cil_ctx_switch(struct xfs_cil *cil, struct xfs_cil_ctx *ctx) { + set_bit(XLOG_CIL_EMPTY, &cil->xc_flags); ctx->sequence = ++cil->xc_current_sequence; ctx->cil = cil; cil->xc_ctx = ctx;@@ -436,13 +437,12 @@ xlog_cil_insert_items(list_splice_init (&tp->t_busy, &ctx->busy_extents);/* -* Now transfer enough transaction reservation to the context ticket -* for the checkpoint. The context ticket is special - the unit
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 445
Quoted 10 lines
fs/xfs/xfs_log_cil.c +5
-4-* reservation has to grow as well as the current reservation as we -* steal from tickets so we can correctly determine the space used -* during the transaction commit. +* We need to take the CIL checkpoint unit reservation on the first +* commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't +* unnecessarily do an atomic op in the fast path here. */ -if (ctx->ticket->t_curr_res ==0 ) { +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags) && +test_and_clear_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) {
Hm, it'll be amusing to see where this goes. Usually I tell myself in siutations like these "I think this is ok, let's see where we are in another 4-5 patches" but now I'm 7 patches out and my brain is getting close to ENOSPC so I'll tentatively say:
With the caveat that I could have more to say after the fact...
--D
Quoted 126 lines
fs/xfs/xfs_log_cil.c +20
-17ctx_res = ctx->ticket->t_unit_res; ctx->ticket->t_curr_res = ctx_res; tp->t_ticket->t_curr_res -= ctx_res;@@ -771,7 +771,7 @@ xlog_cil_push_work(* move on to a new sequence number and so we have to be able to push * this sequence again later. */ -if (list_empty (&cil->xc_cil)) { +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) { cil->xc_push_seq =0 ;spin_unlock (&cil->xc_push_lock); goto out_skip;@@ -1019,9 +1019,10 @@ xlog_cil_push_background(/* * The cil won't be empty because we are called while holding the -* context lock so whatever we added to the CIL will still be there +* context lock so whatever we added to the CIL will still be there. */ ASSERT (!list_empty (&cil->xc_cil)); +ASSERT (!test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));/* * Don't do a background push if we haven't used up all the @@ -1108,7 +1109,8 @@ xlog_cil_push_now(* there's no work we need to do. */ spin_lock (&cil->xc_push_lock); -if (list_empty (&cil->xc_cil) || push_seq <= cil->xc_push_seq) { +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags) || + push_seq <= cil->xc_push_seq) {spin_unlock (&cil->xc_push_lock);return ; }@@ -1128,7 +1130,7 @@ xlog_cil_empty(bool empty = false;spin_lock (&cil->xc_push_lock); -if (list_empty (&cil->xc_cil)) +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) empty = true;spin_unlock (&cil->xc_push_lock);return empty;@@ -1289,7 +1291,7 @@ xlog_cil_force_seq(* we would have found the context on the committing list. */ if (sequence == cil->xc_current_sequence && - !list_empty (&cil->xc_cil)) { + !test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) {spin_unlock (&cil->xc_push_lock); goto restart; }@@ -1320,21 +1322,19 @@ xlog_cil_force_seq(*/ bool xfs_log_item_in_current_chkpt ( -struct xfs_log_item *lip) +struct xfs_log_item *lip) { -struct xfs_cil_ctx *ctx; +struct xfs_cil *cil = lip->li_mountp->m_log->l_cilp; -if (list_empty (&lip->li_cil)) +if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags))return false; - ctx = lip->li_mountp->m_log->l_cilp->xc_ctx; -/* * li_seq is written on the first commit of a log item to record the * first checkpoint it is written to. Hence if it is different to the * current sequence, we're in a new checkpoint. */ -if (XFS_LSN_CMP (lip->li_seq, ctx->sequence) !=0 ) +if (XFS_LSN_CMP (lip->li_seq, cil->xc_ctx->sequence) !=0 )return false;return true; }@@ -1373,13 +1373,16 @@ voidxlog_cil_destroy ( struct xlog *log) { -if (log->l_cilp->xc_ctx) { -if (log->l_cilp->xc_ctx->ticket) -xfs_log_ticket_put (log->l_cilp->xc_ctx->ticket); -kmem_free (log->l_cilp->xc_ctx); +struct xfs_cil *cil = log->l_cilp; + +if (cil->xc_ctx) { +if (cil->xc_ctx->ticket) +xfs_log_ticket_put (cil->xc_ctx->ticket); +kmem_free (cil->xc_ctx); } -ASSERT (list_empty (&log->l_cilp->xc_cil)); -kmem_free (log->l_cilp); +ASSERT (list_empty (&cil->xc_cil)); +ASSERT (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)); +kmem_free (cil); }fs/xfs/xfs_log_priv.h +4
-0@@ -248,6 +248,7 @@ struct xfs_cil_ctx {*/ struct xfs_cil {struct xlog *xc_log; +unsigned long xc_flags;struct list_head xc_cil;spinlock_t xc_cil_lock;@@ -263,6 +264,9 @@ struct xfs_cil {wait_queue_head_t xc_push_wait;/* background push throttle */ }____cacheline_aligned_in_smp ; +/* xc_flags bit values */ +#define XLOG_CIL_EMPTY 1 +/* * The amount of log space we allow the CIL to aggregate is difficult to size. * Whatever we choose, we have to make sure we can get a reservation for the Signature · 1 line
2.28.0
PATCH v3 33/45 xfs: lift init CIL reservation out of xc_cil_lock
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The xc_cil_lock is the most highly contended lock in XFS now. To start the process of getting rid of it, lift the initial reservation of the CIL log space out from under the xc_cil_lock.
--- fs/xfs/xfs_log_cil.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)
fs/xfs/xfs_log_cil.c +12 -15
@@ -430,23 +430,19 @@ xlog_cil_insert_items(*/ xlog_cil_insert_format_items (log, tp, &len); -spin_lock (&cil->xc_cil_lock); - -/* attach the transaction to the CIL if it has any busy extents */ -if (!list_empty (&tp->t_busy)) -list_splice_init (&tp->t_busy, &ctx->busy_extents); -/* * We need to take the CIL checkpoint unit reservation on the first * commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't -* unnecessarily do an atomic op in the fast path here. +* unnecessarily do an atomic op in the fast path here. We don't need to +* hold the xc_cil_lock here to clear the XLOG_CIL_EMPTY bit as we are +* under the xc_ctx_lock here and that needs to be held exclusively to +* reset the XLOG_CIL_EMPTY bit. */ if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags) && -test_and_clear_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) { +test_and_clear_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) ctx_res = ctx->ticket->t_unit_res; - ctx->ticket->t_curr_res = ctx_res; - tp->t_ticket->t_curr_res -= ctx_res; - } + +spin_lock (&cil->xc_cil_lock);/* do we need space for more log record headers? */ iclog_space = log->l_iclog_size - log->l_iclog_hsize;
@@ -456,11 +452,9 @@ xlog_cil_insert_items(/* need to take into account split region headers, too */ split_res *= log->l_iclog_hsize +sizeof (struct xlog_op_header ); ctx->ticket->t_unit_res += split_res; - ctx->ticket->t_curr_res += split_res; - tp->t_ticket->t_curr_res -= split_res; -ASSERT (tp->t_ticket->t_curr_res >= len); } - tp->t_ticket->t_curr_res -= len; + tp->t_ticket->t_curr_res -= split_res + ctx_res + len; + ctx->ticket->t_curr_res += split_res + ctx_res; ctx->space_used += len;/*
@@ -498,6 +492,9 @@ xlog_cil_insert_items(list_move_tail (&lip->li_cil, &cil->xc_cil); } +/* attach the transaction to the CIL if it has any busy extents */ +if (!list_empty (&tp->t_busy)) +list_splice_init (&tp->t_busy, &ctx->busy_extents);spin_unlock (&cil->xc_cil_lock);if (tp->t_ticket->t_curr_res <0 )
Signature · 1 line
2.28.0
Re: [PATCH 33/45] xfs: lift init CIL reservation out of xc_cil_lock
Quoted 16 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The
xc_cil_lockis the most highly contended lock in XFS now. To start the process of getting rid of it, lift the initial reservation of the CIL log space out from under thexc_cil_lock.Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)fs/xfs/xfs_log_cil.c +0
-0
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 433
Quoted 4 lines
fs/xfs/xfs_log_cil.c +0
-1@@ -430,23 +430,19 @@ xlog_cil_insert_items(*/ xlog_cil_insert_format_items (log, tp, &len); -spin_lock (&cil->xc_cil_lock);
Hm, so looking ahead, the next few patches keep kicking this spin_lock call further and further down in the file, and the commit messages give me the impression that this might even go away entirely?
Let me see, the CIL locks are:
xc_ctx_lock, which prevents transactions from committing (into the cil) any time the CIL itself is preparing a new commited item context so that it can xlog_write (to disk) the log vectors associated with the current context.
xc_cil_lock, which serializes transactions adding their items to the CIL in the first place, hence the motivation to reduce this hot lock?
xc_push_lock, which I think is used to coordinate the CIL push worker with all the upper level callers that want to force log items to disk?
And the locking order of these three locks is...
xc_ctx_lock --> xc_push_lock
|
\---------> xc_cil_lockAssuming I grokked all that, then I guess moving the spin_lock call works out because the test_and_clear_bit is atomic. The rest of the accounting stuff here is just getting moved further down in the file and is still protected by xc_cil_lock.
If I understood all that,
--D
Quoted 53 lines
fs/xfs/xfs_log_cil.c +12
-14- -/* attach the transaction to the CIL if it has any busy extents */ -if (!list_empty (&tp->t_busy)) -list_splice_init (&tp->t_busy, &ctx->busy_extents); -/* * We need to take the CIL checkpoint unit reservation on the first * commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't -* unnecessarily do an atomic op in the fast path here. +* unnecessarily do an atomic op in the fast path here. We don't need to +* hold the xc_cil_lock here to clear the XLOG_CIL_EMPTY bit as we are +* under the xc_ctx_lock here and that needs to be held exclusively to +* reset the XLOG_CIL_EMPTY bit. */ if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags) && -test_and_clear_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) { +test_and_clear_bit (XLOG_CIL_EMPTY , &cil->xc_flags)) ctx_res = ctx->ticket->t_unit_res; - ctx->ticket->t_curr_res = ctx_res; - tp->t_ticket->t_curr_res -= ctx_res; - } + +spin_lock (&cil->xc_cil_lock);/* do we need space for more log record headers? */ iclog_space = log->l_iclog_size - log->l_iclog_hsize;@@ -456,11 +452,9 @@ xlog_cil_insert_items(/* need to take into account split region headers, too */ split_res *= log->l_iclog_hsize +sizeof (struct xlog_op_header ); ctx->ticket->t_unit_res += split_res; - ctx->ticket->t_curr_res += split_res; - tp->t_ticket->t_curr_res -= split_res; -ASSERT (tp->t_ticket->t_curr_res >= len); } - tp->t_ticket->t_curr_res -= len; + tp->t_ticket->t_curr_res -= split_res + ctx_res + len; + ctx->ticket->t_curr_res += split_res + ctx_res; ctx->space_used += len;/* @@ -498,6 +492,9 @@ xlog_cil_insert_items(list_move_tail (&lip->li_cil, &cil->xc_cil); } +/* attach the transaction to the CIL if it has any busy extents */ +if (!list_empty (&tp->t_busy)) +list_splice_init (&tp->t_busy, &ctx->busy_extents);spin_unlock (&cil->xc_cil_lock);if (tp->t_ticket->t_curr_res <0 )Signature · 1 line
2.28.0
Re: [PATCH 33/45] xfs: lift init CIL reservation out of xc_cil_lock
Quoted 26 lines · Darrick J. Wong
Quoted 16 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The
xc_cil_lockis the most highly contended lock in XFS now. To start the process of getting rid of it, lift the initial reservation of the CIL log space out from under thexc_cil_lock.Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)fs/xfs/xfs_log_cil.c +0
-0fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 433
Quoted 4 lines
fs/xfs/xfs_log_cil.c +0
-1@@ -430,23 +430,19 @@ xlog_cil_insert_items(*/ xlog_cil_insert_format_items (log, tp, &len); -spin_lock (&cil->xc_cil_lock);Hm, so looking ahead, the next few patches keep kicking this
spin_lockcall further and further down in the file, and the commit messages give me the impression that this might even go away entirely?
Quoted 6 lines
Let me see, the CIL locks are:
xc_ctx_lock, which prevents transactions from committing (into the cil) any time the CIL itself is preparing a new commited item context so that it canxlog_write(to disk) the log vectors associated with the current context.
Yes.
Quoted 2 lines
xc_cil_lock, which serializes transactions adding their items to the CIL in the first place, hence the motivation to reduce this hot lock?
Right - it protects manipulations to the CIL log item tracking and tracking state. This spin lock is the first global serialisation point in the transaction commit path, so it effectively sees unbound concurrency (reservations allow hundreds of transactions can be committing simultaneously).
Quoted 2 lines
xc_push_lock, which I think is used to coordinate the CIL push worker with all the upper level callers that want to force log items to disk?
Yes, this one protects the current push state.
Quoted 10 lines
And the locking order of these three locks is...
xc_ctx_lock-->xc_push_lock| \---------> xc_cil_lockAssuming I grokked all that, then I guess moving the
spin_lockcall works out because thetest_and_clear_bitis atomic. The rest of the accounting stuff here is just getting moved further down in the file and is still protected byxc_cil_lock.
Yes.
Quoted 2 lines
If I understood all that,
Reviewed-by Darrick J. Wong <[email protected]>
Thanks!
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 34/45 xfs: rework per-iclog header CIL reservation
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
For every iclog that a CIL push will use up, we need to ensure we have space reserved for the iclog header in each iclog. It is extremely difficult to do this accurately with a per-cpu counter without expensive summing of the counter in every commit. However, we know what the maximum CIL size is going to be because of the hard space limit we have, and hence we know exactly how many iclogs we are going to need to write out the CIL.
We are constrained by the requirement that small transactions only have reservation space for a single iclog header built into them. At commit time we don't know how much of the current transaction reservation is made up of iclog header reservations as calculated by xfs_log_calc_unit_res() when the ticket was reserved. As larger reservations have multiple header spaces reserved, we can steal more than one iclog header reservation at a time, but we only steal the exact number needed for the given log vector size delta.
As a result, we don't know exactly when we are going to steal iclog header reservations, nor do we know exactly how many we are going to need for a given CIL.
To make things simple, start by calculating the worst case number of iclog headers a full CIL push will require. Record this into an atomic variable in the CIL. Then add a byte counter to the log ticket that records exactly how much iclog header space has been reserved in this ticket by xfs_log_calc_unit_res(). This tells us exactly how much space we can steal from the ticket at transaction commit time.
Now, at transaction commit time, we can check if the CIL has a full iclog header reservation and, if not, steal the entire reservation the current ticket holds for iclog headers. This minimises the number of times we need to do atomic operations in the fast path, but still guarantees we get all the reservations we need.
--- fs/xfs/libxfs/xfs_log_rlimit.c | 2 +- fs/xfs/libxfs/xfs_shared.h | 3 +- fs/xfs/xfs_log.c | 12 +++++--- fs/xfs/xfs_log_cil.c | 55 ++++++++++++++++++++++++++-------- fs/xfs/xfs_log_priv.h | 20 +++++++------ 5 files changed, 64 insertions(+), 28 deletions(-)
fs/xfs/libxfs/xfs_log_rlimit.c +1 -1
@@ -88,7 +88,7 @@ xfs_log_calc_minimum_size(xfs_log_get_max_trans_res (mp, &tres); - max_logres =xfs_log_calc_unit_res (mp, tres.tr_logres); + max_logres =xfs_log_calc_unit_res (mp, tres.tr_logres,NULL );if (tres.tr_logcount >1 ) max_logres *= tres.tr_logcount;
fs/xfs/libxfs/xfs_shared.h +2 -1
@@ -48,7 +48,8 @@ extern const struct xfs_buf_ops xfs_symlink_buf_ops;extern const struct xfs_buf_ops xfs_rtbuf_ops;/* log size calculation functions */ -int xfs_log_calc_unit_res (struct xfs_mount *mp,int unit_bytes); +int xfs_log_calc_unit_res (struct xfs_mount *mp,int unit_bytes, +int *niclogs);int xfs_log_calc_minimum_size (struct xfs_mount *);struct xfs_trans_res ;
fs/xfs/xfs_log.c +8 -4
@@ -3312,7 +3312,8 @@ xfs_log_ticket_get(static int xlog_calc_unit_res (struct xlog *log, -int unit_bytes) +int unit_bytes, +int *niclogs) {int iclog_space;uint num_headers;
@@ -3392,15 +3393,18 @@ xlog_calc_unit_res(/* roundoff padding for transaction data and one for commit record */ unit_bytes +=2 * log->l_iclog_roundoff; +if (niclogs) + *niclogs = num_headers;return unit_bytes; }int xfs_log_calc_unit_res (struct xfs_mount *mp, -int unit_bytes) +int unit_bytes, +int *niclogs) { -return xlog_calc_unit_res (mp->m_log, unit_bytes); +return xlog_calc_unit_res (mp->m_log, unit_bytes, niclogs); }/*
@@ -3418,7 +3422,7 @@ xlog_ticket_alloc( tic =kmem_cache_zalloc (xfs_log_ticket_zone,GFP_NOFS | __GFP_NOFAIL); - unit_res =xlog_calc_unit_res (log, unit_bytes); + unit_res =xlog_calc_unit_res (log, unit_bytes, &tic->t_iclog_hdrs);atomic_set (&tic->t_ref,1 ); tic->t_task = current;
fs/xfs/xfs_log_cil.c +42 -13
89 lines not on this page · raw message
fs/xfs/xfs_log_priv.h +11 -9
@@ -140,15 +140,16 @@ enum xlog_iclog_state {#define XLOG_TIC_LEN_MAX 15typedef struct xlog_ticket { -struct list_head t_queue;/* reserve/write queue */ -struct task_struct *t_task;/* task that owns this ticket */ -xlog_tid_t t_tid;/* transaction identifier : 4 */ -atomic_t t_ref;/* ticket reference count : 4 */ -int t_curr_res;/* current reservation in bytes : 4 */ -int t_unit_res;/* unit reservation in bytes : 4 */ -char t_ocnt;/* original count : 1 */ -char t_cnt;/* current count : 1 */ -char t_flags;/* properties of reservation : 1 */ +struct list_head t_queue;/* reserve/write queue */ +struct task_struct *t_task;/* task that owns this ticket */ +xlog_tid_t t_tid;/* transaction identifier */ +atomic_t t_ref;/* ticket reference count */ +int t_curr_res;/* current reservation */ +int t_unit_res;/* unit reservation */ +char t_ocnt;/* original count */ +char t_cnt;/* current count */ +char t_flags;/* properties of reservation */ +int t_iclog_hdrs;/* iclog hdrs in t_curr_res */ }xlog_ticket_t ;/*
@@ -249,6 +250,7 @@ struct xfs_cil_ctx {struct xfs_cil {struct xlog *xc_log;unsigned long xc_flags; +atomic_t xc_iclog_hdrs;struct list_head xc_cil;spinlock_t xc_cil_lock;
Signature · 1 line
2.28.0
Re: [PATCH 34/45] xfs: rework per-iclog header CIL reservation
Quoted 51 lines · Dave Chinner
From: Dave Chinner <[email protected]>
For every iclog that a CIL push will use up, we need to ensure we have space reserved for the iclog header in each iclog. It is extremely difficult to do this accurately with a per-cpu counter without expensive summing of the counter in every commit. However, we know what the maximum CIL size is going to be because of the hard space limit we have, and hence we know exactly how many iclogs we are going to need to write out the CIL.
We are constrained by the requirement that small transactions only have reservation space for a single iclog header built into them. At commit time we don't know how much of the current transaction reservation is made up of iclog header reservations as calculated by
xfs_log_calc_unit_res()when the ticket was reserved. As larger reservations have multiple header spaces reserved, we can steal more than one iclog header reservation at a time, but we only steal the exact number needed for the given log vector size delta.As a result, we don't know exactly when we are going to steal iclog header reservations, nor do we know exactly how many we are going to need for a given CIL.
To make things simple, start by calculating the worst case number of iclog headers a full CIL push will require. Record this into an atomic variable in the CIL. Then add a byte counter to the log ticket that records exactly how much iclog header space has been reserved in this ticket by
xfs_log_calc_unit_res(). This tells us exactly how much space we can steal from the ticket at transaction commit time.Now, at transaction commit time, we can check if the CIL has a full iclog header reservation and, if not, steal the entire reservation the current ticket holds for iclog headers. This minimises the number of times we need to do atomic operations in the fast path, but still guarantees we get all the reservations we need.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/libxfs/xfs_log_rlimit.c | 2 +- fs/xfs/libxfs/xfs_shared.h | 3 +- fs/xfs/xfs_log.c | 12 +++++--- fs/xfs/xfs_log_cil.c | 55 ++++++++++++++++++++++++++-------- fs/xfs/xfs_log_priv.h | 20 +++++++------ 5 files changed, 64 insertions(+), 28 deletions(-)fs/xfs/libxfs/xfs_log_rlimit.c +0
-0
fs/xfs/libxfs/xfs_log_rlimit.c xfs_log_calc_minimum_size( · line 91
Quoted 5 lines
fs/xfs/libxfs/xfs_log_rlimit.c +1
-1@@ -88,7 +88,7 @@ xfs_log_calc_minimum_size(xfs_log_get_max_trans_res (mp, &tres); - max_logres =xfs_log_calc_unit_res (mp, tres.tr_logres); + max_logres =xfs_log_calc_unit_res (mp, tres.tr_logres,NULL );
This is currently the only call site of xfs_log_calc_unit_res, so if a subsequent patch doesn't make use of that last argument it should go away. (I don't know yet, I haven't looked...)
Quoted 53 lines
fs/xfs/libxfs/xfs_log_rlimit.c +0
-0if (tres.tr_logcount >1 ) max_logres *= tres.tr_logcount;fs/xfs/libxfs/xfs_shared.h +2
-1@@ -48,7 +48,8 @@ extern const struct xfs_buf_ops xfs_symlink_buf_ops;extern const struct xfs_buf_ops xfs_rtbuf_ops;/* log size calculation functions */ -int xfs_log_calc_unit_res (struct xfs_mount *mp,int unit_bytes); +int xfs_log_calc_unit_res (struct xfs_mount *mp,int unit_bytes, +int *niclogs);int xfs_log_calc_minimum_size (struct xfs_mount *);struct xfs_trans_res ;fs/xfs/xfs_log.c +7
-3@@ -3312,7 +3312,8 @@ xfs_log_ticket_get(static int xlog_calc_unit_res (struct xlog *log, -int unit_bytes) +int unit_bytes, +int *niclogs) {int iclog_space;uint num_headers;@@ -3392,15 +3393,18 @@ xlog_calc_unit_res(/* roundoff padding for transaction data and one for commit record */ unit_bytes +=2 * log->l_iclog_roundoff; +if (niclogs) + *niclogs = num_headers;return unit_bytes; }int xfs_log_calc_unit_res (struct xfs_mount *mp, -int unit_bytes) +int unit_bytes, +int *niclogs) { -return xlog_calc_unit_res (mp->m_log, unit_bytes); +return xlog_calc_unit_res (mp->m_log, unit_bytes, niclogs); }/*
fs/xfs/xfs_log.c xlog_ticket_alloc( · line 3425
Quoted 5 lines
fs/xfs/xfs_log.c +1
-1@@ -3418,7 +3422,7 @@ xlog_ticket_alloc( tic =kmem_cache_zalloc (xfs_log_ticket_zone,GFP_NOFS | __GFP_NOFAIL); - unit_res =xlog_calc_unit_res (log, unit_bytes); + unit_res =xlog_calc_unit_res (log, unit_bytes, &tic->t_iclog_hdrs);
Ok, so each transaction ticket now gets to know the maximum number of iclog headers that the transaction can consume if we use every last byte of the reservation...
Quoted 29 lines
fs/xfs/xfs_log.c +0
-0atomic_set (&tic->t_ref,1 ); tic->t_task = current;fs/xfs/xfs_log_cil.c +11
-0@@ -44,9 +44,20 @@ xlog_cil_ticket_alloc(* transaction overhead reservation from the first transaction commit. */ tic->t_curr_res =0 ; + tic->t_iclog_hdrs =0 ;return tic; } +static inline void +xlog_cil_set_iclog_hdr_count (struct xfs_cil *cil) +{ +struct xlog *log = cil->xc_log; + +atomic_set (&cil->xc_iclog_hdrs, + (XLOG_CIL_BLOCKING_SPACE_LIMIT (log) / + (log->l_iclog_size - log->l_iclog_hsize))); +} +/* * Unavoidable forward declaration - xlog_cil_push_work() calls * xlog_cil_ctx_alloc() itself.
fs/xfs/xfs_log_cil.c xlog_cil_ctx_switch( · line 84
Quoted 4 lines
fs/xfs/xfs_log_cil.c +1
-0@@ -70,6 +81,7 @@ xlog_cil_ctx_switch(struct xfs_cil *cil, struct xfs_cil_ctx *ctx) { +xlog_cil_set_iclog_hdr_count (cil );
...and I guess every time the CIL gets a fresh context, we also record the maximum number of iclog headers that we might be pushing to disk in one go? Which I guess happens if someone commits a lot of updates to a filesystem, a comitting thread hits the throttle threshold, and now the CIL has to switch contexts and write the old context's transactions to disk?
Quoted 39 lines
fs/xfs/xfs_log_cil.c +7
-11set_bit (XLOG_CIL_EMPTY , &cil->xc_flags); ctx->sequence = ++cil->xc_current_sequence; ctx->cil = cil;@@ -92,6 +104,7 @@ xlog_cil_init_post_recovery( { log->l_cilp->xc_ctx->ticket =xlog_cil_ticket_alloc (log); log->l_cilp->xc_ctx->sequence =1 ; +xlog_cil_set_iclog_hdr_count (log->l_cilp); } static inline int@@ -419,7 +432,6 @@ xlog_cil_insert_items(struct xfs_cil_ctx *ctx = cil->xc_ctx;struct xfs_log_item *lip;int len =0 ; -int iclog_space;int iovhdr_res =0 , split_res =0 , ctx_res =0 ;ASSERT (tp);@@ -442,19 +454,36 @@ xlog_cil_insert_items( test_and_clear_bit(XLOG_CIL_EMPTY, &cil->xc_flags)) ctx_res = ctx->ticket->t_unit_res; -spin_lock (&cil->xc_cil_lock); - -/* do we need space for more log record headers? */ - iclog_space = log->l_iclog_size - log->l_iclog_hsize; -if (len >0 && (ctx->space_used / iclog_space != - (ctx->space_used + len) / iclog_space)) { - split_res = (len + iclog_space -1 ) / iclog_space; -/* need to take into account split region headers, too */ - split_res *= log->l_iclog_hsize +sizeof (struct xlog_op_header ); - ctx->ticket->t_unit_res += split_res; +/* +* Check if we need to steal iclog headers. atomic_read() is not a +* locked atomic operation, so we can check the value before we do any +* real atomic ops in the fast path. If we've already taken the CIL unit +* reservation from this commit, we've already got one iclog header +* space reserved so we have to account for that otherwise we risk
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 472
Quoted 10 lines
fs/xfs/xfs_log_cil.c +10
-0+* overrunning the reservation on this ticket. +* +* If the CIL is already at the hard limit, we might need more header +* space that originally reserved. So steal more header space from every +* commit that occurs once we are over the hard limit to ensure the CIL +* push won't run out of reservation space. +* +* This can steal more than we need, but that's OK. +*/ + if (atomic_read(&cil->xc_iclog_hdrs) > 0 ||
If we haven't stolen enough iclog header space...
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 473
Quoted 1 line
fs/xfs/xfs_log_cil.c +1
-0+ ctx->space_used + len >= XLOG_CIL_BLOCKING_SPACE_LIMIT(log)) {
...or we've hit a throttling threshold, in which case we know we're going to push, so we might as well take everything and (I guess?) not give back any reservation that would encourage more commits before we're ready?
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 480
Quoted 7 lines
fs/xfs/xfs_log_cil.c +7
-0+int split_res = log->l_iclog_hsize + +sizeof (struct xlog_op_header ); +if (ctx_res) + ctx_res += split_res * (tp->t_ticket->t_iclog_hdrs -1 ); +else + ctx_res = split_res * tp->t_ticket->t_iclog_hdrs; +atomic_sub (tp->t_ticket->t_iclog_hdrs, &cil->xc_iclog_hdrs);
What happens if xc_iclog_hdrs goes negative? Does that merely mean that we stole more space from the transaction than we needed? Or does it indicate that we're trying to cram too much into a single context?
I suppose I worry about what might happen if each transaction's committed items actually somehow eats up every byte of reservation and that actually translates to t_iclog_hdrs iclogs being written out with a particular context, where sum(t_iclog_hdrs) is larger than what xlog_cil_set_iclog_hdr_count() precomputes?
--D
Quoted 52 lines
fs/xfs/xfs_log_cil.c +5
-2} - tp->t_ticket->t_curr_res -= split_res + ctx_res + len; - ctx->ticket->t_curr_res += split_res + ctx_res; + +spin_lock (&cil->xc_cil_lock); + tp->t_ticket->t_curr_res -= ctx_res + len; + ctx->ticket->t_unit_res += ctx_res; + ctx->ticket->t_curr_res += ctx_res; ctx->space_used += len;/* fs/xfs/xfs_log_priv.h +11
-9@@ -140,15 +140,16 @@ enum xlog_iclog_state {#define XLOG_TIC_LEN_MAX 15typedef struct xlog_ticket { -struct list_head t_queue;/* reserve/write queue */ -struct task_struct *t_task;/* task that owns this ticket */ -xlog_tid_t t_tid;/* transaction identifier : 4 */ -atomic_t t_ref;/* ticket reference count : 4 */ -int t_curr_res;/* current reservation in bytes : 4 */ -int t_unit_res;/* unit reservation in bytes : 4 */ -char t_ocnt;/* original count : 1 */ -char t_cnt;/* current count : 1 */ -char t_flags;/* properties of reservation : 1 */ +struct list_head t_queue;/* reserve/write queue */ +struct task_struct *t_task;/* task that owns this ticket */ +xlog_tid_t t_tid;/* transaction identifier */ +atomic_t t_ref;/* ticket reference count */ +int t_curr_res;/* current reservation */ +int t_unit_res;/* unit reservation */ +char t_ocnt;/* original count */ +char t_cnt;/* current count */ +char t_flags;/* properties of reservation */ +int t_iclog_hdrs;/* iclog hdrs in t_curr_res */ }xlog_ticket_t ;/* @@ -249,6 +250,7 @@ struct xfs_cil_ctx {struct xfs_cil {struct xlog *xc_log;unsigned long xc_flags; +atomic_t xc_iclog_hdrs;struct list_head xc_cil;spinlock_t xc_cil_lock;Signature · 1 line
2.28.0
Re: [PATCH 34/45] xfs: rework per-iclog header CIL reservation
Quoted 53 lines · Darrick J. Wong
Quoted 52 lines · Dave Chinner
From: Dave Chinner <[email protected]>
For every iclog that a CIL push will use up, we need to ensure we have space reserved for the iclog header in each iclog. It is extremely difficult to do this accurately with a per-cpu counter without expensive summing of the counter in every commit. However, we know what the maximum CIL size is going to be because of the hard space limit we have, and hence we know exactly how many iclogs we are going to need to write out the CIL.
We are constrained by the requirement that small transactions only have reservation space for a single iclog header built into them. At commit time we don't know how much of the current transaction reservation is made up of iclog header reservations as calculated by
xfs_log_calc_unit_res()when the ticket was reserved. As larger reservations have multiple header spaces reserved, we can steal more than one iclog header reservation at a time, but we only steal the exact number needed for the given log vector size delta.As a result, we don't know exactly when we are going to steal iclog header reservations, nor do we know exactly how many we are going to need for a given CIL.
To make things simple, start by calculating the worst case number of iclog headers a full CIL push will require. Record this into an atomic variable in the CIL. Then add a byte counter to the log ticket that records exactly how much iclog header space has been reserved in this ticket by
xfs_log_calc_unit_res(). This tells us exactly how much space we can steal from the ticket at transaction commit time.Now, at transaction commit time, we can check if the CIL has a full iclog header reservation and, if not, steal the entire reservation the current ticket holds for iclog headers. This minimises the number of times we need to do atomic operations in the fast path, but still guarantees we get all the reservations we need.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/libxfs/xfs_log_rlimit.c | 2 +- fs/xfs/libxfs/xfs_shared.h | 3 +- fs/xfs/xfs_log.c | 12 +++++--- fs/xfs/xfs_log_cil.c | 55 ++++++++++++++++++++++++++-------- fs/xfs/xfs_log_priv.h | 20 +++++++------ 5 files changed, 64 insertions(+), 28 deletions(-)fs/xfs/libxfs/xfs_log_rlimit.c +0
-0@@ -88,7 +88,7 @@ xfs_log_calc_minimum_size(
Quoted 8 lines
fs/xfs/libxfs/xfs_log_rlimit.c xfs_log_calc_minimum_size( · line 91
Quoted 4 lines
fs/xfs/libxfs/xfs_log_rlimit.c +1
-1xfs_log_get_max_trans_res (mp, &tres); - max_logres =xfs_log_calc_unit_res (mp, tres.tr_logres); + max_logres =xfs_log_calc_unit_res (mp, tres.tr_logres,NULL );This is currently the only call site of
xfs_log_calc_unit_res, so if a subsequent patch doesn't make use of that last argument it should go away. (I don't know yet, I haven't looked...)
Can't remember, I'll have to check.
Quoted 10 lines
fs/xfs/libxfs/xfs_log_rlimit.c xlog_ticket_alloc( · line 3425
Quoted 6 lines
fs/xfs/libxfs/xfs_log_rlimit.c +1
-1@@ -3418,7 +3422,7 @@ xlog_ticket_alloc( tic =kmem_cache_zalloc (xfs_log_ticket_zone,GFP_NOFS | __GFP_NOFAIL); - unit_res =xlog_calc_unit_res (log, unit_bytes); + unit_res =xlog_calc_unit_res (log, unit_bytes, &tic->t_iclog_hdrs);Ok, so each transaction ticket now gets to know the maximum number of iclog headers that the transaction can consume if we use every last byte of the reservation...
yes.
Quoted 26 lines
Quoted 26 lines
fs/xfs/xfs_log_cil.c +11
-0@@ -44,9 +44,20 @@ xlog_cil_ticket_alloc(* transaction overhead reservation from the first transaction commit. */ tic->t_curr_res =0 ; + tic->t_iclog_hdrs =0 ;return tic; } +static inline void +xlog_cil_set_iclog_hdr_count (struct xfs_cil *cil) +{ +struct xlog *log = cil->xc_log; + +atomic_set (&cil->xc_iclog_hdrs, + (XLOG_CIL_BLOCKING_SPACE_LIMIT (log) / + (log->l_iclog_size - log->l_iclog_hsize))); +} +/* * Unavoidable forward declaration - xlog_cil_push_work() calls * xlog_cil_ctx_alloc() itself. @@ -70,6 +81,7 @@ xlog_cil_ctx_switch(
Quoted 8 lines
fs/xfs/xfs_log_cil.c xlog_cil_ctx_switch( · line 84
Quoted 4 lines
fs/xfs/xfs_log_cil.c +1
-0struct xfs_cil *cil, struct xfs_cil_ctx *ctx) { +xlog_cil_set_iclog_hdr_count (cil );...and I guess every time the CIL gets a fresh context, we also record the maximum number of iclog headers that we might be pushing to disk in one go?
Yes. that defines the maximum size of the iclog header reservation the CIL checkpoint is going to need if it stays within the hard limit.
Quoted 4 lines
Which I guess happens if someone commits a lot of updates to a filesystem, a comitting thread hits the throttle threshold, and now the CIL has to switch contexts and write the old context's transactions to disk?
Right - it reserves enough space for delays in context switches to use all the overrun without having to do anything ... slow.
Quoted 33 lines
Quoted 20 lines
fs/xfs/xfs_log_cil.c +6
-10@@ -442,19 +454,36 @@ xlog_cil_insert_items( test_and_clear_bit(XLOG_CIL_EMPTY, &cil->xc_flags)) ctx_res = ctx->ticket->t_unit_res; -spin_lock (&cil->xc_cil_lock); - -/* do we need space for more log record headers? */ - iclog_space = log->l_iclog_size - log->l_iclog_hsize; -if (len >0 && (ctx->space_used / iclog_space != - (ctx->space_used + len) / iclog_space)) { - split_res = (len + iclog_space -1 ) / iclog_space; -/* need to take into account split region headers, too */ - split_res *= log->l_iclog_hsize +sizeof (struct xlog_op_header ); - ctx->ticket->t_unit_res += split_res; +/* +* Check if we need to steal iclog headers. atomic_read() is not a +* locked atomic operation, so we can check the value before we do any +* real atomic ops in the fast path. If we've already taken the CIL unit +* reservation from this commit, we've already got one iclog header +* space reserved so we have to account for that otherwise we risk fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 472
Quoted 10 lines
fs/xfs/xfs_log_cil.c +10
-0+* overrunning the reservation on this ticket. +* +* If the CIL is already at the hard limit, we might need more header +* space that originally reserved. So steal more header space from every +* commit that occurs once we are over the hard limit to ensure the CIL +* push won't run out of reservation space. +* +* This can steal more than we need, but that's OK. +*/ + if (atomic_read(&cil->xc_iclog_hdrs) > 0 ||If we haven't stolen enough iclog header space...
Quoted 6 lines
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 473
Quoted 1 line
fs/xfs/xfs_log_cil.c +1
-0+ ctx->space_used + len >= XLOG_CIL_BLOCKING_SPACE_LIMIT(log)) {...or we've hit a throttling threshold, in which case we know we're going to push, so we might as well take everything and (I guess?) not give back any reservation that would encourage more commits before we're ready?
Partially. This is also safety against the CIL bumping back down below and above the space limit multiple times. It just ensures that every transaction that commits over the hard limit is guaranteed to have enough iclog headers reserved to write the CIL when it goes over the hard limit.
Quoted 11 lines
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 480
Quoted 7 lines
fs/xfs/xfs_log_cil.c +7
-0+int split_res = log->l_iclog_hsize + +sizeof (struct xlog_op_header ); +if (ctx_res) + ctx_res += split_res * (tp->t_ticket->t_iclog_hdrs -1 ); +else + ctx_res = split_res * tp->t_ticket->t_iclog_hdrs; +atomic_sub (tp->t_ticket->t_iclog_hdrs, &cil->xc_iclog_hdrs);What happens if
xc_iclog_hdrsgoes negative? Does that merely mean that we stole more space from the transaction than we needed? Or does it indicate that we're trying to cram too much into a single context?
Nothing. Yes. Indicates that we have commits throttling on the hard limit.
Quoted 5 lines
I suppose I worry about what might happen if each transaction's committed items actually somehow eats up every byte of reservation and that actually translates to
t_iclog_hdrsiclogs being written out with a particular context, wheresum(t_iclog_hdrs)is larger than whatxlog_cil_set_iclog_hdr_count()precomputes?
If I understand what you are asking correctly, that should never happen because the iclog header count should always span the maximum number of iclogs that change requires to write into the log. And the CIL context also reserves enough headers to write the entire set of CIL data to the iclogs, so again we should not ever get into an overrun situation because we have maximally dirty transactions being committed. If these sorts of overruns ever occur, we've got a unit reservation calculation issue, not a CIL iclog header space reservation stealling issue...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 35/45 xfs: introduce per-cpu CIL tracking sructure
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
The CIL push lock is highly contended on larger machines, becoming a hard bottleneck that about 700,000 transaction commits/s on >16p machines. To address this, start moving the CIL tracking infrastructure to utilise per-CPU structures.
We need to track the space used, the amount of log reservation space reserved to write the CIL, the log items in the CIL and the busy extents that need to be completed by the CIL commit. This requires a couple of per-cpu counters, an unordered per-cpu list and a globally ordered per-cpu list.
Create a per-cpu structure to hold these and all the management interfaces needed, as well as the hooks to handle hotplug CPUs.
--- fs/xfs/xfs_log_cil.c | 94 ++++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_log_priv.h | 15 ++++++ include/linux/cpuhotplug.h | 1 + 3 files changed, 110 insertions(+)
fs/xfs/xfs_log_cil.c +94 -0
@@ -1365,6 +1365,93 @@ xfs_log_item_in_current_chkpt(return true; } +#ifdef CONFIG_HOTPLUG_CPU +static LIST_HEAD (xlog_cil_pcp_list); +static DEFINE_SPINLOCK (xlog_cil_pcp_lock); +static bool xlog_cil_pcp_init; + +static int +xlog_cil_pcp_dead ( +unsigned int cpu) +{ +struct xfs_cil *cil; + +spin_lock (&xlog_cil_pcp_lock); +list_for_each_entry (cil, &xlog_cil_pcp_list, xc_pcp_list) { +/* move stuff on dead CPU to context */ + } +spin_unlock (&xlog_cil_pcp_lock); +return 0 ; +} + +static int +xlog_cil_pcp_hpadd ( +struct xfs_cil *cil) +{ +if (!xlog_cil_pcp_init) { +int ret; + ret =cpuhp_setup_state_nocalls (CPUHP_XFS_CIL_DEAD , +"xfs/cil_pcp:dead" ,NULL , + xlog_cil_pcp_dead); +if (ret <0 ) { +xfs_warn (cil->xc_log->l_mp, +"Failed to initialise CIL hotplug, error %d. XFS is non-functional." , + ret); +ASSERT (0 ); +return -ENOMEM ; + } + xlog_cil_pcp_init = true; + } + +INIT_LIST_HEAD (&cil->xc_pcp_list); +spin_lock (&xlog_cil_pcp_lock); +list_add (&cil->xc_pcp_list, &xlog_cil_pcp_list); +spin_unlock (&xlog_cil_pcp_lock); +return 0 ; +} + +static void +xlog_cil_pcp_hpremove ( +struct xfs_cil *cil) +{ +spin_lock (&xlog_cil_pcp_lock); +list_del (&cil->xc_pcp_list); +spin_unlock (&xlog_cil_pcp_lock); +} + +#else /* !CONFIG_HOTPLUG_CPU */ +static inline void xlog_cil_pcp_hpadd (struct xfs_cil *cil) {} +static inline void xlog_cil_pcp_hpremove (struct xfs_cil *cil) {} +#endif + +static void __percpu * +xlog_cil_pcp_alloc ( +struct xfs_cil *cil) +{ +struct xlog_cil_pcp *cilpcp; + + cilpcp = alloc_percpu(struct xlog_cil_pcp); +if (!cilpcp) +return NULL ; + +if (xlog_cil_pcp_hpadd (cil) <0 ) { +free_percpu (cilpcp); +return NULL ; + } +return cilpcp; +} + +static void +xlog_cil_pcp_free ( +struct xfs_cil *cil, +struct xlog_cil_pcp *cilpcp) +{ +if (!cilpcp) +return ; +xlog_cil_pcp_hpremove (cil); +free_percpu (cilpcp); +} +/* * Perform initial CIL structure initialisation. */
@@ -1379,6 +1466,12 @@ xlog_cil_init(if (!cil)return -ENOMEM ; + cil->xc_pcp =xlog_cil_pcp_alloc (cil); +if (!cil->xc_pcp) { +kmem_free (cil); +return -ENOMEM ; + } +INIT_LIST_HEAD (&cil->xc_cil);INIT_LIST_HEAD (&cil->xc_committing);spin_lock_init (&cil->xc_cil_lock);
@@ -1409,6 +1502,7 @@ xlog_cil_destroy(ASSERT (list_empty (&cil->xc_cil));ASSERT (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)); +xlog_cil_pcp_free (cil, cil->xc_pcp);kmem_free (cil); }
fs/xfs/xfs_log_priv.h +15 -0
@@ -231,6 +231,16 @@ struct xfs_cil_ctx {struct work_struct push_work; }; +/* +* Per-cpu CIL tracking items +*/ +struct xlog_cil_pcp { +uint32_t space_used; +uint32_t curr_res; +struct list_head busy_extents; +struct list_head log_items; +}; +/* * Committed Item List structure *
@@ -264,6 +274,11 @@ struct xfs_cil {wait_queue_head_t xc_commit_wait;xfs_csn_t xc_current_sequence;wait_queue_head_t xc_push_wait;/* background push throttle */ + +struct xlog_cil_pcp __percpu *xc_pcp; +#ifdef CONFIG_HOTPLUG_CPU +struct list_head xc_pcp_list; +#endif }____cacheline_aligned_in_smp ;/* xc_flags bit values */
include/linux/cpuhotplug.h +1 -0
@@ -52,6 +52,7 @@ enum cpuhp_state {CPUHP_FS_BUFF_DEAD ,CPUHP_PRINTK_DEAD ,CPUHP_MM_MEMCQ_DEAD , +CPUHP_XFS_CIL_DEAD ,CPUHP_PERCPU_CNT_DEAD ,CPUHP_RADIX_DEAD , CPUHP_PAGE_ALLOC_DEAD,
Signature · 1 line
2.28.0
Re: [PATCH 35/45] xfs: introduce per-cpu CIL tracking sructure
Quoted 34 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The CIL push lock is highly contended on larger machines, becoming a hard bottleneck that about 700,000 transaction commits/s on >16p machines. To address this, start moving the CIL tracking infrastructure to utilise per-CPU structures.
We need to track the space used, the amount of log reservation space reserved to write the CIL, the log items in the CIL and the busy extents that need to be completed by the CIL commit. This requires a couple of per-cpu counters, an unordered per-cpu list and a globally ordered per-cpu list.
Create a per-cpu structure to hold these and all the management interfaces needed, as well as the hooks to handle hotplug CPUs.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 94 ++++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_log_priv.h | 15 ++++++ include/linux/cpuhotplug.h | 1 + 3 files changed, 110 insertions(+)fs/xfs/xfs_log_cil.c +3
-0@@ -1365,6 +1365,93 @@ xfs_log_item_in_current_chkpt(return true; } +#ifdef CONFIG_HOTPLUG_CPU +static LIST_HEAD (xlog_cil_pcp_list); +static DEFINE_SPINLOCK (xlog_cil_pcp_lock);
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1380
Quoted 10 lines
fs/xfs/xfs_log_cil.c +10
-0+static bool xlog_cil_pcp_init; + +static int +xlog_cil_pcp_dead ( +unsigned int cpu) +{ +struct xfs_cil *cil; + +spin_lock (&xlog_cil_pcp_lock); +list_for_each_entry (cil, &xlog_cil_pcp_list, xc_pcp_list) {
Weird indentation.
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1381
Quoted 1 line
fs/xfs/xfs_log_cil.c +1
-0+/* move stuff on dead CPU to context */
Should this have some actual code? I don't think any of the remaining patches add anything here.
Quoted 7 lines
fs/xfs/xfs_log_cil.c +7
-0+ } +spin_unlock (&xlog_cil_pcp_lock); +return 0 ; +} + +static int +xlog_cil_pcp_hpadd(
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1398
Quoted 10 lines
fs/xfs/xfs_log_cil.c +10
-0+ struct xfs_cil *cil) +{ +if (!xlog_cil_pcp_init) { +int ret; + ret =cpuhp_setup_state_nocalls (CPUHP_XFS_CIL_DEAD , +"xfs/cil_pcp:dead" ,NULL , + xlog_cil_pcp_dead); +if (ret <0 ) { + xfs_warn(cil->xc_log->l_mp, + "Failed to initialise CIL hotplug, error %d. XFS is non-functional.",
How likely is to happen?
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1400
Quoted 2 lines
fs/xfs/xfs_log_cil.c +2
-0+ ret); +ASSERT (0 );
I guess not that often?
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1401
Quoted 1 line
fs/xfs/xfs_log_cil.c +1
-0+return -ENOMEM ;
Why not return ret here? I guess it's because ret could be any number of (not centrally documented?) error codes, and we don't really care to expose that to userspace?
--D
Quoted 125 lines
fs/xfs/xfs_log_cil.c +60
-0+ } + xlog_cil_pcp_init = true; + } + +INIT_LIST_HEAD (&cil->xc_pcp_list); +spin_lock (&xlog_cil_pcp_lock); +list_add (&cil->xc_pcp_list, &xlog_cil_pcp_list); +spin_unlock (&xlog_cil_pcp_lock); +return 0 ; +} + +static void +xlog_cil_pcp_hpremove ( +struct xfs_cil *cil) +{ +spin_lock (&xlog_cil_pcp_lock); +list_del (&cil->xc_pcp_list); +spin_unlock (&xlog_cil_pcp_lock); +} + +#else /* !CONFIG_HOTPLUG_CPU */ +static inline void xlog_cil_pcp_hpadd (struct xfs_cil *cil) {} +static inline void xlog_cil_pcp_hpremove (struct xfs_cil *cil) {} +#endif + +static void __percpu * +xlog_cil_pcp_alloc ( +struct xfs_cil *cil) +{ +struct xlog_cil_pcp *cilpcp; + + cilpcp = alloc_percpu(struct xlog_cil_pcp); +if (!cilpcp) +return NULL ; + +if (xlog_cil_pcp_hpadd (cil) <0 ) { +free_percpu (cilpcp); +return NULL ; + } +return cilpcp; +} + +static void +xlog_cil_pcp_free ( +struct xfs_cil *cil, +struct xlog_cil_pcp *cilpcp) +{ +if (!cilpcp) +return ; +xlog_cil_pcp_hpremove (cil); +free_percpu (cilpcp); +} +/* * Perform initial CIL structure initialisation. */ @@ -1379,6 +1466,12 @@ xlog_cil_init(if (!cil)return -ENOMEM ; + cil->xc_pcp =xlog_cil_pcp_alloc (cil); +if (!cil->xc_pcp) { +kmem_free (cil); +return -ENOMEM ; + } +INIT_LIST_HEAD (&cil->xc_cil);INIT_LIST_HEAD (&cil->xc_committing);spin_lock_init (&cil->xc_cil_lock);@@ -1409,6 +1502,7 @@ xlog_cil_destroy(ASSERT (list_empty (&cil->xc_cil));ASSERT (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags)); +xlog_cil_pcp_free (cil, cil->xc_pcp);kmem_free (cil); }fs/xfs/xfs_log_priv.h +15
-0@@ -231,6 +231,16 @@ struct xfs_cil_ctx {struct work_struct push_work; }; +/* +* Per-cpu CIL tracking items +*/ +struct xlog_cil_pcp { +uint32_t space_used; +uint32_t curr_res; +struct list_head busy_extents; +struct list_head log_items; +}; +/* * Committed Item List structure * @@ -264,6 +274,11 @@ struct xfs_cil {wait_queue_head_t xc_commit_wait;xfs_csn_t xc_current_sequence;wait_queue_head_t xc_push_wait;/* background push throttle */ + +struct xlog_cil_pcp __percpu *xc_pcp; +#ifdef CONFIG_HOTPLUG_CPU +struct list_head xc_pcp_list; +#endif }____cacheline_aligned_in_smp ;/* xc_flags bit values */ include/linux/cpuhotplug.h +1
-0@@ -52,6 +52,7 @@ enum cpuhp_state {CPUHP_FS_BUFF_DEAD ,CPUHP_PRINTK_DEAD ,CPUHP_MM_MEMCQ_DEAD , +CPUHP_XFS_CIL_DEAD ,CPUHP_PERCPU_CNT_DEAD ,CPUHP_RADIX_DEAD , CPUHP_PAGE_ALLOC_DEAD,Signature · 1 line
2.28.0
Re: [PATCH 35/45] xfs: introduce per-cpu CIL tracking sructure
Quoted 48 lines · Darrick J. Wong
Quoted 34 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The CIL push lock is highly contended on larger machines, becoming a hard bottleneck that about 700,000 transaction commits/s on >16p machines. To address this, start moving the CIL tracking infrastructure to utilise per-CPU structures.
We need to track the space used, the amount of log reservation space reserved to write the CIL, the log items in the CIL and the busy extents that need to be completed by the CIL commit. This requires a couple of per-cpu counters, an unordered per-cpu list and a globally ordered per-cpu list.
Create a per-cpu structure to hold these and all the management interfaces needed, as well as the hooks to handle hotplug CPUs.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 94 ++++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_log_priv.h | 15 ++++++ include/linux/cpuhotplug.h | 1 + 3 files changed, 110 insertions(+)fs/xfs/xfs_log_cil.c +3
-0@@ -1365,6 +1365,93 @@ xfs_log_item_in_current_chkpt(return true; } +#ifdef CONFIG_HOTPLUG_CPU +static LIST_HEAD (xlog_cil_pcp_list); +static DEFINE_SPINLOCK (xlog_cil_pcp_lock);fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1380
Quoted 10 lines
fs/xfs/xfs_log_cil.c +10
-0+static bool xlog_cil_pcp_init; + +static int +xlog_cil_pcp_dead ( +unsigned int cpu) +{ +struct xfs_cil *cil; + +spin_lock (&xlog_cil_pcp_lock); +list_for_each_entry (cil, &xlog_cil_pcp_list, xc_pcp_list) {Weird indentation.
Quoted 4 lines
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1381
Quoted 1 line
fs/xfs/xfs_log_cil.c +1
-0+/* move stuff on dead CPU to context */ Should this have some actual code? I don't think any of the remaining patches add anything here.
They should be moving stuff to the current CIL ctx so it is captured when the CPU goes down.
Quoted 12 lines
Quoted 12 lines
fs/xfs/xfs_log_cil.c +11
-0+ } +spin_unlock (&xlog_cil_pcp_lock); +return 0 ; +} + +static int +xlog_cil_pcp_hpadd ( +struct xfs_cil *cil) +{ +if (!xlog_cil_pcp_init) { +int ret;
Quoted 8 lines
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1398
Quoted 6 lines
fs/xfs/xfs_log_cil.c +6
-0+ ret =cpuhp_setup_state_nocalls (CPUHP_XFS_CIL_DEAD , +"xfs/cil_pcp:dead" ,NULL , + xlog_cil_pcp_dead); +if (ret <0 ) { + xfs_warn(cil->xc_log->l_mp, + "Failed to initialise CIL hotplug, error %d. XFS is non-functional.",How likely is to happen?
AFAICT, very unlikely, but....
Quoted 11 lines
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1400
Quoted 3 lines
fs/xfs/xfs_log_cil.c +2
-0+ ret); +ASSERT (0 );I guess not that often?
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1401
Quoted 1 line
fs/xfs/xfs_log_cil.c +1
-0+return -ENOMEM ;Why not return ret here? I guess it's because ret could be any number of (not centrally documented?) error codes, and we don't really care to expose that to userspace?
... yeah.
The cpu hotplug stuff is poorly documented and the code is another of those "maze of twisty passages" implementations that bounce through many functions that can return all sorts of different stuff from lots of different things that could got wrong. I see at least EINVAL, ENOSPC, ENOMEM, EBUSY and EAGAIN could be returned, and I have no idea what any of them would actually mean went wrong.
In the end I kinda just copied what the radix tree and percpu counter code do with an error failing to init hotplug calls: WARN and then ignore. Hence from the filesystem POV, the error may as well be ENOMEM because we couldn't set something critical up and that saves us getting confused over whether the error is fatal or not at a higher level.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 35/45] xfs: introduce per-cpu CIL tracking sructure
Quoted 49 lines · Dave Chinner
Quoted 48 lines · Darrick J. Wong
Quoted 34 lines · Dave Chinner
From: Dave Chinner <[email protected]>
The CIL push lock is highly contended on larger machines, becoming a hard bottleneck that about 700,000 transaction commits/s on >16p machines. To address this, start moving the CIL tracking infrastructure to utilise per-CPU structures.
We need to track the space used, the amount of log reservation space reserved to write the CIL, the log items in the CIL and the busy extents that need to be completed by the CIL commit. This requires a couple of per-cpu counters, an unordered per-cpu list and a globally ordered per-cpu list.
Create a per-cpu structure to hold these and all the management interfaces needed, as well as the hooks to handle hotplug CPUs.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 94 ++++++++++++++++++++++++++++++++++++++ fs/xfs/xfs_log_priv.h | 15 ++++++ include/linux/cpuhotplug.h | 1 + 3 files changed, 110 insertions(+)fs/xfs/xfs_log_cil.c +3
-0@@ -1365,6 +1365,93 @@ xfs_log_item_in_current_chkpt(return true; } +#ifdef CONFIG_HOTPLUG_CPU +static LIST_HEAD (xlog_cil_pcp_list); +static DEFINE_SPINLOCK (xlog_cil_pcp_lock);fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1380
Quoted 10 lines
fs/xfs/xfs_log_cil.c +10
-0+static bool xlog_cil_pcp_init; + +static int +xlog_cil_pcp_dead ( +unsigned int cpu) +{ +struct xfs_cil *cil; + +spin_lock (&xlog_cil_pcp_lock); +list_for_each_entry (cil, &xlog_cil_pcp_list, xc_pcp_list) {Weird indentation.
Quoted 7 lines
Quoted 4 lines
fs/xfs/xfs_log_cil.c xfs_log_item_in_current_chkpt( · line 1381
Quoted 1 line
fs/xfs/xfs_log_cil.c +1
-0+/* move stuff on dead CPU to context */ Should this have some actual code? I don't think any of the remaining patches add anything here.
They should be moving stuff to the current CIL ctx so it is captured when the CPU goes down.
Yup, looks like I missed updating this. Will add it in the patches that need it.
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 36/45 xfs: implement percpu cil space used calculation
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Now that we have the CIL percpu structures in place, implement the space used counter with a fast sum check similar to the percpu_counter infrastructure.
--- fs/xfs/xfs_log_cil.c | 42 ++++++++++++++++++++++++++++++++++++------ fs/xfs/xfs_log_priv.h | 2 +- 2 files changed, 37 insertions(+), 7 deletions(-)
fs/xfs/xfs_log_cil.c +36 -6
@@ -433,6 +433,8 @@ xlog_cil_insert_items(struct xfs_log_item *lip;int len =0 ;int iovhdr_res =0 , split_res =0 , ctx_res =0 ; +int space_used; +struct xlog_cil_pcp *cilpcp;ASSERT (tp);
@@ -469,8 +471,9 @@ xlog_cil_insert_items(* * This can steal more than we need, but that's OK. */ + space_used =atomic_read (&ctx->space_used);if (atomic_read (&cil->xc_iclog_hdrs) >0 || - ctx->space_used + len >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log)) { + space_used + len >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log)) {int split_res = log->l_iclog_hsize +sizeof (struct xlog_op_header );if (ctx_res)
@@ -480,16 +483,34 @@ xlog_cil_insert_items(atomic_sub (tp->t_ticket->t_iclog_hdrs, &cil->xc_iclog_hdrs); } +/* +* Update the CIL percpu pointer. This updates the global counter when +* over the percpu batch size or when the CIL is over the space limit. +* This means low lock overhead for normal updates, and when over the +* limit the space used is immediately accounted. This makes enforcing +* the hard limit much more accurate. The per cpu fold threshold is +* based on how close we are to the hard limit. +*/ + cilpcp =get_cpu_ptr (cil->xc_pcp); + cilpcp->space_used += len; +if (space_used >=XLOG_CIL_SPACE_LIMIT (log) || + cilpcp->space_used > + ((XLOG_CIL_BLOCKING_SPACE_LIMIT (log) - space_used) / +num_online_cpus ())) { +atomic_add (cilpcp->space_used, &ctx->space_used); + cilpcp->space_used =0 ; + } +put_cpu_ptr (cilpcp); +spin_lock (&cil->xc_cil_lock); - tp->t_ticket->t_curr_res -= ctx_res + len; ctx->ticket->t_unit_res += ctx_res; ctx->ticket->t_curr_res += ctx_res; - ctx->space_used += len;/* * If we've overrun the reservation, dump the tx details before we move * the log items. Shutdown is imminent... */ + tp->t_ticket->t_curr_res -= ctx_res + len;if (WARN_ON (tp->t_ticket->t_curr_res <0 )) {xfs_warn (log->l_mp,"Transaction log reservation overrun:" ); xfs_warn(log->l_mp,
@@ -769,12 +790,20 @@ xlog_cil_push_work(struct bio bio;DECLARE_COMPLETION_ONSTACK (bdev_flush);bool commit_iclog_sync = false; +int cpu; +struct xlog_cil_pcp *cilpcp; new_ctx =xlog_cil_ctx_alloc (); new_ctx->ticket =xlog_cil_ticket_alloc (log);down_write (&cil->xc_ctx_lock); +/* Reset the CIL pcp counters */ +for_each_online_cpu (cpu) { + cilpcp =per_cpu_ptr (cil->xc_pcp, cpu); + cilpcp->space_used =0 ; + } +spin_lock (&cil->xc_push_lock); push_seq = cil->xc_push_seq;ASSERT (push_seq <= ctx->sequence);
@@ -1042,6 +1071,7 @@ xlog_cil_push_background( struct xlog *log) __releases(cil->xc_ctx_lock) {struct xfs_cil *cil = log->l_cilp; +int space_used =atomic_read (&cil->xc_ctx->space_used);/* * The cil won't be empty because we are called while holding the
@@ -1054,7 +1084,7 @@ xlog_cil_push_background(* Don't do a background push if we haven't used up all the * space available yet. */ -if (cil->xc_ctx->space_used <XLOG_CIL_SPACE_LIMIT (log)) { +if (space_used <XLOG_CIL_SPACE_LIMIT (log)) {up_read (&cil->xc_ctx_lock);return ; }
@@ -1083,10 +1113,10 @@ xlog_cil_push_background(* The ctx->xc_push_lock provides the serialisation necessary for safely * using the lockless waitqueue_active() check in this context. */ -if (cil->xc_ctx->space_used >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log) || +if (space_used >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log) ||waitqueue_active (&cil->xc_push_wait)) {trace_xfs_log_cil_wait (log, cil->xc_ctx->ticket); -ASSERT (cil->xc_ctx->space_used < log->l_logsize); +ASSERT (space_used < log->l_logsize);xlog_wait (&cil->xc_push_wait, &cil->xc_push_lock);return ; }
fs/xfs/xfs_log_priv.h +1 -1
@@ -222,7 +222,7 @@ struct xfs_cil_ctx {xfs_lsn_t commit_lsn;/* chkpt commit record lsn */ struct xlog_ticket *ticket;/* chkpt ticket */ int nvecs;/* number of regions */ -int space_used;/* aggregate size of regions */ +atomic_t space_used;/* aggregate size of regions */ struct list_head busy_extents;/* busy extents in chkpt */ struct xfs_log_vec *lv_chain;/* logvecs being pushed */ struct list_head iclog_entry;
Signature · 1 line
2.28.0
Re: [PATCH 36/45] xfs: implement percpu cil space used calculation
Quoted 44 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Now that we have the CIL percpu structures in place, implement the space used counter with a fast sum check similar to the
percpu_counterinfrastructure.Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 42 ++++++++++++++++++++++++++++++++++++------ fs/xfs/xfs_log_priv.h | 2 +- 2 files changed, 37 insertions(+), 7 deletions(-)fs/xfs/xfs_log_cil.c +8
-1@@ -433,6 +433,8 @@ xlog_cil_insert_items(struct xfs_log_item *lip;int len =0 ;int iovhdr_res =0 , split_res =0 , ctx_res =0 ; +int space_used; +struct xlog_cil_pcp *cilpcp;ASSERT (tp);@@ -469,8 +471,9 @@ xlog_cil_insert_items(* * This can steal more than we need, but that's OK. */ + space_used =atomic_read (&ctx->space_used);if (atomic_read (&cil->xc_iclog_hdrs) >0 || - ctx->space_used + len >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log)) { + space_used + len >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log)) {int split_res = log->l_iclog_hsize +sizeof (struct xlog_op_header );if (ctx_res)@@ -480,16 +483,34 @@ xlog_cil_insert_items(atomic_sub (tp->t_ticket->t_iclog_hdrs, &cil->xc_iclog_hdrs); } +/* +* Update the CIL percpu pointer. This updates the global counter when +* over the percpu batch size or when the CIL is over the space limit. +* This means low lock overhead for normal updates, and when over the
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 499
Quoted 10 lines
fs/xfs/xfs_log_cil.c +10
-0+* limit the space used is immediately accounted. This makes enforcing +* the hard limit much more accurate. The per cpu fold threshold is +* based on how close we are to the hard limit. +*/ + cilpcp =get_cpu_ptr (cil->xc_pcp); + cilpcp->space_used += len; +if (space_used >=XLOG_CIL_SPACE_LIMIT (log) || + cilpcp->space_used > + ((XLOG_CIL_BLOCKING_SPACE_LIMIT (log) - space_used) / +num_online_cpus ())) {
What happens if the log is very small and there are hundreds of CPUs? Can we end up on this slow path on a regular basis even if the amount of space used is not that large?
Granted I can't think of a good way out of that, since I suspect that if you do that you're already going to be hurting in 5 other places anyway. That said ... I /do/ keep getting bugs from people with tiny logs on big iron. Some day I'll (ha!) stomp out all the bugs that are "NO do not let your deployment system growfs 10000x, this is not ext4"...
Quoted 6 lines
fs/xfs/xfs_log_cil.c +5
-0+atomic_add (cilpcp->space_used, &ctx->space_used); + cilpcp->space_used =0 ; + } +put_cpu_ptr (cilpcp); +spin_lock (&cil->xc_cil_lock);
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 513
Quoted 10 lines
fs/xfs/xfs_log_cil.c +1
-2- tp->t_ticket->t_curr_res -= ctx_res + len; ctx->ticket->t_unit_res += ctx_res; ctx->ticket->t_curr_res += ctx_res; - ctx->space_used += len;/* * If we've overrun the reservation, dump the tx details before we move * the log items. Shutdown is imminent... */ + tp->t_ticket->t_curr_res -= ctx_res + len;
Is moving this really necessary?
--D
Quoted 70 lines
fs/xfs/xfs_log_cil.c +12
-3if (WARN_ON (tp->t_ticket->t_curr_res <0 )) {xfs_warn (log->l_mp,"Transaction log reservation overrun:" ); xfs_warn(log->l_mp,@@ -769,12 +790,20 @@ xlog_cil_push_work(struct bio bio;DECLARE_COMPLETION_ONSTACK (bdev_flush);bool commit_iclog_sync = false; +int cpu; +struct xlog_cil_pcp *cilpcp; new_ctx =xlog_cil_ctx_alloc (); new_ctx->ticket =xlog_cil_ticket_alloc (log);down_write (&cil->xc_ctx_lock); +/* Reset the CIL pcp counters */ +for_each_online_cpu (cpu) { + cilpcp =per_cpu_ptr (cil->xc_pcp, cpu); + cilpcp->space_used =0 ; + } +spin_lock (&cil->xc_push_lock); push_seq = cil->xc_push_seq;ASSERT (push_seq <= ctx->sequence);@@ -1042,6 +1071,7 @@ xlog_cil_push_background( struct xlog *log) __releases(cil->xc_ctx_lock) {struct xfs_cil *cil = log->l_cilp; +int space_used =atomic_read (&cil->xc_ctx->space_used);/* * The cil won't be empty because we are called while holding the @@ -1054,7 +1084,7 @@ xlog_cil_push_background(* Don't do a background push if we haven't used up all the * space available yet. */ -if (cil->xc_ctx->space_used <XLOG_CIL_SPACE_LIMIT (log)) { +if (space_used <XLOG_CIL_SPACE_LIMIT (log)) {up_read (&cil->xc_ctx_lock);return ; }@@ -1083,10 +1113,10 @@ xlog_cil_push_background(* The ctx->xc_push_lock provides the serialisation necessary for safely * using the lockless waitqueue_active() check in this context. */ -if (cil->xc_ctx->space_used >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log) || +if (space_used >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log) ||waitqueue_active (&cil->xc_push_wait)) {trace_xfs_log_cil_wait (log, cil->xc_ctx->ticket); -ASSERT (cil->xc_ctx->space_used < log->l_logsize); +ASSERT (space_used < log->l_logsize);xlog_wait (&cil->xc_push_wait, &cil->xc_push_lock);return ; }fs/xfs/xfs_log_priv.h +1
-1@@ -222,7 +222,7 @@ struct xfs_cil_ctx {xfs_lsn_t commit_lsn;/* chkpt commit record lsn */ struct xlog_ticket *ticket;/* chkpt ticket */ int nvecs;/* number of regions */ -int space_used;/* aggregate size of regions */ +atomic_t space_used;/* aggregate size of regions */ struct list_head busy_extents;/* busy extents in chkpt */ struct xfs_log_vec *lv_chain;/* logvecs being pushed */ struct list_head iclog_entry;Signature · 1 line
2.28.0
Re: [PATCH 36/45] xfs: implement percpu cil space used calculation
Quoted 51 lines · Darrick J. Wong
Quoted 50 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Now that we have the CIL percpu structures in place, implement the space used counter with a fast sum check similar to the
percpu_counterinfrastructure.Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 42 ++++++++++++++++++++++++++++++++++++------ fs/xfs/xfs_log_priv.h | 2 +- 2 files changed, 37 insertions(+), 7 deletions(-)fs/xfs/xfs_log_cil.c +14
-1@@ -433,6 +433,8 @@ xlog_cil_insert_items(struct xfs_log_item *lip;int len =0 ;int iovhdr_res =0 , split_res =0 , ctx_res =0 ; +int space_used; +struct xlog_cil_pcp *cilpcp;ASSERT (tp);@@ -469,8 +471,9 @@ xlog_cil_insert_items(* * This can steal more than we need, but that's OK. */ + space_used =atomic_read (&ctx->space_used);if (atomic_read (&cil->xc_iclog_hdrs) >0 || - ctx->space_used + len >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log)) { + space_used + len >=XLOG_CIL_BLOCKING_SPACE_LIMIT (log)) {int split_res = log->l_iclog_hsize +sizeof (struct xlog_op_header );if (ctx_res)@@ -480,16 +483,34 @@ xlog_cil_insert_items(atomic_sub (tp->t_ticket->t_iclog_hdrs, &cil->xc_iclog_hdrs); } +/* +* Update the CIL percpu pointer. This updates the global counter when +* over the percpu batch size or when the CIL is over the space limit. +* This means low lock overhead for normal updates, and when over the +* limit the space used is immediately accounted. This makes enforcing +* the hard limit much more accurate. The per cpu fold threshold is +* based on how close we are to the hard limit. +*/ + cilpcp =get_cpu_ptr (cil->xc_pcp); + cilpcp->space_used += len;
Quoted 8 lines
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 499
Quoted 4 lines
fs/xfs/xfs_log_cil.c +4
-0+if (space_used >=XLOG_CIL_SPACE_LIMIT (log) || + cilpcp->space_used > + ((XLOG_CIL_BLOCKING_SPACE_LIMIT (log) - space_used) / +num_online_cpus ())) {What happens if the log is very small and there are hundreds of CPUs? Can we end up on this slow path on a regular basis even if the amount of space used is not that large?
AFAICT, no big deal - the transaction reservations limit the the amount of space and concurrency that we can have here. A small log will not allow many more than a handful of transactions through at a time. IOWs, we'll already be on the transaction reservation slow path that limits concurrency via the grant head spin lock in xlog_grant_head_check() and sleeping in xlog_grant_head_wait()...
Quoted 5 lines
Granted I can't think of a good way out of that, since I suspect that if you do that you're already going to be hurting in 5 other places anyway. That said ... I /do/ keep getting bugs from people with tiny logs on big iron. Some day I'll (ha!) stomp out all the bugs that are "NO do not let your deployment system growfs 10000x, this is not ext4"...
Yeah, that hurts long before we get to this transaction commit path...
Quoted 12 lines
Quoted 12 lines
fs/xfs/xfs_log_cil.c +5
-2+atomic_add (cilpcp->space_used, &ctx->space_used); + cilpcp->space_used =0 ; + } +put_cpu_ptr (cilpcp); +spin_lock (&cil->xc_cil_lock); - tp->t_ticket->t_curr_res -= ctx_res + len; ctx->ticket->t_unit_res += ctx_res; ctx->ticket->t_curr_res += ctx_res; - ctx->space_used += len;
Quoted 7 lines
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 513
Quoted 5 lines
fs/xfs/xfs_log_cil.c +1
-0/* * If we've overrun the reservation, dump the tx details before we move * the log items. Shutdown is imminent... */ + tp->t_ticket->t_curr_res -= ctx_res + len;Is moving this really necessary?
Not really, just gets it out of the way. I moved it because it doesn't need to be inside the spinlock and in the end it needs to be associated with the underrun check. So I moved it here first so that it didn't have to keep moving every time I moved the spinlock or changed the order of the code from this point onwards....
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 37/45 xfs: track CIL ticket reservation in percpu structure
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
To get it out from under the cil spinlock.
--- fs/xfs/xfs_log_cil.c | 11 ++++++----- fs/xfs/xfs_log_priv.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-)
fs/xfs/xfs_log_cil.c +6 -5
@@ -492,6 +492,7 @@ xlog_cil_insert_items(* based on how close we are to the hard limit. */ cilpcp =get_cpu_ptr (cil->xc_pcp); + cilpcp->space_reserved += ctx_res; cilpcp->space_used += len;if (space_used >=XLOG_CIL_SPACE_LIMIT (log) || cilpcp->space_used >
@@ -502,10 +503,6 @@ xlog_cil_insert_items( }put_cpu_ptr (cilpcp); -spin_lock (&cil->xc_cil_lock); - ctx->ticket->t_unit_res += ctx_res; - ctx->ticket->t_curr_res += ctx_res; -/* * If we've overrun the reservation, dump the tx details before we move * the log items. Shutdown is imminent...
@@ -527,6 +524,7 @@ xlog_cil_insert_items(* We do this here so we only need to take the CIL lock once during * the transaction commit. */ +spin_lock (&cil->xc_cil_lock);list_for_each_entry (lip, &tp->t_items, li_trans) {/* Skip items which aren't dirty in this transaction. */
@@ -798,10 +796,13 @@ xlog_cil_push_work(down_write (&cil->xc_ctx_lock); -/* Reset the CIL pcp counters */ +/* Aggregate and reset the CIL pcp counters */ for_each_online_cpu (cpu) { cilpcp =per_cpu_ptr (cil->xc_pcp, cpu); + ctx->ticket->t_curr_res += cilpcp->space_reserved; cilpcp->space_used =0 ; + cilpcp->space_reserved =0 ; + }spin_lock (&cil->xc_push_lock);
fs/xfs/xfs_log_priv.h +1 -1
@@ -236,7 +236,7 @@ struct xfs_cil_ctx {*/ struct xlog_cil_pcp {uint32_t space_used; -uint32_t curr_res; +uint32_t space_reserved;struct list_head busy_extents;struct list_head log_items; };
Signature · 1 line
2.28.0
Re: [PATCH 37/45] xfs: track CIL ticket reservation in percpu structure
Quoted 42 lines · Dave Chinner
From: Dave Chinner <[email protected]>
To get it out from under the cil spinlock.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 11 ++++++----- fs/xfs/xfs_log_priv.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-)fs/xfs/xfs_log_cil.c +2
-4@@ -492,6 +492,7 @@ xlog_cil_insert_items(* based on how close we are to the hard limit. */ cilpcp =get_cpu_ptr (cil->xc_pcp); + cilpcp->space_reserved += ctx_res; cilpcp->space_used += len;if (space_used >=XLOG_CIL_SPACE_LIMIT (log) || cilpcp->space_used >@@ -502,10 +503,6 @@ xlog_cil_insert_items( }put_cpu_ptr (cilpcp); -spin_lock (&cil->xc_cil_lock); - ctx->ticket->t_unit_res += ctx_res; - ctx->ticket->t_curr_res += ctx_res; -/* * If we've overrun the reservation, dump the tx details before we move * the log items. Shutdown is imminent... @@ -527,6 +524,7 @@ xlog_cil_insert_items(* We do this here so we only need to take the CIL lock once during * the transaction commit. */ +spin_lock (&cil->xc_cil_lock);list_for_each_entry (lip, &tp->t_items, li_trans) {/* Skip items which aren't dirty in this transaction. */
fs/xfs/xfs_log_cil.c xlog_cil_push_work( · line 802
Quoted 8 lines
fs/xfs/xfs_log_cil.c +2
-1@@ -798,10 +796,13 @@ xlog_cil_push_work(down_write (&cil->xc_ctx_lock); -/* Reset the CIL pcp counters */ +/* Aggregate and reset the CIL pcp counters */ for_each_online_cpu (cpu) { cilpcp =per_cpu_ptr (cil->xc_pcp, cpu); + ctx->ticket->t_curr_res += cilpcp->space_reserved;
Why isn't it necessary to update ctx->ticket->t_unit_res any more?
(Admittedly I'm struggling to figure out why it matters to keep it updated even in the current code base...)
--D
Quoted 22 lines
fs/xfs/xfs_log_cil.c +2
-0cilpcp->space_used =0 ; + cilpcp->space_reserved =0 ; + }spin_lock (&cil->xc_push_lock);fs/xfs/xfs_log_priv.h +1
-1@@ -236,7 +236,7 @@ struct xfs_cil_ctx {*/ struct xlog_cil_pcp {uint32_t space_used; -uint32_t curr_res; +uint32_t space_reserved;struct list_head busy_extents;struct list_head log_items; };Signature · 1 line
2.28.0
Re: [PATCH 37/45] xfs: track CIL ticket reservation in percpu structure
Quoted 46 lines · Darrick J. Wong
Quoted 45 lines · Dave Chinner
From: Dave Chinner <[email protected]>
To get it out from under the cil spinlock.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 11 ++++++----- fs/xfs/xfs_log_priv.h | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-)fs/xfs/xfs_log_cil.c +2
-4@@ -492,6 +492,7 @@ xlog_cil_insert_items(* based on how close we are to the hard limit. */ cilpcp =get_cpu_ptr (cil->xc_pcp); + cilpcp->space_reserved += ctx_res; cilpcp->space_used += len;if (space_used >=XLOG_CIL_SPACE_LIMIT (log) || cilpcp->space_used >@@ -502,10 +503,6 @@ xlog_cil_insert_items( }put_cpu_ptr (cilpcp); -spin_lock (&cil->xc_cil_lock); - ctx->ticket->t_unit_res += ctx_res; - ctx->ticket->t_curr_res += ctx_res; -/* * If we've overrun the reservation, dump the tx details before we move * the log items. Shutdown is imminent... @@ -527,6 +524,7 @@ xlog_cil_insert_items(* We do this here so we only need to take the CIL lock once during * the transaction commit. */ +spin_lock (&cil->xc_cil_lock);list_for_each_entry (lip, &tp->t_items, li_trans) {/* Skip items which aren't dirty in this transaction. */ @@ -798,10 +796,13 @@ xlog_cil_push_work(down_write (&cil->xc_ctx_lock);
Quoted 7 lines
fs/xfs/xfs_log_cil.c xlog_cil_push_work( · line 802
Quoted 5 lines
fs/xfs/xfs_log_cil.c +2
-1-/* Reset the CIL pcp counters */ +/* Aggregate and reset the CIL pcp counters */ for_each_online_cpu (cpu) { cilpcp =per_cpu_ptr (cil->xc_pcp, cpu); + ctx->ticket->t_curr_res += cilpcp->space_reserved;Why isn't it necessary to update
ctx->ticket->t_unit_resany more?
Because t_unit_res is never used by the CIL ticket becuse they aren't permanent transaction reservations. The unit res is only for granting new space to a ticket, yet the CIL only ever "steals" granted space from an existing ticket. When
the ticket is dropped, we return unused reservations from the CIL ticket, but never touch or look at the unit reservation.
I can add it back in here if you want, but it's largely dead code...
Quoted 2 lines
(Admittedly I'm struggling to figure out why it matters to keep it updated even in the current code base...)
I think I originally did it a decade ago because I probably wasn't 100% sure on what impact not setting it would have. Getting the rest of the delayed logging code right was far more important than sweating on a tiny, largely insignificant detail like this.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 38/45 xfs: convert CIL busy extents to per-cpu
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
To get them out from under the CIL lock.
This is an unordered list, so we can simply punt it to per-cpu lists during transaction commits and reaggregate it back into a single list during the CIL push work.
--- fs/xfs/xfs_log_cil.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)
fs/xfs/xfs_log_cil.c +18 -8
@@ -501,6 +501,9 @@ xlog_cil_insert_items(atomic_add (cilpcp->space_used, &ctx->space_used); cilpcp->space_used =0 ; } +/* attach the transaction to the CIL if it has any busy extents */ +if (!list_empty (&tp->t_busy)) +list_splice_init (&tp->t_busy, &cilpcp->busy_extents);put_cpu_ptr (cilpcp);/*
@@ -540,9 +543,6 @@ xlog_cil_insert_items(list_move_tail (&lip->li_cil, &cil->xc_cil); } -/* attach the transaction to the CIL if it has any busy extents */ -if (!list_empty (&tp->t_busy)) -list_splice_init (&tp->t_busy, &ctx->busy_extents);spin_unlock (&cil->xc_cil_lock);if (tp->t_ticket->t_curr_res <0 )
@@ -802,7 +802,10 @@ xlog_cil_push_work( ctx->ticket->t_curr_res += cilpcp->space_reserved; cilpcp->space_used =0 ; cilpcp->space_reserved =0 ; - +if (!list_empty (&cilpcp->busy_extents)) { +list_splice_init (&cilpcp->busy_extents, + &ctx->busy_extents); + } }spin_lock (&cil->xc_push_lock);
@@ -1459,17 +1462,24 @@ static void __percpu *xlog_cil_pcp_alloc ( struct xfs_cil *cil) { +void __percpu *pcptr;struct xlog_cil_pcp *cilpcp; +int cpu; - cilpcp = alloc_percpu(struct xlog_cil_pcp); -if (!cilpcp) + pcptr = alloc_percpu(struct xlog_cil_pcp); +if (!pcptr)return NULL ; +for_each_possible_cpu (cpu) { + cilpcp =per_cpu_ptr (pcptr, cpu); +INIT_LIST_HEAD (&cilpcp->busy_extents); + } +if (xlog_cil_pcp_hpadd (cil) <0 ) { -free_percpu (cilpcp); +free_percpu (pcptr);return NULL ; } -return cilpcp; +return pcptr; } static void
Signature · 1 line
2.28.0
Re: [PATCH 38/45] xfs: convert CIL busy extents to per-cpu
Quoted 55 lines · Dave Chinner
From: Dave Chinner <[email protected]>
To get them out from under the CIL lock.
This is an unordered list, so we can simply punt it to per-cpu lists during transaction commits and reaggregate it back into a single list during the CIL push work.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)fs/xfs/xfs_log_cil.c +8
-4@@ -501,6 +501,9 @@ xlog_cil_insert_items(atomic_add (cilpcp->space_used, &ctx->space_used); cilpcp->space_used =0 ; } +/* attach the transaction to the CIL if it has any busy extents */ +if (!list_empty (&tp->t_busy)) +list_splice_init (&tp->t_busy, &cilpcp->busy_extents);put_cpu_ptr (cilpcp);/* @@ -540,9 +543,6 @@ xlog_cil_insert_items(list_move_tail (&lip->li_cil, &cil->xc_cil); } -/* attach the transaction to the CIL if it has any busy extents */ -if (!list_empty (&tp->t_busy)) -list_splice_init (&tp->t_busy, &ctx->busy_extents);spin_unlock (&cil->xc_cil_lock);if (tp->t_ticket->t_curr_res <0 )@@ -802,7 +802,10 @@ xlog_cil_push_work( ctx->ticket->t_curr_res += cilpcp->space_reserved; cilpcp->space_used =0 ; cilpcp->space_reserved =0 ; - +if (!list_empty (&cilpcp->busy_extents)) { +list_splice_init (&cilpcp->busy_extents, + &ctx->busy_extents); + } }spin_lock (&cil->xc_push_lock);@@ -1459,17 +1462,24 @@ static void __percpu *xlog_cil_pcp_alloc ( struct xfs_cil *cil) { +void __percpu *pcptr;struct xlog_cil_pcp *cilpcp;
fs/xfs/xfs_log_cil.c static void __percpu * · line 1474
Quoted 10 lines
fs/xfs/xfs_log_cil.c +5
-2+int cpu; - cilpcp = alloc_percpu(struct xlog_cil_pcp); -if (!cilpcp) + pcptr = alloc_percpu(struct xlog_cil_pcp); +if (!pcptr)return NULL ; +for_each_possible_cpu (cpu) { + cilpcp =per_cpu_ptr (pcptr, cpu);
So... in my mind, "cilpcp" and "pcptr" aren't really all that distinct from each other. I /think/ you're trying to use "cilpcp" everywhere else to mean "pointer to a particular CPU's CIL data", and this change makes that usage consistent in the alloc function.
However, this leaves xlog_cil_pcp_free using "cilpcp" to refer to the entire chunk of per-CPU data structures. Given that the first refers to a specific structure and the second refers to them all in aggregate, maybe _pcp_alloc and _pcp_free should use a name that at least sounds plural?
e.g.
void __percpu *all_cilpcps = alloc_percpu(...);
for_each_possible_cpu(cpu) {
cilpcp = per_cpu_ptr(all_cilpcps, cpu);
cilpcp->magicval = 7777;
}and
cil->xc_all_pcps = xlog_cil_pcp_alloc(...);
Hm?
--D
Quoted 16 lines
fs/xfs/xfs_log_cil.c +5
-2+INIT_LIST_HEAD (&cilpcp->busy_extents); + } +if (xlog_cil_pcp_hpadd (cil) <0 ) { -free_percpu (cilpcp); +free_percpu (pcptr);return NULL ; } -return cilpcp; +return pcptr; } static voidSignature · 1 line
2.28.0
Re: [PATCH 38/45] xfs: convert CIL busy extents to per-cpu
Quoted 64 lines · Darrick J. Wong
Quoted 63 lines · Dave Chinner
From: Dave Chinner <[email protected]>
To get them out from under the CIL lock.
This is an unordered list, so we can simply punt it to per-cpu lists during transaction commits and reaggregate it back into a single list during the CIL push work.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)fs/xfs/xfs_log_cil.c +11
-6@@ -501,6 +501,9 @@ xlog_cil_insert_items(atomic_add (cilpcp->space_used, &ctx->space_used); cilpcp->space_used =0 ; } +/* attach the transaction to the CIL if it has any busy extents */ +if (!list_empty (&tp->t_busy)) +list_splice_init (&tp->t_busy, &cilpcp->busy_extents);put_cpu_ptr (cilpcp);/* @@ -540,9 +543,6 @@ xlog_cil_insert_items(list_move_tail (&lip->li_cil, &cil->xc_cil); } -/* attach the transaction to the CIL if it has any busy extents */ -if (!list_empty (&tp->t_busy)) -list_splice_init (&tp->t_busy, &ctx->busy_extents);spin_unlock (&cil->xc_cil_lock);if (tp->t_ticket->t_curr_res <0 )@@ -802,7 +802,10 @@ xlog_cil_push_work( ctx->ticket->t_curr_res += cilpcp->space_reserved; cilpcp->space_used =0 ; cilpcp->space_reserved =0 ; - +if (!list_empty (&cilpcp->busy_extents)) { +list_splice_init (&cilpcp->busy_extents, + &ctx->busy_extents); + } }spin_lock (&cil->xc_push_lock);@@ -1459,17 +1462,24 @@ static void __percpu *xlog_cil_pcp_alloc ( struct xfs_cil *cil) { +void __percpu *pcptr;struct xlog_cil_pcp *cilpcp; +int cpu; - cilpcp = alloc_percpu(struct xlog_cil_pcp); -if (!cilpcp) + pcptr = alloc_percpu(struct xlog_cil_pcp); +if (!pcptr)return NULL ;
Quoted 7 lines
fs/xfs/xfs_log_cil.c static void __percpu * · line 1474
Quoted 2 lines
fs/xfs/xfs_log_cil.c +2
-0+for_each_possible_cpu (cpu) { + cilpcp =per_cpu_ptr (pcptr, cpu);So... in my mind, "cilpcp" and "pcptr" aren't really all that distinct from each other. I /think/ you're trying to use "cilpcp" everywhere else to mean "pointer to a particular CPU's CIL data", and this change makes that usage consistent in the alloc function.
Yeah, it's had to have short, concise, distinct names here because the generic pointer returned is a pointer to per cpu memory that contains CIL specific per-cpu structures...
Quoted 2 lines
However, this leaves
xlog_cil_pcp_freeusing "cilpcp" to refer to the entire chunk of per-CPU data structures.
I'll fix that, I obviously missed that when trying to clean this up to be consistent...
Quoted 5 lines
Given that the first refers to
a specific structure and the second refers to them all in aggregate, maybe_pcp_allocand_pcp_freeshould use a name that at least sounds plural?
Quoted 8 lines
e.g.
void __percpu *all_cilpcps = alloc_percpu(...); for_each_possible_cpu(cpu) { cilpcp = per_cpu_ptr(all_cilpcps, cpu); cilpcp->magicval = 7777; }
The problem with "all" is that it implies a "global" all, not something that is owned by this specific CIL instance. i.e. there will be a per-cpu CIL area for every filesystem that is mounted, and they are actually all linked together into a global list for CPU hotplug to walk. So "all" CIL pcps to me means walking this list:
static LIST_HEAD(xlog_cil_pcp_list);
static DEFINE_SPINLOCK(xlog_cil_pcp_lock);
which is linked by the cil->xc_pcp_list list heads in each CIL instance so that CPU hotplug can do the right thing.
"pcp" is typical shorthand for a "per cpu pointer" but it's horrible when we have pointers to per-cpu lists, lists of per-cpu aware structures, pointers to per-cpu regions, pointers to per-cpu data (structures) within per-cpu regions, etc.
There is no way to win here, it's going to be confusing whatever we do. I've tried to keep it simple:
pcptr - generic pointer to allocated per CPU regioncil->xc_pcp - CIL instance pointer to allocated percpu regioncil->xc_pcp_list - global list for CPU hotplug pcp management
cilpcp - pointer to specfic CPU instance of the CIL
percpu data inside cil->xc_pcp region.
I might just change the generic (void percpu *) regions to "pcp" so that they align with all the other uses of "pcp" in the naming.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 39/45 xfs: Add order IDs to log items in CIL
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Before we split the ordered CIL up into per cpu lists, we need a mechanism to track the order of the items in the CIL. We need to do this because there are rules around the order in which related items must physically appear in the log even inside a single checkpoint transaction.
An example of this is intents - an intent must appear in the log before it's intent done record so taht log recovery can cancel the intent correctly. If we have these two records misordered in the CIL, then they will not be recovered correctly by journal replay.
We also will not be able to move items to the tail of the CIL list when they are relogged, hence the log items will need some mechanism to allow the correct log item order to be recreated before we write log items to the hournal.
Hence we need to have a mechanism for recording global order of transactions in the log items so that we can recover that order from un-ordered per-cpu lists.
Do this with a simple monotonic increasing commit counter in the CIL context. Each log item in the transaction gets stamped with the current commit order ID before it is added to the CIL. If the item is already in the CIL, leave it where it is instead of moving it to the tail of the list and instead sort the list before we start the push work.
XXX: list_sort() under the cil_ctx_lock held exclusive starts hurting that >16 threads. Front end commits are waiting on the push to switch contexts much longer. The item order id should likely be moved into the logvecs when they are detacted from the items, then the sort can be done on the logvec after the cil_ctx_lock has been released. logvecs will need to use a list_head for this rather than a single linked list like they do now....
--- fs/xfs/xfs_log_cil.c | 34 ++++++++++++++++++++++++++-------- fs/xfs/xfs_log_priv.h | 1 + fs/xfs/xfs_trans.h | 1 + 3 files changed, 28 insertions(+), 8 deletions(-)
fs/xfs/xfs_log_cil.c +26 -8
@@ -434,6 +434,7 @@ xlog_cil_insert_items(int len =0 ;int iovhdr_res =0 , split_res =0 , ctx_res =0 ;int space_used; +int order;struct xlog_cil_pcp *cilpcp;ASSERT (tp);
@@ -523,10 +524,12 @@ xlog_cil_insert_items( }/* -* Now (re-)position everything modified at the tail of the CIL. +* Now update the order of everything modified in the transaction +* and insert items into the CIL if they aren't already there. * We do this here so we only need to take the CIL lock once during * the transaction commit. */ + order =atomic_inc_return (&ctx->order_id);spin_lock (&cil->xc_cil_lock);list_for_each_entry (lip, &tp->t_items, li_trans) {
@@ -534,13 +537,10 @@ xlog_cil_insert_items(if (!test_bit (XFS_LI_DIRTY , &lip->li_flags))continue ; -/* -* Only move the item if it isn't already at the tail. This is -* to prevent a transient list_empty() state when reinserting -* an item that is already the only item in the CIL. -*/ -if (!list_is_last (&lip->li_cil, &cil->xc_cil)) -list_move_tail (&lip->li_cil, &cil->xc_cil); + lip->li_order_id = order; +if (!list_empty (&lip->li_cil)) +continue ; +list_add (&lip->li_cil, &cil->xc_cil); }spin_unlock (&cil->xc_cil_lock);
@@ -753,6 +753,22 @@ xlog_cil_build_trans_hdr( tic->t_curr_res -= lvhdr->lv_bytes; } +static int +xlog_cil_order_cmp ( +void *priv, +struct list_head *a, +struct list_head *b) +{ + struct xfs_log_item *l1 = container_of(a, struct xfs_log_item, li_cil); + struct xfs_log_item *l2 = container_of(b, struct xfs_log_item, li_cil); + +if (l1->li_order_id > l2->li_order_id) +return 1 ; +if (l1->li_order_id < l2->li_order_id) +return -1 ; +return 0 ; +} +/* * Push the Committed Item List to the log. *
@@ -891,6 +907,7 @@ xlog_cil_push_work(* needed on the transaction commit side which is currently locked out * by the flush lock. */ +list_sort (NULL , &cil->xc_cil, xlog_cil_order_cmp); lv =NULL ;while (!list_empty (&cil->xc_cil)) {struct xfs_log_item *item;
@@ -898,6 +915,7 @@ xlog_cil_push_work( item =list_first_entry (&cil->xc_cil, struct xfs_log_item, li_cil);list_del_init (&item->li_cil); + item->li_order_id =0 ;if (!ctx->lv_chain) ctx->lv_chain = item->li_lv;else
fs/xfs/xfs_log_priv.h +1 -0
@@ -229,6 +229,7 @@ struct xfs_cil_ctx {struct list_head committing;/* ctx committing list */ struct work_struct discard_endio_work;struct work_struct push_work; +atomic_t order_id; };/*
fs/xfs/xfs_trans.h +1 -0
@@ -44,6 +44,7 @@ struct xfs_log_item {struct xfs_log_vec *li_lv;/* active log vector */ struct xfs_log_vec *li_lv_shadow;/* standby vector */ xfs_csn_t li_seq;/* CIL commit seq */ +uint32_t li_order_id;/* CIL commit order */ };/*
Signature · 1 line
2.28.0
Re: [PATCH 39/45] xfs: Add order IDs to log items in CIL
Quoted 75 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Before we split the ordered CIL up into per cpu lists, we need a mechanism to track the order of the items in the CIL. We need to do this because there are rules around the order in which related items must physically appear in the log even inside a single checkpoint transaction.
An example of this is intents - an intent must appear in the log before it's intent done record so taht log recovery can cancel the intent correctly. If we have these two records misordered in the CIL, then they will not be recovered correctly by journal replay.
We also will not be able to move items to the tail of the CIL list when they are relogged, hence the log items will need some mechanism to allow the correct log item order to be recreated before we write log items to the hournal.
Hence we need to have a mechanism for recording global order of transactions in the log items so that we can recover that order from un-ordered per-cpu lists.
Do this with a simple monotonic increasing commit counter in the CIL context. Each log item in the transaction gets stamped with the current commit order ID before it is added to the CIL. If the item is already in the CIL, leave it where it is instead of moving it to the tail of the list and instead sort the list before we start the push work.
XXX:
list_sort()under thecil_ctx_lockheld exclusive starts hurting that >16 threads. Front end commits are waiting on the push to switch contexts much longer. The item order id should likely be moved into the logvecs when they are detacted from the items, then the sort can be done on the logvec after thecil_ctx_lockhas been released. logvecs will need to use alist_headfor this rather than a single linked list like they do now....Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 34 ++++++++++++++++++++++++++-------- fs/xfs/xfs_log_priv.h | 1 + fs/xfs/xfs_trans.h | 1 + 3 files changed, 28 insertions(+), 8 deletions(-)fs/xfs/xfs_log_cil.c +4
-2@@ -434,6 +434,7 @@ xlog_cil_insert_items(int len =0 ;int iovhdr_res =0 , split_res =0 , ctx_res =0 ;int space_used; +int order;struct xlog_cil_pcp *cilpcp;ASSERT (tp);@@ -523,10 +524,12 @@ xlog_cil_insert_items( }/* -* Now (re-)position everything modified at the tail of the CIL. +* Now update the order of everything modified in the transaction +* and insert items into the CIL if they aren't already there. * We do this here so we only need to take the CIL lock once during * the transaction commit. */ + order =atomic_inc_return (&ctx->order_id);spin_lock (&cil->xc_cil_lock);list_for_each_entry (lip, &tp->t_items, li_trans) {@@ -534,13 +537,10 @@ xlog_cil_insert_items(if (!test_bit (XFS_LI_DIRTY , &lip->li_flags))continue ; -/*
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 543
Quoted 10 lines
fs/xfs/xfs_log_cil.c +4
-6-* Only move the item if it isn't already at the tail. This is -* to prevent a transient list_empty() state when reinserting -* an item that is already the only item in the CIL. -*/ -if (!list_is_last (&lip->li_cil, &cil->xc_cil)) -list_move_tail (&lip->li_cil, &cil->xc_cil); + lip->li_order_id = order; +if (!list_empty (&lip->li_cil)) +continue ; +list_add (&lip->li_cil, &cil->xc_cil);
If the goal here is to end up an xc_cil list where all the log items are sorted in commit order, why isn't the existing strategy of moving dirty items to the tail sufficient to keep them in sorted order?
Hm, looking at the /next/ patch, I see you start adding the items to the per-CPU CIL structure and only combining them into a single list at push time. Maybe that's a better place to talk about this.
--D
Quoted 69 lines
fs/xfs/xfs_log_cil.c +18
-0}spin_unlock (&cil->xc_cil_lock);@@ -753,6 +753,22 @@ xlog_cil_build_trans_hdr( tic->t_curr_res -= lvhdr->lv_bytes; } +static int +xlog_cil_order_cmp ( +void *priv, +struct list_head *a, +struct list_head *b) +{ + struct xfs_log_item *l1 = container_of(a, struct xfs_log_item, li_cil); + struct xfs_log_item *l2 = container_of(b, struct xfs_log_item, li_cil); + +if (l1->li_order_id > l2->li_order_id) +return 1 ; +if (l1->li_order_id < l2->li_order_id) +return -1 ; +return 0 ; +} +/* * Push the Committed Item List to the log. * @@ -891,6 +907,7 @@ xlog_cil_push_work(* needed on the transaction commit side which is currently locked out * by the flush lock. */ +list_sort (NULL , &cil->xc_cil, xlog_cil_order_cmp); lv =NULL ;while (!list_empty (&cil->xc_cil)) {struct xfs_log_item *item;@@ -898,6 +915,7 @@ xlog_cil_push_work( item =list_first_entry (&cil->xc_cil, struct xfs_log_item, li_cil);list_del_init (&item->li_cil); + item->li_order_id =0 ;if (!ctx->lv_chain) ctx->lv_chain = item->li_lv;else fs/xfs/xfs_log_priv.h +1
-0@@ -229,6 +229,7 @@ struct xfs_cil_ctx {struct list_head committing;/* ctx committing list */ struct work_struct discard_endio_work;struct work_struct push_work; +atomic_t order_id; };/* fs/xfs/xfs_trans.h +1
-0@@ -44,6 +44,7 @@ struct xfs_log_item {struct xfs_log_vec *li_lv;/* active log vector */ struct xfs_log_vec *li_lv_shadow;/* standby vector */ xfs_csn_t li_seq;/* CIL commit seq */ +uint32_t li_order_id;/* CIL commit order */ };/* Signature · 1 line
2.28.0
PATCH v3 40/45 xfs: convert CIL to unordered per cpu lists
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
So that we can remove the cil_lock which is a global serialisation point. We've already got ordering sorted, so all we need to do is treat the CIL list like the busy extent list and reconstruct it before the push starts.
This is what we're trying to avoid:
- 75.35% 1.83% [kernel] [k] xfs_log_commit_cil
- 46.35% xfs_log_commit_cil
- 41.54% _raw_spin_lock
- 67.30% do_raw_spin_lock
66.96% __pv_queued_spin_lock_slowpath
Which happens on a 32p system when running a 32-way 'rm -rf' workload. After this patch:
- 20.90% 3.23% [kernel] [k] xfs_log_commit_cil
- 17.67% xfs_log_commit_cil
- 6.51% xfs_log_ticket_ungrant
1.40% xfs_log_space_wake
2.32% memcpy_erms
- 2.18% xfs_buf_item_committing
- 2.12% xfs_buf_item_release
- 1.03% xfs_buf_unlock
0.96% up
0.72% xfs_buf_rele
1.33% xfs_inode_item_format
1.19% down_read
0.91% up_read
0.76% xfs_buf_item_format
- 0.68% kmem_alloc_large
- 0.67% kmem_alloc
0.64% __kmalloc
0.50% xfs_buf_item_sizeIt kinda looks like the workload is running out of log space all the time. But all the spinlock contention is gone and the transaction commit rate has gone from 800k/s to 1.3M/s so the amount of real work being done has gone up a *lot*.
--- fs/xfs/xfs_log_cil.c | 61 ++++++++++++++++++++----------------------- fs/xfs/xfs_log_priv.h | 2 -- 2 files changed, 29 insertions(+), 34 deletions(-)
fs/xfs/xfs_log_cil.c +29 -32
@@ -448,10 +448,9 @@ xlog_cil_insert_items(/* * We need to take the CIL checkpoint unit reservation on the first * commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't -* unnecessarily do an atomic op in the fast path here. We don't need to -* hold the xc_cil_lock here to clear the XLOG_CIL_EMPTY bit as we are -* under the xc_ctx_lock here and that needs to be held exclusively to -* reset the XLOG_CIL_EMPTY bit. +* unnecessarily do an atomic op in the fast path here. We can clear the +* XLOG_CIL_EMPTY bit as we are under the xc_ctx_lock here and that +* needs to be held exclusively to reset the XLOG_CIL_EMPTY bit. */ if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags) &&test_and_clear_bit (XLOG_CIL_EMPTY , &cil->xc_flags))
@@ -505,24 +504,6 @@ xlog_cil_insert_items(/* attach the transaction to the CIL if it has any busy extents */ if (!list_empty (&tp->t_busy))list_splice_init (&tp->t_busy, &cilpcp->busy_extents); -put_cpu_ptr (cilpcp); - -/* -* If we've overrun the reservation, dump the tx details before we move -* the log items. Shutdown is imminent... -*/ - tp->t_ticket->t_curr_res -= ctx_res + len; -if (WARN_ON (tp->t_ticket->t_curr_res <0 )) { -xfs_warn (log->l_mp,"Transaction log reservation overrun:" ); -xfs_warn (log->l_mp, -" log items: %d bytes (iov hdrs: %d bytes)" , - len, iovhdr_res); -xfs_warn (log->l_mp," split region headers: %d bytes" , - split_res); -xfs_warn (log->l_mp," ctx ticket: %d bytes" , ctx_res); -xlog_print_trans (tp); - } -/* * Now update the order of everything modified in the transaction * and insert items into the CIL if they aren't already there.
@@ -530,7 +511,6 @@ xlog_cil_insert_items(* the transaction commit. */ order =atomic_inc_return (&ctx->order_id); -spin_lock (&cil->xc_cil_lock);list_for_each_entry (lip, &tp->t_items, li_trans) {/* Skip items which aren't dirty in this transaction. */
@@ -540,10 +520,26 @@ xlog_cil_insert_items( lip->li_order_id = order;if (!list_empty (&lip->li_cil))continue ; -list_add (&lip->li_cil, &cil->xc_cil); +list_add (&lip->li_cil, &cilpcp->log_items); + } +put_cpu_ptr (cilpcp); + +/* +* If we've overrun the reservation, dump the tx details before we move +* the log items. Shutdown is imminent... +*/ + tp->t_ticket->t_curr_res -= ctx_res + len; +if (WARN_ON (tp->t_ticket->t_curr_res <0 )) { +xfs_warn (log->l_mp,"Transaction log reservation overrun:" ); +xfs_warn (log->l_mp, +" log items: %d bytes (iov hdrs: %d bytes)" , + len, iovhdr_res); +xfs_warn (log->l_mp," split region headers: %d bytes" , + split_res); +xfs_warn (log->l_mp," ctx ticket: %d bytes" , ctx_res); +xlog_print_trans (tp); } -spin_unlock (&cil->xc_cil_lock);if (tp->t_ticket->t_curr_res <0 )xfs_force_shutdown (log->l_mp,SHUTDOWN_LOG_IO_ERROR );
@@ -806,6 +802,7 @@ xlog_cil_push_work(bool commit_iclog_sync = false;int cpu;struct xlog_cil_pcp *cilpcp; +LIST_HEAD (log_items); new_ctx =xlog_cil_ctx_alloc (); new_ctx->ticket =xlog_cil_ticket_alloc (log);
@@ -822,6 +819,9 @@ xlog_cil_push_work(list_splice_init (&cilpcp->busy_extents, &ctx->busy_extents); } +if (!list_empty (&cilpcp->log_items)) { +list_splice_init (&cilpcp->log_items, &log_items); + } }spin_lock (&cil->xc_push_lock);
@@ -907,12 +907,12 @@ xlog_cil_push_work(* needed on the transaction commit side which is currently locked out * by the flush lock. */ -list_sort (NULL , &cil->xc_cil, xlog_cil_order_cmp); +list_sort (NULL , &log_items, xlog_cil_order_cmp); lv =NULL ; -while (!list_empty (&cil->xc_cil)) { +while (!list_empty (&log_items)) {struct xfs_log_item *item; - item =list_first_entry (&cil->xc_cil, + item =list_first_entry (&log_items, struct xfs_log_item, li_cil);list_del_init (&item->li_cil); item->li_order_id =0 ;
@@ -1099,7 +1099,6 @@ xlog_cil_push_background(* The cil won't be empty because we are called while holding the * context lock so whatever we added to the CIL will still be there. */ -ASSERT (!list_empty (&cil->xc_cil));ASSERT (!test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));/*
@@ -1491,6 +1490,7 @@ xlog_cil_pcp_alloc(for_each_possible_cpu (cpu) { cilpcp =per_cpu_ptr (pcptr, cpu);INIT_LIST_HEAD (&cilpcp->busy_extents); +INIT_LIST_HEAD (&cilpcp->log_items); }if (xlog_cil_pcp_hpadd (cil) <0 ) {
@@ -1531,9 +1531,7 @@ xlog_cil_init(return -ENOMEM ; } -INIT_LIST_HEAD (&cil->xc_cil);INIT_LIST_HEAD (&cil->xc_committing); -spin_lock_init (&cil->xc_cil_lock);spin_lock_init (&cil->xc_push_lock);init_waitqueue_head (&cil->xc_push_wait);init_rwsem (&cil->xc_ctx_lock);
@@ -1559,7 +1557,6 @@ xlog_cil_destroy(kmem_free (cil->xc_ctx); } -ASSERT (list_empty (&cil->xc_cil));ASSERT (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));xlog_cil_pcp_free (cil, cil->xc_pcp);kmem_free (cil);
fs/xfs/xfs_log_priv.h +0 -2
@@ -262,8 +262,6 @@ struct xfs_cil {struct xlog *xc_log;unsigned long xc_flags;atomic_t xc_iclog_hdrs; -struct list_head xc_cil; -spinlock_t xc_cil_lock;struct rw_semaphore xc_ctx_lock____cacheline_aligned_in_smp ;struct xfs_cil_ctx *xc_ctx;
Signature · 1 line
2.28.0
Re: [PATCH 40/45] xfs: convert CIL to unordered per cpu lists
Quoted 100 lines · Dave Chinner
From: Dave Chinner <[email protected]>
So that we can remove the
cil_lockwhich is a global serialisation point. We've already got ordering sorted, so all we need to do is treat the CIL list like the busy extent list and reconstruct it before the push starts.This is what we're trying to avoid:
- 75.35% 1.83% [kernel] [k]
xfs_log_commit_cil
- 46.35%xfs_log_commit_cil
- 41.54%_raw_spin_lock
- 67.30%do_raw_spin_lock
66.96%__pv_queued_spin_lock_slowpathWhich happens on a 32p system when running a 32-way 'rm -rf' workload. After this patch:
- 20.90% 3.23% [kernel] [k]
xfs_log_commit_cil
- 17.67%xfs_log_commit_cil- 6.51% xfs_log_ticket_ungrant 1.40% xfs_log_space_wake 2.32% memcpy_erms - 2.18% xfs_buf_item_committing - 2.12% xfs_buf_item_release - 1.03% xfs_buf_unlock 0.96% up 0.72% xfs_buf_rele 1.33% xfs_inode_item_format 1.19% down_read 0.91% up_read 0.76% xfs_buf_item_format - 0.68% kmem_alloc_large - 0.67% kmem_alloc 0.64% __kmalloc 0.50% xfs_buf_item_sizeIt kinda looks like the workload is running out of log space all the time. But all the spinlock contention is gone and the transaction commit rate has gone from 800k/s to 1.3M/s so the amount of real work being done has gone up a *lot*.
Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 61 ++++++++++++++++++++----------------------- fs/xfs/xfs_log_priv.h | 2 -- 2 files changed, 29 insertions(+), 34 deletions(-)fs/xfs/xfs_log_cil.c +3
-23@@ -448,10 +448,9 @@ xlog_cil_insert_items(/* * We need to take the CIL checkpoint unit reservation on the first * commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't -* unnecessarily do an atomic op in the fast path here. We don't need to -* hold the xc_cil_lock here to clear the XLOG_CIL_EMPTY bit as we are -* under the xc_ctx_lock here and that needs to be held exclusively to -* reset the XLOG_CIL_EMPTY bit. +* unnecessarily do an atomic op in the fast path here. We can clear the +* XLOG_CIL_EMPTY bit as we are under the xc_ctx_lock here and that +* needs to be held exclusively to reset the XLOG_CIL_EMPTY bit. */ if (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags) &&test_and_clear_bit (XLOG_CIL_EMPTY , &cil->xc_flags))@@ -505,24 +504,6 @@ xlog_cil_insert_items(/* attach the transaction to the CIL if it has any busy extents */ if (!list_empty (&tp->t_busy))list_splice_init (&tp->t_busy, &cilpcp->busy_extents); -put_cpu_ptr (cilpcp); - -/* -* If we've overrun the reservation, dump the tx details before we move -* the log items. Shutdown is imminent... -*/ - tp->t_ticket->t_curr_res -= ctx_res + len; -if (WARN_ON (tp->t_ticket->t_curr_res <0 )) { -xfs_warn (log->l_mp,"Transaction log reservation overrun:" ); -xfs_warn (log->l_mp, -" log items: %d bytes (iov hdrs: %d bytes)" , - len, iovhdr_res); -xfs_warn (log->l_mp," split region headers: %d bytes" , - split_res); -xfs_warn (log->l_mp," ctx ticket: %d bytes" , ctx_res); -xlog_print_trans (tp); - } -/* * Now update the order of everything modified in the transaction * and insert items into the CIL if they aren't already there. @@ -530,7 +511,6 @@ xlog_cil_insert_items(* the transaction commit. */ order =atomic_inc_return (&ctx->order_id); -spin_lock (&cil->xc_cil_lock);list_for_each_entry (lip, &tp->t_items, li_trans) {/* Skip items which aren't dirty in this transaction. */
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 523
Quoted 5 lines
fs/xfs/xfs_log_cil.c +1
-1@@ -540,10 +520,26 @@ xlog_cil_insert_items( lip->li_order_id = order;if (!list_empty (&lip->li_cil))continue ; -list_add (&lip->li_cil, &cil->xc_cil); +list_add (&lip->li_cil, &cilpcp->log_items);
Ok, so if I understand this correctly -- every time a transaction commits, it marks every dirty log item with a monotonically increasing counter. If the log item isn't already on another CPU's CIL list, it gets added to the current CPU's CIL list...
Quoted 32 lines
fs/xfs/xfs_log_cil.c +18
-1+ } +put_cpu_ptr (cilpcp); + +/* +* If we've overrun the reservation, dump the tx details before we move +* the log items. Shutdown is imminent... +*/ + tp->t_ticket->t_curr_res -= ctx_res + len; +if (WARN_ON (tp->t_ticket->t_curr_res <0 )) { +xfs_warn (log->l_mp,"Transaction log reservation overrun:" ); +xfs_warn (log->l_mp, +" log items: %d bytes (iov hdrs: %d bytes)" , + len, iovhdr_res); +xfs_warn (log->l_mp," split region headers: %d bytes" , + split_res); +xfs_warn (log->l_mp," ctx ticket: %d bytes" , ctx_res); +xlog_print_trans (tp); } -spin_unlock (&cil->xc_cil_lock);if (tp->t_ticket->t_curr_res <0 )xfs_force_shutdown (log->l_mp,SHUTDOWN_LOG_IO_ERROR );@@ -806,6 +802,7 @@ xlog_cil_push_work(bool commit_iclog_sync = false;int cpu;struct xlog_cil_pcp *cilpcp; +LIST_HEAD (log_items); new_ctx =xlog_cil_ctx_alloc (); new_ctx->ticket =xlog_cil_ticket_alloc (log);
fs/xfs/xfs_log_cil.c xlog_cil_push_work( · line 823
Quoted 5 lines
fs/xfs/xfs_log_cil.c +2
-0@@ -822,6 +819,9 @@ xlog_cil_push_work(list_splice_init (&cilpcp->busy_extents, &ctx->busy_extents); } +if (!list_empty (&cilpcp->log_items)) { +list_splice_init (&cilpcp->log_items, &log_items);
...and then at CIL push time, we splice each per-CPU list into a big list, sort the dirty log items by counter number, and process them.
The first thought I had was that it's a darn shame that _insert_items can't steal a log item from another CPU's CIL list, because you could then mergesort the per-CPU CIL lists into @log_items. Unfortunately, I don't think there's a safe way to steal items from a per-CPU list without involving locks.
The second thought I had was that we have the xfs_pwork mechanism for launching a bunch of worker threads. A pwork workqueue is (probably) too costly when the item list is short or there aren't that many CPUs, but once list_sort starts getting painful, would it be faster to launch a bunch of threads in push_work to sort each per-CPU list and then merge sort them into the final list?
FWIW at least mechanically, the last two patches look reasonable to me.
--D
Quoted 70 lines
fs/xfs/xfs_log_cil.c +5
-7+ } }spin_lock (&cil->xc_push_lock);@@ -907,12 +907,12 @@ xlog_cil_push_work(* needed on the transaction commit side which is currently locked out * by the flush lock. */ -list_sort (NULL , &cil->xc_cil, xlog_cil_order_cmp); +list_sort (NULL , &log_items, xlog_cil_order_cmp); lv =NULL ; -while (!list_empty (&cil->xc_cil)) { +while (!list_empty (&log_items)) {struct xfs_log_item *item; - item =list_first_entry (&cil->xc_cil, + item =list_first_entry (&log_items, struct xfs_log_item, li_cil);list_del_init (&item->li_cil); item->li_order_id =0 ;@@ -1099,7 +1099,6 @@ xlog_cil_push_background(* The cil won't be empty because we are called while holding the * context lock so whatever we added to the CIL will still be there. */ -ASSERT (!list_empty (&cil->xc_cil));ASSERT (!test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));/* @@ -1491,6 +1490,7 @@ xlog_cil_pcp_alloc(for_each_possible_cpu (cpu) { cilpcp =per_cpu_ptr (pcptr, cpu);INIT_LIST_HEAD (&cilpcp->busy_extents); +INIT_LIST_HEAD (&cilpcp->log_items); }if (xlog_cil_pcp_hpadd (cil) <0 ) {@@ -1531,9 +1531,7 @@ xlog_cil_init(return -ENOMEM ; } -INIT_LIST_HEAD (&cil->xc_cil);INIT_LIST_HEAD (&cil->xc_committing); -spin_lock_init (&cil->xc_cil_lock);spin_lock_init (&cil->xc_push_lock);init_waitqueue_head (&cil->xc_push_wait);init_rwsem (&cil->xc_ctx_lock);@@ -1559,7 +1557,6 @@ xlog_cil_destroy(kmem_free (cil->xc_ctx); } -ASSERT (list_empty (&cil->xc_cil));ASSERT (test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));xlog_cil_pcp_free (cil, cil->xc_pcp);kmem_free (cil);fs/xfs/xfs_log_priv.h +0
-2@@ -262,8 +262,6 @@ struct xfs_cil {struct xlog *xc_log;unsigned long xc_flags;atomic_t xc_iclog_hdrs; -struct list_head xc_cil; -spinlock_t xc_cil_lock;struct rw_semaphore xc_ctx_lock____cacheline_aligned_in_smp ;struct xfs_cil_ctx *xc_ctx;Signature · 1 line
2.28.0
Re: [PATCH 40/45] xfs: convert CIL to unordered per cpu lists
Quoted 7 lines · Darrick J. Wong
Quoted 6 lines · Dave Chinner
From: Dave Chinner <[email protected]>
So that we can remove the
cil_lockwhich is a global serialisation point. We've already got ordering sorted, so all we need to do is treat the CIL list like the busy extent list and reconstruct it before the push starts.
....
Quoted 7 lines
Quoted 7 lines
fs/xfs/xfs_log_cil.c +0
-1@@ -530,7 +511,6 @@ xlog_cil_insert_items(* the transaction commit. */ order =atomic_inc_return (&ctx->order_id); -spin_lock (&cil->xc_cil_lock);list_for_each_entry (lip, &tp->t_items, li_trans) {
Quoted 12 lines
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 523
Quoted 7 lines
fs/xfs/xfs_log_cil.c +1
-1/* Skip items which aren't dirty in this transaction. */ @@ -540,10 +520,26 @@ xlog_cil_insert_items( lip->li_order_id = order;if (!list_empty (&lip->li_cil))continue ; -list_add (&lip->li_cil, &cil->xc_cil); +list_add (&lip->li_cil, &cilpcp->log_items);Ok, so if I understand this correctly -- every time a transaction commits, it marks every dirty log item with a monotonically increasing counter. If the log item isn't already on another CPU's CIL list, it gets added to the current CPU's CIL list...
Correct.
Quoted 29 lines
Quoted 29 lines
fs/xfs/xfs_log_cil.c +18
-1+ } +put_cpu_ptr (cilpcp); + +/* +* If we've overrun the reservation, dump the tx details before we move +* the log items. Shutdown is imminent... +*/ + tp->t_ticket->t_curr_res -= ctx_res + len; +if (WARN_ON (tp->t_ticket->t_curr_res <0 )) { +xfs_warn (log->l_mp,"Transaction log reservation overrun:" ); +xfs_warn (log->l_mp, +" log items: %d bytes (iov hdrs: %d bytes)" , + len, iovhdr_res); +xfs_warn (log->l_mp," split region headers: %d bytes" , + split_res); +xfs_warn (log->l_mp," ctx ticket: %d bytes" , ctx_res); +xlog_print_trans (tp); } -spin_unlock (&cil->xc_cil_lock);if (tp->t_ticket->t_curr_res <0 )xfs_force_shutdown (log->l_mp,SHUTDOWN_LOG_IO_ERROR );@@ -806,6 +802,7 @@ xlog_cil_push_work(bool commit_iclog_sync = false;int cpu;struct xlog_cil_pcp *cilpcp; +LIST_HEAD (log_items);
Quoted 11 lines
fs/xfs/xfs_log_cil.c xlog_cil_push_work( · line 823
Quoted 8 lines
fs/xfs/xfs_log_cil.c +2
-0new_ctx =xlog_cil_ctx_alloc (); new_ctx->ticket =xlog_cil_ticket_alloc (log);@@ -822,6 +819,9 @@ xlog_cil_push_work(list_splice_init (&cilpcp->busy_extents, &ctx->busy_extents); } +if (!list_empty (&cilpcp->log_items)) { +list_splice_init (&cilpcp->log_items, &log_items);...and then at CIL push time, we splice each per-CPU list into a big list, sort the dirty log items by counter number, and process them.
Yup, that's pretty much it. I'm replacing insert time ordering with push-time ordering to get rid of the serialisation overhead of insert time ordering.
Quoted 5 lines
The first thought I had was that it's a darn shame that
_insert_itemscan't steal a log item from another CPU's CIL list, because you could then mergesort the per-CPU CIL lists into @log_items. Unfortunately, I don't think there's a safe way to steal items from a per-CPU list without involving locks.
Yeah, it needs locks because we then have to serialise local inserts with remote removals. It can be done fairly easily - I just need to replace the "order ID" field with the CPU ID of the list it is on.
The problem is that relogging happens a lot, so in some workloads we might be bouncing a set of commonly accessed log items around CPUs frequently. That said, I'm not sure this would end up a huge problem, but it still needs a mergesort to be performed in the push code...
Quoted 6 lines
The second thought I had was that we have the
xfs_pworkmechanism for launching a bunch of worker threads. A pwork workqueue is (probably) too costly when the item list is short or there aren't that many CPUs, but oncelist_sortstarts getting painful, would it be faster to launch a bunch of threads inpush_workto sort each per-CPU list and then merge sort them into the final list?
Not sure, because now you have N work threads competing with the userspace workload for CPU to do maybe 10ms of work. The scheduling latency when the system is CPU bound is likely to introduce more latency than you save by spreading the work out....
I've largely put these sorts of questions aside because optimising this code further can be done later. The code as it stands doubles the throughput of the commit path and I don't think that further optimisation is immediately necessary. Ensuring that the splitting and recombining of the lists still results in correctly ordered log items is more important right now, and I think it does that.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 41/45 xfs: move CIL ordering to the logvec chain
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Adding a list_sort() call to the CIL push work while the xc_ctx_lock is held exclusively has resulted in fairly long lock hold times and that stops all front end transaction commits from making progress.
We can move the sorting out of the xc_ctx_lock if we can transfer the ordering information to the log vectors as they are detached from the log items and then we can sort the log vectors. This requires log vectors to use a list_head rather than a single linked list and to hold an order ID field. With these changes, we can move the list_sort() call to just before we call xlog_write() when we aren't holding any locks at all.
--- fs/xfs/xfs_log.c | 46 +++++++++++++++++++++--------- fs/xfs/xfs_log.h | 3 +- fs/xfs/xfs_log_cil.c | 63 +++++++++++++++++++++++++---------------- fs/xfs/xfs_log_priv.h | 4 +-- fs/xfs/xfs_trans.c | 4 +-- fs/xfs/xfs_trans_priv.h | 4 +-- 6 files changed, 78 insertions(+), 46 deletions(-)
fs/xfs/xfs_log.c +32 -14
@@ -846,6 +846,9 @@ xlog_write_unmount_record( .lv_niovecs = 1, .lv_iovecp = ®, }; +LIST_HEAD (lv_chain); +INIT_LIST_HEAD (&vec.lv_chain); +list_add (&vec.lv_chain, &lv_chain);/* account for space used by record data */ ticket->t_curr_res -=sizeof (unmount_rec);
@@ -857,8 +860,8 @@ xlog_write_unmount_record(*/ if (log->l_targ != log->l_mp->m_ddev_targp)blkdev_issue_flush (log->l_targ->bt_bdev); -return xlog_write (log, &vec, ticket,NULL ,NULL ,XLOG_UNMOUNT_TRANS , - reg.i_len); +return xlog_write (log, &lv_chain, ticket,NULL ,NULL , +XLOG_UNMOUNT_TRANS , reg.i_len); }/*
@@ -1571,14 +1574,17 @@ xlog_commit_record( .lv_iovecp = ®, };int error; +LIST_HEAD (lv_chain); +INIT_LIST_HEAD (&vec.lv_chain); +list_add (&vec.lv_chain, &lv_chain);if (XLOG_FORCED_SHUTDOWN (log))return -EIO ;/* account for space used by record data */ ticket->t_curr_res -= reg.i_len; - error =xlog_write (log, &vec, ticket, lsn, iclog,XLOG_COMMIT_TRANS , - reg.i_len); + error =xlog_write (log, &lv_chain, ticket, lsn, iclog, +XLOG_COMMIT_TRANS , reg.i_len);if (error)xfs_force_shutdown (log->l_mp,SHUTDOWN_LOG_IO_ERROR );return error;
@@ -2109,6 +2115,7 @@ xlog_print_trans(*/ static struct xfs_log_vec * xlog_write_single( + struct list_head *lv_chain, struct xfs_log_vec *log_vector, struct xlog_ticket *ticket, struct xlog_in_core *iclog,
@@ -2117,7 +2124,7 @@ xlog_write_single(uint32_t *record_cnt, uint32_t *data_cnt) { - struct xfs_log_vec *lv = log_vector; + struct xfs_log_vec *lv;void *ptr;int index;
@@ -2125,10 +2132,13 @@ xlog_write_single( iclog->ic_state == XLOG_STATE_WANT_SYNC); ptr = iclog->ic_datap + *log_offset; -for (lv = log_vector; lv; lv = lv->lv_next) { +for (lv = log_vector; + !list_entry_is_head (lv, lv_chain, lv_chain); + lv =list_next_entry (lv, lv_chain)) {/* -* If the entire log vec does not fit in the iclog, punt it to -* the partial copy loop which can handle this case. +* If the log vec contains data that needs to be copied and does +* not entirely fit in the iclog, punt it to the partial copy +* loop which can handle this case. */ if (lv->lv_niovecs && lv->lv_bytes > iclog->ic_size - *log_offset)
@@ -2154,6 +2164,8 @@ xlog_write_single( *data_cnt += reg->i_len; } } +if (list_entry_is_head (lv, lv_chain, lv_chain)) + lv =NULL ;ASSERT (*len ==0 || lv);return lv; }
@@ -2199,6 +2211,7 @@ xlog_write_get_more_iclog_space(static struct xfs_log_vec * xlog_write_partial( struct xlog *log, + struct list_head *lv_chain, struct xfs_log_vec *log_vector, struct xlog_ticket *ticket, struct xlog_in_core **iclogp,
@@ -2338,7 +2351,10 @@ xlog_write_partial(* the caller so it can go back to fast path copying. */ *iclogp = iclog; -return lv->lv_next; + lv =list_next_entry (lv, lv_chain); +if (list_entry_is_head (lv, lv_chain, lv_chain)) +return NULL ; +return lv; }/*
@@ -2384,7 +2400,7 @@ xlog_write_partial( int xlog_write( struct xlog *log, - struct xfs_log_vec *log_vector, + struct list_head *lv_chain, struct xlog_ticket *ticket, xfs_lsn_t *start_lsn, struct xlog_in_core **commit_iclog,
@@ -2392,7 +2408,7 @@ xlog_write( uint32_t len) {struct xlog_in_core *iclog =NULL ; -struct xfs_log_vec *lv = log_vector; +struct xfs_log_vec *lv;int record_cnt =0 ;int data_cnt =0 ;int error =0 ;
@@ -2424,15 +2440,17 @@ xlog_write(if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) iclog->ic_flags |= (XLOG_ICL_NEED_FLUSH |XLOG_ICL_NEED_FUA ); + lv = list_first_entry_or_null(lv_chain, struct xfs_log_vec, lv_chain);while (lv) { - lv =xlog_write_single (lv, ticket, iclog, &log_offset, + lv =xlog_write_single (lv_chain, lv, ticket, iclog, &log_offset, &len, &record_cnt, &data_cnt);if (!lv)break ;ASSERT (!(optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS ))); - lv =xlog_write_partial (log, lv, ticket, &iclog, &log_offset, - &len, &record_cnt, &data_cnt); + lv =xlog_write_partial (log, lv_chain, lv, ticket, &iclog, + &log_offset, &len, &record_cnt, + &data_cnt);if (IS_ERR_OR_NULL (lv)) { error =PTR_ERR_OR_ZERO (lv);break ;
fs/xfs/xfs_log.h +2 -1
@@ -9,7 +9,8 @@struct xfs_cil_ctx ;struct xfs_log_vec { -struct xfs_log_vec *lv_next;/* next lv in build list */ +struct list_head lv_chain;/* lv chain ptrs */ +int lv_order_id;/* chain ordering info */ int lv_niovecs;/* number of iovecs in lv */ struct xfs_log_iovec *lv_iovecp;/* iovec array */ struct xfs_log_item *lv_item;/* owner */
fs/xfs/xfs_log_cil.c +38 -25
152 lines not on this page · raw message
fs/xfs/xfs_log_priv.h +2 -2
@@ -224,7 +224,7 @@ struct xfs_cil_ctx {int nvecs;/* number of regions */ atomic_t space_used;/* aggregate size of regions */ struct list_head busy_extents;/* busy extents in chkpt */ -struct xfs_log_vec *lv_chain;/* logvecs being pushed */ +struct list_head lv_chain;/* logvecs being pushed */ struct list_head iclog_entry;struct list_head committing;/* ctx committing list */ struct work_struct discard_endio_work;
@@ -480,7 +480,7 @@ xlog_write_adv_cnt(void **ptr, int *len, int *off, size_t bytes)void xlog_print_tic_res (struct xfs_mount *mp,struct xlog_ticket *ticket);void xlog_print_trans (struct xfs_trans *); -int xlog_write (struct xlog *log,struct xfs_log_vec *log_vector, +int xlog_write (struct xlog *log,struct list_head *lv_chain,struct xlog_ticket *tic,xfs_lsn_t *start_lsn,struct xlog_in_core **commit_iclog,uint optype,uint32_t len); int xlog_commit_record(struct xlog *log, struct xlog_ticket *ticket,
fs/xfs/xfs_trans.c +2 -2
@@ -747,7 +747,7 @@ xfs_log_item_batch_insert(void xfs_trans_committed_bulk (struct xfs_ail *ailp, -struct xfs_log_vec *log_vector, +struct list_head *lv_chain,xfs_lsn_t commit_lsn,bool aborted) {
@@ -762,7 +762,7 @@ xfs_trans_committed_bulk(spin_unlock (&ailp->ail_lock);/* unpin all the log items */ -for (lv = log_vector; lv; lv = lv->lv_next ) { +list_for_each_entry (lv, lv_chain, lv_chain) {struct xfs_log_item *lip = lv->lv_item;xfs_lsn_t item_lsn;
fs/xfs/xfs_trans_priv.h +2 -2
@@ -18,8 +18,8 @@ void xfs_trans_add_item(struct xfs_trans *, struct xfs_log_item *);void xfs_trans_del_item (struct xfs_log_item *);void xfs_trans_unreserve_and_mod_sb (struct xfs_trans *tp); -void xfs_trans_committed_bulk (struct xfs_ail *ailp,struct xfs_log_vec *lv, -xfs_lsn_t commit_lsn,bool aborted); +void xfs_trans_committed_bulk (struct xfs_ail *ailp, +struct list_head *lv_chain,xfs_lsn_t commit_lsn,bool aborted);/* * AIL traversal cursor. *
Signature · 1 line
2.28.0
Re: [PATCH 41/45] xfs: move CIL ordering to the logvec chain
Quoted 5 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Adding a
list_sort()call to the CIL push work while thexc_ctx_lockis held exclusively has resulted in fairly long lock hold times and that stops all front end transaction commits from making progress.
Heh, nice solution. :)
Quoted 5 lines
We can move the sorting out of the
xc_ctx_lockif we can transfer the ordering information to the log vectors as they are detached from the log items and then we can sort the log vectors. This requires log vectors to use alist_headrather than a single linked list
Ergh, could pull out the list conversion into a separate piece? Some of the lv_chain usage is ... not entirely textbook.
Quoted 166 lines
and to hold an order ID field. With these changes, we can move the
list_sort()call to just before we callxlog_write()when we aren't holding any locks at all.Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log.c | 46 +++++++++++++++++++++--------- fs/xfs/xfs_log.h | 3 +- fs/xfs/xfs_log_cil.c | 63 +++++++++++++++++++++++++---------------- fs/xfs/xfs_log_priv.h | 4 +-- fs/xfs/xfs_trans.c | 4 +-- fs/xfs/xfs_trans_priv.h | 4 +-- 6 files changed, 78 insertions(+), 46 deletions(-)fs/xfs/xfs_log.c +32
-14@@ -846,6 +846,9 @@ xlog_write_unmount_record( .lv_niovecs = 1, .lv_iovecp = ®, }; +LIST_HEAD (lv_chain); +INIT_LIST_HEAD (&vec.lv_chain); +list_add (&vec.lv_chain, &lv_chain);/* account for space used by record data */ ticket->t_curr_res -=sizeof (unmount_rec);@@ -857,8 +860,8 @@ xlog_write_unmount_record(*/ if (log->l_targ != log->l_mp->m_ddev_targp)blkdev_issue_flush (log->l_targ->bt_bdev); -return xlog_write (log, &vec, ticket,NULL ,NULL ,XLOG_UNMOUNT_TRANS , - reg.i_len); +return xlog_write (log, &lv_chain, ticket,NULL ,NULL , +XLOG_UNMOUNT_TRANS , reg.i_len); }/* @@ -1571,14 +1574,17 @@ xlog_commit_record( .lv_iovecp = ®, };int error; +LIST_HEAD (lv_chain); +INIT_LIST_HEAD (&vec.lv_chain); +list_add (&vec.lv_chain, &lv_chain);if (XLOG_FORCED_SHUTDOWN (log))return -EIO ;/* account for space used by record data */ ticket->t_curr_res -= reg.i_len; - error =xlog_write (log, &vec, ticket, lsn, iclog,XLOG_COMMIT_TRANS , - reg.i_len); + error =xlog_write (log, &lv_chain, ticket, lsn, iclog, +XLOG_COMMIT_TRANS , reg.i_len);if (error)xfs_force_shutdown (log->l_mp,SHUTDOWN_LOG_IO_ERROR );return error;@@ -2109,6 +2115,7 @@ xlog_print_trans(*/ static struct xfs_log_vec * xlog_write_single( + struct list_head *lv_chain, struct xfs_log_vec *log_vector, struct xlog_ticket *ticket, struct xlog_in_core *iclog,@@ -2117,7 +2124,7 @@ xlog_write_single(uint32_t *record_cnt, uint32_t *data_cnt) { - struct xfs_log_vec *lv = log_vector; + struct xfs_log_vec *lv;void *ptr;int index;@@ -2125,10 +2132,13 @@ xlog_write_single( iclog->ic_state == XLOG_STATE_WANT_SYNC); ptr = iclog->ic_datap + *log_offset; -for (lv = log_vector; lv; lv = lv->lv_next) { +for (lv = log_vector; + !list_entry_is_head (lv, lv_chain, lv_chain); + lv =list_next_entry (lv, lv_chain)) {/* -* If the entire log vec does not fit in the iclog, punt it to -* the partial copy loop which can handle this case. +* If the log vec contains data that needs to be copied and does +* not entirely fit in the iclog, punt it to the partial copy +* loop which can handle this case. */ if (lv->lv_niovecs && lv->lv_bytes > iclog->ic_size - *log_offset)@@ -2154,6 +2164,8 @@ xlog_write_single( *data_cnt += reg->i_len; } } +if (list_entry_is_head (lv, lv_chain, lv_chain)) + lv =NULL ;ASSERT (*len ==0 || lv);return lv; }@@ -2199,6 +2211,7 @@ xlog_write_get_more_iclog_space(static struct xfs_log_vec * xlog_write_partial( struct xlog *log, + struct list_head *lv_chain, struct xfs_log_vec *log_vector, struct xlog_ticket *ticket, struct xlog_in_core **iclogp,@@ -2338,7 +2351,10 @@ xlog_write_partial(* the caller so it can go back to fast path copying. */ *iclogp = iclog; -return lv->lv_next; + lv =list_next_entry (lv, lv_chain); +if (list_entry_is_head (lv, lv_chain, lv_chain)) +return NULL ; +return lv; }/* @@ -2384,7 +2400,7 @@ xlog_write_partial( int xlog_write( struct xlog *log, - struct xfs_log_vec *log_vector, + struct list_head *lv_chain, struct xlog_ticket *ticket, xfs_lsn_t *start_lsn, struct xlog_in_core **commit_iclog,@@ -2392,7 +2408,7 @@ xlog_write( uint32_t len) {struct xlog_in_core *iclog =NULL ; -struct xfs_log_vec *lv = log_vector; +struct xfs_log_vec *lv;int record_cnt =0 ;int data_cnt =0 ;int error =0 ;@@ -2424,15 +2440,17 @@ xlog_write(if (optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS )) iclog->ic_flags |= (XLOG_ICL_NEED_FLUSH |XLOG_ICL_NEED_FUA ); + lv = list_first_entry_or_null(lv_chain, struct xfs_log_vec, lv_chain);while (lv) { - lv =xlog_write_single (lv, ticket, iclog, &log_offset, + lv =xlog_write_single (lv_chain, lv, ticket, iclog, &log_offset, &len, &record_cnt, &data_cnt);if (!lv)break ;ASSERT (!(optype & (XLOG_COMMIT_TRANS |XLOG_UNMOUNT_TRANS ))); - lv =xlog_write_partial (log, lv, ticket, &iclog, &log_offset, - &len, &record_cnt, &data_cnt); + lv =xlog_write_partial (log, lv_chain, lv, ticket, &iclog, + &log_offset, &len, &record_cnt, + &data_cnt);if (IS_ERR_OR_NULL (lv)) { error =PTR_ERR_OR_ZERO (lv);break ;fs/xfs/xfs_log.h +0
-0
fs/xfs/xfs_log.h line 13
Quoted 6 lines
fs/xfs/xfs_log.h +2
-1@@ -9,7 +9,8 @@struct xfs_cil_ctx ;struct xfs_log_vec { -struct xfs_log_vec *lv_next;/* next lv in build list */ +struct list_head lv_chain;/* lv chain ptrs */ +int lv_order_id;/* chain ordering info */
uint32_t to match li_order_id?
Quoted 32 lines
fs/xfs/xfs_log.h +0
-0int lv_niovecs;/* number of iovecs in lv */ struct xfs_log_iovec *lv_iovecp;/* iovec array */ struct xfs_log_item *lv_item;/* owner */ fs/xfs/xfs_log_cil.c +2
-1@@ -72,6 +72,7 @@ xlog_cil_ctx_alloc(void) ctx =kmem_zalloc (sizeof (*ctx),KM_NOFS );INIT_LIST_HEAD (&ctx->committing);INIT_LIST_HEAD (&ctx->busy_extents); +INIT_LIST_HEAD (&ctx->lv_chain);INIT_WORK (&ctx->push_work, xlog_cil_push_work);return ctx; }@@ -237,6 +238,7 @@ xlog_cil_alloc_shadow_bufs( lv =kmem_alloc_large (buf_size,KM_NOFS );memset (lv,0 ,xlog_cil_iovec_space (niovecs)); +INIT_LIST_HEAD (&lv->lv_chain); lv->lv_item = lip; lv->lv_size = buf_size;if (ordered)@@ -252,7 +254,6 @@ xlog_cil_alloc_shadow_bufs(else lv->lv_buf_len =0 ; lv->lv_bytes =0 ; - lv->lv_next =NULL ; }/* Ensure the lv is set up according to ->iop_size */
fs/xfs/xfs_log_cil.c xlog_cil_insert_format_items( · line 382
Quoted 4 lines
fs/xfs/xfs_log_cil.c +0
-1@@ -379,8 +380,6 @@ xlog_cil_insert_format_items(if (lip->li_lv && shadow->lv_size <= lip->li_lv->lv_size) {/* same or smaller, optimise common overwrite case */ lv = lip->li_lv; - lv->lv_next =NULL ;
What /did/ these null assignments do?
Quoted 6 lines
fs/xfs/xfs_log_cil.c +0
-1-if (ordered) goto insert;@@ -547,14 +546,14 @@ xlog_cil_insert_items(
fs/xfs/xfs_log_cil.c xlog_cil_insert_items( · line 553
Quoted 10 lines
fs/xfs/xfs_log_cil.c +2
-3static void xlog_cil_free_logvec ( -struct xfs_log_vec *log_vector) +struct list_head *lv_chain) {struct xfs_log_vec *lv; -for (lv = log_vector; lv; ) { -struct xfs_log_vec *next = lv->lv_next; +while (!list_empty (lv_chain)) {
Nit: space after "while".
Quoted 72 lines
fs/xfs/xfs_log_cil.c +12
-15+ lv = list_first_entry(lv_chain, struct xfs_log_vec, lv_chain); +list_del_init (&lv->lv_chain);kmem_free (lv); - lv = next; } }@@ -653,7 +652,7 @@ xlog_cil_committed(spin_unlock (&ctx->cil->xc_push_lock); } -xfs_trans_committed_bulk (ctx->cil->xc_log->l_ailp, ctx->lv_chain, +xfs_trans_committed_bulk (ctx->cil->xc_log->l_ailp, &ctx->lv_chain, ctx->start_lsn, abort);xfs_extent_busy_sort (&ctx->busy_extents);@@ -664,7 +663,7 @@ xlog_cil_committed(list_del (&ctx->committing);spin_unlock (&ctx->cil->xc_push_lock); -xlog_cil_free_logvec (ctx->lv_chain); +xlog_cil_free_logvec (&ctx->lv_chain);if (!list_empty (&ctx->busy_extents))xlog_discard_busy_extents (mp, ctx);@@ -744,7 +743,7 @@ xlog_cil_build_trans_hdr( lvhdr->lv_niovecs =2 ; lvhdr->lv_iovecp = &hdr->lhdr[0 ]; lvhdr->lv_bytes = hdr->lhdr[0 ].i_len + hdr->lhdr[1 ].i_len; - lvhdr->lv_next = ctx->lv_chain; +list_add (&lvhdr->lv_chain, &ctx->lv_chain); tic->t_curr_res -= lvhdr->lv_bytes; }@@ -755,12 +754,14 @@ xlog_cil_order_cmp(struct list_head *a, struct list_head *b) { - struct xfs_log_item *l1 = container_of(a, struct xfs_log_item, li_cil); - struct xfs_log_item *l2 = container_of(b, struct xfs_log_item, li_cil); + struct xfs_log_vec *l1 = container_of(a, struct xfs_log_vec, + lv_chain); + struct xfs_log_vec *l2 = container_of(b, struct xfs_log_vec, + lv_chain); -if (l1->li_order_id > l2->li_order_id) +if (l1->lv_order_id > l2->lv_order_id)return 1 ; -if (l1->li_order_id < l2->li_order_id) +if (l1->lv_order_id < l2->lv_order_id)return -1 ;return 0 ; }@@ -907,26 +908,25 @@ xlog_cil_push_work(* needed on the transaction commit side which is currently locked out * by the flush lock. */ -list_sort (NULL , &log_items, xlog_cil_order_cmp); lv =NULL ;while (!list_empty (&log_items)) {struct xfs_log_item *item; item =list_first_entry (&log_items, struct xfs_log_item, li_cil); -list_del_init (&item->li_cil); - item->li_order_id =0 ; -if (!ctx->lv_chain) - ctx->lv_chain = item->li_lv; -else - lv->lv_next = item->li_lv; + lv = item->li_lv;
fs/xfs/xfs_log_cil.c xlog_cil_push_work( · line 926
Quoted 10 lines
fs/xfs/xfs_log_cil.c +4
-2- item->li_lv =NULL ; + lv->lv_order_id = item->li_order_id; num_iovecs += lv->lv_niovecs; -/* we don't write ordered log vectors */ if (lv->lv_buf_len !=XFS_LOG_VEC_ORDERED ) num_bytes += lv->lv_bytes; +list_add_tail (&lv->lv_chain, &ctx->lv_chain); + +list_del_init (&item->li_cil);
Do the list manipulations need moving, or could they have stayed further up in the loop body for a cleaner patch?
Quoted 99 lines
fs/xfs/xfs_log_cil.c +18
-2+ item->li_order_id =0 ; + item->li_lv =NULL ; + }/* @@ -959,6 +959,13 @@ xlog_cil_push_work(spin_unlock (&cil->xc_push_lock);up_write (&cil->xc_ctx_lock); +/* +* Sort the log vector chain before we add the transaction headers. +* This ensures we always have the transaction headers at the start +* of the chain. +*/ +list_sort (NULL , &ctx->lv_chain, xlog_cil_order_cmp); +/* * Build a checkpoint transaction header and write it to the log to * begin the transaction. We need to account for the space used by the @@ -981,8 +988,14 @@ xlog_cil_push_work(* use the commit record lsn then we can move the tail beyond the grant * write head. */ - error =xlog_write (log, &lvhdr, ctx->ticket, &ctx->start_lsn,NULL , -XLOG_START_TRANS , num_bytes); + error =xlog_write (log, &ctx->lv_chain, ctx->ticket, &ctx->start_lsn, +NULL ,XLOG_START_TRANS , num_bytes); + +/* +* Take the lvhdr back off the lv_chain as it should not be passed +* to log IO completion. +*/ +list_del (&lvhdr.lv_chain);if (error) goto out_abort_free_ticket;fs/xfs/xfs_log_priv.h +2
-2@@ -224,7 +224,7 @@ struct xfs_cil_ctx {int nvecs;/* number of regions */ atomic_t space_used;/* aggregate size of regions */ struct list_head busy_extents;/* busy extents in chkpt */ -struct xfs_log_vec *lv_chain;/* logvecs being pushed */ +struct list_head lv_chain;/* logvecs being pushed */ struct list_head iclog_entry;struct list_head committing;/* ctx committing list */ struct work_struct discard_endio_work;@@ -480,7 +480,7 @@ xlog_write_adv_cnt(void **ptr, int *len, int *off, size_t bytes)void xlog_print_tic_res (struct xfs_mount *mp,struct xlog_ticket *ticket);void xlog_print_trans (struct xfs_trans *); -int xlog_write (struct xlog *log,struct xfs_log_vec *log_vector, +int xlog_write (struct xlog *log,struct list_head *lv_chain,struct xlog_ticket *tic,xfs_lsn_t *start_lsn,struct xlog_in_core **commit_iclog,uint optype,uint32_t len); int xlog_commit_record(struct xlog *log, struct xlog_ticket *ticket,fs/xfs/xfs_trans.c +2
-2@@ -747,7 +747,7 @@ xfs_log_item_batch_insert(void xfs_trans_committed_bulk (struct xfs_ail *ailp, -struct xfs_log_vec *log_vector, +struct list_head *lv_chain,xfs_lsn_t commit_lsn,bool aborted) {@@ -762,7 +762,7 @@ xfs_trans_committed_bulk(spin_unlock (&ailp->ail_lock);/* unpin all the log items */ -for (lv = log_vector; lv; lv = lv->lv_next ) { +list_for_each_entry (lv, lv_chain, lv_chain) {struct xfs_log_item *lip = lv->lv_item;xfs_lsn_t item_lsn;fs/xfs/xfs_trans_priv.h +2
-2@@ -18,8 +18,8 @@ void xfs_trans_add_item(struct xfs_trans *, struct xfs_log_item *);void xfs_trans_del_item (struct xfs_log_item *);void xfs_trans_unreserve_and_mod_sb (struct xfs_trans *tp); -void xfs_trans_committed_bulk (struct xfs_ail *ailp,struct xfs_log_vec *lv, -xfs_lsn_t commit_lsn,bool aborted); +void xfs_trans_committed_bulk (struct xfs_ail *ailp, +struct list_head *lv_chain,xfs_lsn_t commit_lsn,bool aborted);/* * AIL traversal cursor. * Signature · 1 line
2.28.0
Re: [PATCH 41/45] xfs: move CIL ordering to the logvec chain
Quoted 9 lines · Darrick J. Wong
Quoted 5 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Adding a
list_sort()call to the CIL push work while thexc_ctx_lockis held exclusively has resulted in fairly long lock hold times and that stops all front end transaction commits from making progress.Heh, nice solution. :)
Quoted 8 lines
Quoted 5 lines
We can move the sorting out of the
xc_ctx_lockif we can transfer the ordering information to the log vectors as they are detached from the log items and then we can sort the log vectors. This requires log vectors to use alist_headrather than a single linked listErgh, could pull out the list conversion into a separate piece? Some of the
lv_chainusage is ... not entirely textbook.
Yes, I can probably do that.
Quoted 7 lines
Quoted 7 lines
fs/xfs/xfs_log.h +0
-0@@ -9,7 +9,8 @@struct xfs_cil_ctx ;
Quoted 6 lines
fs/xfs/xfs_log.h line 13
Quoted 4 lines
fs/xfs/xfs_log.h +2
-1struct xfs_log_vec { -struct xfs_log_vec *lv_next;/* next lv in build list */ +struct list_head lv_chain;/* lv chain ptrs */ +int lv_order_id;/* chain ordering info */
uint32_tto matchli_order_id?
*nod*
Quoted 27 lines
Quoted 27 lines
fs/xfs/xfs_log_cil.c +2
-1@@ -72,6 +72,7 @@ xlog_cil_ctx_alloc(void) ctx =kmem_zalloc (sizeof (*ctx),KM_NOFS );INIT_LIST_HEAD (&ctx->committing);INIT_LIST_HEAD (&ctx->busy_extents); +INIT_LIST_HEAD (&ctx->lv_chain);INIT_WORK (&ctx->push_work, xlog_cil_push_work);return ctx; }@@ -237,6 +238,7 @@ xlog_cil_alloc_shadow_bufs( lv =kmem_alloc_large (buf_size,KM_NOFS );memset (lv,0 ,xlog_cil_iovec_space (niovecs)); +INIT_LIST_HEAD (&lv->lv_chain); lv->lv_item = lip; lv->lv_size = buf_size;if (ordered)@@ -252,7 +254,6 @@ xlog_cil_alloc_shadow_bufs(else lv->lv_buf_len =0 ; lv->lv_bytes =0 ; - lv->lv_next =NULL ; }
Quoted 8 lines
fs/xfs/xfs_log_cil.c xlog_cil_insert_format_items( · line 382
Quoted 6 lines
fs/xfs/xfs_log_cil.c +0
-1/* Ensure the lv is set up according to ->iop_size */ @@ -379,8 +380,6 @@ xlog_cil_insert_format_items(if (lip->li_lv && shadow->lv_size <= lip->li_lv->lv_size) {/* same or smaller, optimise common overwrite case */ lv = lip->li_lv; - lv->lv_next =NULL ;What /did/ these null assignments do?
IIRC, at one point they ensured that the lv chain was correctly terminated when a lv was reused and added to the tail of an existing chain. I think that became redundant when we added the shadow buffers to allow allocation outside the CIL lock contexts...
Quoted 13 lines
Quoted 13 lines
fs/xfs/xfs_log_cil.c +2
-8-list_del_init (&item->li_cil); - item->li_order_id =0 ; -if (!ctx->lv_chain) - ctx->lv_chain = item->li_lv; -else - lv->lv_next = item->li_lv; + lv = item->li_lv; - item->li_lv =NULL ; + lv->lv_order_id = item->li_order_id; num_iovecs += lv->lv_niovecs; -/* we don't write ordered log vectors */
Quoted 8 lines
fs/xfs/xfs_log_cil.c xlog_cil_push_work( · line 926
Quoted 5 lines
fs/xfs/xfs_log_cil.c +3
-0if (lv->lv_buf_len !=XFS_LOG_VEC_ORDERED ) num_bytes += lv->lv_bytes; +list_add_tail (&lv->lv_chain, &ctx->lv_chain); + +list_del_init (&item->li_cil);Do the list manipulations need moving, or could they have stayed further up in the loop body for a cleaner patch?
I moved them so the code was structured as:
<transfer item state to log vec>
<manipulate lists>
<clear item state>
Because there was no clear separation between state and list manipulations. This will clean up if I separate the list manipulations into their own patch...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 42/45 xfs: __percpu_counter_compare() inode count debug too expensive
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
- 21.92% __xfs_trans_commit
- 21.62% xfs_log_commit_cil
- 11.69% xfs_trans_unreserve_and_mod_sb
- 11.58% __percpu_counter_compare
- 11.45% __percpu_counter_sum
- 10.29% _raw_spin_lock_irqsave
- 10.28% do_raw_spin_lock __pv_queued_spin_lock_slowpath
We debated just getting rid of it last time this came up and there was no real objection to removing it. Now it's the biggest scalability limitation for debug kernels even on smallish machines, so let's just get rid of it.
--- fs/xfs/xfs_trans.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
fs/xfs/xfs_trans.c +2 -9
@@ -616,19 +616,12 @@ xfs_trans_unreserve_and_mod_sb(ASSERT (!error); } -if (idelta) { +if (idelta)percpu_counter_add_batch (&mp->m_icount, idelta,XFS_ICOUNT_BATCH ); -if (idelta <0 ) -ASSERT (__percpu_counter_compare (&mp->m_icount,0 , -XFS_ICOUNT_BATCH ) >=0 ); - } -if (ifreedelta) { +if (ifreedelta)percpu_counter_add (&mp->m_ifree, ifreedelta); -if (ifreedelta <0 ) -ASSERT (percpu_counter_compare (&mp->m_ifree,0 ) >=0 ); - }if (rtxdelta ==0 && !(tp->t_flags &XFS_TRANS_SB_DIRTY ))return ;
Signature · 1 line
2.28.0
Re: [PATCH 42/45] xfs: __percpu_counter_compare() inode count debug too expensive
Quoted 11 lines · Dave Chinner
From: Dave Chinner <[email protected]>
- 21.92%
__xfs_trans_commit
- 21.62%xfs_log_commit_cil
- 11.69%xfs_trans_unreserve_and_mod_sb
- 11.58%__percpu_counter_compare
- 11.45%__percpu_counter_sum
- 10.29%_raw_spin_lock_irqsave
- 10.28%do_raw_spin_lock__pv_queued_spin_lock_slowpath
Quoted 6 lines
We debated just getting rid of it last time this came up and there was no real objection to removing it. Now it's the biggest scalability limitation for debug kernels even on smallish machines, so let's just get rid of it.
Signed-off-by Dave Chinner <[email protected]>
...unless you want a CONFIG_XFS_DEBUG_SLOW to hide these things behind?
--D
Quoted 33 lines
--- fs/xfs/xfs_trans.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)fs/xfs/xfs_trans.c +2
-9@@ -616,19 +616,12 @@ xfs_trans_unreserve_and_mod_sb(ASSERT (!error); } -if (idelta) { +if (idelta)percpu_counter_add_batch (&mp->m_icount, idelta,XFS_ICOUNT_BATCH ); -if (idelta <0 ) -ASSERT (__percpu_counter_compare (&mp->m_icount,0 , -XFS_ICOUNT_BATCH ) >=0 ); - } -if (ifreedelta) { +if (ifreedelta)percpu_counter_add (&mp->m_ifree, ifreedelta); -if (ifreedelta <0 ) -ASSERT (percpu_counter_compare (&mp->m_ifree,0 ) >=0 ); - }if (rtxdelta ==0 && !(tp->t_flags &XFS_TRANS_SB_DIRTY ))return ;Signature · 1 line
2.28.0
PATCH v3 43/45 xfs: avoid cil push lock if possible
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
Because now it hurts when the CIL fills up.
- 37.20% __xfs_trans_commit
- 35.84% xfs_log_commit_cil
- 19.34% _raw_spin_lock
- do_raw_spin_lock
19.01% __pv_queued_spin_lock_slowpath
- 4.20% xfs_log_ticket_ungrant
0.90% xfs_log_space_wake
--- fs/xfs/xfs_log_cil.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
fs/xfs/xfs_log_cil.c +11 -3
@@ -1115,10 +1115,18 @@ xlog_cil_push_background(ASSERT (!test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));/* -* Don't do a background push if we haven't used up all the -* space available yet. +* We are done if: +* - we haven't used up all the space available yet; or +* - we've already queued up a push; and +* - we're not over the hard limit; and +* - nothing has been over the hard limit. +* +* If so, we don't need to take the push lock as there's nothing to do. */ -if (space_used <XLOG_CIL_SPACE_LIMIT (log)) { +if (space_used <XLOG_CIL_SPACE_LIMIT (log) || + (cil->xc_push_seq == cil->xc_current_sequence && + space_used <XLOG_CIL_BLOCKING_SPACE_LIMIT (log) && + !waitqueue_active (&cil->xc_push_wait))) {up_read (&cil->xc_ctx_lock);return ; }
Signature · 1 line
2.28.0
Re: [PATCH 43/45] xfs: avoid cil push lock if possible
Quoted 23 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Because now it hurts when the CIL fills up.
- 37.20%
__xfs_trans_commit
- 35.84%xfs_log_commit_cil
- 19.34%_raw_spin_lock
-do_raw_spin_lock
19.01%__pv_queued_spin_lock_slowpath
- 4.20%xfs_log_ticket_ungrant
0.90%xfs_log_space_wakeSigned-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)fs/xfs/xfs_log_cil.c +0
-0
fs/xfs/xfs_log_cil.c xlog_cil_push_background( · line 1122
Quoted 10 lines
fs/xfs/xfs_log_cil.c +5
-2@@ -1115,10 +1115,18 @@ xlog_cil_push_background(ASSERT (!test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));/* -* Don't do a background push if we haven't used up all the -* space available yet. +* We are done if: +* - we haven't used up all the space available yet; or +* - we've already queued up a push; and +* - we're not over the hard limit; and +* - nothing has been over the hard limit.
Er... do these last three bullet points correspond to the last three lines of the if test? I'm not sure how !waitqueue_active() determines that nothing has been over the hard limit? Or for that matter how comparing push_seq against current_seq tells us if we've queued a push?
--D
Quoted 14 lines
fs/xfs/xfs_log_cil.c +6
-1+* +* If so, we don't need to take the push lock as there's nothing to do. */ -if (space_used <XLOG_CIL_SPACE_LIMIT (log)) { +if (space_used <XLOG_CIL_SPACE_LIMIT (log) || + (cil->xc_push_seq == cil->xc_current_sequence && + space_used <XLOG_CIL_BLOCKING_SPACE_LIMIT (log) && + !waitqueue_active (&cil->xc_push_wait))) {up_read (&cil->xc_ctx_lock);return ; }Signature · 1 line
2.28.0
Re: [PATCH 43/45] xfs: avoid cil push lock if possible
Quoted 26 lines · Darrick J. Wong
Quoted 25 lines · Dave Chinner
From: Dave Chinner <[email protected]>
Because now it hurts when the CIL fills up.
- 37.20%
__xfs_trans_commit
- 35.84%xfs_log_commit_cil
- 19.34%_raw_spin_lock
-do_raw_spin_lock
19.01%__pv_queued_spin_lock_slowpath
- 4.20%xfs_log_ticket_ungrant
0.90%xfs_log_space_wakeSigned-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log_cil.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)fs/xfs/xfs_log_cil.c +0
-0@@ -1115,10 +1115,18 @@ xlog_cil_push_background(ASSERT (!test_bit (XLOG_CIL_EMPTY , &cil->xc_flags));
Quoted 12 lines
fs/xfs/xfs_log_cil.c xlog_cil_push_background( · line 1122
Quoted 8 lines
fs/xfs/xfs_log_cil.c +5
-2/* -* Don't do a background push if we haven't used up all the -* space available yet. +* We are done if: +* - we haven't used up all the space available yet; or +* - we've already queued up a push; and +* - we're not over the hard limit; and +* - nothing has been over the hard limit. Er... do these last three bullet points correspond to the last three lines of the if test? I'm not sure how !waitqueue_active() determines that nothing has been over the hard limit?
If a commit has made space used go over the hard limit, it will be throttled and put to sleep on the push wait queue. Another commit can then return space (inode fork gets smaller) and bring us back under the hard limit. Hence just checking against the space used does not tell us if we've hit the hard limit or not, but checking if there is a throttled process on the wait queue does...
Quoted 3 lines
Or for that matter how
comparingpush_seqagainstcurrent_seqtells us if we've queued a push?
We only set push_seq == current_seq when we queue up a push in xlog_cil_push_now() or xlog_cil_push_background(). Hence if no push has been queued, then push_seq will be less than current_seq.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 44/45 xfs: xlog_sync() manually adjusts grant head space
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
When xlog_sync() rounds off the tail the iclog that is being flushed, it manually subtracts that space from the grant heads. This space is actually reserved by the transaction ticket that covers the xlog_sync() call from xlog_write(), but we don't plumb the ticket down far enough for it to account for the space consumed in the current log ticket.
The grant heads are hot, so we really should be accounting this to the ticket is we can, rather than adding thousands of extra grant head updates every CIL commit.
Interestingly, this actually indicates a potential log space overrun can occur when we force the log. By the time that xfs_log_force() pushes out an active iclog and consumes the roundoff space, the reservation for that roundoff space has been returned to the grant heads and is no longer covered by a reservation. In theory the roundoff added to log force on an already full log could push the write head past the tail. In practice, the CIL commit that writes to the log and needs the iclog pushed will have reserved space for roundoff, so when it releases the ticket there will still be physical space for the roundoff to be committed to the log, even though it is no longer reserved. This roundoff won't be enough space to allow a transaction to be woken if the log is full, so overruns should not actually occur in practice.
That said, it indicates that we should not release the CIL context log ticket until after we've released the commit iclog. It also means that xlog_sync() still needs the direct grant head manipulation if we don't provide it with a ticket. Log forces are rare when we are in fast paths running 1.5 million transactions/s that make the grant heads hot, so let's optimise the hot case and pass CIL log tickets down to the xlog_sync() code.
--- fs/xfs/xfs_log.c | 39 +++++++++++++++++++++++++-------------- fs/xfs/xfs_log_cil.c | 19 ++++++++++++++----- fs/xfs/xfs_log_priv.h | 3 ++- 3 files changed, 41 insertions(+), 20 deletions(-)
fs/xfs/xfs_log.c +25 -14
@@ -55,7 +55,8 @@ xlog_grant_push_ail( STATIC voidxlog_sync (struct xlog *log, -struct xlog_in_core *iclog); +struct xlog_in_core *iclog, +struct xlog_ticket *ticket);#if defined(DEBUG ) STATIC void xlog_verify_dest_ptr(
@@ -535,7 +536,8 @@ __xlog_state_release_iclog(int xlog_state_release_iclog (struct xlog *log, -struct xlog_in_core *iclog) +struct xlog_in_core *iclog, +struct xlog_ticket *ticket) {lockdep_assert_held (&log->l_icloglock);
@@ -545,7 +547,7 @@ xlog_state_release_iclog(if (atomic_dec_and_test (&iclog->ic_refcnt) &&__xlog_state_release_iclog (log, iclog)) {spin_unlock (&log->l_icloglock); -xlog_sync (log, iclog); +xlog_sync (log, iclog, ticket);spin_lock (&log->l_icloglock); }
@@ -898,7 +900,7 @@ xlog_unmount_write(else ASSERT (iclog->ic_state ==XLOG_STATE_WANT_SYNC || iclog->ic_state ==XLOG_STATE_IOERROR ); - error =xlog_state_release_iclog (log, iclog); + error =xlog_state_release_iclog (log, iclog, tic);xlog_wait_on_iclog (iclog);if (tic) {
@@ -1930,7 +1932,8 @@ xlog_calc_iclog_size( STATIC voidxlog_sync (struct xlog *log, -struct xlog_in_core *iclog) +struct xlog_in_core *iclog, +struct xlog_ticket *ticket) {unsigned int count;/* byte count of bwrite */ unsigned int roundoff;/* roundoff to BB or stripe */
@@ -1941,12 +1944,20 @@ xlog_sync( count =xlog_calc_iclog_size (log, iclog, &roundoff); -/* move grant heads by roundoff in sync */ -xlog_grant_add_space (log, &log->l_reserve_head.grant, roundoff); -xlog_grant_add_space (log, &log->l_write_head.grant, roundoff); +/* +* If we have a ticket, account for the roundoff via the ticket +* reservation to avoid touching the hot grant heads needlessly. +* Otherwise, we have to move grant heads directly. +*/ +if (ticket) { + ticket->t_curr_res -= roundoff; + }else { +xlog_grant_add_space (log, &log->l_reserve_head.grant, roundoff); +xlog_grant_add_space (log, &log->l_write_head.grant, roundoff); + }/* put cycle number in every block */ -xlog_pack_data (log, iclog, roundoff); +xlog_pack_data (log, iclog, roundoff);/* real byte length */ size = iclog->ic_offset;
@@ -2187,7 +2198,7 @@ xlog_write_get_more_iclog_space(xlog_state_finish_copy (log, iclog, *record_cnt, *data_cnt);ASSERT (iclog->ic_state ==XLOG_STATE_WANT_SYNC || iclog->ic_state ==XLOG_STATE_IOERROR ); - error =xlog_state_release_iclog (log, iclog); + error =xlog_state_release_iclog (log, iclog, ticket);spin_unlock (&log->l_icloglock);if (error)return error;
@@ -2470,7 +2481,7 @@ xlog_write(ASSERT (optype &XLOG_COMMIT_TRANS ); *commit_iclog = iclog; }else { - error =xlog_state_release_iclog (log, iclog); + error =xlog_state_release_iclog (log, iclog, ticket); }spin_unlock (&log->l_icloglock);
@@ -2929,7 +2940,7 @@ xlog_state_get_iclog_space(* reference to the iclog. */ if (!atomic_add_unless (&iclog->ic_refcnt,-1 ,1 )) - error =xlog_state_release_iclog (log, iclog); + error =xlog_state_release_iclog (log, iclog, ticket);spin_unlock (&log->l_icloglock);if (error)return error;
@@ -3157,7 +3168,7 @@ xfs_log_force(atomic_inc (&iclog->ic_refcnt); lsn =be64_to_cpu (iclog->ic_header.h_lsn);xlog_state_switch_iclogs (log, iclog,0 ); -if (xlog_state_release_iclog (log, iclog)) +if (xlog_state_release_iclog (log, iclog,NULL )) goto out_error;if (be64_to_cpu (iclog->ic_header.h_lsn) != lsn)
@@ -3250,7 +3261,7 @@ xlog_force_lsn( }atomic_inc (&iclog->ic_refcnt);xlog_state_switch_iclogs (log, iclog,0 ); -if (xlog_state_release_iclog (log, iclog)) +if (xlog_state_release_iclog (log, iclog,NULL )) goto out_error;if (log_flushed) *log_flushed =1 ;
fs/xfs/xfs_log_cil.c +14 -5
@@ -804,6 +804,7 @@ xlog_cil_push_work(int cpu;struct xlog_cil_pcp *cilpcp;LIST_HEAD (log_items); +struct xlog_ticket *ticket; new_ctx =xlog_cil_ctx_alloc (); new_ctx->ticket =xlog_cil_ticket_alloc (log);
@@ -1037,12 +1038,10 @@ xlog_cil_push_work(if (error) goto out_abort_free_ticket; -xfs_log_ticket_ungrant (log, ctx->ticket); -spin_lock (&commit_iclog->ic_callback_lock);if (commit_iclog->ic_state ==XLOG_STATE_IOERROR ) {spin_unlock (&commit_iclog->ic_callback_lock); - goto out_abort; + goto out_abort_free_ticket; }ASSERT_ALWAYS (commit_iclog->ic_state ==XLOG_STATE_ACTIVE || commit_iclog->ic_state ==XLOG_STATE_WANT_SYNC );
@@ -1073,12 +1072,23 @@ xlog_cil_push_work( commit_iclog->ic_flags &= ~XLOG_ICL_NEED_FLUSH ; } +/* +* Pull the ticket off the ctx so we can ungrant it after releasing the +* commit_iclog. The ctx may be freed by the time we return from +* releasing the commit_iclog (i.e. checkpoint has been completed and +* callback run) so we can't reference the ctx after the call to +* xlog_state_release_iclog(). +*/ + ticket = ctx->ticket; +/* release the hounds! */ spin_lock (&log->l_icloglock);if (commit_iclog_sync && commit_iclog->ic_state ==XLOG_STATE_ACTIVE )xlog_state_switch_iclogs (log, commit_iclog,0 ); -xlog_state_release_iclog (log, commit_iclog); +xlog_state_release_iclog (log, commit_iclog, ticket);spin_unlock (&log->l_icloglock); + +xfs_log_ticket_ungrant (log, ticket);return ; out_skip:
@@ -1089,7 +1099,6 @@ xlog_cil_push_work( out_abort_free_ticket:xfs_log_ticket_ungrant (log, ctx->ticket); -out_abort:ASSERT (XLOG_FORCED_SHUTDOWN (log));xlog_cil_committed (ctx); }
fs/xfs/xfs_log_priv.h +2 -1
@@ -487,7 +487,8 @@ int xlog_commit_record(struct xlog *log, struct xlog_ticket *ticket, struct xlog_in_core **iclog, xfs_lsn_t *lsn);void xlog_state_switch_iclogs (struct xlog *log,struct xlog_in_core *iclog,int eventual_size); -int xlog_state_release_iclog (struct xlog *xlog,struct xlog_in_core *iclog); +int xlog_state_release_iclog (struct xlog *xlog,struct xlog_in_core *iclog, +struct xlog_ticket *ticket);void xfs_log_ticket_ungrant (struct xlog *log,struct xlog_ticket *ticket);void xfs_log_ticket_regrant (struct xlog *log,struct xlog_ticket *ticket);
Signature · 1 line
2.28.0
Re: [PATCH 44/45] xfs: xlog_sync() manually adjusts grant head space
Quoted 9 lines · Dave Chinner
From: Dave Chinner <[email protected]>
When
xlog_sync()rounds off the tail the iclog that is being flushed, it manually subtracts that space from the grant heads. This space is actually reserved by the transaction ticket that covers thexlog_sync()call fromxlog_write(), but we don't plumb the ticket down far enough for it to account for the space consumed in the current log ticket.
Quoted 7 lines
The grant heads are hot, so we really should be accounting this to the ticket is we can, rather than adding thousands of extra grant head updates every CIL commit.
Interestingly, this actually indicates a potential log space overrun can occur when we force the log. By the time that
xfs_log_force()pushes out an active iclog and consumes the roundoff space, the
Ok I was wondering about that when I was trying to figure out what all this ticket space stealing code was doing.
So in addition to fixing the theoretical overrun, I guess the performance fix here is that every time we write an iclog we might have to move the grant heads forward so that we always write a full log sector / log stripe unit? And since a CIL context might write a lot of iclogs, it's cheaper to make those grant adjustments to the CIL ticket (which already asked for enough space to handle the roundoffs) since the ticket only jumps in the hot path once when the ticket is ungranted?
If I got that right,
--D
Quoted 224 lines
reservation for that roundoff space has been returned to the grant heads and is no longer covered by a reservation. In theory the roundoff added to log force on an already full log could push the write head past the tail. In practice, the CIL commit that writes to the log and needs the iclog pushed will have reserved space for roundoff, so when it releases the ticket there will still be physical space for the roundoff to be committed to the log, even though it is no longer reserved. This roundoff won't be enough space to allow a transaction to be woken if the log is full, so overruns should not actually occur in practice.
That said, it indicates that we should not release the CIL context log ticket until after we've released the commit iclog. It also means that
xlog_sync()still needs the direct grant head manipulation if we don't provide it with a ticket. Log forces are rare when we are in fast paths running 1.5 million transactions/s that make the grant heads hot, so let's optimise the hot case and pass CIL log tickets down to thexlog_sync()code.Signed-off-by Dave Chinner <[email protected]>--- fs/xfs/xfs_log.c | 39 +++++++++++++++++++++++++-------------- fs/xfs/xfs_log_cil.c | 19 ++++++++++++++----- fs/xfs/xfs_log_priv.h | 3 ++- 3 files changed, 41 insertions(+), 20 deletions(-)fs/xfs/xfs_log.c +25
-14@@ -55,7 +55,8 @@ xlog_grant_push_ail( STATIC voidxlog_sync (struct xlog *log, -struct xlog_in_core *iclog); +struct xlog_in_core *iclog, +struct xlog_ticket *ticket);#if defined(DEBUG ) STATIC void xlog_verify_dest_ptr(@@ -535,7 +536,8 @@ __xlog_state_release_iclog(int xlog_state_release_iclog (struct xlog *log, -struct xlog_in_core *iclog) +struct xlog_in_core *iclog, +struct xlog_ticket *ticket) {lockdep_assert_held (&log->l_icloglock);@@ -545,7 +547,7 @@ xlog_state_release_iclog(if (atomic_dec_and_test (&iclog->ic_refcnt) &&__xlog_state_release_iclog (log, iclog)) {spin_unlock (&log->l_icloglock); -xlog_sync (log, iclog); +xlog_sync (log, iclog, ticket);spin_lock (&log->l_icloglock); }@@ -898,7 +900,7 @@ xlog_unmount_write(else ASSERT (iclog->ic_state ==XLOG_STATE_WANT_SYNC || iclog->ic_state ==XLOG_STATE_IOERROR ); - error =xlog_state_release_iclog (log, iclog); + error =xlog_state_release_iclog (log, iclog, tic);xlog_wait_on_iclog (iclog);if (tic) {@@ -1930,7 +1932,8 @@ xlog_calc_iclog_size( STATIC voidxlog_sync (struct xlog *log, -struct xlog_in_core *iclog) +struct xlog_in_core *iclog, +struct xlog_ticket *ticket) {unsigned int count;/* byte count of bwrite */ unsigned int roundoff;/* roundoff to BB or stripe */ @@ -1941,12 +1944,20 @@ xlog_sync( count =xlog_calc_iclog_size (log, iclog, &roundoff); -/* move grant heads by roundoff in sync */ -xlog_grant_add_space (log, &log->l_reserve_head.grant, roundoff); -xlog_grant_add_space (log, &log->l_write_head.grant, roundoff); +/* +* If we have a ticket, account for the roundoff via the ticket +* reservation to avoid touching the hot grant heads needlessly. +* Otherwise, we have to move grant heads directly. +*/ +if (ticket) { + ticket->t_curr_res -= roundoff; + }else { +xlog_grant_add_space (log, &log->l_reserve_head.grant, roundoff); +xlog_grant_add_space (log, &log->l_write_head.grant, roundoff); + }/* put cycle number in every block */ -xlog_pack_data (log, iclog, roundoff); +xlog_pack_data (log, iclog, roundoff);/* real byte length */ size = iclog->ic_offset;@@ -2187,7 +2198,7 @@ xlog_write_get_more_iclog_space(xlog_state_finish_copy (log, iclog, *record_cnt, *data_cnt);ASSERT (iclog->ic_state ==XLOG_STATE_WANT_SYNC || iclog->ic_state ==XLOG_STATE_IOERROR ); - error =xlog_state_release_iclog (log, iclog); + error =xlog_state_release_iclog (log, iclog, ticket);spin_unlock (&log->l_icloglock);if (error)return error;@@ -2470,7 +2481,7 @@ xlog_write(ASSERT (optype &XLOG_COMMIT_TRANS ); *commit_iclog = iclog; }else { - error =xlog_state_release_iclog (log, iclog); + error =xlog_state_release_iclog (log, iclog, ticket); }spin_unlock (&log->l_icloglock);@@ -2929,7 +2940,7 @@ xlog_state_get_iclog_space(* reference to the iclog. */ if (!atomic_add_unless (&iclog->ic_refcnt,-1 ,1 )) - error =xlog_state_release_iclog (log, iclog); + error =xlog_state_release_iclog (log, iclog, ticket);spin_unlock (&log->l_icloglock);if (error)return error;@@ -3157,7 +3168,7 @@ xfs_log_force(atomic_inc (&iclog->ic_refcnt); lsn =be64_to_cpu (iclog->ic_header.h_lsn);xlog_state_switch_iclogs (log, iclog,0 ); -if (xlog_state_release_iclog (log, iclog)) +if (xlog_state_release_iclog (log, iclog,NULL )) goto out_error;if (be64_to_cpu (iclog->ic_header.h_lsn) != lsn)@@ -3250,7 +3261,7 @@ xlog_force_lsn( }atomic_inc (&iclog->ic_refcnt);xlog_state_switch_iclogs (log, iclog,0 ); -if (xlog_state_release_iclog (log, iclog)) +if (xlog_state_release_iclog (log, iclog,NULL )) goto out_error;if (log_flushed) *log_flushed =1 ;fs/xfs/xfs_log_cil.c +14
-5@@ -804,6 +804,7 @@ xlog_cil_push_work(int cpu;struct xlog_cil_pcp *cilpcp;LIST_HEAD (log_items); +struct xlog_ticket *ticket; new_ctx =xlog_cil_ctx_alloc (); new_ctx->ticket =xlog_cil_ticket_alloc (log);@@ -1037,12 +1038,10 @@ xlog_cil_push_work(if (error) goto out_abort_free_ticket; -xfs_log_ticket_ungrant (log, ctx->ticket); -spin_lock (&commit_iclog->ic_callback_lock);if (commit_iclog->ic_state ==XLOG_STATE_IOERROR ) {spin_unlock (&commit_iclog->ic_callback_lock); - goto out_abort; + goto out_abort_free_ticket; }ASSERT_ALWAYS (commit_iclog->ic_state ==XLOG_STATE_ACTIVE || commit_iclog->ic_state ==XLOG_STATE_WANT_SYNC );@@ -1073,12 +1072,23 @@ xlog_cil_push_work( commit_iclog->ic_flags &= ~XLOG_ICL_NEED_FLUSH ; } +/* +* Pull the ticket off the ctx so we can ungrant it after releasing the +* commit_iclog. The ctx may be freed by the time we return from +* releasing the commit_iclog (i.e. checkpoint has been completed and +* callback run) so we can't reference the ctx after the call to +* xlog_state_release_iclog(). +*/ + ticket = ctx->ticket; +/* release the hounds! */ spin_lock (&log->l_icloglock);if (commit_iclog_sync && commit_iclog->ic_state ==XLOG_STATE_ACTIVE )xlog_state_switch_iclogs (log, commit_iclog,0 ); -xlog_state_release_iclog (log, commit_iclog); +xlog_state_release_iclog (log, commit_iclog, ticket);spin_unlock (&log->l_icloglock); + +xfs_log_ticket_ungrant (log, ticket);return ; out_skip:@@ -1089,7 +1099,6 @@ xlog_cil_push_work( out_abort_free_ticket:xfs_log_ticket_ungrant (log, ctx->ticket); -out_abort:ASSERT (XLOG_FORCED_SHUTDOWN (log));xlog_cil_committed (ctx); }fs/xfs/xfs_log_priv.h +2
-1@@ -487,7 +487,8 @@ int xlog_commit_record(struct xlog *log, struct xlog_ticket *ticket, struct xlog_in_core **iclog, xfs_lsn_t *lsn);void xlog_state_switch_iclogs (struct xlog *log,struct xlog_in_core *iclog,int eventual_size); -int xlog_state_release_iclog (struct xlog *xlog,struct xlog_in_core *iclog); +int xlog_state_release_iclog (struct xlog *xlog,struct xlog_in_core *iclog, +struct xlog_ticket *ticket);void xfs_log_ticket_ungrant (struct xlog *log,struct xlog_ticket *ticket);void xfs_log_ticket_regrant (struct xlog *log,struct xlog_ticket *ticket);Signature · 1 line
2.28.0
Re: [PATCH 44/45] xfs: xlog_sync() manually adjusts grant head space
Quoted 29 lines · Darrick J. Wong
Quoted 9 lines · Dave Chinner
From: Dave Chinner <[email protected]>
When
xlog_sync()rounds off the tail the iclog that is being flushed, it manually subtracts that space from the grant heads. This space is actually reserved by the transaction ticket that covers thexlog_sync()call fromxlog_write(), but we don't plumb the ticket down far enough for it to account for the space consumed in the current log ticket.Quoted 7 lines
The grant heads are hot, so we really should be accounting this to the ticket is we can, rather than adding thousands of extra grant head updates every CIL commit.
Interestingly, this actually indicates a potential log space overrun can occur when we force the log. By the time that
xfs_log_force()pushes out an active iclog and consumes the roundoff space, theOk I was wondering about that when I was trying to figure out what all this ticket space stealing code was doing.
So in addition to fixing the theoretical overrun, I guess the performance fix here is that every time we write an iclog we might have to move the grant heads forward so that we always write a full log sector / log stripe unit? And since a CIL context might write a lot of iclogs, it's cheaper to make those grant adjustments to the CIL ticket (which already asked for enough space to handle the roundoffs) since the ticket only jumps in the hot path once when the ticket is ungranted?
Quoted 2 lines
If I got that right,
Reviewed-by Darrick J. Wong <[email protected]>
You got it right. :)
Thanks!
-Dave.
Signature · 2 lines
Dave Chinner [email protected]
PATCH v3 45/45 xfs: expanding delayed logging design with background material
In reply to Dave ChinnerFrom: Dave Chinner <[email protected]>
I wrote up a description of how transactions, space reservations and relogging work together in response to a question for background material on the delayed logging design. Add this to the existing document for ease of future reference.
--- .../xfs-delayed-logging-design.rst | 362 ++++++++++++++++-- 1 file changed, 323 insertions(+), 39 deletions(-)
Documentation/filesystems/xfs-delayed-logging-design.rst +323 -39
@@ -1,29 +1,315 @@ .. SPDX-License-Identifier: GPL-2.0 -========================== -XFS Delayed Logging Design -========================== - -Introduction to Re-logging in XFS -================================= - -XFS logging is a combination of logical and physical logging. Some objects, -such as inodes and dquots, are logged in logical format where the details -logged are made up of the changes to in-core structures rather than on-disk -structures. Other objects - typically buffers - have their physical changes -logged. The reason for these differences is to reduce the amount of log space -required for objects that are frequently logged. Some parts of inodes are more -frequently logged than others, and inodes are typically more frequently logged -than any other object (except maybe the superblock buffer) so keeping the -amount of metadata logged low is of prime importance. - -The reason that this is such a concern is that XFS allows multiple separate -modifications to a single object to be carried in the log at any given time. -This allows the log to avoid needing to flush each change to disk before -recording a new change to the object. XFS does this via a method called -"re-logging". Conceptually, this is quite simple - all it requires is that any -new change to the object is recorded with a *new copy* of all the existing -changes in the new transaction that is written to the log. +================== +XFS Logging Design +================== + +Preamble +======== + +This document describes the design and algorithms that the XFS journalling +subsystem is based on. While originally focussed on just the design of +the delayed logging extension introduced in 2010, it assumed the reader already +had a fair amount of in-depth knowledge about how XFS transactions are formed +and executed. It also largely omitted any details of how journal space +reservations are accounted for to ensure the operation of the logging subsystem +is guaranteed to be deadlock-free. + +Much of the original document is retained unmodified because it is still valid +and correct. It also allows the new background material to avoid long +descriptions for various algorithms because they are already well documented +in the original document (e.g. what "relogging" is and why it is needed). + +Hence we first start with an overview of transactions, followed by the way +transaction reservations are structured and accounted, +and then move into how we guarantee forwards progress for long running +transactions with finite initial reservations bounds. At this point we need +to explain how relogging works, and that is where the original document +started. + +Introduction +============ + +XFS uses Write Ahead Logging for ensuring changes to the filesystem metadata +are atomic and recoverable. For reasons of space and time efficiency, the +logging mechanisms are varied and complex, combining intents, logical and +physical logging mechanisms to provide the necessary recovery guarantees the +filesystem requires. + +Some objects, such as inodes and dquots, are logged in logical format where the +details logged are made up of the changes to in-core structures rather than +on-disk structures. Other objects - typically buffers - have their physical +changes logged. And long running atomic modifications have individual changes +chained together by intents, ensuring that journal recovery can restart and +finish an operation that was only partially done when the system stopped +functioning. + +The reason for these differences is to keep the amount of log space and CPU time +required to process objects being modified as small as possible and hence the +logging overhead as low as possible. Some items are very frequently modified, +and some parts of objects are more frequently modified than others, so so +keeping the overhead of metadata logging low is of prime importance. + +The method used to log an item or chain modifications together isn't +particularly important in the scope of this document. It suffices to know that +the method used for logging a particular object or chaining modifications +together are different and are dependent on the object and/or modification being +performed. The logging subsystem only cares that certain specific rules are +followed to guarantee forwards progress and prevent deadlocks. + + +Transactions in XFS +=================== + +XFS has two types of high level transactions, defined by the type of log space +reservation they take. These are known as "one shot" and "permanent" +transactions. Permanent transaction reservations can be used for one-shot +transactions, but one-shot reservations cannot be used for permanent +transactions. Reservations must be matched to the modification taking place. + +In the code, a one-shot transaction pattern looks somewhat like this:: + + tp = xfs_trans_alloc(<reservation>) + <lock items> + <do modification> + xfs_trans_commit(tp); + +As items are modified in the transaction, the dirty regions in those items are +Once the transaction is committed, all resources joined to it are released, +along with the remaining unused reservation space that was taken at the +transaction allocation time. + +In contrast, a permanent transaction is made up of multiple linked individual +transactions, and the pattern looks like this:: + + tp = xfs_trans_alloc(<reservation>) + xfs_ilock(ip, XFS_ILOCK_EXCL) + + loop { + xfs_trans_ijoin(tp, 0); + <do modification> + xfs_trans_log_inode(tp, ip); + xfs_trans_roll(&tp); + } + + xfs_trans_commit(tp); + xfs_iunlock(ip, XFS_ILOCK_EXCL); + +While this might look similar to a one-shot transaction, there is an important +difference: xfs_trans_roll() performs a specific operation that links two +transactions together:: + + ntp = xfs_trans_dup(tp); + xfs_trans_commit(tp); + xfs_log_reserve(ntp); + +This results in a series of "rolling transactions" where the inode is locked +across the entire chain of transactions. Hence while this series of rolling +transactions is running, nothing else can read from or write to the inode and +this provides a mechanism for complex changes to appear atomic from an external +observer's point of view. + +It is important to note that a series of rolling transactions in a permanent +transaction does not form an atomic change in the journal. While each +individual modification is atomic, the chain is *not atomic*. If we crash half +way through, then recovery will only replay up to the last transactional +modification the loop made that was committed to the journal. + + +Transactions are Asynchronous +============================= + +In XFS, all high level transactions are asynchronous by default. This means that +xfs_trans_commit() does not guarantee that the modification has been committed +to stable storage when it returns. Hence when a system crashes, not all the +completed transactions will be replayed during recovery. + +However, the logging subsystem does provide global ordering guarantees, such +that if a specific change is seen after recovery, all metadata modifications +that were committed prior to that change will also be seen. + +This affects long running permanent transactions in that it is not possible to +predict how much of a long running operation will actually be recovered because +there is no guarantee of how much of the operation reached stale storage. Hence +if a long running operation requires multiple transactions to fully complete, +the high level operation must use intents and defered operations to guarantee +recovery can complete the operation once the first modification reached the +journal. + +For single shot operations that need to reach stable storage immediately, or +ensuring that a long running permanent transaction is fully committed once it is +complete, we can explicitly tag a transaction as synchronous. This will trigger +a "log force" to flush the outstanding committed transactions to stable storage +in the journal and wait for that to complete. + +Synchronous transactions are rarely used, however, because they limit logging +throughput to the IO latency limitations of the underlying storage. Instead, we +tend to use log forces to ensure modifications are on stable storage only when +a user operation requires a synchronisation point to occur (e.g. fsync). + + +Transaction Reservations +======================== + +It has been mentioned a number of times now that the logging subsystem needs to +provide a forwards progress guarantee so that no modification ever stalls +because it can't be written to the journal due to a lack of space in the +journal. This is acheived by the transaction reservations that are made when +a transaction is first allocated. For permanent transactions, these reservations +are maintained as part of the transaction rolling mechanism. + +A transaction reservation provides a guarantee that there is physical log space +available to write the modification into the journal before we start making +modifications to objects and items. As such, the reservation needs to be large +enough to take into account the amount of metadata that the change might need to +log in the worst case. This means that if we are modifying a btree in the +transaction, we have to reserve enough space to record a full leaf-to-root split +of the btree. As such, the reservations are quite complex because we have to +take into account all the hidden changes that might occur. + +For example, a user data extent allocation involves allocating an extent from +free space, which modifies the free space trees. That's two btrees. Then +inserting the extent into the inode extent map requires modify another btree, +which might require mor allocation that modifies the free space btrees again. +Then we might have to update reverse mappings, which modifies yet another btree +which might require more space. And so on. Hence the amount of metadata that a +"simple" operation can modify can be quite large. + +This "worst case" calculation provides us with the static "unit reservation" +for the transaction that is calculated at mount time. We must guarantee that the +log has this much space available before the transaction is allowed to proceed +so that when we come to write the dirty metadata into the log we don't run out +of log space half way through the write. + +For one-shot transactions, a single unit space reservation is all that is +required for the transaction to proceed. For permanent transactions, however, we +also have a "log count" that affects the size of the reservation that is to be +made. + +While a permanent transaction can get by with a single unit of space +reservation, it is somewhat inefficient to do this as it requires the +transaction rolling mechanism to re-reserve space on every transaction roll. We +know from the implementation of the permanent transactions how many transaction +rolls are likely for the common modifications that need to be made. + +For example, and inode allocation is typically two transactions - one to +physically allocate a free inode chunk on disk, and another to allocate an inode +from an inode chunk that has free inodes in it. Hence for an inode allocation +transaction, we might set the reservation log count to a value of 2 to indicate +that the common/fast path transaction will commit two linked transactions in a +chain. Each time a permanent transaction rolls, it consumes an entire unit +reservation. + +Hence when the permanent transaction is first allocated, the log space +reservation is increases from a single unit reservation to multiple unit +reservations. That multiple is defined by the reservation log count, and this +means we can roll the transaction multiple times before we have to re-reserve +log space when we roll the transaction. This ensures that the common +modifications we make only need to reserve log space once. + +If the log count for a permanent transation reaches zero, then it needs to +re-reserve physical space in the log. This is somewhat complex, and requires +an understanding of how the log accounts for space that has been reserved. + + +Log Space Accounting +==================== + +The position in the log is typically referred to as a Log Sequence Number (LSN). +The log is circular, so the positions in the log are defined by the combination +of a cycle number - the number of times the log has been overwritten - and the +offset into the log. A LSN carries the cycle in the upper 32 bits and the +offset in the lower 32 bits. The offset is in units of "basic blocks" (512 +bytes). Hence we can do realtively simple LSN based math to keep track of +available space in the log. + +Log space accounting is done via a pair of constructs called "grant heads". The +position of the grant heads is an absolute value, so the amount of space +available in the log is defined by the distance between the position of the +grant head and the current log tail. That is, how much space can be +reserved/consumed before the grant heads would fully wrap the log and overtake +the tail position. + +The first grant head is the "reserve" head. This tracks the byte count of the +reservations currently held by active transactions. It is a purely in-memory +accounting of the space reservation and, as such, actually tracks byte offsets +into the log rather than basic blocks. Hence it technically isn't using LSNs to +represent the log position, but it is still treated like a split {cycle,offset} +tuple for the purposes of tracking reservation space. + +The reserve grant head is used to accurately account for exact transaction +reservations amounts and the exact byte count that modifications actually make +and need to write into the log. The reserve head is used to prevent new +transactions from taking new reservations when the head reaches the current +tail. It will block new reservations in a FIFO queue and as the log tail moves +forward it will wake them in order once sufficient space is available. This FIFO +mechanism ensures no transaction is starved of resources when log space +shortages occur. + +The other grant head is the "write" head. Unlike the reserve head, this grant +head contains an LSN and it tracks the physical space usage in the log. While +this might sound like it is accounting the same state as the reserve grant head +- and it mostly does track exactly the same location as the reserve grant head - +there are critical differences in behaviour between them that provides the +forwards progress guarantees that rolling permanent transactions require. + +These differences when a permanent transaction is rolled and the internal "log +count" reaches zero and the initial set of unit reservations have been +exhausted. At this point, we still require a log space reservation to continue +the next transaction in the sequeunce, but we have none remaining. We cannot +sleep during the transaction commit process waiting for new log space to become +available, as we may end up on the end of the FIFO queue and the items we have +locked while we sleep could end up pinning the tail of the log before there is +enough free space in the log to fulfil all of the pending reservations and +then wake up transaction commit in progress. + +To take a new reservation without sleeping requires us to be able to take a +reservation even if there is no reservation space currently available. That is, +we need to be able to *overcommit* the log reservation space. As has already +been detailed, we cannot overcommit physical log space. However, the reserve +grant head does not track physical space - it only accounts for the amount of +reservations we currently have outstanding. Hence if the reserve head passes +over the tail of the log all it means is that new reservations will be throttled +immediately and remain throttled until the log tail is moved forward far enough +to remove the overcommit and start taking new reservations. In other words, we +can overcommit the reserve head without violating the physical log head and tail +rules. + +As a result, permanent transactions only "regrant" reservation space during +xfs_trans_commit() calls, while the physical log space reservation - tracked by +the write head - is then reserved separately by a call to xfs_log_reserve() +after the commit completes. Once the commit completes, we can sleep waiting for +physical log space to be reserved from the write grant head, but only if one +critical rule has been observed:: + + Code using permanent reservations must always log the items they hold + locked across each transaction they roll in the chain. + +"Re-logging" the locked items on every transaction roll ensures that the items +the transaction chain is rolling are always relocated to the physical head of +the log and so do not pin the tail of the log. If a locked item pins the tail of +the log when we sleep on the write reservation, then we will deadlock the log as +we cannot take the locks needed to write back that item and move the tail of the +log forwards to free up write grant space. Re-logging the locked items avoids +this deadlock and guarantees that the log reservation we are making cannot +self-deadlock. + +If all rolling transactions obey this rule, then they can all make forwards +progress independently because nothing will block the progress of the log +tail moving forwards and hence ensuring that write grant space is always +(eventually) made available to permanent transactions no matter how many times +they roll. + + +Re-logging Explained +==================== + +XFS allows multiple separate modifications to a single object to be carried in +the log at any given time. This allows the log to avoid needing to flush each +change to disk before recording a new change to the object. XFS does this via a +method called "re-logging". Conceptually, this is quite simple - all it requires +is that any new change to the object is recorded with a *new copy* of all the +existing changes in the new transaction that is written to the log. That is, if we have a sequence of changes A through to F, and the object was written to disk after change D, we would see in the log the following series
@@ -42,16 +328,13 @@ transaction:: In other words, each time an object is relogged, the new transaction contains the aggregation of all the previous changes currently held only in the log. -This relogging technique also allows objects to be moved forward in the log so -that an object being relogged does not prevent the tail of the log from ever -moving forward. This can be seen in the table above by the changing -(increasing) LSN of each subsequent transaction - the LSN is effectively a -direct encoding of the location in the log of the transaction. +This relogging technique allows objects to be moved forward in the log so that +an object being relogged does not prevent the tail of the log from ever moving +forward. This can be seen in the table above by the changing (increasing) LSN +of each subsequent transaction, and it's the technique that allows us to +implement long-running, multiple-commit permanent transactions. -This relogging is also used to implement long-running, multiple-commit -transactions. These transaction are known as rolling transactions, and require -a special log reservation known as a permanent transaction reservation. A -typical example of a rolling transaction is the removal of extents from an +A typical example of a rolling transaction is the removal of extents from an inode which can only be done at a rate of two extents per transaction because of reservation size limitations. Hence a rolling extent removal transaction keeps relogging the inode and btree buffers as they get modified in each
@@ -67,12 +350,13 @@ the log over and over again. Worse is the fact that objects tend to get dirtier as they get relogged, so each subsequent transaction is writing more metadata into the log. -Another feature of the XFS transaction subsystem is that most transactions are -asynchronous. That is, they don't commit to disk until either a log buffer is -filled (a log buffer can hold multiple transactions) or a synchronous operation -forces the log buffers holding the transactions to disk. This means that XFS is -doing aggregation of transactions in memory - batching them, if you like - to -minimise the impact of the log IO on transaction throughput. +It should now also be obvious how relogging and asynchronous transactions go +hand in hand. That is, transactions don't get written to the physical journal +until either a log buffer is filled (a log buffer can hold multiple +transactions) or a synchronous operation forces the log buffers holding the +transactions to disk. This means that XFS is doing aggregation of transactions +in memory - batching them, if you like - to minimise the impact of the log IO on +transaction throughput. The limitation on asynchronous transaction throughput is the number and size of log buffers made available by the log manager. By default there are 8 log
Signature · 1 line
2.28.0
Re: [PATCH 45/45] xfs: expanding delayed logging design with background material
Quoted 52 lines · Dave Chinner
From: Dave Chinner <[email protected]>
I wrote up a description of how transactions, space reservations and relogging work together in response to a question for background material on the delayed logging design. Add this to the existing document for ease of future reference.
Signed-off-by Dave Chinner <[email protected]>--- .../xfs-delayed-logging-design.rst | 362 ++++++++++++++++-- 1 file changed, 323 insertions(+), 39 deletions(-)Documentation/filesystems/xfs-delayed-logging-design.rst +9
-24@@ -1,29 +1,315 @@ .. SPDX-License-Identifier: GPL-2.0 -========================== -XFS Delayed Logging Design -========================== - -Introduction to Re-logging in XFS -================================= - -XFS logging is a combination of logical and physical logging. Some objects, -such as inodes and dquots, are logged in logical format where the details -logged are made up of the changes to in-core structures rather than on-disk -structures. Other objects - typically buffers - have their physical changes -logged. The reason for these differences is to reduce the amount of log space -required for objects that are frequently logged. Some parts of inodes are more -frequently logged than others, and inodes are typically more frequently logged -than any other object (except maybe the superblock buffer) so keeping the -amount of metadata logged low is of prime importance. - -The reason that this is such a concern is that XFS allows multiple separate -modifications to a single object to be carried in the log at any given time. -This allows the log to avoid needing to flush each change to disk before -recording a new change to the object. XFS does this via a method called -"re-logging". Conceptually, this is quite simple - all it requires is that any -new change to the object is recorded with a *new copy* of all the existing -changes in the new transaction that is written to the log. +================== +XFS Logging Design +================== + +Preamble +======== + +This document describes the design and algorithms that the XFS journalling +subsystem is based on. While originally focussed on just the design of
Documentation/filesystems/xfs-delayed-logging-design.rst line 21
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+the delayed logging extension introduced in 2010, it assumed the reader already +had a fair amount of in-depth knowledge about how XFS transactions are formed +and executed. It also largely omitted any details of how journal space +reservations are accounted for to ensure the operation of the logging subsystem +is guaranteed to be deadlock-free. + +Much of the original document is retained unmodified because it is still valid +and correct. It also allows the new background material to avoid long +descriptions for various algorithms because they are already well documented +in the original document (e.g. what "relogging" is and why it is needed).
I wonder how useful is this synopsis of this document's history? When I'm reading a piece of documentation I really only care about what's in the document I'm reading now, not what it looked like in 2010. I think you could shorten the preamble by deleting everything between the second sentence and the last paragraph:
"This document describes the design and algorithms that the XFS journalling subsystem is based on. Readers may wish to familiarize themselves with the general concepts of how transaction processing works. We begin with an overview of transactions in XFS, followed by the way transaction reservations..."
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+Hence we first start with an overview of transactions, followed by the way +transaction reservations are structured and accounted, +and then move into how we guarantee forwards progress for long running +transactions with finite initial reservations bounds. At this point we need +to explain how relogging works, and that is where the original document +started. + +Introduction +============ +
Documentation/filesystems/xfs-delayed-logging-design.rst line 42
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+XFS uses Write Ahead Logging for ensuring changes to the filesystem metadata +are atomic and recoverable. For reasons of space and time efficiency, the +logging mechanisms are varied and complex, combining intents, logical and +physical logging mechanisms to provide the necessary recovery guarantees the +filesystem requires. + +Some objects, such as inodes and dquots, are logged in logical format where the +details logged are made up of the changes to in-core structures rather than +on-disk structures. Other objects - typically buffers - have their physical +changes logged. And long running atomic modifications have individual changes
No need for the 'And' at the start of the sentence.
Documentation/filesystems/xfs-delayed-logging-design.rst line 50
Quoted 8 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +8
-0+chained together by intents, ensuring that journal recovery can restart and +finish an operation that was only partially done when the system stopped +functioning. + +The reason for these differences is to keep the amount of log space and CPU time +required to process objects being modified as small as possible and hence the +logging overhead as low as possible. Some items are very frequently modified, +and some parts of objects are more frequently modified than others, so so
Double "so".
Documentation/filesystems/xfs-delayed-logging-design.rst line 58
Quoted 8 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +8
-0+keeping the overhead of metadata logging low is of prime importance. + +The method used to log an item or chain modifications together isn't +particularly important in the scope of this document. It suffices to know that +the method used for logging a particular object or chaining modifications +together are different and are dependent on the object and/or modification being +performed. The logging subsystem only cares that certain specific rules are +followed to guarantee forwards progress and prevent deadlocks.
(Aww, maybe we /should/ document how we choose between intents, logical updates, and physical updates. But that can come in a later patch.)
Documentation/filesystems/xfs-delayed-logging-design.rst line 65
Quoted 7 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +7
-0+ + +Transactions in XFS +=================== + +XFS has two types of high level transactions, defined by the type of log space +reservation they take. These are known as "one shot" and "permanent"
(Ugh, I wish we could call them 'compound' or 'rolling' transactions....)
Documentation/filesystems/xfs-delayed-logging-design.rst line 65
Quoted 1 line
Documentation/filesystems/xfs-delayed-logging-design.rst +1
-0+transactions.
It might be a good idea to introduce the idea that a permanent transaction has the ability to reserve space across commit boundaries.
Documentation/filesystems/xfs-delayed-logging-design.rst line 68
Quoted 3 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +3
-0+Permanent transaction reservations can be used for one-shot +transactions, but one-shot reservations cannot be used for permanent +transactions. Reservations must be matched to the modification taking place.
"The size and type of reservation must be matched..."
Documentation/filesystems/xfs-delayed-logging-design.rst line 74
Quoted 6 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +6
-0+ +In the code, a one-shot transaction pattern looks somewhat like this:: + + tp = xfs_trans_alloc(<reservation>) + <lock items> + <do modification>
Indenting is not consistent here.
Documentation/filesystems/xfs-delayed-logging-design.rst line 77
Quoted 3 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +3
-0+ xfs_trans_commit(tp); + +As items are modified in the transaction, the dirty regions in those items are
...are what? I think this sentence got truncated.
Quoted 29 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +29
-0+Once the transaction is committed, all resources joined to it are released, +along with the remaining unused reservation space that was taken at the +transaction allocation time. + +In contrast, a permanent transaction is made up of multiple linked individual +transactions, and the pattern looks like this:: + + tp = xfs_trans_alloc(<reservation>) + xfs_ilock(ip, XFS_ILOCK_EXCL) + + loop { + xfs_trans_ijoin(tp, 0); + <do modification> + xfs_trans_log_inode(tp, ip); + xfs_trans_roll(&tp); + } + + xfs_trans_commit(tp); + xfs_iunlock(ip, XFS_ILOCK_EXCL); + +While this might look similar to a one-shot transaction, there is an important +difference: xfs_trans_roll() performs a specific operation that links two +transactions together:: + + ntp = xfs_trans_dup(tp); + xfs_trans_commit(tp); + xfs_log_reserve(ntp); + +This results in a series of "rolling transactions" where the inode is locked
Documentation/filesystems/xfs-delayed-logging-design.rst line 116
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+across the entire chain of transactions. Hence while this series of rolling +transactions is running, nothing else can read from or write to the inode and +this provides a mechanism for complex changes to appear atomic from an external +observer's point of view. + +It is important to note that a series of rolling transactions in a permanent +transaction does not form an atomic change in the journal. While each +individual modification is atomic, the chain is *not atomic*. If we crash half +way through, then recovery will only replay up to the last transactional +modification the loop made that was committed to the journal.
Can you add:
"Log intent items are used to track the progress of and to restart metadata updates that span multiple transactions."
Or maybe just hoist the third paragraph of the next section into a separate section introducing log intents?
Quoted 9 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +9
-0+ + +Transactions are Asynchronous +============================= + +In XFS, all high level transactions are asynchronous by default. This means that +xfs_trans_commit() does not guarantee that the modification has been committed +to stable storage when it returns. Hence when a system crashes, not all the +completed transactions will be replayed during recovery.
Documentation/filesystems/xfs-delayed-logging-design.rst line 135
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+ +However, the logging subsystem does provide global ordering guarantees, such +that if a specific change is seen after recovery, all metadata modifications +that were committed prior to that change will also be seen. + +This affects long running permanent transactions in that it is not possible to +predict how much of a long running operation will actually be recovered because +there is no guarantee of how much of the operation reached stale storage. Hence +if a long running operation requires multiple transactions to fully complete, +the high level operation must use intents and defered operations to guarantee
"deferred"
Documentation/filesystems/xfs-delayed-logging-design.rst line 137
Quoted 2 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +2
-0+recovery can complete the operation once the first modification reached the +journal.
"...once the first transaction is persisted in the on-disk journal."
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+ +For single shot operations that need to reach stable storage immediately, or +ensuring that a long running permanent transaction is fully committed once it is +complete, we can explicitly tag a transaction as synchronous. This will trigger +a "log force" to flush the outstanding committed transactions to stable storage +in the journal and wait for that to complete. + +Synchronous transactions are rarely used, however, because they limit logging +throughput to the IO latency limitations of the underlying storage. Instead, we +tend to use log forces to ensure modifications are on stable storage only when
Documentation/filesystems/xfs-delayed-logging-design.rst line 157
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+a user operation requires a synchronisation point to occur (e.g. fsync). + + +Transaction Reservations +======================== + +It has been mentioned a number of times now that the logging subsystem needs to +provide a forwards progress guarantee so that no modification ever stalls +because it can't be written to the journal due to a lack of space in the +journal. This is acheived by the transaction reservations that are made when
"achieved"
Quoted 6 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +6
-0+a transaction is first allocated. For permanent transactions, these reservations +are maintained as part of the transaction rolling mechanism. + +A transaction reservation provides a guarantee that there is physical log space +available to write the modification into the journal before we start making +modifications to objects and items. As such, the reservation needs to be large
Documentation/filesystems/xfs-delayed-logging-design.rst line 173
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+enough to take into account the amount of metadata that the change might need to +log in the worst case. This means that if we are modifying a btree in the +transaction, we have to reserve enough space to record a full leaf-to-root split +of the btree. As such, the reservations are quite complex because we have to +take into account all the hidden changes that might occur. + +For example, a user data extent allocation involves allocating an extent from +free space, which modifies the free space trees. That's two btrees. Then +inserting the extent into the inode extent map requires modify another btree, +which might require mor allocation that modifies the free space btrees again.
"Inserting the extent into the inode's extent map might require a split of the extent map btree, which requires another allocation that can modify the free space btrees again."
Quoted 27 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +27
-0+Then we might have to update reverse mappings, which modifies yet another btree +which might require more space. And so on. Hence the amount of metadata that a +"simple" operation can modify can be quite large. + +This "worst case" calculation provides us with the static "unit reservation" +for the transaction that is calculated at mount time. We must guarantee that the +log has this much space available before the transaction is allowed to proceed +so that when we come to write the dirty metadata into the log we don't run out +of log space half way through the write. + +For one-shot transactions, a single unit space reservation is all that is +required for the transaction to proceed. For permanent transactions, however, we +also have a "log count" that affects the size of the reservation that is to be +made. + +While a permanent transaction can get by with a single unit of space +reservation, it is somewhat inefficient to do this as it requires the +transaction rolling mechanism to re-reserve space on every transaction roll. We +know from the implementation of the permanent transactions how many transaction +rolls are likely for the common modifications that need to be made. + +For example, and inode allocation is typically two transactions - one to +physically allocate a free inode chunk on disk, and another to allocate an inode +from an inode chunk that has free inodes in it. Hence for an inode allocation +transaction, we might set the reservation log count to a value of 2 to indicate +that the common/fast path transaction will commit two linked transactions in a +chain. Each time a permanent transaction rolls, it consumes an entire unit
Documentation/filesystems/xfs-delayed-logging-design.rst line 210
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+reservation. + +Hence when the permanent transaction is first allocated, the log space +reservation is increases from a single unit reservation to multiple unit +reservations. That multiple is defined by the reservation log count, and this +means we can roll the transaction multiple times before we have to re-reserve +log space when we roll the transaction. This ensures that the common +modifications we make only need to reserve log space once. + +If the log count for a permanent transation reaches zero, then it needs to
"transaction"
Quoted 18 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +18
-0+re-reserve physical space in the log. This is somewhat complex, and requires +an understanding of how the log accounts for space that has been reserved. + + +Log Space Accounting +==================== + +The position in the log is typically referred to as a Log Sequence Number (LSN). +The log is circular, so the positions in the log are defined by the combination +of a cycle number - the number of times the log has been overwritten - and the +offset into the log. A LSN carries the cycle in the upper 32 bits and the +offset in the lower 32 bits. The offset is in units of "basic blocks" (512 +bytes). Hence we can do realtively simple LSN based math to keep track of +available space in the log. + +Log space accounting is done via a pair of constructs called "grant heads". The +position of the grant heads is an absolute value, so the amount of space +available in the log is defined by the distance between the position of the
Documentation/filesystems/xfs-delayed-logging-design.rst line 238
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+grant head and the current log tail. That is, how much space can be +reserved/consumed before the grant heads would fully wrap the log and overtake +the tail position. + +The first grant head is the "reserve" head. This tracks the byte count of the +reservations currently held by active transactions. It is a purely in-memory +accounting of the space reservation and, as such, actually tracks byte offsets +into the log rather than basic blocks. Hence it technically isn't using LSNs to +represent the log position, but it is still treated like a split {cycle,offset} +tuple for the purposes of tracking reservation space.
Lol, the grant head is delalloc for transactions.
Quoted 28 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +28
-0+ +The reserve grant head is used to accurately account for exact transaction +reservations amounts and the exact byte count that modifications actually make +and need to write into the log. The reserve head is used to prevent new +transactions from taking new reservations when the head reaches the current +tail. It will block new reservations in a FIFO queue and as the log tail moves +forward it will wake them in order once sufficient space is available. This FIFO +mechanism ensures no transaction is starved of resources when log space +shortages occur. + +The other grant head is the "write" head. Unlike the reserve head, this grant +head contains an LSN and it tracks the physical space usage in the log. While +this might sound like it is accounting the same state as the reserve grant head +- and it mostly does track exactly the same location as the reserve grant head - +there are critical differences in behaviour between them that provides the +forwards progress guarantees that rolling permanent transactions require. + +These differences when a permanent transaction is rolled and the internal "log +count" reaches zero and the initial set of unit reservations have been +exhausted. At this point, we still require a log space reservation to continue +the next transaction in the sequeunce, but we have none remaining. We cannot +sleep during the transaction commit process waiting for new log space to become +available, as we may end up on the end of the FIFO queue and the items we have +locked while we sleep could end up pinning the tail of the log before there is +enough free space in the log to fulfil all of the pending reservations and +then wake up transaction commit in progress. + +To take a new reservation without sleeping requires us to be able to take a
Documentation/filesystems/xfs-delayed-logging-design.rst line 276
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +10
-0+reservation even if there is no reservation space currently available. That is, +we need to be able to *overcommit* the log reservation space. As has already +been detailed, we cannot overcommit physical log space. However, the reserve +grant head does not track physical space - it only accounts for the amount of +reservations we currently have outstanding. Hence if the reserve head passes +over the tail of the log all it means is that new reservations will be throttled +immediately and remain throttled until the log tail is moved forward far enough +to remove the overcommit and start taking new reservations. In other words, we +can overcommit the reserve head without violating the physical log head and tail +rules.
I hadn't fully realized this. Good information. :)
Quoted 68 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +41
-12+As a result, permanent transactions only "regrant" reservation space during +xfs_trans_commit() calls, while the physical log space reservation - tracked by +the write head - is then reserved separately by a call to xfs_log_reserve() +after the commit completes. Once the commit completes, we can sleep waiting for +physical log space to be reserved from the write grant head, but only if one +critical rule has been observed:: + + Code using permanent reservations must always log the items they hold + locked across each transaction they roll in the chain. + +"Re-logging" the locked items on every transaction roll ensures that the items +the transaction chain is rolling are always relocated to the physical head of +the log and so do not pin the tail of the log. If a locked item pins the tail of +the log when we sleep on the write reservation, then we will deadlock the log as +we cannot take the locks needed to write back that item and move the tail of the +log forwards to free up write grant space. Re-logging the locked items avoids +this deadlock and guarantees that the log reservation we are making cannot +self-deadlock. + +If all rolling transactions obey this rule, then they can all make forwards +progress independently because nothing will block the progress of the log +tail moving forwards and hence ensuring that write grant space is always +(eventually) made available to permanent transactions no matter how many times +they roll. + + +Re-logging Explained +==================== + +XFS allows multiple separate modifications to a single object to be carried in +the log at any given time. This allows the log to avoid needing to flush each +change to disk before recording a new change to the object. XFS does this via a +method called "re-logging". Conceptually, this is quite simple - all it requires +is that any new change to the object is recorded with a *new copy* of all the +existing changes in the new transaction that is written to the log. That is, if we have a sequence of changes A through to F, and the object was written to disk after change D, we would see in the log the following series@@ -42,16 +328,13 @@ transaction:: In other words, each time an object is relogged, the new transaction contains the aggregation of all the previous changes currently held only in the log. -This relogging technique also allows objects to be moved forward in the log so -that an object being relogged does not prevent the tail of the log from ever -moving forward. This can be seen in the table above by the changing -(increasing) LSN of each subsequent transaction - the LSN is effectively a -direct encoding of the location in the log of the transaction. +This relogging technique allows objects to be moved forward in the log so that +an object being relogged does not prevent the tail of the log from ever moving +forward. This can be seen in the table above by the changing (increasing) LSN +of each subsequent transaction, and it's the technique that allows us to +implement long-running, multiple-commit permanent transactions. -This relogging is also used to implement long-running, multiple-commit -transactions. These transaction are known as rolling transactions, and require -a special log reservation known as a permanent transaction reservation. A -typical example of a rolling transaction is the removal of extents from an +A typical example of a rolling transaction is the removal of extents from an inode which can only be done at a rate of two extents per transaction because of reservation size limitations. Hence a rolling extent removal transaction keeps relogging the inode and btree buffers as they get modified in each@@ -67,12 +350,13 @@ the log over and over again. Worse is the fact that objects tend to get dirtier as they get relogged, so each subsequent transaction is writing more metadata into the log. -Another feature of the XFS transaction subsystem is that most transactions are -asynchronous. That is, they don't commit to disk until either a log buffer is -filled (a log buffer can hold multiple transactions) or a synchronous operation
Documentation/filesystems/xfs-delayed-logging-design.rst the log over and over again. Worse is the fact that objects tend to get · line 359
Quoted 10 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +7
-3-forces the log buffers holding the transactions to disk. This means that XFS is -doing aggregation of transactions in memory - batching them, if you like - to -minimise the impact of the log IO on transaction throughput. +It should now also be obvious how relogging and asynchronous transactions go +hand in hand. That is, transactions don't get written to the physical journal +until either a log buffer is filled (a log buffer can hold multiple +transactions) or a synchronous operation forces the log buffers holding the +transactions to disk. This means that XFS is doing aggregation of transactions +in memory - batching them, if you like - to minimise the impact of the log IO on +transaction throughput.
...microtransaction fusion, yippee!
--D
Quoted 6 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +0
-0The limitation on asynchronous transaction throughput is the number and size of log buffers made available by the log manager. By default there are 8 logSignature · 1 line
2.28.0
Re: [PATCH 45/45] xfs: expanding delayed logging design with background material
Quoted 10 lines · Darrick J. Wong
Documentation/filesystems/xfs-delayed-logging-design.rst line 58
Quoted 6 lines · Dave Chinner
Documentation/filesystems/xfs-delayed-logging-design.rst +6
-0+The method used to log an item or chain modifications together isn't +particularly important in the scope of this document. It suffices to know that +the method used for logging a particular object or chaining modifications +together are different and are dependent on the object and/or modification being +performed. The logging subsystem only cares that certain specific rules are +followed to guarantee forwards progress and prevent deadlocks.(Aww, maybe we /should/ document how we choose between intents, logical updates, and physical updates. But that can come in a later patch.)
*nod*
Quoted 8 lines
Documentation/filesystems/xfs-delayed-logging-design.rst line 65
Quoted 5 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +5
-0+Transactions in XFS +=================== + +XFS has two types of high level transactions, defined by the type of log space +reservation they take. These are known as "one shot" and "permanent"(Ugh, I wish we could call them 'compound' or 'rolling' transactions....)
I guess that's a set of followup patches at some point. Calling them 'rolling transactions' everywhere would certainly be an improvement.
Quoted 18 lines
Quoted 18 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +18
-0+Log Space Accounting +==================== + +The position in the log is typically referred to as a Log Sequence Number (LSN). +The log is circular, so the positions in the log are defined by the combination +of a cycle number - the number of times the log has been overwritten - and the +offset into the log. A LSN carries the cycle in the upper 32 bits and the +offset in the lower 32 bits. The offset is in units of "basic blocks" (512 +bytes). Hence we can do realtively simple LSN based math to keep track of +available space in the log. + +Log space accounting is done via a pair of constructs called "grant heads". The +position of the grant heads is an absolute value, so the amount of space +available in the log is defined by the distance between the position of the +grant head and the current log tail. That is, how much space can be +reserved/consumed before the grant heads would fully wrap the log and overtake +the tail position. +
Quoted 8 lines
Documentation/filesystems/xfs-delayed-logging-design.rst line 238
Quoted 6 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +6
-0+The first grant head is the "reserve" head. This tracks the byte count of the +reservations currently held by active transactions. It is a purely in-memory +accounting of the space reservation and, as such, actually tracks byte offsets +into the log rather than basic blocks. Hence it technically isn't using LSNs to +represent the log position, but it is still treated like a split {cycle,offset} +tuple for the purposes of tracking reservation space.Lol, the grant head is delalloc for transactions.
Yes, in effect that's exactly what it does. The log is at ENOSPC when we run out of reservation space, and the AIL is the garbage collector that reclaims used space....
Quoted 4 lines
Quoted 4 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +0
-0@@ -67,12 +350,13 @@ the log over and over again. Worse is the fact that objects tend to get dirtier as they get relogged, so each subsequent transaction is writing more metadata into the log.
Quoted 15 lines
Documentation/filesystems/xfs-delayed-logging-design.rst the log over and over again. Worse is the fact that objects tend to get · line 359
Quoted 13 lines
Documentation/filesystems/xfs-delayed-logging-design.rst +7
-6-Another feature of the XFS transaction subsystem is that most transactions are -asynchronous. That is, they don't commit to disk until either a log buffer is -filled (a log buffer can hold multiple transactions) or a synchronous operation -forces the log buffers holding the transactions to disk. This means that XFS is -doing aggregation of transactions in memory - batching them, if you like - to -minimise the impact of the log IO on transaction throughput. +It should now also be obvious how relogging and asynchronous transactions go +hand in hand. That is, transactions don't get written to the physical journal +until either a log buffer is filled (a log buffer can hold multiple +transactions) or a synchronous operation forces the log buffers holding the +transactions to disk. This means that XFS is doing aggregation of transactions +in memory - batching them, if you like - to minimise the impact of the log IO on +transaction throughput....microtransaction fusion, yippee!
I'll have to remember that next time I play algorithm buzzword bingo... :P
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]