Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp961266imw; Fri, 15 Jul 2022 16:55:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sCD+pPbLxyRxW7sp9HUkvhUvyxXyR70LFNmJfvGFRyp3d36HEiFkjOxL5y9gL2JZtqBRl2 X-Received: by 2002:a17:902:c102:b0:16c:acf1:60e7 with SMTP id 2-20020a170902c10200b0016cacf160e7mr11161516pli.93.1657929345026; Fri, 15 Jul 2022 16:55:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657929345; cv=none; d=google.com; s=arc-20160816; b=yfK11yNxYOHmFp3PVRSeWgj/KfeQUL8a5XzP+dE4VnAlJJQwSdijOtVnDcVwV/Mmw8 zpj+5cXy8wJg//GSkExWDMCP3AJCw296dmD2kuTiRkprENxTfgbiGZQbJMAYL/VzFiNE 7C7gssIJiU9Li0azcgCUzP/UJ5/2l9KohGbPqMnjvW+TNdtPFMY6V4RhjJ1kaPZbutan Zrv6b3Y9rluzRlouNTQ/KHekkqtavuuGctKC9E5SOyQK/CslW240qGb+VfSByw4yrzOb p7hPai1eHRmkuKLnR90lC2W8bwVatnPjKg6Pn1AHt52okVELgX07gHXB/5/CVVZD0uH2 onPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:date:from:references:in-reply-to :message-id:dkim-signature; bh=RZjBSUb81N+yJRTwiRNL5/W/pr19dZwDiM2ueBOpRVE=; b=X1JtQIkTmSmsppFeE4jU3iH/1Y/K4eQYbujSO1DDPgGED5XJOKYX/X65Y4b6qYK2dd MiVHs7fdXupmLsF569Pgnf0k6KcweOXZk2eQsyQ9gTN4sPNhq3SYcFkUfHV3PW5sIvnO YTKJpSZnwx0GoVoQmZ9D7dwvsuiHbNr2ScjIZAxbA+9ETDd0ai2vxVEi/ksNpXZExZaI Def+gXW/OcG15yctAUFyeR7AUVZH/UfPKaHNJQ4OoWeB97YilLtD8+oA08XThsCPvDbI nd22mSSMl56dnuHWF8kLgUdGDYrGWVF4j4aHaenzJ70m65eeF42lrDutJdZVOxTyQCis DSSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crudebyte.com header.s=lizzy header.b="L/yyuD0A"; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=crudebyte.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lb10-20020a17090b4a4a00b001ecdcd18eb7si6300757pjb.154.2022.07.15.16.55.28; Fri, 15 Jul 2022 16:55:45 -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; dkim=pass header.i=@crudebyte.com header.s=lizzy header.b="L/yyuD0A"; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=crudebyte.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232338AbiGOXZm (ORCPT + 99 others); Fri, 15 Jul 2022 19:25:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232142AbiGOXZf (ORCPT ); Fri, 15 Jul 2022 19:25:35 -0400 X-Greylist: delayed 1792 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 15 Jul 2022 16:25:33 PDT Received: from lizzy.crudebyte.com (lizzy.crudebyte.com [91.194.90.13]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D84355467A for ; Fri, 15 Jul 2022 16:25:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=crudebyte.com; s=lizzy; h=Cc:To:Subject:Date:From:References:In-Reply-To: Message-Id:Content-Type:Content-Transfer-Encoding:MIME-Version:Content-ID: Content-Description; bh=RZjBSUb81N+yJRTwiRNL5/W/pr19dZwDiM2ueBOpRVE=; b=L/yyu D0A+4p87Zus6SXcq23d2tfpLD4mIu6FsarA91Xzi3wJwCS8oqxAkOvHsxkuqItYO6pdQx+eukfKH6 Jw9pau/Hy5Dcyjh+MSE7XUdN2fz5YKZFrKgwqJqysxvupHKEiccj1Vl2DABzV0cwkuEIPH+gtmOe1 wSzetu+b46C3jRzve4he/wJ1tqoeN7Nt41KzAinwaglqp5qB6f0mo2Cak5wR4WSUnIYo4yoZRfElB Xue5BZuGjjVgcWGmgkkiXfHCqwwOdmJ2036/tE6cKaezeW2+HzV+JHH9Nm4mAJ30ET8cj9dwrmbpk s3wvoU7jpiMswsU4l+4gVra3W0nNw==; Message-Id: <006266b4ef857c8218b517b83db5835b4908ebe3.1657920926.git.linux_oss@crudebyte.com> In-Reply-To: References: From: Christian Schoenebeck Date: Fri, 15 Jul 2022 23:32:25 +0200 Subject: [PATCH v6 06/11] 9p/trans_virtio: resize sg lists to whatever is possible To: v9fs-developer@lists.sourceforge.net Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Dominique Martinet , Eric Van Hensbergen , Latchesar Ionkov , Nikolay Kichukov X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_NONE 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: X-Mailing-List: linux-kernel@vger.kernel.org Right now vq_sg_resize() used a lazy implementation following the all-or-nothing princible. So it either resized exactly to the requested new amount of sg lists, or it did not resize at all. The problem with this is if a user supplies a very large msize value, resize would simply fail and the user would stick to the default maximum msize supported by the virtio transport. To resolve this potential issue, change vq_sg_resize() to resize the passed sg list to whatever is possible on the machine. Signed-off-by: Christian Schoenebeck --- As previously discussed in v5, this patch could probably be dropped. net/9p/trans_virtio.c | 76 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c index 921caa022570..52d00cb3c105 100644 --- a/net/9p/trans_virtio.c +++ b/net/9p/trans_virtio.c @@ -218,24 +218,67 @@ static struct virtqueue_sg *vq_sg_alloc(unsigned int nsgl) * amount of lists * @_vq_sg: scatter/gather lists to be resized * @nsgl: new amount of scatter/gather lists + * + * Old scatter/gather lists are retained. Only growing the size is supported. + * If the requested amount cannot be satisfied, then lists are increased to + * whatever is possible. */ static int vq_sg_resize(struct virtqueue_sg **_vq_sg, unsigned int nsgl) { struct virtqueue_sg *vq_sg; + unsigned int i; + size_t sz; + int ret = 0; BUG_ON(!_vq_sg || !nsgl); vq_sg = *_vq_sg; + if (nsgl > VIRTQUEUE_SG_NSGL_MAX) + nsgl = VIRTQUEUE_SG_NSGL_MAX; if (vq_sg->nsgl == nsgl) return 0; + if (vq_sg->nsgl > nsgl) + return -ENOTSUPP; + + vq_sg = kzalloc(sizeof(struct virtqueue_sg) + + nsgl * sizeof(struct scatterlist *), + GFP_KERNEL); - /* lazy resize implementation for now */ - vq_sg = vq_sg_alloc(nsgl); if (!vq_sg) return -ENOMEM; + /* copy over old scatter gather lists */ + sz = sizeof(struct virtqueue_sg) + + (*_vq_sg)->nsgl * sizeof(struct scatterlist *); + memcpy(vq_sg, *_vq_sg, sz); + + vq_sg->nsgl = nsgl; + + for (i = (*_vq_sg)->nsgl; i < nsgl; ++i) { + vq_sg->sgl[i] = kmalloc_array( + SG_MAX_SINGLE_ALLOC, sizeof(struct scatterlist), + GFP_KERNEL + ); + /* + * handle failed allocation as soft error, we take whatever + * we get + */ + if (!vq_sg->sgl[i]) { + ret = -ENOMEM; + vq_sg->nsgl = nsgl = i; + break; + } + sg_init_table(vq_sg->sgl[i], SG_MAX_SINGLE_ALLOC); + if (i) { + /* chain the lists */ + sg_chain(vq_sg->sgl[i - 1], SG_MAX_SINGLE_ALLOC, + vq_sg->sgl[i]); + } + } + sg_mark_end(&vq_sg->sgl[nsgl - 1][SG_MAX_SINGLE_ALLOC - 1]); + kfree(*_vq_sg); *_vq_sg = vq_sg; - return 0; + return ret; } /** @@ -860,16 +903,23 @@ p9_virtio_create(struct p9_client *client, const char *devname, char *args) * if resize fails, no big deal, then just continue with * whatever we got */ - if (!vq_sg_resize(&chan->vq_sg, nsgl)) { - /* - * decrement 2 pages as both 9p request and 9p reply have - * to fit into the virtio round-trip message - */ - client->trans_maxsize = - PAGE_SIZE * - clamp_t(int, - (nsgl * SG_USER_PAGES_PER_LIST) - 2, - 0, VIRTIO_MAX_DESCRIPTORS - 2); + vq_sg_resize(&chan->vq_sg, nsgl); + /* + * actual allocation size might be less than requested, so use + * vq_sg->nsgl instead of nsgl, and decrement 2 pages as both + * 9p request and 9p reply have to fit into the virtio + * round-trip message + */ + client->trans_maxsize = + PAGE_SIZE * + clamp_t(int, + (chan->vq_sg->nsgl * SG_USER_PAGES_PER_LIST) - 2, + 0, VIRTIO_MAX_DESCRIPTORS - 2); + if (nsgl > chan->vq_sg->nsgl) { + pr_info("limiting 'msize' to %d as only %d " + "of %zu SG lists could be allocated", + client->trans_maxsize, + chan->vq_sg->nsgl, nsgl); } } #endif /* CONFIG_ARCH_NO_SG_CHAIN */ -- 2.30.2