From nobody Sat Oct 4 19:15:10 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A2072798E1; Thu, 14 Aug 2025 07:25:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755156321; cv=none; b=FlmYv3LtqgvTMGKOjAiOP55tc61N6EOch93ntBHHOL5NisDWvLBd3vtgwp6OK8ABlUcmwxN4MNLVQRFtdddnCgC/nSLMqQdtwcKgqezQi9WXzZItissI/TbHxjSp9jq2vTi0qKpO7tYGcghWwZtDvmJ8E+Cba6eBtjsQzULK6mA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755156321; c=relaxed/simple; bh=ikP8D9+lVXMFYOcdbTvUdAH0C7+FbtOqJ0QHfm3h56s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fFw2+nb9+996aSZ7ObZbtEGkZrt3ny5iT6qAYdAeb9lQd+4KcXxiPxOjrBjAMRVWJvjgSWXvG5gLEl2+kgVF+cZNpmq6y8NBLNOdVjinHD2Nu/uOkT96UvKnt/FbeK7KrrxjF+mGztTohvqAqZmnAQSgRBg/AUi/dKDXt3N2hZs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lx+X+Zco; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lx+X+Zco" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCD36C4CEF4; Thu, 14 Aug 2025 07:25:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755156320; bh=ikP8D9+lVXMFYOcdbTvUdAH0C7+FbtOqJ0QHfm3h56s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lx+X+ZcoYMK4SRF+LjGdMB2nmlbx0kSbYgyVH4F+b/iC/ThhKYft87mpMZFg+SY5d 3kh3HNA9eEkARzCA+lYhFwTRZ0oDRXEnJidte6LAIo+AMsIAqpVe5nm0VcNPBMDpbG VtRS5crBZYc3+JhDyb7QhPL6AE7/kJmRrFhaydSOGqfSfoV9zFVYYw3QiE0C6gIE4T P1XMIdkjDIfsGQbrTJg51rKHdANqyhPMG85CTBah7Xd8zJ6N0k38oZla5gVeAwvjKt AQUCKwvDearKpyH4DEQlY6OysbzhUlL8GxZmmAPS81Jzgb6WV7HkhoIMfBEQCMgIRB v6LhTFExkZYWw== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 14/16] tty/vt: use guard()s in con_font_set/get() and con_{set,get}_unimap() Date: Thu, 14 Aug 2025 09:24:54 +0200 Message-ID: <20250814072456.182853-15-jirislaby@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250814072456.182853-1-jirislaby@kernel.org> References: <20250814072456.182853-1-jirislaby@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Having all the new guards, use them in the 8250_rsa code. This improves readability, makes error handling easier, and marks locked portions of code explicit. The new __free()-annotated declarations are moved to the allocation points as is preferred: https://lore.kernel.org/all/CAHk-=3Dwjvh_LUpa=3D864joG2JJXs3+viO-kLzLidR2JL= yMr4MNwA@mail.gmail.com/ Except font_data in con_font_get(). The scope in there would not match the expected lifetime. But the declaration is moved closer. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/vt/consolemap.c | 84 ++++++++++++++++------------------ drivers/tty/vt/vt.c | 89 +++++++++++++++++-------------------- 2 files changed, 78 insertions(+), 95 deletions(-) diff --git a/drivers/tty/vt/consolemap.c b/drivers/tty/vt/consolemap.c index bb4bb272ebec..8eb9d745a868 100644 --- a/drivers/tty/vt/consolemap.c +++ b/drivers/tty/vt/consolemap.c @@ -637,32 +637,28 @@ static struct uni_pagedict *con_unshare_unimap(struct= vc_data *vc, =20 int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *l= ist) { - int err =3D 0, err1; struct uni_pagedict *dict; - struct unipair *unilist, *plist; + struct unipair *plist; + int err =3D 0; =20 if (!ct) return 0; =20 - unilist =3D vmemdup_array_user(list, ct, sizeof(*unilist)); + struct unipair *unilist __free(kvfree) =3D vmemdup_array_user(list, ct, s= izeof(*unilist)); if (IS_ERR(unilist)) return PTR_ERR(unilist); =20 - console_lock(); + guard(console_lock)(); =20 /* Save original vc_unipagdir_loc in case we allocate a new one */ dict =3D *vc->uni_pagedict_loc; - if (!dict) { - err =3D -EINVAL; - goto out_unlock; - } + if (!dict) + return -EINVAL; =20 if (dict->refcount > 1) { dict =3D con_unshare_unimap(vc, dict); - if (IS_ERR(dict)) { - err =3D PTR_ERR(dict); - goto out_unlock; - } + if (IS_ERR(dict)) + return PTR_ERR(dict); } else if (dict =3D=3D dflt) { dflt =3D NULL; } @@ -671,7 +667,7 @@ int con_set_unimap(struct vc_data *vc, ushort ct, struc= t unipair __user *list) * Insert user specified unicode pairs into new table. */ for (plist =3D unilist; ct; ct--, plist++) { - err1 =3D con_insert_unipair(dict, plist->unicode, plist->fontpos); + int err1 =3D con_insert_unipair(dict, plist->unicode, plist->fontpos); if (err1) err =3D err1; } @@ -680,15 +676,12 @@ int con_set_unimap(struct vc_data *vc, ushort ct, str= uct unipair __user *list) * Merge with fontmaps of any other virtual consoles. */ if (con_unify_unimap(vc, dict)) - goto out_unlock; + return err; =20 for (enum translation_map m =3D FIRST_MAP; m <=3D LAST_MAP; m++) set_inverse_transl(vc, dict, m); set_inverse_trans_unicode(dict); =20 -out_unlock: - console_unlock(); - kvfree(unilist); return err; } =20 @@ -787,50 +780,49 @@ int con_get_unimap(struct vc_data *vc, ushort ct, ush= ort __user *uct, { ushort ect; struct uni_pagedict *dict; - struct unipair *unilist; unsigned int d, r, g; - int ret =3D 0; =20 - unilist =3D kvmalloc_array(ct, sizeof(*unilist), GFP_KERNEL); + struct unipair *unilist __free(kvfree) =3D kvmalloc_array(ct, sizeof(*uni= list), GFP_KERNEL); if (!unilist) return -ENOMEM; =20 - console_lock(); - - ect =3D 0; - dict =3D *vc->uni_pagedict_loc; - if (!dict) - goto unlock; - - for (d =3D 0; d < UNI_DIRS; d++) { - u16 **dir =3D dict->uni_pgdir[d]; - if (!dir) - continue; + scoped_guard(console_lock) { + ect =3D 0; + dict =3D *vc->uni_pagedict_loc; + if (!dict) + break; =20 - for (r =3D 0; r < UNI_DIR_ROWS; r++) { - u16 *row =3D dir[r]; - if (!row) + for (d =3D 0; d < UNI_DIRS; d++) { + u16 **dir =3D dict->uni_pgdir[d]; + if (!dir) continue; =20 - for (g =3D 0; g < UNI_ROW_GLYPHS; g++, row++) { - if (*row >=3D MAX_GLYPH) + for (r =3D 0; r < UNI_DIR_ROWS; r++) { + u16 *row =3D dir[r]; + if (!row) continue; - if (ect < ct) { - unilist[ect].unicode =3D UNI(d, r, g); - unilist[ect].fontpos =3D *row; + + for (g =3D 0; g < UNI_ROW_GLYPHS; g++, row++) { + if (*row >=3D MAX_GLYPH) + continue; + if (ect < ct) { + unilist[ect].unicode =3D UNI(d, r, g); + unilist[ect].fontpos =3D *row; + } + ect++; } - ect++; } } } -unlock: - console_unlock(); + if (copy_to_user(list, unilist, min(ect, ct) * sizeof(*unilist))) - ret =3D -EFAULT; + return -EFAULT; if (put_user(ect, uct)) - ret =3D -EFAULT; - kvfree(unilist); - return ret ? ret : (ect <=3D ct) ? 0 : -ENOMEM; + return -EFAULT; + if (ect > ct) + return -ENOMEM; + + return 0; } =20 /* diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c index 62049ceb34de..100d6cb26887 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -4801,57 +4801,51 @@ void reset_palette(struct vc_data *vc) static int con_font_get(struct vc_data *vc, struct console_font_op *op) { struct console_font font; - int rc =3D -EINVAL; int c; unsigned int vpitch =3D op->op =3D=3D KD_FONT_OP_GET_TALL ? op->height : = 32; =20 if (vpitch > max_font_height) return -EINVAL; =20 + void *font_data __free(kvfree) =3D NULL; if (op->data) { - font.data =3D kvzalloc(max_font_size, GFP_KERNEL); + font.data =3D font_data =3D kvzalloc(max_font_size, GFP_KERNEL); if (!font.data) return -ENOMEM; } else font.data =3D NULL; =20 - console_lock(); - if (vc->vc_mode !=3D KD_TEXT) - rc =3D -EINVAL; - else if (vc->vc_sw->con_font_get) - rc =3D vc->vc_sw->con_font_get(vc, &font, vpitch); - else - rc =3D -ENOSYS; - console_unlock(); + scoped_guard(console_lock) { + if (vc->vc_mode !=3D KD_TEXT) + return -EINVAL; + if (!vc->vc_sw->con_font_get) + return -ENOSYS; =20 - if (rc) - goto out; + int ret =3D vc->vc_sw->con_font_get(vc, &font, vpitch); + if (ret) + return ret; + } =20 c =3D (font.width+7)/8 * vpitch * font.charcount; =20 if (op->data && font.charcount > op->charcount) - rc =3D -ENOSPC; + return -ENOSPC; if (font.width > op->width || font.height > op->height) - rc =3D -ENOSPC; - if (rc) - goto out; + return -ENOSPC; =20 op->height =3D font.height; op->width =3D font.width; op->charcount =3D font.charcount; =20 if (op->data && copy_to_user(op->data, font.data, c)) - rc =3D -EFAULT; + return -EFAULT; =20 -out: - kvfree(font.data); - return rc; + return 0; } =20 static int con_font_set(struct vc_data *vc, const struct console_font_op *= op) { struct console_font font; - int rc =3D -EINVAL; int size; unsigned int vpitch =3D op->op =3D=3D KD_FONT_OP_SET_TALL ? op->height : = 32; =20 @@ -4870,7 +4864,7 @@ static int con_font_set(struct vc_data *vc, const str= uct console_font_op *op) if (size > max_font_size) return -ENOSPC; =20 - font.data =3D memdup_user(op->data, size); + void *font_data __free(kfree) =3D font.data =3D memdup_user(op->data, siz= e); if (IS_ERR(font.data)) return PTR_ERR(font.data); =20 @@ -4878,18 +4872,17 @@ static int con_font_set(struct vc_data *vc, const s= truct console_font_op *op) font.width =3D op->width; font.height =3D op->height; =20 - console_lock(); + guard(console_lock)(); + if (vc->vc_mode !=3D KD_TEXT) - rc =3D -EINVAL; - else if (vc->vc_sw->con_font_set) { - if (vc_is_sel(vc)) - clear_selection(); - rc =3D vc->vc_sw->con_font_set(vc, &font, vpitch, op->flags); - } else - rc =3D -ENOSYS; - console_unlock(); - kfree(font.data); - return rc; + return -EINVAL; + if (!vc->vc_sw->con_font_set) + return -ENOSYS; + + if (vc_is_sel(vc)) + clear_selection(); + + return vc->vc_sw->con_font_set(vc, &font, vpitch, op->flags); } =20 static int con_font_default(struct vc_data *vc, struct console_font_op *op) @@ -4897,8 +4890,6 @@ static int con_font_default(struct vc_data *vc, struc= t console_font_op *op) struct console_font font =3D {.width =3D op->width, .height =3D op->heigh= t}; char name[MAX_FONT_NAME]; char *s =3D name; - int rc; - =20 if (!op->data) s =3D NULL; @@ -4907,23 +4898,23 @@ static int con_font_default(struct vc_data *vc, str= uct console_font_op *op) else name[MAX_FONT_NAME - 1] =3D 0; =20 - console_lock(); - if (vc->vc_mode !=3D KD_TEXT) { - console_unlock(); - return -EINVAL; - } - if (vc->vc_sw->con_font_default) { + scoped_guard(console_lock) { + if (vc->vc_mode !=3D KD_TEXT) + return -EINVAL; + if (!vc->vc_sw->con_font_default) + return -ENOSYS; + if (vc_is_sel(vc)) clear_selection(); - rc =3D vc->vc_sw->con_font_default(vc, &font, s); - } else - rc =3D -ENOSYS; - console_unlock(); - if (!rc) { - op->width =3D font.width; - op->height =3D font.height; + int ret =3D vc->vc_sw->con_font_default(vc, &font, s); + if (ret) + return ret; } - return rc; + + op->width =3D font.width; + op->height =3D font.height; + + return 0; } =20 int con_font_op(struct vc_data *vc, struct console_font_op *op) --=20 2.50.1