Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH] RDMA/core: Reduce cond_resched() frequency in __ib_umem_release
@ 2025-11-26  2:51 lirongqing
  2025-11-26  8:15 ` Leon Romanovsky
  2025-11-26  8:15 ` Leon Romanovsky
  0 siblings, 2 replies; 3+ messages in thread
From: lirongqing @ 2025-11-26  2:51 UTC (permalink / raw)
  To: Jason Gunthorpe, Leon Romanovsky, huangjunxian6, linux-rdma; +Cc: Li RongQing

From: Li RongQing <lirongqing@baidu.com>

The current implementation calls cond_resched() for every SG entry
in __ib_umem_release(), which can increase needless overhead.

This patch introduces RESCHED_LOOP_CNT_THRESHOLD (0x1000) to limit
how often cond_resched() is called. The function now yields the CPU
once every 4096 iterations, and yield at the very first iteration
for lots of small umem case, to reduce scheduling overhead.

Signed-off-by: Li RongQing <lirongqing@baidu.com>
---
 drivers/infiniband/core/umem.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c
index 8fd84aa..ff540a2 100644
--- a/drivers/infiniband/core/umem.c
+++ b/drivers/infiniband/core/umem.c
@@ -45,6 +45,8 @@
 
 #include "uverbs.h"
 
+#define RESCHED_LOOP_CNT_THRESHOLD 0x1000
+
 static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
 {
 	bool make_dirty = umem->writable && dirty;
@@ -58,7 +60,9 @@ static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int d
 	for_each_sgtable_sg(&umem->sgt_append.sgt, sg, i) {
 		unpin_user_page_range_dirty_lock(sg_page(sg),
 			DIV_ROUND_UP(sg->length, PAGE_SIZE), make_dirty);
-		cond_resched();
+
+		if (!(i % RESCHED_LOOP_CNT_THRESHOLD))
+			cond_resched();
 	}
 
 	sg_free_append_table(&umem->sgt_append);
-- 
2.9.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] RDMA/core: Reduce cond_resched() frequency in __ib_umem_release
  2025-11-26  2:51 [PATCH] RDMA/core: Reduce cond_resched() frequency in __ib_umem_release lirongqing
@ 2025-11-26  8:15 ` Leon Romanovsky
  2025-11-26  8:15 ` Leon Romanovsky
  1 sibling, 0 replies; 3+ messages in thread
From: Leon Romanovsky @ 2025-11-26  8:15 UTC (permalink / raw)
  To: lirongqing; +Cc: Jason Gunthorpe, huangjunxian6, linux-rdma

On Wed, Nov 26, 2025 at 10:51:47AM +0800, lirongqing wrote:
> From: Li RongQing <lirongqing@baidu.com>
> 
> The current implementation calls cond_resched() for every SG entry
> in __ib_umem_release(), which can increase needless overhead.
> 
> This patch introduces RESCHED_LOOP_CNT_THRESHOLD (0x1000) to limit
> how often cond_resched() is called. The function now yields the CPU
> once every 4096 iterations, and yield at the very first iteration
> for lots of small umem case, to reduce scheduling overhead.
> 
> Signed-off-by: Li RongQing <lirongqing@baidu.com>
> ---
>  drivers/infiniband/core/umem.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c
> index 8fd84aa..ff540a2 100644
> --- a/drivers/infiniband/core/umem.c
> +++ b/drivers/infiniband/core/umem.c
> @@ -45,6 +45,8 @@
>  
>  #include "uverbs.h"
>  
> +#define RESCHED_LOOP_CNT_THRESHOLD 0x1000
> +
>  static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int dirty)
>  {
>  	bool make_dirty = umem->writable && dirty;
> @@ -58,7 +60,9 @@ static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int d
>  	for_each_sgtable_sg(&umem->sgt_append.sgt, sg, i) {
>  		unpin_user_page_range_dirty_lock(sg_page(sg),
>  			DIV_ROUND_UP(sg->length, PAGE_SIZE), make_dirty);
> -		cond_resched();
> +
> +		if (!(i % RESCHED_LOOP_CNT_THRESHOLD))
> +			cond_resched();


I applied it, but added a fix to skip the initial cond_resched() call on the
first iteration.

diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c
index ff540a230297..8137031c2a65 100644
--- a/drivers/infiniband/core/umem.c
+++ b/drivers/infiniband/core/umem.c
@@ -61,7 +61,7 @@ static void __ib_umem_release(struct ib_device *dev, struct ib_umem *umem, int d
                unpin_user_page_range_dirty_lock(sg_page(sg),
                        DIV_ROUND_UP(sg->length, PAGE_SIZE), make_dirty);

-               if (!(i % RESCHED_LOOP_CNT_THRESHOLD))
+               if (i && !(i % RESCHED_LOOP_CNT_THRESHOLD))
                        cond_resched();
        }



>  	}
>  
>  	sg_free_append_table(&umem->sgt_append);
> -- 
> 2.9.4
> 

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] RDMA/core: Reduce cond_resched() frequency in __ib_umem_release
  2025-11-26  2:51 [PATCH] RDMA/core: Reduce cond_resched() frequency in __ib_umem_release lirongqing
  2025-11-26  8:15 ` Leon Romanovsky
@ 2025-11-26  8:15 ` Leon Romanovsky
  1 sibling, 0 replies; 3+ messages in thread
From: Leon Romanovsky @ 2025-11-26  8:15 UTC (permalink / raw)
  To: Jason Gunthorpe, huangjunxian6, linux-rdma, lirongqing


On Wed, 26 Nov 2025 10:51:47 +0800, lirongqing wrote:
> The current implementation calls cond_resched() for every SG entry
> in __ib_umem_release(), which can increase needless overhead.
> 
> This patch introduces RESCHED_LOOP_CNT_THRESHOLD (0x1000) to limit
> how often cond_resched() is called. The function now yields the CPU
> once every 4096 iterations, and yield at the very first iteration
> for lots of small umem case, to reduce scheduling overhead.
> 
> [...]

Applied, thanks!

[1/1] RDMA/core: Reduce cond_resched() frequency in __ib_umem_release
      https://git.kernel.org/rdma/rdma/c/f37e2868792335

Best regards,
-- 
Leon Romanovsky <leon@kernel.org>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-11-26  8:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-26  2:51 [PATCH] RDMA/core: Reduce cond_resched() frequency in __ib_umem_release lirongqing
2025-11-26  8:15 ` Leon Romanovsky
2025-11-26  8:15 ` Leon Romanovsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox