Skip to contentSkip to conversation

f2fs: fix long latency due to discard during umount

PATCH Superseded Sahitya Tummala 3 participants 3 messages Apr 15, 2020 F2FS file system

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 f2fs: fix long latency due to discard during umount

F2FS already has a default timeout of 5 secs for discards that can be issued during umount, but it can take more than the 5 sec timeout if the underlying UFS device queue is already full and there are no more available free tags to be used. Fix this by submitting a small batch of discard requests so that it won't cause the device queue to be full at any time and thus doesn't incur its wait time in the umount context.

---
v4:
-Implement as per Jaegeuk's Suggestion to control the number of outstanding discard requests.

 fs/f2fs/segment.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/f2fs/segment.c +8 -2
index 1a62b27..764bab5 100644
@@ -1099,7 +1099,6 @@ static void __init_discard_policy(struct f2fs_sb_info *sbi,
 	} else if (discard_type == DPOLICY_FSTRIM) {
 		dpolicy->io_aware = false;
 	} else if (discard_type == DPOLICY_UMOUNT) {
-		dpolicy->max_requests = UINT_MAX;
 		dpolicy->io_aware = false;
 		/* we need to issue all to keep CP_TRIMMED_FLAG */
 		dpolicy->granularity = 1;
@@ -1470,12 +1469,14 @@ static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
 	struct list_head *pend_list;
 	struct discard_cmd *dc, *tmp;
 	struct blk_plug plug;
-	int i, issued = 0;
+	int i, issued;
 	bool io_interrupted = false;
 
 	if (dpolicy->timeout != 0)
 		f2fs_update_time(sbi, dpolicy->timeout);
 
+retry:
+	issued = 0;
 	for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
 		if (dpolicy->timeout != 0 &&
 				f2fs_time_over(sbi, dpolicy->timeout))
@@ -1522,6 +1523,11 @@ static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
 			break;
 	}
 
+	if (dpolicy->type == DPOLICY_UMOUNT && issued) {
+		__wait_all_discard_cmd(sbi, dpolicy);
+		goto retry;
+	}
+
 	if (!issued && io_interrupted)
 		issued = -1;
 
Signature · 8 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.


_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Reviewed-by on v4 1/1
#1 lore raw
Quoted 9 lines · Sahitya Tummala

F2FS already has a default timeout of 5 secs for discards that can be issued during umount, but it can take more than the 5 sec timeout if the underlying UFS device queue is already full and there are no more available free tags to be used. Fix this by submitting a small batch of discard requests so that it won't cause the device queue to be full at any time and thus doesn't incur its wait time in the umount context.

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#2 lore raw
In reply to Sahitya Tummala
Thank you for the patch! Yet something to improve:

Hi Sahitya,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on v5.6]
[also build test ERROR on next-20200414]
[cannot apply to f2fs/dev-test v5.7-rc1]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Sahitya-Tummala/f2fs-fix-long-latency-due-to-discard-during-umount/20200415-115539
base:    7111951b8d4973bda27ff663f2cf18b663d15b48
config: nios2-allyesconfig (attached as .config)
compiler: nios2-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=9.3.0 make.cross ARCH=nios2

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

fs/f2fs/segment.c: In function '__issue_discard_cmd':

>> fs/f2fs/segment.c:1525:3: error: implicit declaration of function '__wait_all_discard_cmd'; did you mean '__attach_discard_cmd'? [-Werror=implicit-function-declaration]
    1525 |   __wait_all_discard_cmd(sbi, dpolicy);
         |   ^~~~~~~~~~~~~~~~~~~~~~
         |   __attach_discard_cmd
   fs/f2fs/segment.c: At top level:
>> fs/f2fs/segment.c:1623:21: error: conflicting types for '__wait_all_discard_cmd'
    1623 | static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
         |                     ^~~~~~~~~~~~~~~~~~~~~~
   fs/f2fs/segment.c:1525:3: note: previous implicit declaration of '__wait_all_discard_cmd' was here
    1525 |   __wait_all_discard_cmd(sbi, dpolicy);
         |   ^~~~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

vim +1525 fs/f2fs/segment.c

