From patchwork Mon Oct 19 07:10:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: yalin wang X-Patchwork-Id: 532177 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id D1BD7140273 for ; Mon, 19 Oct 2015 18:11:18 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=YKW3r8KN; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752137AbbJSHK6 (ORCPT ); Mon, 19 Oct 2015 03:10:58 -0400 Received: from mail-pa0-f52.google.com ([209.85.220.52]:32992 "EHLO mail-pa0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750799AbbJSHK5 (ORCPT ); Mon, 19 Oct 2015 03:10:57 -0400 Received: by pabrc13 with SMTP id rc13so182495607pab.0; Mon, 19 Oct 2015 00:10:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=9gVyqmbIJo5V9Ucvtmmr1rdGiUpjpiXgO6bCQVJSnck=; b=YKW3r8KN6Y01gS/+Im2mJpIBkG0PkZ5qVa99pijffdhTLeLxxw35TRnaRmoDryt+mH 0qYrTB1V1fcMyaVHLCQVLGL+Xc6NTXfhR9sqTi2a55jxnU3iP/CHxWCISqlZrldMZ1xp N8siYwMZ1HNhG2xKML8v00WI+aSe8JpWtU8yOXbZ4/9j49747H1P1Mv5jMmvmWgz91kb Vo9HGNk7IHZ8hS0ktGdMbFrw6sl3mLsWjgOWn/4GDnr8ZkBvcxeO5AW/29/o9NusKsiY /PCD9NzAe64nq2VBpgQ1C7xEyXWnQURakXNiqh//4XBX5LH0UAjeakrY298y+7jaDpAv Xjuw== X-Received: by 10.66.229.67 with SMTP id so3mr33449777pac.66.1445238656520; Mon, 19 Oct 2015 00:10:56 -0700 (PDT) Received: from ubuntu.localdomain ([17.87.20.100]) by smtp.googlemail.com with ESMTPSA id pq4sm34374191pbc.53.2015.10.19.00.10.53 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 19 Oct 2015 00:10:55 -0700 (PDT) From: yalin wang To: ast@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: yalin wang Subject: [RFC] bpf: change bpf syacall to use u64 temp variables Date: Mon, 19 Oct 2015 15:10:46 +0800 Message-Id: <1445238646-9379-1-git-send-email-yalin.wang2010@gmail.com> X-Mailer: git-send-email 1.9.1 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This patch change map_lookup_elem() and map_update_elem() function to use u64 temp variable if the key_size or value_size is less than u64, we don't need use kmalloc() for these small variables. Signed-off-by: yalin wang --- kernel/bpf/syscall.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index f640e5f..c82d7bf 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -189,7 +189,8 @@ static int map_lookup_elem(union bpf_attr *attr) void __user *uvalue = u64_to_ptr(attr->value); int ufd = attr->map_fd; struct bpf_map *map; - void *key, *value, *ptr; + u64 key_buf, value_buf; + void *key = &key_buf, *value = &value_buf, *ptr; struct fd f; int err; @@ -202,7 +203,8 @@ static int map_lookup_elem(union bpf_attr *attr) return PTR_ERR(map); err = -ENOMEM; - key = kmalloc(map->key_size, GFP_USER); + if (map->key_size > sizeof(u64)) + key = kmalloc(map->key_size, GFP_USER); if (!key) goto err_put; @@ -211,7 +213,8 @@ static int map_lookup_elem(union bpf_attr *attr) goto free_key; err = -ENOMEM; - value = kmalloc(map->value_size, GFP_USER); + if (map->value_size > sizeof(u64)) + value = kmalloc(map->value_size, GFP_USER); if (!value) goto free_key; @@ -232,9 +235,11 @@ static int map_lookup_elem(union bpf_attr *attr) err = 0; free_value: - kfree(value); + if (value != &value_buf) + kfree(value); free_key: - kfree(key); + if (key != &key_buf) + kfree(key); err_put: fdput(f); return err; @@ -248,7 +253,8 @@ static int map_update_elem(union bpf_attr *attr) void __user *uvalue = u64_to_ptr(attr->value); int ufd = attr->map_fd; struct bpf_map *map; - void *key, *value; + u64 key_buf, value_buf; + void *key = &key_buf, *value = &value_buf; struct fd f; int err; @@ -261,7 +267,8 @@ static int map_update_elem(union bpf_attr *attr) return PTR_ERR(map); err = -ENOMEM; - key = kmalloc(map->key_size, GFP_USER); + if (map->key_size > sizeof(u64)) + key = kmalloc(map->key_size, GFP_USER); if (!key) goto err_put; @@ -270,7 +277,8 @@ static int map_update_elem(union bpf_attr *attr) goto free_key; err = -ENOMEM; - value = kmalloc(map->value_size, GFP_USER); + if (map->value_size > sizeof(u64)) + value = kmalloc(map->value_size, GFP_USER); if (!value) goto free_key; @@ -286,9 +294,11 @@ static int map_update_elem(union bpf_attr *attr) rcu_read_unlock(); free_value: - kfree(value); + if (value != &value_buf) + kfree(value); free_key: - kfree(key); + if (key != &key_buf) + kfree(key); err_put: fdput(f); return err;