From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85C09C388F9 for ; Tue, 27 Oct 2020 16:58:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 92B4621D7B for ; Tue, 27 Oct 2020 16:58:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b="jVifv3wc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 92B4621D7B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A71E46B0072; Tue, 27 Oct 2020 12:58:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A22EA6B0073; Tue, 27 Oct 2020 12:58:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E9356B0074; Tue, 27 Oct 2020 12:58:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0157.hostedemail.com [216.40.44.157]) by kanga.kvack.org (Postfix) with ESMTP id 61F226B0072 for ; Tue, 27 Oct 2020 12:58:57 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 044A68249980 for ; Tue, 27 Oct 2020 16:58:57 +0000 (UTC) X-FDA: 77418315114.20.cakes56_1a06b8e2727d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id CF6E0180C07AF for ; Tue, 27 Oct 2020 16:58:56 +0000 (UTC) X-HE-Tag: cakes56_1a06b8e2727d X-Filterd-Recvd-Size: 6597 Received: from mail-ej1-f66.google.com (mail-ej1-f66.google.com [209.85.218.66]) by imf18.hostedemail.com (Postfix) with ESMTP for ; Tue, 27 Oct 2020 16:58:56 +0000 (UTC) Received: by mail-ej1-f66.google.com with SMTP id p9so3268825eji.4 for ; Tue, 27 Oct 2020 09:58:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=jbe/1MUIOazjJNoYCLbZSaF/EYC3tySch0SP+bPnG10=; b=jVifv3wcHJtbLJO22cQhSQScaHCOkHk0uy60SV24XvvF7WpqIaa807hmufMDVKqnPF 3xm47weUtJacIjSqbcrwXivMAtenN13kZafFSMO2m8dHHXFLXYX56OUdOoMTcRWlbgDG Thpj0UzfAOOdteCoRx63Xv7ZHZnjkyReDZ1Swoq+LyDarWGF0Mhj1KXWH3CO+MSgLHKb ZCBeQPWb0noZpqy+qKiKznJoq/vsPZZzkzwPD8DcAe8kPG1EHjLxd45J8Yu892ID37T7 4/FpLOCqsI/vnQoRJn422RcKliIdkwc27vyhzCgMqyr6Vk10qaJ+O1CUPcx0OY5r8WIV v25A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=jbe/1MUIOazjJNoYCLbZSaF/EYC3tySch0SP+bPnG10=; b=YGaNJiV9ggtzdoGdJR5W2HHhgaG1mv9NQVgSAILpAu/A2mWkcf8xEA5XadnO4gFNZT SBshLeiP0LKtRTJ/jz9FTC7Saj3vqdA7zOX+jiyJcCQmNFENlzW3xlqT5+/QmyGwRgAT fUkOxRy9lUPh2xCJTWCA3w5+TqLS6oizM60uWSWQSKxjfoZF2QxCemjQww4vzouAHfiP za2hWkHvKs/qlJT6b6n3yzImwqwxyAqSwQ1MHOBNsg5nqaht4WzJZe8uw5iKaBkENzI+ ob8GF4CojCzeWMhD4T5/0xov5qdWuvm+1EXqpdczfBz2sfauRMTT+RkukCIuIowkGXoW fRxQ== X-Gm-Message-State: AOAM533kJaVa/PBJQEWu2lELM9EdDNhUbrEOk4GM5Xyz9+0b3nI1XI5d ZHg1Wgr7usfJL8LD2X1MOqdirPc2fHlnl+HYtADxnA== X-Google-Smtp-Source: ABdhPJzA1XzwQDZrf0aO0vJ0GFK7TlKkhiOIHp72TDLrdQ3tpl8EeLXanxCM3mEYAMp7LHDhMqHnk4D8XhUXPqwrAS0= X-Received: by 2002:a17:906:d159:: with SMTP id br25mr3508153ejb.155.1603817934690; Tue, 27 Oct 2020 09:58:54 -0700 (PDT) MIME-Version: 1.0 References: <20201027121725.24660-1-brgl@bgdev.pl> <20201027121725.24660-4-brgl@bgdev.pl> <20201027112607-mutt-send-email-mst@kernel.org> <685d850347a1191bba8ba7766fc409b140d18f03.camel@perches.com> In-Reply-To: <685d850347a1191bba8ba7766fc409b140d18f03.camel@perches.com> From: Bartosz Golaszewski Date: Tue, 27 Oct 2020 17:58:43 +0100 Message-ID: Subject: Re: [PATCH 3/8] vhost: vringh: use krealloc_array() To: Joe Perches Cc: "Michael S. Tsirkin" , Bartosz Golaszewski , Andy Shevchenko , Sumit Semwal , Gustavo Padovan , =?UTF-8?Q?Christian_K=C3=B6nig?= , Mauro Carvalho Chehab , Borislav Petkov , Tony Luck , James Morse , Robert Richter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Alexander Shishkin , Linus Walleij , Jason Wang , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Jaroslav Kysela , Takashi Iwai , linux-media , linux-drm , linaro-mm-sig@lists.linaro.org, LKML , linux-edac@vger.kernel.org, linux-gpio , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev , linux-mm@kvack.org, Linux-ALSA Content-Type: text/plain; charset="UTF-8" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Oct 27, 2020 at 5:50 PM Joe Perches wrote: > > On Tue, 2020-10-27 at 11:28 -0400, Michael S. Tsirkin wrote: > > On Tue, Oct 27, 2020 at 01:17:20PM +0100, Bartosz Golaszewski wrote: > > > From: Bartosz Golaszewski > > > > > > Use the helper that checks for overflows internally instead of manually > > > calculating the size of the new array. > > > > > > Signed-off-by: Bartosz Golaszewski > > > > No problem with the patch, it does introduce some symmetry in the code. > > Perhaps more symmetry by using kmemdup > --- > drivers/vhost/vringh.c | 23 ++++++++++------------- > 1 file changed, 10 insertions(+), 13 deletions(-) > > diff --git a/drivers/vhost/vringh.c b/drivers/vhost/vringh.c > index 8bd8b403f087..99222a3651cd 100644 > --- a/drivers/vhost/vringh.c > +++ b/drivers/vhost/vringh.c > @@ -191,26 +191,23 @@ static int move_to_indirect(const struct vringh *vrh, > static int resize_iovec(struct vringh_kiov *iov, gfp_t gfp) > { > struct kvec *new; > - unsigned int flag, new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2; > + size_t new_num = (iov->max_num & ~VRINGH_IOV_ALLOCATED) * 2; > + size_t size; > > if (new_num < 8) > new_num = 8; > > - flag = (iov->max_num & VRINGH_IOV_ALLOCATED); > - if (flag) > - new = krealloc(iov->iov, new_num * sizeof(struct iovec), gfp); > - else { > - new = kmalloc_array(new_num, sizeof(struct iovec), gfp); > - if (new) { > - memcpy(new, iov->iov, > - iov->max_num * sizeof(struct iovec)); > - flag = VRINGH_IOV_ALLOCATED; > - } > - } > + if (unlikely(check_mul_overflow(new_num, sizeof(struct iovec), &size))) > + return -ENOMEM; > + The whole point of using helpers such as kmalloc_array() is not doing these checks manually. Bartosz > + if (iov->max_num & VRINGH_IOV_ALLOCATED) > + new = krealloc(iov->iov, size, gfp); > + else > + new = kmemdup(iov->iov, size, gfp); > if (!new) > return -ENOMEM; > iov->iov = new; > - iov->max_num = (new_num | flag); > + iov->max_num = new_num | VRINGH_IOV_ALLOCATED; > return 0; > } > > >