1462
1463 static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1464 struct discard_policy *dpolicy)
1465 {
1466 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1467 struct list_head *pend_list;
1468 struct discard_cmd *dc, *tmp;
1469 struct blk_plug plug;
1470 int i, issued;
1471 bool io_interrupted = false;
1472
1473 if (dpolicy->timeout != 0)
1474 f2fs_update_time(sbi, dpolicy->timeout);
1475
1476 retry:
1477 issued = 0;
1478 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1479 if (dpolicy->timeout != 0 &&
1480 f2fs_time_over(sbi, dpolicy->timeout))
1481 break;
1482
1483 if (i + 1 < dpolicy->granularity)
1484 break;
1485
1486 if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
1487 return __issue_discard_cmd_orderly(sbi, dpolicy);
1488
1489 pend_list = &dcc->pend_list[i];
1490
1491 mutex_lock(&dcc->cmd_lock);
1492 if (list_empty(pend_list))
1493 goto next;
1494 if (unlikely(dcc->rbtree_check))
1495 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
1496 &dcc->root));
1497 blk_start_plug(&plug);
1498 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1499 f2fs_bug_on(sbi, dc->state != D_PREP);
1500
1501 if (dpolicy->timeout != 0 &&
1502 f2fs_time_over(sbi, dpolicy->timeout))
1503 break;
1504
1505 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
1506 !is_idle(sbi, DISCARD_TIME)) {
1507 io_interrupted = true;
1508 break;
1509 }
1510
1511 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
1512
1513 if (issued >= dpolicy->max_requests)
1514 break;
1515 }
1516 blk_finish_plug(&plug);
1517 next:
1518 mutex_unlock(&dcc->cmd_lock);
1519
1520 if (issued >= dpolicy->max_requests || io_interrupted)
1521 break;
1522 }
1523
1524 if (dpolicy->type == DPOLICY_UMOUNT && issued) {

> 1525			__wait_all_discard_cmd(sbi, dpolicy);
  1526			goto retry;
  1527		}
  1528	
  1529		if (!issued && io_interrupted)
  1530			issued = -1;
  1531	
  1532		return issued;
  1533	}
  1534	
  1535	static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
  1536	{
  1537		struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1538		struct list_head *pend_list;
  1539		struct discard_cmd *dc, *tmp;
  1540		int i;
  1541		bool dropped = false;
  1542	
  1543		mutex_lock(&dcc->cmd_lock);
  1544		for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
  1545			pend_list = &dcc->pend_list[i];
  1546			list_for_each_entry_safe(dc, tmp, pend_list, list) {
  1547				f2fs_bug_on(sbi, dc->state != D_PREP);
  1548				__remove_discard_cmd(sbi, dc);
  1549				dropped = true;
  1550			}
  1551		}
  1552		mutex_unlock(&dcc->cmd_lock);
  1553	
  1554		return dropped;
  1555	}
  1556	
  1557	void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
  1558	{
  1559		__drop_discard_cmd(sbi);
  1560	}
  1561	
  1562	static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
  1563								struct discard_cmd *dc)
  1564	{
  1565		struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1566		unsigned int len = 0;
  1567	
  1568		wait_for_completion_io(&dc->wait);
  1569		mutex_lock(&dcc->cmd_lock);
  1570		f2fs_bug_on(sbi, dc->state != D_DONE);
  1571		dc->ref--;
  1572		if (!dc->ref) {
  1573			if (!dc->error)
  1574				len = dc->len;
  1575			__remove_discard_cmd(sbi, dc);
  1576		}
  1577		mutex_unlock(&dcc->cmd_lock);
  1578	
  1579		return len;
  1580	}
  1581	
  1582	static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
  1583							struct discard_policy *dpolicy,
  1584							block_t start, block_t end)
  1585	{
  1586		struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1587		struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
  1588						&(dcc->fstrim_list) : &(dcc->wait_list);
  1589		struct discard_cmd *dc, *tmp;
  1590		bool need_wait;
  1591		unsigned int trimmed = 0;
  1592	
  1593	next:
  1594		need_wait = false;
  1595	
  1596		mutex_lock(&dcc->cmd_lock);
  1597		list_for_each_entry_safe(dc, tmp, wait_list, list) {
  1598			if (dc->lstart + dc->len <= start || end <= dc->lstart)
  1599				continue;
  1600			if (dc->len < dpolicy->granularity)
  1601				continue;
  1602			if (dc->state == D_DONE && !dc->ref) {
  1603				wait_for_completion_io(&dc->wait);
  1604				if (!dc->error)
  1605					trimmed += dc->len;
  1606				__remove_discard_cmd(sbi, dc);
  1607			} else {
  1608				dc->ref++;
  1609				need_wait = true;
  1610				break;
  1611			}
  1612		}
  1613		mutex_unlock(&dcc->cmd_lock);
  1614	
  1615		if (need_wait) {
  1616			trimmed += __wait_one_discard_bio(sbi, dc);
  1617			goto next;
  1618		}
  1619	
  1620		return trimmed;
  1621	}
  1622	
> 1623	static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,

1624 struct discard_policy *dpolicy)
1625 {
1626 struct discard_policy dp;
1627 unsigned int discard_blks;
1628
1629 if (dpolicy)
1630 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
1631
1632 /* wait all */
1633 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
1634 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1635 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
1636 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1637
1638 return discard_blks;
1639 }
1640

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org