Message ID | 20230420030500.1578756-1-suijingfeng@loongson.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp36292vqo; Wed, 19 Apr 2023 20:10:47 -0700 (PDT) X-Google-Smtp-Source: AKy350Y5xD+xKk0OhxsBhbMeChWcIaWtLuRJndbKBpNsVBS+7IV8mhcEqHSV1cKDuD6KfHZOSuNz X-Received: by 2002:a05:6808:2104:b0:38e:2616:ac4a with SMTP id r4-20020a056808210400b0038e2616ac4amr124708oiw.48.1681960247019; Wed, 19 Apr 2023 20:10:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681960247; cv=none; d=google.com; s=arc-20160816; b=U1pJMFGRsyOIcOCsyHGBZo4xjnrb0l9ok2mhI0Ln4YfYs5b6RKAgaJRc6Uvj7d1L0y ezV8k2/kYOiQqT75aBw9w0lODG0Vub2IpIron56bwLRMKWQi1FDoAg0LQyXk7sbqzUH3 ZdcykstyCdSdRgYZqkpi7fVjchdyG4tHsr6wbllStI33Ho54xEQtj/5NcsYXJ2yznrok zqC+CeAy83P4pbA9lypBhBX0NH6tn5MNBVt+QsA5DHCZpQZ9L3OhVzYYrtGMV71ckMAY dZd7VGfZqPVBUPJ11vArh3kUgO1Q6373eiYlyzlxj7U40ryE0deXfsBkx7oKPQNQbcvh jAdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=XsS/yZRBwr6wMurbbvN0ApBxf+LLdFwthkmcRu5wgPo=; b=y71TolpydDrGgAioHFjih+S6orM8WVfNN/GXDDxKLbHuSdCqnavk9Q+xF7aRQwCo0u N/ANI06J7NxRGjkjPm0W13q+zubd/qkOOH7SMA4pfNiUJvchqANnRTN699HacEcweOGv xkrM9DzOy8DQCCP446pKmac6xlaQ0cXbnXvFV5iviEVVxS8zueQYSahpG5Xe9y53lvI0 hMlZFhFuxXOgzhSGPkRk6VlnD7kHEFpaF1DmYswCOxLKMKNLJfIdiV+jF31yEeEcHPdD yB9m9lo+MP6+eGELyvHXS9q57miiwJ87Qbmm6vr59q3jHDwbBPg6g00YSakDDXphTZ/d z1Ag== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i185-20020acaeac2000000b0038c1f535e2fsi431786oih.169.2023.04.19.20.10.33; Wed, 19 Apr 2023 20:10:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233714AbjDTDFQ (ORCPT <rfc822;peter110.wang@gmail.com> + 99 others); Wed, 19 Apr 2023 23:05:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232955AbjDTDFI (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 19 Apr 2023 23:05:08 -0400 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0CA662103; Wed, 19 Apr 2023 20:05:05 -0700 (PDT) Received: from loongson.cn (unknown [10.20.42.43]) by gateway (Coremail) with SMTP id _____8BxONngq0Bkd0MfAA--.48810S3; Thu, 20 Apr 2023 11:05:04 +0800 (CST) Received: from openarena.loongson.cn (unknown [10.20.42.43]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxKLLeq0Bkc3AwAA--.68S2; Thu, 20 Apr 2023 11:05:02 +0800 (CST) From: Sui Jingfeng <suijingfeng@loongson.cn> To: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>, Maxime Ripard <mripard@kernel.org>, Thomas Zimmermann <tzimmermann@suse.de>, David Airlie <airlied@gmail.com>, Daniel Vetter <daniel@ffwll.ch>, Geert Uytterhoeven <geert+renesas@glider.be>, Sui Jingfeng <suijingfeng@loongson.cn>, Li Yi <liyi@loongson.cn>, Helge Deller <deller@gmx.de>, Lucas De Marchi <lucas.demarchi@intel.com> Cc: linux-kernel@vger.kernel.org, linux-fbdev@vger.kernel.org, dri-devel@lists.freedesktop.org, loongson-kernel@lists.loongnix.cn Subject: [PATCH v5] drm/fbdev-generic: prohibit potential out-of-bounds access Date: Thu, 20 Apr 2023 11:05:00 +0800 Message-Id: <20230420030500.1578756-1-suijingfeng@loongson.cn> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8CxKLLeq0Bkc3AwAA--.68S2 X-CM-SenderInfo: xvxlyxpqjiv03j6o00pqjv00gofq/ X-Coremail-Antispam: 1Uk129KBjvJXoWxCF15Kr1fuF1kKw43Aw1UJrb_yoWrKF4fpa y7Ga1UKrsYqF1DWrW7A3WrAw15Wan7AFyIgr97G342vF43A3Z29FyUKF4jgFy5Jr1fZr1a qws09w1I9r12kaUanT9S1TB71UUUUbUqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bSkYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_Jrv_JF1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8Jr0_Cr1UM2 8EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_Cr1j6rxd M2kKe7AKxVWUAVWUtwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zV CFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUtVWrXwAv7VC2 z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxkF7I 0En4kS14v26r126r1DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCI bckI1I0E14v26r126r1DMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_Jr I_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v2 6r1I6r4UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj4 0_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8 JrUvcSsGvfC2KfnxnUUI43ZEXa7IU8_gA5UUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763638865613278563?= X-GMAIL-MSGID: =?utf-8?q?1763663147976377251?= |
Series |
[v5] drm/fbdev-generic: prohibit potential out-of-bounds access
|
|
Commit Message
Sui Jingfeng
April 20, 2023, 3:05 a.m. UTC
The fbdev test of IGT may write after EOF, which lead to out-of-bound access for drm drivers hire fbdev-generic. For example, run fbdev test on a x86+ast2400 platform, with 1680x1050 resolution, will cause the linux kernel hang with the following call trace: Oops: 0000 [#1] PREEMPT SMP PTI [IGT] fbdev: starting subtest eof Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] [IGT] fbdev: starting subtest nullptr RIP: 0010:memcpy_erms+0xa/0x20 RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 FS: 0000000000000000(0000) GS:ffff895257380000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 Call Trace: <TASK> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] process_one_work+0x21f/0x430 worker_thread+0x4e/0x3c0 ? __pfx_worker_thread+0x10/0x10 kthread+0xf4/0x120 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x2c/0x50 </TASK> CR2: ffffa17d40e0b000 ---[ end trace 0000000000000000 ]--- The is because damage rectangles computed by drm_fb_helper_memory_range_to_clip() function does not guaranteed to be bound in the screen's active display area. Possible reasons are: 1) Buffers are allocated in the granularity of page size, for mmap system call support. The shadow screen buffer consumed by fbdev emulation may also choosed be page size aligned. 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() will introduce off-by-one error. For example, on a 16KB page size system, in order to store a 1920x1080 XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size 1920*1080*4 can not be divided exactly by 16KB. 1920 * 1080 * 4 = 8294400 bytes 506 * 16 * 1024 = 8290304 bytes 507 * 16 * 1024 = 8306688 bytes line_length = 1920*4 = 7680 bytes 507 * 16 * 1024 / 7680 = 1081.6 off / line_length = 507 * 16 * 1024 / 7680 = 1081 DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 memcpy_toio() typically issue the copy line by line, when copy the last line, out-of-bound access will be happen. Because: 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 Note that userspace may stil write to the invisiable area if a larger buffer than width x stride is exposed. But it is not a big issue as long as there still have memory resolve the access if not drafting so far. - Also limit the y1 (Daniel) - keep fix patch it to minimal (Daniel) - screen_size is page size aligned because of it need mmap (Thomas) - Adding fixes tag (Thomas) Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> Link: https://lore.kernel.org/dri-devel/ad44df29-3241-0d9e-e708-b0338bf3c623@189.cn/ --- drivers/gpu/drm/drm_fb_helper.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
Comments
Hi, this patch looks to me good and I'd like to merge it, if no one objects. In the near future, after i915 has switched to struct drm_client, I intend to move DRM's deferred-I/O helpers into fbdev-generic and i915. Those are the two users, but they are fairly different. They can then both have something tailored towards their needs. Best regards Thomas Am 20.04.23 um 05:05 schrieb Sui Jingfeng: > The fbdev test of IGT may write after EOF, which lead to out-of-bound > access for drm drivers hire fbdev-generic. For example, run fbdev test > on a x86+ast2400 platform, with 1680x1050 resolution, will cause the > linux kernel hang with the following call trace: > > Oops: 0000 [#1] PREEMPT SMP PTI > [IGT] fbdev: starting subtest eof > Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] > [IGT] fbdev: starting subtest nullptr > > RIP: 0010:memcpy_erms+0xa/0x20 > RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 > RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 > RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 > RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 > R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 > R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 > FS: 0000000000000000(0000) GS:ffff895257380000(0000) knlGS:0000000000000000 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 > Call Trace: > <TASK> > ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] > drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] > process_one_work+0x21f/0x430 > worker_thread+0x4e/0x3c0 > ? __pfx_worker_thread+0x10/0x10 > kthread+0xf4/0x120 > ? __pfx_kthread+0x10/0x10 > ret_from_fork+0x2c/0x50 > </TASK> > CR2: ffffa17d40e0b000 > ---[ end trace 0000000000000000 ]--- > > The is because damage rectangles computed by > drm_fb_helper_memory_range_to_clip() function does not guaranteed to be > bound in the screen's active display area. Possible reasons are: > > 1) Buffers are allocated in the granularity of page size, for mmap system > call support. The shadow screen buffer consumed by fbdev emulation may > also choosed be page size aligned. > > 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() > will introduce off-by-one error. > > For example, on a 16KB page size system, in order to store a 1920x1080 > XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size > 1920*1080*4 can not be divided exactly by 16KB. > > 1920 * 1080 * 4 = 8294400 bytes > 506 * 16 * 1024 = 8290304 bytes > 507 * 16 * 1024 = 8306688 bytes > > line_length = 1920*4 = 7680 bytes > > 507 * 16 * 1024 / 7680 = 1081.6 > > off / line_length = 507 * 16 * 1024 / 7680 = 1081 > DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 > > memcpy_toio() typically issue the copy line by line, when copy the last > line, out-of-bound access will be happen. Because: > > 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 > > Note that userspace may stil write to the invisiable area if a larger > buffer than width x stride is exposed. But it is not a big issue as > long as there still have memory resolve the access if not drafting so > far. > > - Also limit the y1 (Daniel) > - keep fix patch it to minimal (Daniel) > - screen_size is page size aligned because of it need mmap (Thomas) > - Adding fixes tag (Thomas) > > Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") > > Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> > Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> > Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> > Link: https://lore.kernel.org/dri-devel/ad44df29-3241-0d9e-e708-b0338bf3c623@189.cn/ > --- > drivers/gpu/drm/drm_fb_helper.c | 16 ++++++++++++---- > 1 file changed, 12 insertions(+), 4 deletions(-) > > diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c > index 64458982be40..6bb1b8b27d7a 100644 > --- a/drivers/gpu/drm/drm_fb_helper.c > +++ b/drivers/gpu/drm/drm_fb_helper.c > @@ -641,19 +641,27 @@ static void drm_fb_helper_damage(struct drm_fb_helper *helper, u32 x, u32 y, > static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, off_t off, size_t len, > struct drm_rect *clip) > { > + u32 line_length = info->fix.line_length; > + u32 fb_height = info->var.yres; > off_t end = off + len; > u32 x1 = 0; > - u32 y1 = off / info->fix.line_length; > + u32 y1 = off / line_length; > u32 x2 = info->var.xres; > - u32 y2 = DIV_ROUND_UP(end, info->fix.line_length); > + u32 y2 = DIV_ROUND_UP(end, line_length); > + > + /* Don't allow any of them beyond the bottom bound of display area */ > + if (y1 > fb_height) > + y1 = fb_height; > + if (y2 > fb_height) > + y2 = fb_height; > > if ((y2 - y1) == 1) { > /* > * We've only written to a single scanline. Try to reduce > * the number of horizontal pixels that need an update. > */ > - off_t bit_off = (off % info->fix.line_length) * 8; > - off_t bit_end = (end % info->fix.line_length) * 8; > + off_t bit_off = (off % line_length) * 8; > + off_t bit_end = (end % line_length) * 8; > > x1 = bit_off / info->var.bits_per_pixel; > x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel); -- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstrasse 146, 90461 Nuernberg, Germany GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)
Hi Am 20.04.23 um 09:04 schrieb Thomas Zimmermann: > Hi, > > this patch looks to me good and I'd like to merge it, if no one objects. Rereading it, I might have been too eager. What happened to the setting of screen_size = buffer->gem->size ? It is not relevant? Best regards Thomas > > In the near future, after i915 has switched to struct drm_client, I > intend to move DRM's deferred-I/O helpers into fbdev-generic and i915. > Those are the two users, but they are fairly different. They can then > both have something tailored towards their needs. > > Best regards > Thomas > > Am 20.04.23 um 05:05 schrieb Sui Jingfeng: >> The fbdev test of IGT may write after EOF, which lead to out-of-bound >> access for drm drivers hire fbdev-generic. For example, run fbdev test >> on a x86+ast2400 platform, with 1680x1050 resolution, will cause the >> linux kernel hang with the following call trace: >> >> Oops: 0000 [#1] PREEMPT SMP PTI >> [IGT] fbdev: starting subtest eof >> Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] >> [IGT] fbdev: starting subtest nullptr >> >> RIP: 0010:memcpy_erms+0xa/0x20 >> RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 >> RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 >> RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 >> RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 >> R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 >> R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 >> FS: 0000000000000000(0000) GS:ffff895257380000(0000) >> knlGS:0000000000000000 >> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >> CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 >> Call Trace: >> <TASK> >> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] >> drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] >> process_one_work+0x21f/0x430 >> worker_thread+0x4e/0x3c0 >> ? __pfx_worker_thread+0x10/0x10 >> kthread+0xf4/0x120 >> ? __pfx_kthread+0x10/0x10 >> ret_from_fork+0x2c/0x50 >> </TASK> >> CR2: ffffa17d40e0b000 >> ---[ end trace 0000000000000000 ]--- >> >> The is because damage rectangles computed by >> drm_fb_helper_memory_range_to_clip() function does not guaranteed to be >> bound in the screen's active display area. Possible reasons are: >> >> 1) Buffers are allocated in the granularity of page size, for mmap system >> call support. The shadow screen buffer consumed by fbdev emulation >> may >> also choosed be page size aligned. >> >> 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() >> will introduce off-by-one error. >> >> For example, on a 16KB page size system, in order to store a 1920x1080 >> XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size >> 1920*1080*4 can not be divided exactly by 16KB. >> >> 1920 * 1080 * 4 = 8294400 bytes >> 506 * 16 * 1024 = 8290304 bytes >> 507 * 16 * 1024 = 8306688 bytes >> >> line_length = 1920*4 = 7680 bytes >> >> 507 * 16 * 1024 / 7680 = 1081.6 >> >> off / line_length = 507 * 16 * 1024 / 7680 = 1081 >> DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 >> >> memcpy_toio() typically issue the copy line by line, when copy the last >> line, out-of-bound access will be happen. Because: >> >> 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 >> >> Note that userspace may stil write to the invisiable area if a larger >> buffer than width x stride is exposed. But it is not a big issue as >> long as there still have memory resolve the access if not drafting so >> far. >> >> - Also limit the y1 (Daniel) >> - keep fix patch it to minimal (Daniel) >> - screen_size is page size aligned because of it need mmap (Thomas) >> - Adding fixes tag (Thomas) >> >> Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") >> >> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> >> Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> >> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> >> Link: >> https://lore.kernel.org/dri-devel/ad44df29-3241-0d9e-e708-b0338bf3c623@189.cn/ >> --- >> drivers/gpu/drm/drm_fb_helper.c | 16 ++++++++++++---- >> 1 file changed, 12 insertions(+), 4 deletions(-) >> >> diff --git a/drivers/gpu/drm/drm_fb_helper.c >> b/drivers/gpu/drm/drm_fb_helper.c >> index 64458982be40..6bb1b8b27d7a 100644 >> --- a/drivers/gpu/drm/drm_fb_helper.c >> +++ b/drivers/gpu/drm/drm_fb_helper.c >> @@ -641,19 +641,27 @@ static void drm_fb_helper_damage(struct >> drm_fb_helper *helper, u32 x, u32 y, >> static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, >> off_t off, size_t len, >> struct drm_rect *clip) >> { >> + u32 line_length = info->fix.line_length; >> + u32 fb_height = info->var.yres; >> off_t end = off + len; >> u32 x1 = 0; >> - u32 y1 = off / info->fix.line_length; >> + u32 y1 = off / line_length; >> u32 x2 = info->var.xres; >> - u32 y2 = DIV_ROUND_UP(end, info->fix.line_length); >> + u32 y2 = DIV_ROUND_UP(end, line_length); >> + >> + /* Don't allow any of them beyond the bottom bound of display >> area */ >> + if (y1 > fb_height) >> + y1 = fb_height; >> + if (y2 > fb_height) >> + y2 = fb_height; >> if ((y2 - y1) == 1) { >> /* >> * We've only written to a single scanline. Try to reduce >> * the number of horizontal pixels that need an update. >> */ >> - off_t bit_off = (off % info->fix.line_length) * 8; >> - off_t bit_end = (end % info->fix.line_length) * 8; >> + off_t bit_off = (off % line_length) * 8; >> + off_t bit_end = (end % line_length) * 8; >> x1 = bit_off / info->var.bits_per_pixel; >> x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel); > -- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstrasse 146, 90461 Nuernberg, Germany GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)
Hi Sui, On Thu, Apr 20, 2023 at 5:09 AM Sui Jingfeng <suijingfeng@loongson.cn> wrote: > The fbdev test of IGT may write after EOF, which lead to out-of-bound > access for drm drivers hire fbdev-generic. For example, run fbdev test > on a x86+ast2400 platform, with 1680x1050 resolution, will cause the > linux kernel hang with the following call trace: > > Oops: 0000 [#1] PREEMPT SMP PTI > [IGT] fbdev: starting subtest eof > Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] > [IGT] fbdev: starting subtest nullptr > > RIP: 0010:memcpy_erms+0xa/0x20 > RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 > RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 > RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 > RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 > R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 > R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 > FS: 0000000000000000(0000) GS:ffff895257380000(0000) knlGS:0000000000000000 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 > Call Trace: > <TASK> > ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] > drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] > process_one_work+0x21f/0x430 > worker_thread+0x4e/0x3c0 > ? __pfx_worker_thread+0x10/0x10 > kthread+0xf4/0x120 > ? __pfx_kthread+0x10/0x10 > ret_from_fork+0x2c/0x50 > </TASK> > CR2: ffffa17d40e0b000 > ---[ end trace 0000000000000000 ]--- > > The is because damage rectangles computed by > drm_fb_helper_memory_range_to_clip() function does not guaranteed to be > bound in the screen's active display area. Possible reasons are: > > 1) Buffers are allocated in the granularity of page size, for mmap system > call support. The shadow screen buffer consumed by fbdev emulation may > also choosed be page size aligned. > > 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() > will introduce off-by-one error. > > For example, on a 16KB page size system, in order to store a 1920x1080 > XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size > 1920*1080*4 can not be divided exactly by 16KB. > > 1920 * 1080 * 4 = 8294400 bytes > 506 * 16 * 1024 = 8290304 bytes > 507 * 16 * 1024 = 8306688 bytes > > line_length = 1920*4 = 7680 bytes > > 507 * 16 * 1024 / 7680 = 1081.6 > > off / line_length = 507 * 16 * 1024 / 7680 = 1081 > DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 > > memcpy_toio() typically issue the copy line by line, when copy the last > line, out-of-bound access will be happen. Because: > > 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 > > Note that userspace may stil write to the invisiable area if a larger > buffer than width x stride is exposed. But it is not a big issue as > long as there still have memory resolve the access if not drafting so > far. > > - Also limit the y1 (Daniel) > - keep fix patch it to minimal (Daniel) > - screen_size is page size aligned because of it need mmap (Thomas) > - Adding fixes tag (Thomas) > > Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") > > Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> > Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> > Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> Thanks for the update! This v5 is completely different from the v3 I tested before, so keeping my Tested-by is not really appropriate... I have retested fbtest with shmob-drm on Armadillo-800-EVA (800x480@RG16, i.e. 187.5 pages), and fortunately this version still works fine, so Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Hi, On 2023/4/20 17:04, Geert Uytterhoeven wrote: > Hi Sui, > > On Thu, Apr 20, 2023 at 5:09 AM Sui Jingfeng <suijingfeng@loongson.cn> wrote: >> The fbdev test of IGT may write after EOF, which lead to out-of-bound >> access for drm drivers hire fbdev-generic. For example, run fbdev test >> on a x86+ast2400 platform, with 1680x1050 resolution, will cause the >> linux kernel hang with the following call trace: >> >> Oops: 0000 [#1] PREEMPT SMP PTI >> [IGT] fbdev: starting subtest eof >> Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] >> [IGT] fbdev: starting subtest nullptr >> >> RIP: 0010:memcpy_erms+0xa/0x20 >> RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 >> RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 >> RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 >> RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 >> R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 >> R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 >> FS: 0000000000000000(0000) GS:ffff895257380000(0000) knlGS:0000000000000000 >> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >> CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 >> Call Trace: >> <TASK> >> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] >> drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] >> process_one_work+0x21f/0x430 >> worker_thread+0x4e/0x3c0 >> ? __pfx_worker_thread+0x10/0x10 >> kthread+0xf4/0x120 >> ? __pfx_kthread+0x10/0x10 >> ret_from_fork+0x2c/0x50 >> </TASK> >> CR2: ffffa17d40e0b000 >> ---[ end trace 0000000000000000 ]--- >> >> The is because damage rectangles computed by >> drm_fb_helper_memory_range_to_clip() function does not guaranteed to be >> bound in the screen's active display area. Possible reasons are: >> >> 1) Buffers are allocated in the granularity of page size, for mmap system >> call support. The shadow screen buffer consumed by fbdev emulation may >> also choosed be page size aligned. >> >> 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() >> will introduce off-by-one error. >> >> For example, on a 16KB page size system, in order to store a 1920x1080 >> XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size >> 1920*1080*4 can not be divided exactly by 16KB. >> >> 1920 * 1080 * 4 = 8294400 bytes >> 506 * 16 * 1024 = 8290304 bytes >> 507 * 16 * 1024 = 8306688 bytes >> >> line_length = 1920*4 = 7680 bytes >> >> 507 * 16 * 1024 / 7680 = 1081.6 >> >> off / line_length = 507 * 16 * 1024 / 7680 = 1081 >> DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 >> >> memcpy_toio() typically issue the copy line by line, when copy the last >> line, out-of-bound access will be happen. Because: >> >> 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 >> >> Note that userspace may stil write to the invisiable area if a larger >> buffer than width x stride is exposed. But it is not a big issue as >> long as there still have memory resolve the access if not drafting so >> far. >> >> - Also limit the y1 (Daniel) >> - keep fix patch it to minimal (Daniel) >> - screen_size is page size aligned because of it need mmap (Thomas) >> - Adding fixes tag (Thomas) >> >> Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") >> >> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> >> Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> >> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> > Thanks for the update! This v5 is completely different from the v3 > I tested before, so keeping my Tested-by is not really appropriate... Indeed, I will be carefully next time. > I have retested fbtest with shmob-drm on Armadillo-800-EVA > (800x480@RG16, i.e. 187.5 pages), and fortunately this version still > works fine, so > Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> Thanks a lot. > Gr{oetje,eeting}s, > > Geert > > -- > Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org > > In personal conversations with technical people, I call myself a hacker. But > when I'm talking to journalists I just say "programmer" or something like that. > -- Linus Torvalds
Hi On 2023/4/20 15:07, Thomas Zimmermann wrote: > Hi > > Am 20.04.23 um 09:04 schrieb Thomas Zimmermann: >> Hi, >> >> this patch looks to me good and I'd like to merge it, if no one objects. > > Rereading it, I might have been too eager. What happened to the > setting of screen_size = buffer->gem->size ? It is not relevant? > Short answer is that it is not relevant. As long as the computed damage rectangle is sane, it's OK to allocate a bit more than needed. I think it's turn out to be *correct*, if not extremely. Because it is page size aligned, writing to invisible area for some case is not a serve issue. It also guarantee that the size of shadow screen buffer is exactly the same size with its GEM counterpart. Otherwise I have to answer the question What will happen if the 'screen_size' is not page_size aligned and mmap will mapping in the granularity of pages ? I see efifb also align the buffer going to be mapped with page size. > Best regards > Thomas > >> >> In the near future, after i915 has switched to struct drm_client, I >> intend to move DRM's deferred-I/O helpers into fbdev-generic and >> i915. Those are the two users, but they are fairly different. They >> can then both have something tailored towards their needs. >> >> Best regards >> Thomas >> >> Am 20.04.23 um 05:05 schrieb Sui Jingfeng: >>> The fbdev test of IGT may write after EOF, which lead to out-of-bound >>> access for drm drivers hire fbdev-generic. For example, run fbdev test >>> on a x86+ast2400 platform, with 1680x1050 resolution, will cause the >>> linux kernel hang with the following call trace: >>> >>> Oops: 0000 [#1] PREEMPT SMP PTI >>> [IGT] fbdev: starting subtest eof >>> Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] >>> [IGT] fbdev: starting subtest nullptr >>> >>> RIP: 0010:memcpy_erms+0xa/0x20 >>> RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 >>> RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 >>> RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 >>> RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 >>> R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 >>> R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 >>> FS: 0000000000000000(0000) GS:ffff895257380000(0000) >>> knlGS:0000000000000000 >>> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >>> CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 >>> Call Trace: >>> <TASK> >>> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] >>> drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] >>> process_one_work+0x21f/0x430 >>> worker_thread+0x4e/0x3c0 >>> ? __pfx_worker_thread+0x10/0x10 >>> kthread+0xf4/0x120 >>> ? __pfx_kthread+0x10/0x10 >>> ret_from_fork+0x2c/0x50 >>> </TASK> >>> CR2: ffffa17d40e0b000 >>> ---[ end trace 0000000000000000 ]--- >>> >>> The is because damage rectangles computed by >>> drm_fb_helper_memory_range_to_clip() function does not guaranteed to be >>> bound in the screen's active display area. Possible reasons are: >>> >>> 1) Buffers are allocated in the granularity of page size, for mmap >>> system >>> call support. The shadow screen buffer consumed by fbdev >>> emulation may >>> also choosed be page size aligned. >>> >>> 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() >>> will introduce off-by-one error. >>> >>> For example, on a 16KB page size system, in order to store a 1920x1080 >>> XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size >>> 1920*1080*4 can not be divided exactly by 16KB. >>> >>> 1920 * 1080 * 4 = 8294400 bytes >>> 506 * 16 * 1024 = 8290304 bytes >>> 507 * 16 * 1024 = 8306688 bytes >>> >>> line_length = 1920*4 = 7680 bytes >>> >>> 507 * 16 * 1024 / 7680 = 1081.6 >>> >>> off / line_length = 507 * 16 * 1024 / 7680 = 1081 >>> DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 >>> >>> memcpy_toio() typically issue the copy line by line, when copy the last >>> line, out-of-bound access will be happen. Because: >>> >>> 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 >>> >>> Note that userspace may stil write to the invisiable area if a larger >>> buffer than width x stride is exposed. But it is not a big issue as >>> long as there still have memory resolve the access if not drafting so >>> far. >>> >>> - Also limit the y1 (Daniel) >>> - keep fix patch it to minimal (Daniel) >>> - screen_size is page size aligned because of it need mmap (Thomas) >>> - Adding fixes tag (Thomas) >>> >>> Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") >>> >>> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> >>> Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> >>> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> >>> Link: >>> https://lore.kernel.org/dri-devel/ad44df29-3241-0d9e-e708-b0338bf3c623@189.cn/ >>> --- >>> drivers/gpu/drm/drm_fb_helper.c | 16 ++++++++++++---- >>> 1 file changed, 12 insertions(+), 4 deletions(-) >>> >>> diff --git a/drivers/gpu/drm/drm_fb_helper.c >>> b/drivers/gpu/drm/drm_fb_helper.c >>> index 64458982be40..6bb1b8b27d7a 100644 >>> --- a/drivers/gpu/drm/drm_fb_helper.c >>> +++ b/drivers/gpu/drm/drm_fb_helper.c >>> @@ -641,19 +641,27 @@ static void drm_fb_helper_damage(struct >>> drm_fb_helper *helper, u32 x, u32 y, >>> static void drm_fb_helper_memory_range_to_clip(struct fb_info >>> *info, off_t off, size_t len, >>> struct drm_rect *clip) >>> { >>> + u32 line_length = info->fix.line_length; >>> + u32 fb_height = info->var.yres; >>> off_t end = off + len; >>> u32 x1 = 0; >>> - u32 y1 = off / info->fix.line_length; >>> + u32 y1 = off / line_length; >>> u32 x2 = info->var.xres; >>> - u32 y2 = DIV_ROUND_UP(end, info->fix.line_length); >>> + u32 y2 = DIV_ROUND_UP(end, line_length); >>> + >>> + /* Don't allow any of them beyond the bottom bound of display >>> area */ >>> + if (y1 > fb_height) >>> + y1 = fb_height; >>> + if (y2 > fb_height) >>> + y2 = fb_height; >>> if ((y2 - y1) == 1) { >>> /* >>> * We've only written to a single scanline. Try to reduce >>> * the number of horizontal pixels that need an update. >>> */ >>> - off_t bit_off = (off % info->fix.line_length) * 8; >>> - off_t bit_end = (end % info->fix.line_length) * 8; >>> + off_t bit_off = (off % line_length) * 8; >>> + off_t bit_end = (end % line_length) * 8; >>> x1 = bit_off / info->var.bits_per_pixel; >>> x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel); >> >
Hi, On 2023/4/20 17:04, Geert Uytterhoeven wrote: > Hi Sui, > > On Thu, Apr 20, 2023 at 5:09 AM Sui Jingfeng <suijingfeng@loongson.cn> wrote: >> The fbdev test of IGT may write after EOF, which lead to out-of-bound >> access for drm drivers hire fbdev-generic. For example, run fbdev test >> on a x86+ast2400 platform, with 1680x1050 resolution, will cause the >> linux kernel hang with the following call trace: >> >> Oops: 0000 [#1] PREEMPT SMP PTI >> [IGT] fbdev: starting subtest eof >> Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] >> [IGT] fbdev: starting subtest nullptr >> >> RIP: 0010:memcpy_erms+0xa/0x20 >> RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 >> RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 >> RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 >> RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 >> R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 >> R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 >> FS: 0000000000000000(0000) GS:ffff895257380000(0000) knlGS:0000000000000000 >> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >> CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 >> Call Trace: >> <TASK> >> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] >> drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] >> process_one_work+0x21f/0x430 >> worker_thread+0x4e/0x3c0 >> ? __pfx_worker_thread+0x10/0x10 >> kthread+0xf4/0x120 >> ? __pfx_kthread+0x10/0x10 >> ret_from_fork+0x2c/0x50 >> </TASK> >> CR2: ffffa17d40e0b000 >> ---[ end trace 0000000000000000 ]--- >> >> The is because damage rectangles computed by >> drm_fb_helper_memory_range_to_clip() function does not guaranteed to be >> bound in the screen's active display area. Possible reasons are: >> >> 1) Buffers are allocated in the granularity of page size, for mmap system >> call support. The shadow screen buffer consumed by fbdev emulation may >> also choosed be page size aligned. >> >> 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() >> will introduce off-by-one error. >> >> For example, on a 16KB page size system, in order to store a 1920x1080 >> XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size >> 1920*1080*4 can not be divided exactly by 16KB. >> >> 1920 * 1080 * 4 = 8294400 bytes >> 506 * 16 * 1024 = 8290304 bytes >> 507 * 16 * 1024 = 8306688 bytes >> >> line_length = 1920*4 = 7680 bytes >> >> 507 * 16 * 1024 / 7680 = 1081.6 >> >> off / line_length = 507 * 16 * 1024 / 7680 = 1081 >> DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 >> >> memcpy_toio() typically issue the copy line by line, when copy the last >> line, out-of-bound access will be happen. Because: >> >> 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 >> >> Note that userspace may stil write to the invisiable area if a larger >> buffer than width x stride is exposed. But it is not a big issue as >> long as there still have memory resolve the access if not drafting so >> far. >> >> - Also limit the y1 (Daniel) >> - keep fix patch it to minimal (Daniel) >> - screen_size is page size aligned because of it need mmap (Thomas) >> - Adding fixes tag (Thomas) >> >> Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") >> >> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> >> Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> >> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> > Thanks for the update! This v5 is completely different from the v3 > I tested before, so keeping my Tested-by is not really appropriate... Thanks for testing. I'm a bit of confident that it will works. Your tested-by is valuable, really don't want drop this. So I keep it. > I have retested fbtest with shmob-drm on Armadillo-800-EVA > (800x480@RG16, i.e. 187.5 pages), and fortunately this version still > works fine, so > Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> > > Gr{oetje,eeting}s, > > Geert > > -- > Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org > > In personal conversations with technical people, I call myself a hacker. But > when I'm talking to journalists I just say "programmer" or something like that. > -- Linus Torvalds
Hi, On 2023/4/20 15:04, Thomas Zimmermann wrote: > Hi, > > this patch looks to me good and I'd like to merge it, if no one objects. > > In the near future, after i915 has switched to struct drm_client, I > intend to move DRM's deferred-I/O helpers into fbdev-generic and i915. That sound fine, I can help to test then. But I think, it may better to make it a 100% DRM function? > Those are the two users, but they are fairly different. They can then > both have something tailored towards their needs. > > Best regards > Thomas > > Am 20.04.23 um 05:05 schrieb Sui Jingfeng: >> The fbdev test of IGT may write after EOF, which lead to out-of-bound >> access for drm drivers hire fbdev-generic. For example, run fbdev test >> on a x86+ast2400 platform, with 1680x1050 resolution, will cause the >> linux kernel hang with the following call trace: >> >> Oops: 0000 [#1] PREEMPT SMP PTI >> [IGT] fbdev: starting subtest eof >> Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] >> [IGT] fbdev: starting subtest nullptr >> >> RIP: 0010:memcpy_erms+0xa/0x20 >> RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 >> RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 >> RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 >> RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 >> R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 >> R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 >> FS: 0000000000000000(0000) GS:ffff895257380000(0000) >> knlGS:0000000000000000 >> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >> CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 >> Call Trace: >> <TASK> >> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] >> drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] >> process_one_work+0x21f/0x430 >> worker_thread+0x4e/0x3c0 >> ? __pfx_worker_thread+0x10/0x10 >> kthread+0xf4/0x120 >> ? __pfx_kthread+0x10/0x10 >> ret_from_fork+0x2c/0x50 >> </TASK> >> CR2: ffffa17d40e0b000 >> ---[ end trace 0000000000000000 ]--- >> >> The is because damage rectangles computed by >> drm_fb_helper_memory_range_to_clip() function does not guaranteed to be >> bound in the screen's active display area. Possible reasons are: >> >> 1) Buffers are allocated in the granularity of page size, for mmap >> system >> call support. The shadow screen buffer consumed by fbdev >> emulation may >> also choosed be page size aligned. >> >> 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() >> will introduce off-by-one error. >> >> For example, on a 16KB page size system, in order to store a 1920x1080 >> XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size >> 1920*1080*4 can not be divided exactly by 16KB. >> >> 1920 * 1080 * 4 = 8294400 bytes >> 506 * 16 * 1024 = 8290304 bytes >> 507 * 16 * 1024 = 8306688 bytes >> >> line_length = 1920*4 = 7680 bytes >> >> 507 * 16 * 1024 / 7680 = 1081.6 >> >> off / line_length = 507 * 16 * 1024 / 7680 = 1081 >> DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 >> >> memcpy_toio() typically issue the copy line by line, when copy the last >> line, out-of-bound access will be happen. Because: >> >> 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 >> >> Note that userspace may stil write to the invisiable area if a larger >> buffer than width x stride is exposed. But it is not a big issue as >> long as there still have memory resolve the access if not drafting so >> far. >> >> - Also limit the y1 (Daniel) >> - keep fix patch it to minimal (Daniel) >> - screen_size is page size aligned because of it need mmap (Thomas) >> - Adding fixes tag (Thomas) >> >> Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") >> >> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> >> Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> >> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> >> Link: >> https://lore.kernel.org/dri-devel/ad44df29-3241-0d9e-e708-b0338bf3c623@189.cn/ >> --- >> drivers/gpu/drm/drm_fb_helper.c | 16 ++++++++++++---- >> 1 file changed, 12 insertions(+), 4 deletions(-) >> >> diff --git a/drivers/gpu/drm/drm_fb_helper.c >> b/drivers/gpu/drm/drm_fb_helper.c >> index 64458982be40..6bb1b8b27d7a 100644 >> --- a/drivers/gpu/drm/drm_fb_helper.c >> +++ b/drivers/gpu/drm/drm_fb_helper.c >> @@ -641,19 +641,27 @@ static void drm_fb_helper_damage(struct >> drm_fb_helper *helper, u32 x, u32 y, >> static void drm_fb_helper_memory_range_to_clip(struct fb_info >> *info, off_t off, size_t len, >> struct drm_rect *clip) >> { >> + u32 line_length = info->fix.line_length; >> + u32 fb_height = info->var.yres; >> off_t end = off + len; >> u32 x1 = 0; >> - u32 y1 = off / info->fix.line_length; >> + u32 y1 = off / line_length; >> u32 x2 = info->var.xres; >> - u32 y2 = DIV_ROUND_UP(end, info->fix.line_length); >> + u32 y2 = DIV_ROUND_UP(end, line_length); >> + >> + /* Don't allow any of them beyond the bottom bound of display >> area */ >> + if (y1 > fb_height) >> + y1 = fb_height; >> + if (y2 > fb_height) >> + y2 = fb_height; >> if ((y2 - y1) == 1) { >> /* >> * We've only written to a single scanline. Try to reduce >> * the number of horizontal pixels that need an update. >> */ >> - off_t bit_off = (off % info->fix.line_length) * 8; >> - off_t bit_end = (end % info->fix.line_length) * 8; >> + off_t bit_off = (off % line_length) * 8; >> + off_t bit_end = (end % line_length) * 8; >> x1 = bit_off / info->var.bits_per_pixel; >> x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel); >
Hi Am 20.04.23 um 12:04 schrieb Sui Jingfeng: > Hi > > On 2023/4/20 15:07, Thomas Zimmermann wrote: >> Hi >> >> Am 20.04.23 um 09:04 schrieb Thomas Zimmermann: >>> Hi, >>> >>> this patch looks to me good and I'd like to merge it, if no one objects. >> >> Rereading it, I might have been too eager. What happened to the >> setting of screen_size = buffer->gem->size ? It is not relevant? >> > Short answer is that it is not relevant. > > As long as the computed damage rectangle is sane, it's OK to allocate a > bit more than needed. > > I think it's turn out to be *correct*, if not extremely. > > Because it is page size aligned, writing to invisible area for some > case is not a serve issue. > > It also guarantee that the size of shadow screen buffer is exactly the > same size with its GEM counterpart. That's good enough for me. :) > > > Otherwise I have to answer the question > > What will happen if the 'screen_size' is not page_size aligned and mmap > will mapping in the granularity of pages ? You need to map at page granularity. If screen_size is not page-size aligned, there's this trailing buffer that is accessible, but cannot be displayed. But userspace has no direct way of knowing that, so let's ignore that problem for now. Best regards Thomas > > > I see efifb also align the buffer going to be mapped with page size. > > >> Best regards >> Thomas >> >>> >>> In the near future, after i915 has switched to struct drm_client, I >>> intend to move DRM's deferred-I/O helpers into fbdev-generic and >>> i915. Those are the two users, but they are fairly different. They >>> can then both have something tailored towards their needs. >>> >>> Best regards >>> Thomas >>> >>> Am 20.04.23 um 05:05 schrieb Sui Jingfeng: >>>> The fbdev test of IGT may write after EOF, which lead to out-of-bound >>>> access for drm drivers hire fbdev-generic. For example, run fbdev test >>>> on a x86+ast2400 platform, with 1680x1050 resolution, will cause the >>>> linux kernel hang with the following call trace: >>>> >>>> Oops: 0000 [#1] PREEMPT SMP PTI >>>> [IGT] fbdev: starting subtest eof >>>> Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] >>>> [IGT] fbdev: starting subtest nullptr >>>> >>>> RIP: 0010:memcpy_erms+0xa/0x20 >>>> RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 >>>> RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 >>>> RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 >>>> RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 >>>> R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 >>>> R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 >>>> FS: 0000000000000000(0000) GS:ffff895257380000(0000) >>>> knlGS:0000000000000000 >>>> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >>>> CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 >>>> Call Trace: >>>> <TASK> >>>> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] >>>> drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] >>>> process_one_work+0x21f/0x430 >>>> worker_thread+0x4e/0x3c0 >>>> ? __pfx_worker_thread+0x10/0x10 >>>> kthread+0xf4/0x120 >>>> ? __pfx_kthread+0x10/0x10 >>>> ret_from_fork+0x2c/0x50 >>>> </TASK> >>>> CR2: ffffa17d40e0b000 >>>> ---[ end trace 0000000000000000 ]--- >>>> >>>> The is because damage rectangles computed by >>>> drm_fb_helper_memory_range_to_clip() function does not guaranteed to be >>>> bound in the screen's active display area. Possible reasons are: >>>> >>>> 1) Buffers are allocated in the granularity of page size, for mmap >>>> system >>>> call support. The shadow screen buffer consumed by fbdev >>>> emulation may >>>> also choosed be page size aligned. >>>> >>>> 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() >>>> will introduce off-by-one error. >>>> >>>> For example, on a 16KB page size system, in order to store a 1920x1080 >>>> XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size >>>> 1920*1080*4 can not be divided exactly by 16KB. >>>> >>>> 1920 * 1080 * 4 = 8294400 bytes >>>> 506 * 16 * 1024 = 8290304 bytes >>>> 507 * 16 * 1024 = 8306688 bytes >>>> >>>> line_length = 1920*4 = 7680 bytes >>>> >>>> 507 * 16 * 1024 / 7680 = 1081.6 >>>> >>>> off / line_length = 507 * 16 * 1024 / 7680 = 1081 >>>> DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 >>>> >>>> memcpy_toio() typically issue the copy line by line, when copy the last >>>> line, out-of-bound access will be happen. Because: >>>> >>>> 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 >>>> >>>> Note that userspace may stil write to the invisiable area if a larger >>>> buffer than width x stride is exposed. But it is not a big issue as >>>> long as there still have memory resolve the access if not drafting so >>>> far. >>>> >>>> - Also limit the y1 (Daniel) >>>> - keep fix patch it to minimal (Daniel) >>>> - screen_size is page size aligned because of it need mmap (Thomas) >>>> - Adding fixes tag (Thomas) >>>> >>>> Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") >>>> >>>> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> >>>> Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> >>>> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> >>>> Link: >>>> https://lore.kernel.org/dri-devel/ad44df29-3241-0d9e-e708-b0338bf3c623@189.cn/ >>>> --- >>>> drivers/gpu/drm/drm_fb_helper.c | 16 ++++++++++++---- >>>> 1 file changed, 12 insertions(+), 4 deletions(-) >>>> >>>> diff --git a/drivers/gpu/drm/drm_fb_helper.c >>>> b/drivers/gpu/drm/drm_fb_helper.c >>>> index 64458982be40..6bb1b8b27d7a 100644 >>>> --- a/drivers/gpu/drm/drm_fb_helper.c >>>> +++ b/drivers/gpu/drm/drm_fb_helper.c >>>> @@ -641,19 +641,27 @@ static void drm_fb_helper_damage(struct >>>> drm_fb_helper *helper, u32 x, u32 y, >>>> static void drm_fb_helper_memory_range_to_clip(struct fb_info >>>> *info, off_t off, size_t len, >>>> struct drm_rect *clip) >>>> { >>>> + u32 line_length = info->fix.line_length; >>>> + u32 fb_height = info->var.yres; >>>> off_t end = off + len; >>>> u32 x1 = 0; >>>> - u32 y1 = off / info->fix.line_length; >>>> + u32 y1 = off / line_length; >>>> u32 x2 = info->var.xres; >>>> - u32 y2 = DIV_ROUND_UP(end, info->fix.line_length); >>>> + u32 y2 = DIV_ROUND_UP(end, line_length); >>>> + >>>> + /* Don't allow any of them beyond the bottom bound of display >>>> area */ >>>> + if (y1 > fb_height) >>>> + y1 = fb_height; >>>> + if (y2 > fb_height) >>>> + y2 = fb_height; >>>> if ((y2 - y1) == 1) { >>>> /* >>>> * We've only written to a single scanline. Try to reduce >>>> * the number of horizontal pixels that need an update. >>>> */ >>>> - off_t bit_off = (off % info->fix.line_length) * 8; >>>> - off_t bit_end = (end % info->fix.line_length) * 8; >>>> + off_t bit_off = (off % line_length) * 8; >>>> + off_t bit_end = (end % line_length) * 8; >>>> x1 = bit_off / info->var.bits_per_pixel; >>>> x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel); >>> >> -- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstrasse 146, 90461 Nuernberg, Germany GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)
Hi Thomas, On Thu, Apr 20, 2023 at 1:10 PM Thomas Zimmermann <tzimmermann@suse.de> wrote: > Am 20.04.23 um 12:04 schrieb Sui Jingfeng: > > What will happen if the 'screen_size' is not page_size aligned and mmap > > will mapping in the granularity of pages ? > > You need to map at page granularity. If screen_size is not page-size > aligned, there's this trailing buffer that is accessible, but cannot be > displayed. But userspace has no direct way of knowing that, so let's > ignore that problem for now. Userspace can know, if fb_fix_screeninfo.smem_{start,len} match the actual offset and size. Gr{oetje,eeting}s, Geert
Hi Am 20.04.23 um 14:25 schrieb Geert Uytterhoeven: > Hi Thomas, > > On Thu, Apr 20, 2023 at 1:10 PM Thomas Zimmermann <tzimmermann@suse.de> wrote: >> Am 20.04.23 um 12:04 schrieb Sui Jingfeng: >>> What will happen if the 'screen_size' is not page_size aligned and mmap >>> will mapping in the granularity of pages ? >> >> You need to map at page granularity. If screen_size is not page-size >> aligned, there's this trailing buffer that is accessible, but cannot be >> displayed. But userspace has no direct way of knowing that, so let's >> ignore that problem for now. > > Userspace can know, if fb_fix_screeninfo.smem_{start,len} match > the actual offset and size. Can you elaborate? How can userspace detect/compute the actually usable space? From grep'ing fbdev drivers, smem_len appears to be a multiple of the pagesize. (?) screen_size is not exported and line_length in the fixed portion. Or can line_length change between modes? In that case it should be (yres_virtual * line_length), right? Best regards Thomas > > Gr{oetje,eeting}s, > > Geert > -- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstrasse 146, 90461 Nuernberg, Germany GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)
Hi Thomas, On Thu, Apr 20, 2023 at 2:59 PM Thomas Zimmermann <tzimmermann@suse.de> wrote: > Am 20.04.23 um 14:25 schrieb Geert Uytterhoeven: > > On Thu, Apr 20, 2023 at 1:10 PM Thomas Zimmermann <tzimmermann@suse.de> wrote: > >> Am 20.04.23 um 12:04 schrieb Sui Jingfeng: > >>> What will happen if the 'screen_size' is not page_size aligned and mmap > >>> will mapping in the granularity of pages ? > >> > >> You need to map at page granularity. If screen_size is not page-size > >> aligned, there's this trailing buffer that is accessible, but cannot be > >> displayed. But userspace has no direct way of knowing that, so let's > >> ignore that problem for now. > > > > Userspace can know, if fb_fix_screeninfo.smem_{start,len} match > > the actual offset and size. > > Can you elaborate? How can userspace detect/compute the actually usable > space? By looking at fb_fix_screeninfo.smem_{start,len}, which are the (physical) address and length of the frame buffer. > From grep'ing fbdev drivers, smem_len appears to be a multiple of the > pagesize. (?) screen_size is not exported and line_length in the fixed > > portion. Or can line_length change between modes? In that case it should > be (yres_virtual * line_length), right? smem_{start,len} are not guaranteed to be page-aligned. Fbtest[1] and Xorg should handle that fine. line_length can change between modes. It's part of fb_fix_screeninfo, because it's fixed in the sense that it depends on the video mode, and the user cannot specify its value. [1] https://git.kernel.org/pub/scm/linux/kernel/git/geert/fbtest.git/tree/fb.c#n199 Gr{oetje,eeting}s, Geert
Hi Am 20.04.23 um 05:05 schrieb Sui Jingfeng: > The fbdev test of IGT may write after EOF, which lead to out-of-bound > access for drm drivers hire fbdev-generic. For example, run fbdev test > on a x86+ast2400 platform, with 1680x1050 resolution, will cause the > linux kernel hang with the following call trace: I've merged this patch into drm-misc-fixes. Thanks for the fix. Best regards Thomas > > Oops: 0000 [#1] PREEMPT SMP PTI > [IGT] fbdev: starting subtest eof > Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] > [IGT] fbdev: starting subtest nullptr > > RIP: 0010:memcpy_erms+0xa/0x20 > RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 > RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 > RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 > RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 > R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 > R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 > FS: 0000000000000000(0000) GS:ffff895257380000(0000) knlGS:0000000000000000 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 > Call Trace: > <TASK> > ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] > drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] > process_one_work+0x21f/0x430 > worker_thread+0x4e/0x3c0 > ? __pfx_worker_thread+0x10/0x10 > kthread+0xf4/0x120 > ? __pfx_kthread+0x10/0x10 > ret_from_fork+0x2c/0x50 > </TASK> > CR2: ffffa17d40e0b000 > ---[ end trace 0000000000000000 ]--- > > The is because damage rectangles computed by > drm_fb_helper_memory_range_to_clip() function does not guaranteed to be > bound in the screen's active display area. Possible reasons are: > > 1) Buffers are allocated in the granularity of page size, for mmap system > call support. The shadow screen buffer consumed by fbdev emulation may > also choosed be page size aligned. > > 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() > will introduce off-by-one error. > > For example, on a 16KB page size system, in order to store a 1920x1080 > XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size > 1920*1080*4 can not be divided exactly by 16KB. > > 1920 * 1080 * 4 = 8294400 bytes > 506 * 16 * 1024 = 8290304 bytes > 507 * 16 * 1024 = 8306688 bytes > > line_length = 1920*4 = 7680 bytes > > 507 * 16 * 1024 / 7680 = 1081.6 > > off / line_length = 507 * 16 * 1024 / 7680 = 1081 > DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 > > memcpy_toio() typically issue the copy line by line, when copy the last > line, out-of-bound access will be happen. Because: > > 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 > > Note that userspace may stil write to the invisiable area if a larger > buffer than width x stride is exposed. But it is not a big issue as > long as there still have memory resolve the access if not drafting so > far. > > - Also limit the y1 (Daniel) > - keep fix patch it to minimal (Daniel) > - screen_size is page size aligned because of it need mmap (Thomas) > - Adding fixes tag (Thomas) > > Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") > > Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> > Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> > Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> > Link: https://lore.kernel.org/dri-devel/ad44df29-3241-0d9e-e708-b0338bf3c623@189.cn/ > --- > drivers/gpu/drm/drm_fb_helper.c | 16 ++++++++++++---- > 1 file changed, 12 insertions(+), 4 deletions(-) > > diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c > index 64458982be40..6bb1b8b27d7a 100644 > --- a/drivers/gpu/drm/drm_fb_helper.c > +++ b/drivers/gpu/drm/drm_fb_helper.c > @@ -641,19 +641,27 @@ static void drm_fb_helper_damage(struct drm_fb_helper *helper, u32 x, u32 y, > static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, off_t off, size_t len, > struct drm_rect *clip) > { > + u32 line_length = info->fix.line_length; > + u32 fb_height = info->var.yres; > off_t end = off + len; > u32 x1 = 0; > - u32 y1 = off / info->fix.line_length; > + u32 y1 = off / line_length; > u32 x2 = info->var.xres; > - u32 y2 = DIV_ROUND_UP(end, info->fix.line_length); > + u32 y2 = DIV_ROUND_UP(end, line_length); > + > + /* Don't allow any of them beyond the bottom bound of display area */ > + if (y1 > fb_height) > + y1 = fb_height; > + if (y2 > fb_height) > + y2 = fb_height; > > if ((y2 - y1) == 1) { > /* > * We've only written to a single scanline. Try to reduce > * the number of horizontal pixels that need an update. > */ > - off_t bit_off = (off % info->fix.line_length) * 8; > - off_t bit_end = (end % info->fix.line_length) * 8; > + off_t bit_off = (off % line_length) * 8; > + off_t bit_end = (end % line_length) * 8; > > x1 = bit_off / info->var.bits_per_pixel; > x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel); -- Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Frankenstrasse 146, 90461 Nuernberg, Germany GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman HRB 36809 (AG Nuernberg)
Hi On 2023/4/21 16:09, Thomas Zimmermann wrote: > Hi > > Am 20.04.23 um 05:05 schrieb Sui Jingfeng: >> The fbdev test of IGT may write after EOF, which lead to out-of-bound >> access for drm drivers hire fbdev-generic. For example, run fbdev test >> on a x86+ast2400 platform, with 1680x1050 resolution, will cause the >> linux kernel hang with the following call trace: > > I've merged this patch into drm-misc-fixes. Thanks for the fix. > Thanks a lot! > Best regards > Thomas > >> >> Oops: 0000 [#1] PREEMPT SMP PTI >> [IGT] fbdev: starting subtest eof >> Workqueue: events drm_fb_helper_damage_work [drm_kms_helper] >> [IGT] fbdev: starting subtest nullptr >> >> RIP: 0010:memcpy_erms+0xa/0x20 >> RSP: 0018:ffffa17d40167d98 EFLAGS: 00010246 >> RAX: ffffa17d4eb7fa80 RBX: ffffa17d40e0aa80 RCX: 00000000000014c0 >> RDX: 0000000000001a40 RSI: ffffa17d40e0b000 RDI: ffffa17d4eb80000 >> RBP: ffffa17d40167e20 R08: 0000000000000000 R09: ffff89522ecff8c0 >> R10: ffffa17d4e4c5000 R11: 0000000000000000 R12: ffffa17d4eb7fa80 >> R13: 0000000000001a40 R14: 000000000000041a R15: ffffa17d40167e30 >> FS: 0000000000000000(0000) GS:ffff895257380000(0000) >> knlGS:0000000000000000 >> CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 >> CR2: ffffa17d40e0b000 CR3: 00000001eaeca006 CR4: 00000000001706e0 >> Call Trace: >> <TASK> >> ? drm_fbdev_generic_helper_fb_dirty+0x207/0x330 [drm_kms_helper] >> drm_fb_helper_damage_work+0x8f/0x170 [drm_kms_helper] >> process_one_work+0x21f/0x430 >> worker_thread+0x4e/0x3c0 >> ? __pfx_worker_thread+0x10/0x10 >> kthread+0xf4/0x120 >> ? __pfx_kthread+0x10/0x10 >> ret_from_fork+0x2c/0x50 >> </TASK> >> CR2: ffffa17d40e0b000 >> ---[ end trace 0000000000000000 ]--- >> >> The is because damage rectangles computed by >> drm_fb_helper_memory_range_to_clip() function does not guaranteed to be >> bound in the screen's active display area. Possible reasons are: >> >> 1) Buffers are allocated in the granularity of page size, for mmap >> system >> call support. The shadow screen buffer consumed by fbdev >> emulation may >> also choosed be page size aligned. >> >> 2) The DIV_ROUND_UP() used in drm_fb_helper_memory_range_to_clip() >> will introduce off-by-one error. >> >> For example, on a 16KB page size system, in order to store a 1920x1080 >> XRGB framebuffer, we need allocate 507 pages. Unfortunately, the size >> 1920*1080*4 can not be divided exactly by 16KB. >> >> 1920 * 1080 * 4 = 8294400 bytes >> 506 * 16 * 1024 = 8290304 bytes >> 507 * 16 * 1024 = 8306688 bytes >> >> line_length = 1920*4 = 7680 bytes >> >> 507 * 16 * 1024 / 7680 = 1081.6 >> >> off / line_length = 507 * 16 * 1024 / 7680 = 1081 >> DIV_ROUND_UP(507 * 16 * 1024, 7680) will yeild 1082 >> >> memcpy_toio() typically issue the copy line by line, when copy the last >> line, out-of-bound access will be happen. Because: >> >> 1082 * line_length = 1082 * 7680 = 8309760, and 8309760 > 8306688 >> >> Note that userspace may stil write to the invisiable area if a larger >> buffer than width x stride is exposed. But it is not a big issue as >> long as there still have memory resolve the access if not drafting so >> far. >> >> - Also limit the y1 (Daniel) >> - keep fix patch it to minimal (Daniel) >> - screen_size is page size aligned because of it need mmap (Thomas) >> - Adding fixes tag (Thomas) >> >> Fixes: aa15c677cc34 ("drm/fb-helper: Fix vertical damage clipping") >> >> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn> >> Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de> >> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> >> Link: >> https://lore.kernel.org/dri-devel/ad44df29-3241-0d9e-e708-b0338bf3c623@189.cn/ >> --- >> drivers/gpu/drm/drm_fb_helper.c | 16 ++++++++++++---- >> 1 file changed, 12 insertions(+), 4 deletions(-) >> >> diff --git a/drivers/gpu/drm/drm_fb_helper.c >> b/drivers/gpu/drm/drm_fb_helper.c >> index 64458982be40..6bb1b8b27d7a 100644 >> --- a/drivers/gpu/drm/drm_fb_helper.c >> +++ b/drivers/gpu/drm/drm_fb_helper.c >> @@ -641,19 +641,27 @@ static void drm_fb_helper_damage(struct >> drm_fb_helper *helper, u32 x, u32 y, >> static void drm_fb_helper_memory_range_to_clip(struct fb_info >> *info, off_t off, size_t len, >> struct drm_rect *clip) >> { >> + u32 line_length = info->fix.line_length; >> + u32 fb_height = info->var.yres; >> off_t end = off + len; >> u32 x1 = 0; >> - u32 y1 = off / info->fix.line_length; >> + u32 y1 = off / line_length; >> u32 x2 = info->var.xres; >> - u32 y2 = DIV_ROUND_UP(end, info->fix.line_length); >> + u32 y2 = DIV_ROUND_UP(end, line_length); >> + >> + /* Don't allow any of them beyond the bottom bound of display >> area */ >> + if (y1 > fb_height) >> + y1 = fb_height; >> + if (y2 > fb_height) >> + y2 = fb_height; >> if ((y2 - y1) == 1) { >> /* >> * We've only written to a single scanline. Try to reduce >> * the number of horizontal pixels that need an update. >> */ >> - off_t bit_off = (off % info->fix.line_length) * 8; >> - off_t bit_end = (end % info->fix.line_length) * 8; >> + off_t bit_off = (off % line_length) * 8; >> + off_t bit_end = (end % line_length) * 8; >> x1 = bit_off / info->var.bits_per_pixel; >> x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel); >
diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c index 64458982be40..6bb1b8b27d7a 100644 --- a/drivers/gpu/drm/drm_fb_helper.c +++ b/drivers/gpu/drm/drm_fb_helper.c @@ -641,19 +641,27 @@ static void drm_fb_helper_damage(struct drm_fb_helper *helper, u32 x, u32 y, static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, off_t off, size_t len, struct drm_rect *clip) { + u32 line_length = info->fix.line_length; + u32 fb_height = info->var.yres; off_t end = off + len; u32 x1 = 0; - u32 y1 = off / info->fix.line_length; + u32 y1 = off / line_length; u32 x2 = info->var.xres; - u32 y2 = DIV_ROUND_UP(end, info->fix.line_length); + u32 y2 = DIV_ROUND_UP(end, line_length); + + /* Don't allow any of them beyond the bottom bound of display area */ + if (y1 > fb_height) + y1 = fb_height; + if (y2 > fb_height) + y2 = fb_height; if ((y2 - y1) == 1) { /* * We've only written to a single scanline. Try to reduce * the number of horizontal pixels that need an update. */ - off_t bit_off = (off % info->fix.line_length) * 8; - off_t bit_end = (end % info->fix.line_length) * 8; + off_t bit_off = (off % line_length) * 8; + off_t bit_end = (end % line_length) * 8; x1 = bit_off / info->var.bits_per_pixel; x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel);