Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2409614ybl; Mon, 20 Jan 2020 02:22:54 -0800 (PST) X-Google-Smtp-Source: APXvYqzrGWpBgBfGiUcqi5x6WTFOPSybOVqNfE/kqzF+ZJpG5o+/9zICG7X8tN3CVnTa+PLSuMNy X-Received: by 2002:aca:8d5:: with SMTP id 204mr11904925oii.141.1579515774752; Mon, 20 Jan 2020 02:22:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579515774; cv=none; d=google.com; s=arc-20160816; b=uQl8Kf20ZRjJLm6q/X8tpOoZC09OpSNdjBjbsEnwI/KTWuolUT3zpV9gUrRz6u0Awq XYdfmsgCIwkoy7EoHgfVKinav+vStcAij4rkyOf4576cW213fc5cj4KM/kuj/Ux+O3My FDQdKmVRQ7A7fUH5zcHug3FYJdrfkNC3p/wEMn0SvGD7vdUDvXb5ckX1czGEx97HXAUX KdAlf7WmFnwrwZ2LuhJR0J2bvg18U4DJfwjC/lmR4b1NGRwWg+XCwqJxO6HjOVTeSDHy msz3qtfnhtFQvU/s63yCj4BXe71Qgcp4o9i4vgERvis87jcQdzsee8WjZR/HWs0Z+wrA KG3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=67Jq//WKsjDLFPDEaYBVfnLMTqVMtRKgNH9ezxHhqMg=; b=yM+swI8ngM3MELswuPuQLtXyMczZZ2S3YPzJmwH0uO+b2Zp4l0xkTxL1RBhGtZID0K LhDLiaDthqetR8xzKmvgfPX/HGi7d42GlCNxxOj0lwvK0e99ITplCjkToyTvDIogTT+f IqRE9y1K/VaDZsXp9G7uVyWKfnkZWQwx46/ubLPDGtoE24MvxzkG0dC7B5eCKMEjP/tF UqicgLYbpPzsQMouLXw07bWP4q2yqvEZgNMyZefIIF76Sx6nkz4SHNJBV5TsTYvh+p5X xxTqfBnFnRc6SSBQdnXOIoJvfUTJcdQegDXYnANC1xOKvhs6H9pNlhRhLpzvi0qbUlCX uRgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KqQ4T0pr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i20si19170490otk.270.2020.01.20.02.22.42; Mon, 20 Jan 2020 02:22:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=KqQ4T0pr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726587AbgATKVp (ORCPT + 99 others); Mon, 20 Jan 2020 05:21:45 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:32522 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726130AbgATKVo (ORCPT ); Mon, 20 Jan 2020 05:21:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579515703; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=67Jq//WKsjDLFPDEaYBVfnLMTqVMtRKgNH9ezxHhqMg=; b=KqQ4T0prMUyF2sn9KgAAu3rDEqiquTt1Z5WCtFWWxzn/1hXc4R1wXzUPxQSSIBfn68IOcc 5f/Q4YXkOpws4HTsYMQC26/VbnTqKML1lNFuF0znoNhOIJrnfPD2oGWY5sKQaOgzWyRbQ0 gp5gYrsDlEzeC05w+Esn7h/SEfFNwTo= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-221-vsinma0gOAKJLKcZcU6G9g-1; Mon, 20 Jan 2020 05:21:42 -0500 X-MC-Unique: vsinma0gOAKJLKcZcU6G9g-1 Received: by mail-wm1-f70.google.com with SMTP id c4so3547381wmb.8 for ; Mon, 20 Jan 2020 02:21:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=67Jq//WKsjDLFPDEaYBVfnLMTqVMtRKgNH9ezxHhqMg=; b=t3lHuUVl026aTO9p9IIAWbM0Hqo+OV8fHYm0DjjCrWsGXzZBWGoOf/IcSolW3/SFvr eqT0arfk38DupJOntllL/QEL8tCqHvmM4iobYxUwMhVuGHTes8NiBIrVK8y6CuBjPPly Lmt8Oprabnh9yu61FEhv8IqdMLDGCNxSdTE5pV6X1MwMt5P6076CJB87tCeu2mwrP/iC MT76wvcyFcJJnxt3EkqisCsBhw8jTgINXXYalfMjYQFcCw5bDDHzRSkiwtVY/jjhfA9o 3kNhKFgb3wJDS1qp5JYFL+nTldoTSbxRJM3SY5xV+yJ7IG5+2r/C8o1Ucr875cTf2MbW ZGtQ== X-Gm-Message-State: APjAAAVLYIVftm41VLN7kYQikbfTJRFHvAXBqaH+yGoxpY4GKk/q4jMm P0yizJRX4XEYIauw855aCzvNgDEaKf/OqKXH6HrvMUOm2SdQK8AtyX92zJwxAo0skRldaJJ7g0Q xiE5ivSFZLiedHHyafXJwmsMb X-Received: by 2002:a05:600c:2150:: with SMTP id v16mr17832468wml.156.1579515700323; Mon, 20 Jan 2020 02:21:40 -0800 (PST) X-Received: by 2002:a05:600c:2150:: with SMTP id v16mr17832449wml.156.1579515700094; Mon, 20 Jan 2020 02:21:40 -0800 (PST) Received: from vitty.brq.redhat.com (nat-pool-brq-t.redhat.com. [213.175.37.10]) by smtp.gmail.com with ESMTPSA id f1sm21994131wmc.45.2020.01.20.02.21.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jan 2020 02:21:39 -0800 (PST) From: Vitaly Kuznetsov To: Michael Kelley , "lantianyu1986\@gmail.com" , KY Srinivasan , Haiyang Zhang , Stephen Hemminger , "sashal\@kernel.org" Cc: Tianyu Lan , "linux-hyperv\@vger.kernel.org" , "linux-kernel\@vger.kernel.org" , "stable\@vger.kernel.org" Subject: RE: [PATCH V2] x86/Hyper-V: Balloon up according to request page number In-Reply-To: References: <20200116141600.23391-1-Tianyu.Lan@microsoft.com> Date: Mon, 20 Jan 2020 11:21:38 +0100 Message-ID: <87v9p6fmx9.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Michael Kelley writes: > From: Tianyu Lan Sent: Thursday, January 16, 2020 6:16 AM >> >> Current code has assumption that balloon request memory size aligns >> with 2MB. But actually Hyper-V doesn't guarantee such alignment. When >> balloon driver receives non-aligned balloon request, it produces warning >> and balloon up more memory than requested in order to keep 2MB alignment. >> Remove the warning and balloon up memory according to actual requested >> memory size. >> >> Fixes: f6712238471a ("hv: hv_balloon: avoid memory leak on alloc_error of 2MB memory >> block") >> Cc: stable@vger.kernel.org >> Signed-off-by: Tianyu Lan >> --- >> Change since v2: >> - Change logic of switching alloc_unit from 2MB to 4KB >> in the balloon_up() to avoid redundant iteration when >> handle non-aligned page request. >> - Remove 2MB alignment operation and comment in balloon_up() >> --- >> drivers/hv/hv_balloon.c | 12 ++++-------- >> 1 file changed, 4 insertions(+), 8 deletions(-) >> >> diff --git a/drivers/hv/hv_balloon.c b/drivers/hv/hv_balloon.c >> index 7f3e7ab22d5d..536807efbc35 100644 >> --- a/drivers/hv/hv_balloon.c >> +++ b/drivers/hv/hv_balloon.c >> @@ -1684,7 +1684,7 @@ static unsigned int alloc_balloon_pages(struct >> hv_dynmem_device *dm, >> if (num_pages < alloc_unit) >> return 0; > > The above test is no longer necessary. The num_pages < alloc_unit > case is handled implicitly by your new 'for' loop condition. > >> >> - for (i = 0; (i * alloc_unit) < num_pages; i++) { >> + for (i = 0; i < num_pages / alloc_unit; i++) { >> if (bl_resp->hdr.size + sizeof(union dm_mem_page_range) > >> HV_HYP_PAGE_SIZE) >> return i * alloc_unit; >> @@ -1722,7 +1722,7 @@ static unsigned int alloc_balloon_pages(struct >> hv_dynmem_device *dm, >> >> } >> >> - return num_pages; >> + return i * alloc_unit; >> } >> >> static void balloon_up(union dm_msg_info *msg_info) >> @@ -1737,9 +1737,6 @@ static void balloon_up(union dm_msg_info *msg_info) >> long avail_pages; >> unsigned long floor; >> >> - /* The host balloons pages in 2M granularity. */ >> - WARN_ON_ONCE(num_pages % PAGES_IN_2M != 0); >> - >> /* >> * We will attempt 2M allocations. However, if we fail to >> * allocate 2M chunks, we will go back to PAGE_SIZE allocations. >> @@ -1749,14 +1746,13 @@ static void balloon_up(union dm_msg_info *msg_info) >> avail_pages = si_mem_available(); >> floor = compute_balloon_floor(); >> >> - /* Refuse to balloon below the floor, keep the 2M granularity. */ >> + /* Refuse to balloon below the floor. */ >> if (avail_pages < num_pages || avail_pages - num_pages < floor) { >> pr_warn("Balloon request will be partially fulfilled. %s\n", >> avail_pages < num_pages ? "Not enough memory." : >> "Balloon floor reached."); >> >> num_pages = avail_pages > floor ? (avail_pages - floor) : 0; >> - num_pages -= num_pages % PAGES_IN_2M; >> } >> >> while (!done) { >> @@ -1770,7 +1766,7 @@ static void balloon_up(union dm_msg_info *msg_info) >> num_ballooned = alloc_balloon_pages(&dm_device, num_pages, >> bl_resp, alloc_unit); >> >> - if (alloc_unit != 1 && num_ballooned == 0) { >> + if (alloc_unit != 1 && num_ballooned != num_pages) { > > Maybe I'm missing something, but I don't think Vitaly's optimization works. If > alloc_unit specifies 2 Mbytes, and num_pages specifies 3 Mbytes, then num_ballooned > will come back as 2 Mbytes, which is correct. But if we revert alloc_unit to 1 page and > "continue" in that case, we will lose the 2 Mbytes of memory (it's not freed), and the > next time through the loop will try to allocate only 1 Mbyte (because num_pages > will be decremented by num_ballooned). I think the original code does the right thing. > True, nice catch! What I meant to say is that we can avoid sending two replies to the host and get away with just one. Unfortunately, "num_ballooned != num_pages" modification is not sufficient as alloc_balloon_pages() will overwrite bl_resp->range_array[] (as it always starts from 0). I think we can still achieve the goal by allocating bl_resp outside of the loop and filling it from range_array[range_count] instead of range_array[i] but it seems to big of a change for now particular gain. Let's just drop the idea. > Michael > >> alloc_unit = 1; >> continue; >> } >> -- >> 2.14.5 > -- Vitaly