Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1100523rbb; Sun, 25 Feb 2024 20:31:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU/kFDTqWGXVwBDBqCW7aPCvU7kS86go7sUn3uEC2rIJz+a9gMoD8y8PZVI2Tm8+1CeB1IxzoRm+onvJK/j49TBpVoaJM1tEAnkYEzI9Q== X-Google-Smtp-Source: AGHT+IGncx/EB0h1FykWzgSmvA87i9XoHPl7Y2aPpLC5ggXD1ql82EmrqKz27kTEWLxviGUvkRVS X-Received: by 2002:a05:6512:3ca5:b0:512:aa57:f38b with SMTP id h37-20020a0565123ca500b00512aa57f38bmr4969596lfv.53.1708921896436; Sun, 25 Feb 2024 20:31:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708921896; cv=pass; d=google.com; s=arc-20160816; b=wBNj0LI9y6CtsR5ag7NnyT0SFIkpqTfkTtakb6XPG8d7mt/ZyLik4IlK/08SSe+ahS fyADGkhSHAcqEFhK6LqYnNjQbi8PlbJKQzerVNLGj4uHcTnP07BE5Lsex3j1Tdd9ONOJ EUiqaX/6tTOPm2wA1rnG1l3rre8voIF7S9CzcOPM2S2Oi9+pvkECKKNqMj95gzbAW5xP 4Jw6hiHfZ8YTnf6Hy5iM1tds0nptpkqrccqxhPY+K/tfYkw0Uc3mjq72fNB2Vdv312K4 yM9u+20HmLgCVaMBn4eI3LNJQjQDmgK0yCKgPJVmxltQLmC4dJ5VKWwSSV3aij8Q0TBb SwVA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=DW6Gg9E4iphRs2T7njr0TuJitmVKKihxKcXnC4puSug=; fh=0+BX6kfOi1UGaRH9uCRB06kExYysuKlTjn7xnoIkun4=; b=JuE2d/y0vFARE7cC5pj6M2efW3P9n2MU/LdDts/PmK8qDAd3qIEasbV8BRuzbNjgsJ 9LwxwHAVdTCZEx2dgYnKqK5ayQASG2dNY25MgcX7LCMvjuE0VOZEYkxXhbF8wgbtxB4X O3YZVsYNWo0j0ZYWP0hOZvnlb62DelGHq53djpuwObT1HL7+v+ExZNULokb9NOXqRvsA UPFBB+t3QNDPCu++PG66dPyFbBgyfTNfUtkQwDBUcpSASCupi7iJIoE4NE6KPHvAgAoJ FwERlbp9HB8WJBX/zwH+COdWspcRH3JZvU+SZp+9V+A1DWZShMw4JhZq34RcQreT+HOx i0Cw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=DhUMw5zz; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-80549-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80549-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id y22-20020a170906449600b00a3fd13eb0b6si1738678ejo.418.2024.02.25.20.31.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 20:31:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80549-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=DhUMw5zz; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-80549-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80549-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id D24391F24EEE for ; Mon, 26 Feb 2024 04:26:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1757F1AACD; Mon, 26 Feb 2024 04:26:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DhUMw5zz" Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 256311759F; Mon, 26 Feb 2024 04:25:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708921558; cv=none; b=PqUXmsm+qWIe31ZqzOOyJ4y8DF9i/igOPkJeaavUKP9XK0kWS1bpjH11m9+P12KAy/xBn8ssDm95yThgEIv0mXa0+zdhv9AoYDZCmwuEaKtf9AkyKRNHvU7MzF4w1lwODifYkPEJnkiks3Psp9j87gN+VQF2nqKtspx1HO+0yUo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708921558; c=relaxed/simple; bh=v3SlkIW6y6ipn1NCWJZvMnmhDwH0Fh2zjcrmBtZ0vWU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=dNTPKlzbZvQ50oNpDm49XaX80lVFKIu0REKx0MARwCjez2nT2X2bCmwTeOqSfg0QdqhPyhSdesKTANvzJbLg5T2UALuy/mkqbDw1Qv+LXe2jXvF79m0k2hhIQfqtdDhBFhx2zf9QO8p4dvHGtqyUNEY0RkJKH0zRGmdeyGAsy9A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DhUMw5zz; arc=none smtp.client-ip=209.85.222.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qk1-f170.google.com with SMTP id af79cd13be357-787aca0b502so85563585a.1; Sun, 25 Feb 2024 20:25:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708921556; x=1709526356; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=DW6Gg9E4iphRs2T7njr0TuJitmVKKihxKcXnC4puSug=; b=DhUMw5zzGr6ThqogwfYEY+SFnPv0YsGgobL0ItCp7cyZzJj/Yq7lAxmelVytHT3xhy lTDDGlSJTmO8ebwvh5oIBIM76brt4PwuynPuCBrYWLH7yoXhpaJ/vB+yfM41E4ffPRqs T3rhHPUTSTz0MO3rbhi4KYluhR8PdPAT/UCz3OtpG90g5m3UwLt1opQyQKsyyHlGmaBc GauwvP7J+YNeJtkb8m/7wWMQsvMCtRIAKnzT3+86t1eWvOJagTxY4oKZeb4yh6AVBtQ7 4phTrP6LgLyuYECSpADufU+DTlFo2B+S1VVRsOOuXab9oFbOdi20IkiJQVo7f7S6uFL7 aWJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708921556; x=1709526356; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DW6Gg9E4iphRs2T7njr0TuJitmVKKihxKcXnC4puSug=; b=DJeBEMwfEJczk8KK9JJ2eIRK3Gs9Z463TE/XV6J3vCiP9t3yGQbn/jgdg4gyzIIF2m wkY9djPlzwRzqXB1Aod1MmYX6nlDiE1sLaa6h9TKnzKT6EbdfDHasrxzN25BPN6bMm3V wtcSBkko0JVZRWLLVhjgE1vnHludy7ORh9VsK74eELjAJeX1/KJneyGcpTWd7UjR+SKI M+jLwIXDoSBaWLTGKI7LeVF3hXldiQFC0rrV18hlJIIGUCH54QREXH51gf7ltiNLOIdi xkrjudAk9k3VNd7mglnvQjLcp2NC1nFHKABMpIM0S8A4wtdwEMAKSI2NiwV3E7qlkxQP YzVQ== X-Forwarded-Encrypted: i=1; AJvYcCUUopUs1xIjeNpT2qH9O2k/BBWVyxumy0EY/0xlrC84GarMsWqW4LFZ+KDlML+Q14duGSxPB947CfYkHcGhPYSsFIBIS1S8pqFCvILqQHdOqxPBfs3txx4sQql81bxFiqDUlZo/WdOl5uzv X-Gm-Message-State: AOJu0Yx3YGG6hR9LxmUKhFJMH/j582s5qiLYupu4PmXeszgWrHynHatN 9V2J+euOhGFuApPbCo3UTnyWB42MHWubVABloHRALJr8OFOIri7D X-Received: by 2002:a05:620a:359:b0:787:6f72:2080 with SMTP id t25-20020a05620a035900b007876f722080mr7142934qkm.26.1708921556012; Sun, 25 Feb 2024 20:25:56 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id q7-20020ae9e407000000b00787ba4095dfsm2095596qkc.32.2024.02.25.20.25.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 20:25:55 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id 30DE327C005B; Sun, 25 Feb 2024 23:25:55 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Sun, 25 Feb 2024 23:25:55 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrgedugdejudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhquhhn ucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrth htvghrnhephedugfduffffteeutddvheeuveelvdfhleelieevtdeguefhgeeuveeiudff iedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsg hoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieeg qddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigi hmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 25 Feb 2024 23:25:54 -0500 (EST) Date: Sun, 25 Feb 2024 20:25:20 -0800 From: Boqun Feng To: Michael Kelley Cc: Saurabh Singh Sengar , "haiyangz@microsoft.com" , "wei.liu@kernel.org" , "decui@microsoft.com" , "linux-kernel@vger.kernel.org" , "linux-hyperv@vger.kernel.org" Subject: Re: [PATCH 1/1] Drivers: hv: vmbus: Calculate ring buffer size for more efficient use of memory Message-ID: References: <20240213061959.782110-1-mhklinux@outlook.com> <20240220063007.GA17584@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Tue, Feb 20, 2024 at 06:15:52PM +0000, Michael Kelley wrote: > From: Boqun Feng Sent: Tuesday, February 20, 2024 9:29 AM > > > > On Mon, Feb 19, 2024 at 10:30:07PM -0800, Saurabh Singh Sengar wrote: > > > On Mon, Feb 12, 2024 at 10:19:59PM -0800, mhkelley58@gmail.com wrote: > > > > From: Michael Kelley > > > > > > > > The VMBUS_RING_SIZE macro adds space for a ring buffer header to the > > > > requested ring buffer size. The header size is always 1 page, and so > > > > its size varies based on the PAGE_SIZE for which the kernel is built. > > > > If the requested ring buffer size is a large power-of-2 size and the header > > > > size is small, the resulting size is inefficient in its use of memory. > > > > For example, a 512 Kbyte ring buffer with a 4 Kbyte page size results in > > > > a 516 Kbyte allocation, which is rounded to up 1 Mbyte by the memory > > > > allocator, and wastes 508 Kbytes of memory. > > > > > > > > In such situations, the exact size of the ring buffer isn't that important, > > > > and it's OK to allocate the 4 Kbyte header at the beginning of the 512 > > > > Kbytes, leaving the ring buffer itself with just 508 Kbytes. The memory > > > > allocation can be 512 Kbytes instead of 1 Mbyte and nothing is wasted. > > > > > > > > Update VMBUS_RING_SIZE to implement this approach for "large" ring buffer > > > > sizes. "Large" is somewhat arbitrarily defined as 8 times the size of > > > > the ring buffer header (which is of size PAGE_SIZE). For example, for > > > > 4 Kbyte PAGE_SIZE, ring buffers of 32 Kbytes and larger use the first > > > > 4 Kbytes as the ring buffer header. For 64 Kbyte PAGE_SIZE, ring buffers > > > > of 512 Kbytes and larger use the first 64 Kbytes as the ring buffer > > > > header. In both cases, smaller sizes add space for the header so > > > > the ring size isn't reduced too much by using part of the space for > > > > the header. For example, with a 64 Kbyte page size, we don't want > > > > a 128 Kbyte ring buffer to be reduced to 64 Kbytes by allocating half > > > > of the space for the header. In such a case, the memory allocation > > > > is less efficient, but it's the best that can be done. > > > > > > > > Fixes: c1135c7fd0e9 ("Drivers: hv: vmbus: Introduce types of GPADL") > > > > Signed-off-by: Michael Kelley > > > > --- > > > > include/linux/hyperv.h | 22 +++++++++++++++++++++- > > > > 1 file changed, 21 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h > > > > index 2b00faf98017..6ef0557b4bff 100644 > > > > --- a/include/linux/hyperv.h > > > > +++ b/include/linux/hyperv.h > > > > @@ -164,8 +164,28 @@ struct hv_ring_buffer { > > > > u8 buffer[]; > > > > } __packed; > > > > > > > > + > > > > +/* > > > > + * If the requested ring buffer size is at least 8 times the size of the > > > > + * header, steal space from the ring buffer for the header. Otherwise, add > > > > + * space for the header so that is doesn't take too much of the ring buffer > > > > + * space. > > > > + * > > > > + * The factor of 8 is somewhat arbitrary. The goal is to prevent adding a > > > > + * relatively small header (4 Kbytes on x86) to a large-ish power-of-2 ring > > > > + * buffer size (such as 128 Kbytes) and so end up making a nearly twice as > > > > + * large allocation that will be almost half wasted. As a contrasting example, > > > > + * on ARM64 with 64 Kbyte page size, we don't want to take 64 Kbytes for the > > > > + * header from a 128 Kbyte allocation, leaving only 64 Kbytes for the ring. > > > > + * In this latter case, we must add 64 Kbytes for the header and not worry > > > > + * about what's wasted. > > > > + */ > > > > +#define VMBUS_HEADER_ADJ(payload_sz) \ > > > > + ((payload_sz) >= 8 * sizeof(struct hv_ring_buffer) ? \ > > > > + 0 : sizeof(struct hv_ring_buffer)) > > > > + > > > > /* Calculate the proper size of a ringbuffer, it must be page-aligned */ > > > > -#define VMBUS_RING_SIZE(payload_sz) PAGE_ALIGN(sizeof(struct hv_ring_buffer) + \ > > > > +#define VMBUS_RING_SIZE(payload_sz) PAGE_ALIGN(VMBUS_HEADER_ADJ(payload_sz) + \ > > > > (payload_sz)) > > > > I generally see the point of this patch, however, it changes the > > semantics of VMBUS_RING_SIZE() (similiar as Saurabh mentioned below), > > before VMBUS_RING_SIZE() will give you a ring buffer size which has at > > least "payload_sz" bytes, but after the change, you may not get "enough" > > bytes for the vmbus ring buffer. > > Storvsc and netvsc were previously not using VMBUS_RING_SIZE(), > so space for the ring buffer header was already being "stolen" from > the specified ring size. But if this new version of VMBUS_RING_SIZE() > still allows the ring buffer header space to be "stolen" from the > large-ish ring buffers, I don't see that as a problem. The ring buffer > sizes have always been a swag value for the low-speed devices with > small ring buffers. For the high-speed devices (netvsc and storvsc) > the ring buffer sizes have been somewhat determined by perf > measurements, but the ring buffers are much bigger, so stealing > a little bit of space doesn't have any noticeable effect. Even with > the perf measurements, the sizes aren't very exact. Performance > just isn't sensitive to the size of the ring buffer except at a gross level. > Fair enough. > > > > One cause of the waste memory is using alloc_pages() to get physical > > continuous, however, after a quick look into GPADL, looks like it also > > supports uncontinuous pages. Maybe that's the longer-term solution? > > Yes, that seems like a desirable long-term solution. While the GPADL > code handles vmalloc memory correctly (because the netvsc send and > receive buffers are vmalloc memory), hv_ringbuffer_init() assumes > physically contiguous memory. It would need to use vmalloc_to_page() > in building the pages_wraparound array instead of just indexing into > the struct page array. But that's an easy fix. Another problem is the > uio_hv_generic.c driver, where hv_uio_ring_mmap() assumes a physically > contiguous ring. Maybe there's a straightforward way to fix it as well, > but it isn't immediately obvious to me. > > Using vmalloc memory for ring buffers has another benefit as well. > Today, adding a new NIC to a VM that has been running for a while > could fail because of not being able to allocate 1 Mbyte contiguous > memory for the ring buffers used by each VMBus channel. There > could be plenty of memory available, but fragmentation could prevent > getting 1 Mbyte contiguous. Using vmalloc'ed ring buffers would > solve this problem. > Yep, that will be ideal. Regards, Boqun > Michael > > > > > Regards, > > Boqun > > > > > > > > > > struct hv_ring_buffer_info { > > > > > > Thanks for the patch. > > > It's worth noting that this will affect the size of ringbuffer calculation for > > > some of the drivers: netvsc, storvsc_drv, hid-hyperv, and hyperv-keyboard.c. > > > It will be nice to have this comment added in commit for future reference. > > > > > > Looks a good improvement to me, > > > Reviewed-by: Saurabh Sengar > > > > > > > -- > > > > 2.25.1 > > > >