From patchwork Fri Jul 3 11:51:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudio Imbrenda X-Patchwork-Id: 11641751 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8AE2760D for ; Fri, 3 Jul 2020 11:51:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7AE38207FF for ; Fri, 3 Jul 2020 11:51:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726111AbgGCLvS (ORCPT ); Fri, 3 Jul 2020 07:51:18 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:50924 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726063AbgGCLvR (ORCPT ); Fri, 3 Jul 2020 07:51:17 -0400 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 063BWxVo087192 for ; Fri, 3 Jul 2020 07:51:17 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 320ss4wtrn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 03 Jul 2020 07:51:16 -0400 Received: from m0098396.ppops.net (m0098396.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 063BXb9a088923 for ; Fri, 3 Jul 2020 07:51:16 -0400 Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0a-001b2d01.pphosted.com with ESMTP id 320ss4wtr0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 03 Jul 2020 07:51:16 -0400 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 063BpE1a000713; Fri, 3 Jul 2020 11:51:14 GMT Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by ppma01fra.de.ibm.com with ESMTP id 321vnvg75f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 03 Jul 2020 11:51:13 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 063BpBFr55378056 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 3 Jul 2020 11:51:11 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9EC2D11C052; Fri, 3 Jul 2020 11:51:11 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3C2D311C050; Fri, 3 Jul 2020 11:51:11 +0000 (GMT) Received: from ibm-vm.ibmuc.com (unknown [9.145.9.164]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 3 Jul 2020 11:51:11 +0000 (GMT) From: Claudio Imbrenda To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: frankja@linux.ibm.com, thuth@redhat.com, david@redhat.com Subject: [kvm-unit-tests PATCH v1 1/4] lib/vmalloc: fix pages count local variable to be size_t Date: Fri, 3 Jul 2020 13:51:06 +0200 Message-Id: <20200703115109.39139-2-imbrenda@linux.ibm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200703115109.39139-1-imbrenda@linux.ibm.com> References: <20200703115109.39139-1-imbrenda@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-07-03_06:2020-07-02,2020-07-03 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 cotscore=-2147483648 spamscore=0 clxscore=1015 phishscore=0 adultscore=0 priorityscore=1501 bulkscore=0 mlxlogscore=999 suspectscore=0 mlxscore=0 impostorscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2007030078 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Since size is of type size_t, size >> PAGE_SHIFT might still be too big for a normal unsigned int. Signed-off-by: Claudio Imbrenda --- lib/vmalloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vmalloc.c b/lib/vmalloc.c index 10f15af..9237a0f 100644 --- a/lib/vmalloc.c +++ b/lib/vmalloc.c @@ -40,7 +40,7 @@ void *alloc_vpage(void) void *vmap(phys_addr_t phys, size_t size) { void *mem, *p; - unsigned pages; + size_t pages; size = PAGE_ALIGN(size); pages = size / PAGE_SIZE; @@ -58,7 +58,7 @@ void *vmap(phys_addr_t phys, size_t size) static void *vm_memalign(size_t alignment, size_t size) { void *mem, *p; - unsigned pages; + size_t pages; assert(alignment <= PAGE_SIZE); size = PAGE_ALIGN(size); From patchwork Fri Jul 3 11:51:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudio Imbrenda X-Patchwork-Id: 11641753 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5E916912 for ; Fri, 3 Jul 2020 11:51:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4FFD1207FF for ; Fri, 3 Jul 2020 11:51:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726147AbgGCLvT (ORCPT ); Fri, 3 Jul 2020 07:51:19 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:62918 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725984AbgGCLvS (ORCPT ); Fri, 3 Jul 2020 07:51:18 -0400 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 063BVN14148877 for ; Fri, 3 Jul 2020 07:51:17 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 320s8d0gyj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 03 Jul 2020 07:51:16 -0400 Received: from m0098419.ppops.net (m0098419.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 063BVPpi149074 for ; Fri, 3 Jul 2020 07:51:16 -0400 Received: from ppma05fra.de.ibm.com (6c.4a.5195.ip4.static.sl-reverse.com [149.81.74.108]) by mx0b-001b2d01.pphosted.com with ESMTP id 320s8d0gy1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 03 Jul 2020 07:51:16 -0400 Received: from pps.filterd (ppma05fra.de.ibm.com [127.0.0.1]) by ppma05fra.de.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 063BoCfv022100; Fri, 3 Jul 2020 11:51:14 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma05fra.de.ibm.com with ESMTP id 31wwr8bddv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 03 Jul 2020 11:51:14 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 063BpCXS50266152 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 3 Jul 2020 11:51:12 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5FF1211C052; Fri, 3 Jul 2020 11:51:12 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B583711C050; Fri, 3 Jul 2020 11:51:11 +0000 (GMT) Received: from ibm-vm.ibmuc.com (unknown [9.145.9.164]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 3 Jul 2020 11:51:11 +0000 (GMT) From: Claudio Imbrenda To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: frankja@linux.ibm.com, thuth@redhat.com, david@redhat.com Subject: [kvm-unit-tests PATCH v1 2/4] lib/alloc_page: change some parameter types Date: Fri, 3 Jul 2020 13:51:07 +0200 Message-Id: <20200703115109.39139-3-imbrenda@linux.ibm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200703115109.39139-1-imbrenda@linux.ibm.com> References: <20200703115109.39139-1-imbrenda@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-07-03_03:2020-07-02,2020-07-03 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 malwarescore=0 suspectscore=2 priorityscore=1501 clxscore=1015 mlxscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 cotscore=-2147483648 adultscore=0 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2007030078 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org For size parameters, size_t is probably semantically more appropriate than unsigned long (although they map to the same value). For order, unsigned long is just too big. Also, get_order returns an unsigned int anyway. Signed-off-by: Claudio Imbrenda --- lib/alloc_page.h | 6 +++--- lib/alloc_page.c | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/alloc_page.h b/lib/alloc_page.h index 6181299..d9aceb7 100644 --- a/lib/alloc_page.h +++ b/lib/alloc_page.h @@ -11,10 +11,10 @@ bool page_alloc_initialized(void); void page_alloc_ops_enable(void); void *alloc_page(void); -void *alloc_pages(unsigned long order); +void *alloc_pages(unsigned int order); void free_page(void *page); -void free_pages(void *mem, unsigned long size); -void free_pages_by_order(void *mem, unsigned long order); +void free_pages(void *mem, size_t size); +void free_pages_by_order(void *mem, unsigned int order); unsigned int get_order(size_t size); #endif diff --git a/lib/alloc_page.c b/lib/alloc_page.c index 8769c3f..f16eaad 100644 --- a/lib/alloc_page.c +++ b/lib/alloc_page.c @@ -21,7 +21,7 @@ bool page_alloc_initialized(void) return freelist != 0; } -void free_pages(void *mem, unsigned long size) +void free_pages(void *mem, size_t size) { void *old_freelist; void *end; @@ -53,7 +53,7 @@ void free_pages(void *mem, unsigned long size) spin_unlock(&lock); } -void free_pages_by_order(void *mem, unsigned long order) +void free_pages_by_order(void *mem, unsigned int order) { free_pages(mem, 1ul << (order + PAGE_SHIFT)); } @@ -79,7 +79,7 @@ void *alloc_page() * Allocates (1 << order) physically contiguous and naturally aligned pages. * Returns NULL if there's no memory left. */ -void *alloc_pages(unsigned long order) +void *alloc_pages(unsigned int order) { /* Generic list traversal. */ void *prev; @@ -150,7 +150,7 @@ void free_page(void *page) static void *page_memalign(size_t alignment, size_t size) { unsigned long n = ALIGN(size, PAGE_SIZE) >> PAGE_SHIFT; - unsigned long order; + unsigned int order; if (!size) return NULL; From patchwork Fri Jul 3 11:51:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudio Imbrenda X-Patchwork-Id: 11641755 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 65CC0912 for ; Fri, 3 Jul 2020 11:51:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 514CF206DF for ; Fri, 3 Jul 2020 11:51:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726236AbgGCLvU (ORCPT ); Fri, 3 Jul 2020 07:51:20 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:5056 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726063AbgGCLvT (ORCPT ); Fri, 3 Jul 2020 07:51:19 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 063BYXw4039310 for ; Fri, 3 Jul 2020 07:51:18 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 320yr6e2ff-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 03 Jul 2020 07:51:18 -0400 Received: from m0098404.ppops.net (m0098404.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 063Bl4Dk093500 for ; Fri, 3 Jul 2020 07:51:17 -0400 Received: from ppma06ams.nl.ibm.com (66.31.33a9.ip4.static.sl-reverse.com [169.51.49.102]) by mx0a-001b2d01.pphosted.com with ESMTP id 320yr6e2ev-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 03 Jul 2020 07:51:17 -0400 Received: from pps.filterd (ppma06ams.nl.ibm.com [127.0.0.1]) by ppma06ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 063BojYZ001316; Fri, 3 Jul 2020 11:51:15 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma06ams.nl.ibm.com with ESMTP id 31wwch6s09-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 03 Jul 2020 11:51:14 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 063BpCBN59572280 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 3 Jul 2020 11:51:12 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D2CE711C04C; Fri, 3 Jul 2020 11:51:12 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 774C511C058; Fri, 3 Jul 2020 11:51:12 +0000 (GMT) Received: from ibm-vm.ibmuc.com (unknown [9.145.9.164]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 3 Jul 2020 11:51:12 +0000 (GMT) From: Claudio Imbrenda To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: frankja@linux.ibm.com, thuth@redhat.com, david@redhat.com Subject: [kvm-unit-tests PATCH v1 3/4] lib/alloc_page: move get_order and is_power_of_2 to a bitops.h Date: Fri, 3 Jul 2020 13:51:08 +0200 Message-Id: <20200703115109.39139-4-imbrenda@linux.ibm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200703115109.39139-1-imbrenda@linux.ibm.com> References: <20200703115109.39139-1-imbrenda@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-07-03_06:2020-07-02,2020-07-03 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 malwarescore=0 cotscore=-2147483648 clxscore=1015 spamscore=0 suspectscore=2 adultscore=0 phishscore=0 mlxlogscore=946 priorityscore=1501 bulkscore=0 impostorscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2007030078 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The functions get_order and is_power_of_2 are simple and should probably be in a header, like similar simple functions in bitops.h Since they concern bit manipulation, the logical place for them is in bitops.h Signed-off-by: Claudio Imbrenda --- lib/alloc_page.h | 1 - lib/bitops.h | 10 ++++++++++ lib/libcflat.h | 5 ----- lib/alloc.c | 1 + lib/alloc_page.c | 5 ----- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/alloc_page.h b/lib/alloc_page.h index d9aceb7..88540d1 100644 --- a/lib/alloc_page.h +++ b/lib/alloc_page.h @@ -15,6 +15,5 @@ void *alloc_pages(unsigned int order); void free_page(void *page); void free_pages(void *mem, size_t size); void free_pages_by_order(void *mem, unsigned int order); -unsigned int get_order(size_t size); #endif diff --git a/lib/bitops.h b/lib/bitops.h index b310a22..308aa86 100644 --- a/lib/bitops.h +++ b/lib/bitops.h @@ -74,4 +74,14 @@ static inline unsigned long fls(unsigned long word) } #endif +static inline bool is_power_of_2(unsigned long n) +{ + return n && !(n & (n - 1)); +} + +static inline unsigned int get_order(size_t size) +{ + return size ? fls(size) + !is_power_of_2(size) : 0; +} + #endif diff --git a/lib/libcflat.h b/lib/libcflat.h index 7092af2..ec0f58b 100644 --- a/lib/libcflat.h +++ b/lib/libcflat.h @@ -147,11 +147,6 @@ do { \ } \ } while (0) -static inline bool is_power_of_2(unsigned long n) -{ - return n && !(n & (n - 1)); -} - /* * One byte per bit, a ' between each group of 4 bits, and a null terminator. */ diff --git a/lib/alloc.c b/lib/alloc.c index 6c89f98..9d89d24 100644 --- a/lib/alloc.c +++ b/lib/alloc.c @@ -1,4 +1,5 @@ #include "alloc.h" +#include "bitops.h" #include "asm/page.h" #include "bitops.h" diff --git a/lib/alloc_page.c b/lib/alloc_page.c index f16eaad..fa3c527 100644 --- a/lib/alloc_page.c +++ b/lib/alloc_page.c @@ -175,8 +175,3 @@ void page_alloc_ops_enable(void) { alloc_ops = &page_alloc_ops; } - -unsigned int get_order(size_t size) -{ - return is_power_of_2(size) ? fls(size) : fls(size) + 1; -} From patchwork Fri Jul 3 11:51:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudio Imbrenda X-Patchwork-Id: 11641757 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CA278912 for ; Fri, 3 Jul 2020 11:51:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BB70F206DF for ; Fri, 3 Jul 2020 11:51:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726272AbgGCLvX (ORCPT ); Fri, 3 Jul 2020 07:51:23 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:38604 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726108AbgGCLvT (ORCPT ); Fri, 3 Jul 2020 07:51:19 -0400 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 063BVNhq148913 for ; Fri, 3 Jul 2020 07:51:17 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 320s8d0gyx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 03 Jul 2020 07:51:17 -0400 Received: from m0098419.ppops.net (m0098419.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 063BWGsd151440 for ; Fri, 3 Jul 2020 07:51:17 -0400 Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0b-001b2d01.pphosted.com with ESMTP id 320s8d0gy9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 03 Jul 2020 07:51:17 -0400 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 063BoIZw000485; Fri, 3 Jul 2020 11:51:15 GMT Received: from b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by ppma01fra.de.ibm.com with ESMTP id 321vnvg75g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 03 Jul 2020 11:51:15 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 063BpD0J11338190 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 3 Jul 2020 11:51:13 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5887811C04A; Fri, 3 Jul 2020 11:51:13 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E99D411C050; Fri, 3 Jul 2020 11:51:12 +0000 (GMT) Received: from ibm-vm.ibmuc.com (unknown [9.145.9.164]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 3 Jul 2020 11:51:12 +0000 (GMT) From: Claudio Imbrenda To: kvm@vger.kernel.org, pbonzini@redhat.com Cc: frankja@linux.ibm.com, thuth@redhat.com, david@redhat.com Subject: [kvm-unit-tests PATCH v1 4/4] lib/vmalloc: allow vm_memalign with alignment > PAGE_SIZE Date: Fri, 3 Jul 2020 13:51:09 +0200 Message-Id: <20200703115109.39139-5-imbrenda@linux.ibm.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200703115109.39139-1-imbrenda@linux.ibm.com> References: <20200703115109.39139-1-imbrenda@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-07-03_03:2020-07-02,2020-07-03 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 malwarescore=0 suspectscore=0 priorityscore=1501 clxscore=1015 mlxscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 cotscore=-2147483648 adultscore=0 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2007030078 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Allow allocating aligned virtual memory with alignment larger than only one page. Add a check that the backing pages were actually allocated. Export the alloc_vpages_aligned function to allow users to allocate non-backed aligned virtual addresses. Signed-off-by: Claudio Imbrenda Reviewed-by: Andrew Jones --- lib/vmalloc.h | 3 +++ lib/vmalloc.c | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/vmalloc.h b/lib/vmalloc.h index 2b563f4..fa3fa22 100644 --- a/lib/vmalloc.h +++ b/lib/vmalloc.h @@ -5,6 +5,9 @@ /* Allocate consecutive virtual pages (without backing) */ extern void *alloc_vpages(ulong nr); +/* Allocate consecutive and aligned virtual pages (without backing) */ +extern void *alloc_vpages_aligned(ulong nr, unsigned int alignment_pages); + /* Allocate one virtual page (without backing) */ extern void *alloc_vpage(void); /* Set the top of the virtual address space */ diff --git a/lib/vmalloc.c b/lib/vmalloc.c index 9237a0f..2c482aa 100644 --- a/lib/vmalloc.c +++ b/lib/vmalloc.c @@ -12,19 +12,28 @@ #include "alloc.h" #include "alloc_phys.h" #include "alloc_page.h" +#include #include "vmalloc.h" static struct spinlock lock; static void *vfree_top = 0; static void *page_root; -void *alloc_vpages(ulong nr) +/* + * Allocate a certain number of pages from the virtual address space (without + * physical backing). + * + * nr is the number of pages to allocate + * alignment_pages is the alignment of the allocation *in pages* + */ +static void *alloc_vpages_intern(ulong nr, unsigned int alignment_pages) { uintptr_t ptr; spin_lock(&lock); ptr = (uintptr_t)vfree_top; ptr -= PAGE_SIZE * nr; + ptr &= GENMASK_ULL(63, PAGE_SHIFT + get_order(alignment_pages)); vfree_top = (void *)ptr; spin_unlock(&lock); @@ -32,6 +41,16 @@ void *alloc_vpages(ulong nr) return (void *)ptr; } +void *alloc_vpages(ulong nr) +{ + return alloc_vpages_intern(nr, 1); +} + +void *alloc_vpages_aligned(ulong nr, unsigned int alignment_pages) +{ + return alloc_vpages_intern(nr, alignment_pages); +} + void *alloc_vpage(void) { return alloc_vpages(1); @@ -55,17 +74,22 @@ void *vmap(phys_addr_t phys, size_t size) return mem; } +/* + * Allocate virtual memory, with the specified minimum alignment. + */ static void *vm_memalign(size_t alignment, size_t size) { + phys_addr_t pa; void *mem, *p; - size_t pages; - assert(alignment <= PAGE_SIZE); - size = PAGE_ALIGN(size); - pages = size / PAGE_SIZE; - mem = p = alloc_vpages(pages); - while (pages--) { - phys_addr_t pa = virt_to_phys(alloc_page()); + assert(is_power_of_2(alignment)); + + size = PAGE_ALIGN(size) / PAGE_SIZE; + alignment = PAGE_ALIGN(alignment) / PAGE_SIZE; + mem = p = alloc_vpages_intern(size, alignment); + while (size--) { + pa = virt_to_phys(alloc_page()); + assert(pa); install_page(page_root, pa, p); p += PAGE_SIZE; }