Skip to contentSkip to conversation

drm/ttm: fix bulk_move corruption when adding a entry

PATCH Superseded Yunxiang Li 3 participants 4 messages Jun 21, 2023 – Jun 22, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/ttm: fix bulk_move corruption when adding a entry

When the resource is the first in the bulk_move range, adding it again (thus moving it to the tail) will corrupt the list since the first pointer is not moved. This eventually lead to null pointer deref in ttm_lru_bulk_move_del()

Fixes fee2ede15542 ("drm/ttm: rework bulk move handling v5")
---
 drivers/gpu/drm/ttm/ttm_resource.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_resource.c +6 -1
index 7333f7a87a2f..4b84cfb5f916 100644
@@ -86,6 +86,9 @@ static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos,
 				       struct ttm_resource *res)
 {
 	if (pos->last != res) {
+		if (pos->first == res) {
+			pos->first = list_next_entry(res, lru);
+		}
 		list_move(&res->lru, &pos->last->lru);
 		pos->last = res;
 	}
@@ -111,7 +114,9 @@ static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
 {
 	struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
 
-	if (unlikely(pos->first == res && pos->last == res)) {
+	if (unlikely(!pos->first)) {
+		WARN(1, "ttm_lru_bulk_move_pos is corrupt");
+	} else if (unlikely(pos->first == res && pos->last == res)) {
 		pos->first = NULL;
 		pos->last = NULL;
 	} else if (pos->first == res) {
Signature · 1 line
2.41.0
#1 lore raw

Am 21.06.23 um 17:42 schrieb Yunxiang Li:

Quoted 4 lines

When the resource is the first in the bulk_move range, adding it again (thus moving it to the tail) will corrupt the list since the first pointer is not moved. This eventually lead to null pointer deref in ttm_lru_bulk_move_del()

Good catch.

Quoted 12 lines
Fixes fee2ede15542 ("drm/ttm: rework bulk move handling v5")
---
  drivers/gpu/drm/ttm/ttm_resource.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_resource.c +0 -0
index 7333f7a87a2f..4b84cfb5f916 100644

drivers/gpu/drm/ttm/ttm_resource.c static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos, · line 91

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
@@ -86,6 +86,9 @@ static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos,
  				       struct ttm_resource *res)
  {
  	if (pos->last != res) {
+		if (pos->first == res) {
+			pos->first = list_next_entry(res, lru);
+		}

Please drop the extra {}.

drivers/gpu/drm/ttm/ttm_resource.c static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk, · line 118

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_resource.c +2 -1
  		list_move(&res->lru, &pos->last->lru);
  		pos->last = res;
  	}
@@ -111,7 +114,9 @@ static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
  {
  	struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
 
-	if (unlikely(pos->first == res && pos->last == res)) {
+	if (unlikely(!pos->first)) {
+		WARN(1, "ttm_lru_bulk_move_pos is corrupt");

Rather do a WARN_ON(!pos->first || !pos->last) before the ifs and change the last if to check for pos->last.

Regards,
Christian.

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_resource.c +1 -0
+	} else if (unlikely(pos->first == res && pos->last == res)) {
  		pos->first = NULL;
  		pos->last = NULL;
  	} else if (pos->first == res) {
#2 lore raw

PATCH v2 1/1 drm/ttm: fix bulk_move corruption when adding a entry

In reply to Yunxiang Li

When the resource is the first in the bulk_move range, adding it again (thus moving it to the tail) will corrupt the list since the first pointer is not moved. This eventually lead to null pointer deref in ttm_lru_bulk_move_del()

Fixes fee2ede15542 ("drm/ttm: rework bulk move handling v5")

---
v2: style cleanup

 drivers/gpu/drm/ttm/ttm_resource.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_resource.c +4 -1
index 7333f7a87a2f..e51dbc7a2d53 100644
@@ -86,6 +86,8 @@ static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos,
 				       struct ttm_resource *res)
 {
 	if (pos->last != res) {
+		if (pos->first == res)
+			pos->first = list_next_entry(res, lru);
 		list_move(&res->lru, &pos->last->lru);
 		pos->last = res;
 	}
@@ -111,7 +113,8 @@ static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
 {
 	struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
 
-	if (unlikely(pos->first == res && pos->last == res)) {
+	if (unlikely(WARN_ON(!pos->first || !pos->last) ||
+		     pos->first == res && pos->last == res)) {
 		pos->first = NULL;
 		pos->last = NULL;
 	} else if (pos->first == res) {
Signature · 1 line
2.41.0
#3 lore raw
kernel test robot noticed the following build warnings:

Hi Yunxiang,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm-misc/drm-misc-next]
[also build test WARNING on drm/drm-next drm-exynos/exynos-drm-next drm-intel/for-linux-next drm-intel/for-linux-next-fixes linus/master v6.4-rc7 next-20230622] [cannot apply to drm-tip/drm-tip]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Yunxiang-Li/drm-ttm-fix-bulk_move-corruption-when-adding-a-entry/20230622-222504
base:   git://anongit.freedesktop.org/drm/drm-misc drm-misc-next
patch link:    https://lore.kernel.org/r/20230622141902.28718-3-Yunxiang.Li%40amd.com
patch subject: [PATCH v2] drm/ttm: fix bulk_move corruption when adding a entry
config: alpha-allyesconfig (https://download.01.org/0day-ci/archive/20230623/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230623/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from include/linux/build_bug.h:5,

                    from include/linux/init.h:5,
                    from include/linux/io.h:10,
                    from include/linux/iosys-map.h:10,
                    from drivers/gpu/drm/ttm/ttm_resource.c:25:
   drivers/gpu/drm/ttm/ttm_resource.c: In function 'ttm_lru_bulk_move_del':
>> drivers/gpu/drm/ttm/ttm_resource.c:117:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
     117 |                      pos->first == res && pos->last == res)) {
         |                      ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
   include/linux/compiler.h:77:45: note: in definition of macro 'unlikely'
      77 | # define unlikely(x)    __builtin_expect(!!(x), 0)
         |                                             ^

vim +117 drivers/gpu/drm/ttm/ttm_resource.c

109
110 /* Remove the resource from a bulk_move range */
111 static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
112 struct ttm_resource *res)
113 {
114 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
115
116 if (unlikely(WARN_ON(!pos->first || !pos->last) ||

 > 117			     pos->first == res && pos->last == res)) {

118 pos->first = NULL;
119 pos->last = NULL;
120 } else if (pos->first == res) {
121 pos->first = list_next_entry(res, lru);
122 } else if (pos->last == res) {
123 pos->last = list_prev_entry(res, lru);
124 } else {
125 list_move(&res->lru, &pos->last->lru);
126 }
127 }
128

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki