Received: by 10.223.176.46 with SMTP id f43csp1627109wra; Wed, 24 Jan 2018 20:38:50 -0800 (PST) X-Google-Smtp-Source: AH8x226hx/mJgt+91FFl+iBPkCtFRbrBORRkfU7n1WnFtk47g1TK5nEHotMqZewKDtg/ZITqPIBN X-Received: by 10.99.115.94 with SMTP id d30mr3183902pgn.172.1516855129899; Wed, 24 Jan 2018 20:38:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516855129; cv=none; d=google.com; s=arc-20160816; b=n+UJryyUyVlDRPaF9iMiZa0ELKXsac3Qagpy52sg/yRG6tVoARcgF1EdgKpXHn4l9g Y+IbAt6QJQPP7epFcdAnHX1NDbBF1iih4rRCrmGfBtoXOuG7Ldd7h9/bKdNrHfSSyrbx yTJYhWZndnode7lOEGG/q4njvlCPy7CNXwre48qTj9BG6S5T9jQM79HLnf/YpLAIxp3W XPKVfuN7XD9AtGyFFZost1uILRF/BHrN+Vdy1Z2GNy133RBD2Fvr+3F8flQYyAA0e0vj zv+c8VNwkFAgVeppZ78bnGPmpJWiIOaYfL/z3UUz63fcBqwxy0d76GUSaCcJRvcAzrU1 ZW7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=2MjvQrPELmf9t37RRRKrVTGKrrtd/5lVc69CNhnO8Pk=; b=xcp0Y8Aymz3Pi30S4JqkbgoxE4Kt3E5YzkcQwhVdFQV4yz/dSeMoOOXmY4huLhh9b7 TYqPWRilxop5UgwEGgHIjT1QmuFlBBKWJPQOmg9kBZ1QmfMACSS02HKuPbTA+SFAwI0t NGQcRRkfdSvrO7rmNI1KKak2H+U1MfXybuPxETmm5gbLSv692NDmCMl6qGeyfpc1JquU 3/mKQXZfUzIwvA5cDWaYqw/trJ7BGb09HazdwQujJUHPXPPtorP+qJYeahMbqq2AKcnf OkPDWCviS+DHNAh8vFSlQ4WmAw41p+16DdnQTmPAu6fIlf0QHHouGoTuYM09dlisU36G pqeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=KrB3UDhl; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m14si1039233pgs.302.2018.01.24.20.38.35; Wed, 24 Jan 2018 20:38:49 -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=@google.com header.s=20161025 header.b=KrB3UDhl; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933473AbeAYEiI (ORCPT + 99 others); Wed, 24 Jan 2018 23:38:08 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:44670 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933369AbeAYEiH (ORCPT ); Wed, 24 Jan 2018 23:38:07 -0500 Received: by mail-pf0-f195.google.com with SMTP id m26so4925551pfj.11 for ; Wed, 24 Jan 2018 20:38:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=2MjvQrPELmf9t37RRRKrVTGKrrtd/5lVc69CNhnO8Pk=; b=KrB3UDhllMKwNXX0oktynVtenmirKLLkoJoQLA4c0iqgBTfoBoERS47T9nWR2uB7dx +h2yMIMAmNykxBEGYjfmkzoA70cVMv6Ok/5QmULzyfVo5JLt+6BFnI4AxPtlmIzMjwJA f8NbAk0uhFYvCsdD7V/QqcnXUnZnduH7aGcoyPmIMLkEIxZb6BHb4eBXgYhUbeeii8E9 BUF4ga3uAonLfhJ7OEWgP90UacQA6JP1rq+XBCPVyzdH4vYmLLG+G9h+uh95/IiTdSXZ /c7GR5jnipKjvtrji28a81m05zrkwGmgv0zPlMqheXvzaEVl7ipsR9h4LzuQtrt0gT2Z RAig== 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:date:message-id; bh=2MjvQrPELmf9t37RRRKrVTGKrrtd/5lVc69CNhnO8Pk=; b=uTZdT41jzjAFu9b+GCGFRaZYwck4lRPBP6UPVOfJxeM3rqeGuQh8jFWIYWJFRauNib TjS0lvk/NTEiC0Wi6LhFpjHcECkMIvWEJIZiWkKEGEyZQqdI8I/XmfpeedswwVHm0n7w n9kBCNgxiJZ3jDtI3A9WdvMwkpivmMOI3LraKrEWeac13EaNKaU15hvKNz7wNg8OURYn Y5ir+LNp+TqyETrbxaOAOCQ3nDpnnqo1IC4qxfiCqKoGvmPH8BlxB51F/B0lHwwWVSm9 Y921aac0HR5WpBFAQtZ5eh15rcVPISgqV3dMo8lkCd0kd9+CVJ4g8bsOvBM55f+gwQOX PZaA== X-Gm-Message-State: AKwxytfCDQlbSWjW8KEQEFLwQ/+/IkrGM3BOL0D820G2twYvjhf4LEQ3 8pa7RtlWeDPnRcRmUSQnt2RKYOSWQsU= X-Received: by 2002:a17:902:848c:: with SMTP id c12-v6mr10424959plo.329.1516855085982; Wed, 24 Jan 2018 20:38:05 -0800 (PST) Received: from joelaf-glaptop0.roam.corp.google.com ([24.6.205.35]) by smtp.gmail.com with ESMTPSA id r27sm11649486pfj.75.2018.01.24.20.38.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 24 Jan 2018 20:38:05 -0800 (PST) From: Joel Fernandes To: linux-kernel@vger.kernel.org Cc: iovisor-dev@lists.iovisor.org, kernel-team@android.com, Joel Fernandes , Alexei Starovoitov , Daniel Borkmann , Brendan Gregg , Brenden Blanco Subject: [PATCH] bpf/stackmap: Implement bpf_get_next_key Date: Wed, 24 Jan 2018 20:37:52 -0800 Message-Id: <20180125043752.35179-1-joelaf@google.com> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently stackmaps can't be iterated over. The keys are obtained through other maps and look ups have to be performed. In new usecases, its useful to be able to iterate over the stackmap independently. Implement bpf_get_next_key to make this possible. More details of use case: Currently iterating over stack maps is done like so, for example in BCC tools offcputime script: 1. Obtain keys from the 'counts' hash map. 2. Look up stackmap with each of the keys obtained from step 1. This makes the iteration of stackmap dependent on the counts map. In a new tool I'm working on called BPFd [1], it gives a huge speed up when I dump entire maps before transmitting them to BCC tools on a different machine [2]. This patch makes it possible to dump stackmaps independent of other maps. Tested on x86 and arm64 machines. [1] https://lwn.net/Articles/744522/ [2] https://github.com/joelagnel/bpfd/issues/8 Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Brendan Gregg Cc: Brenden Blanco Signed-off-by: Joel Fernandes --- kernel/bpf/stackmap.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index a15bc636cc98..b0bf7d009f76 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -228,7 +228,23 @@ int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) static int stack_map_get_next_key(struct bpf_map *map, void *key, void *next_key) { - return -EINVAL; + struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map); + u32 id = 0; + + if (key) { + id = *(u32 *)key; + if (unlikely(id >= smap->n_buckets)) + return -ENOENT; + id++; + } + + for ( ; id < smap->n_buckets; id++) { + if (READ_ONCE(smap->buckets[id])) { + *(u32 *)next_key = id; + return 0; + } + } + return -ENOENT; } static int stack_map_update_elem(struct bpf_map *map, void *key, void *value, -- 2.16.0.rc1.238.g530d649a79-goog