Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp4812576ybf; Wed, 4 Mar 2020 11:10:14 -0800 (PST) X-Google-Smtp-Source: ADFU+vtpWzdkZ22HA3kT7wElllI/G+I9diJI4lLcyg4TdqlGBPhYEqVf1ELy0DXoFkmIj2vBmcib X-Received: by 2002:a54:4197:: with SMTP id 23mr2727747oiy.48.1583349014533; Wed, 04 Mar 2020 11:10:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583349014; cv=none; d=google.com; s=arc-20160816; b=zTO9NUldShf1ZwAcEV9F8WuG/vazwoksNvzpgmWAxnZ9OMi0e88RZG0LDGEjoeSqEk 0wWVjm1QiUJQMiU5DoOO/3rYCfHyQm5z1OLqiWVMq21olvd34G/WB1LRaNSkDX6p15cf BGFB26g3iG734eYEDChrFv06Akbo4754TR8KhJ+Ryh5xVPWj/poAOU3S7w4xNYGRM1fX nJ5OLE+3tLh9rZK0aeBFNq7+fAarUCLv2vlc14vfJv4PYbvqU0b/1cMo4p5fc88IegGw loHfVR7YhBPCcnPLsUdzXDhDUyKqLBv6FO5Nl5UcpfMepPe0WB+U4HoPw96dmVWvI//L 3G6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:date:from:dkim-signature; bh=3SS4fekyQGzGk+bEcZn0ToSHy9OyXafStxdmWHS/l5c=; b=z5dGYdOYK3z0YoVBZGFGbMmzFJAGR1ZSsxsFG/odIjgBqG3+dI+P9icDqgd6687lOj M1rsiTDt7BVppFxaUPrO7/3uTbXw829/gQeD9mr7v6zrd3v36yY4oa2aylog51c4WKKX J670j6iaeNMY6qT1Nule+DaHjdDugO+8WnDCsEjrIrLoRNdn6MuBVkv0Lzz8Bv0diSe9 wBzkRr25AaGSKKw4IBtNdSbWjEAIelIUPeFERoRJs5UzJSxzC4/i5fBi9uT1+RCDkjGx zV/Ej8/hPMlPFw7vMDPkbLZBVtx7cvtG2ELWS7oNsuMV4ZxFR7elBQ+dFfzfs+rOybzs qN2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=g1F21Jna; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h7si1775976otq.194.2020.03.04.11.10.02; Wed, 04 Mar 2020 11:10:14 -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=@chromium.org header.s=google header.b=g1F21Jna; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729267AbgCDTI7 (ORCPT + 99 others); Wed, 4 Mar 2020 14:08:59 -0500 Received: from mail-wr1-f68.google.com ([209.85.221.68]:41286 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726440AbgCDTI7 (ORCPT ); Wed, 4 Mar 2020 14:08:59 -0500 Received: by mail-wr1-f68.google.com with SMTP id v4so3835194wrs.8 for ; Wed, 04 Mar 2020 11:08:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=3SS4fekyQGzGk+bEcZn0ToSHy9OyXafStxdmWHS/l5c=; b=g1F21JnaXgZ/b+fqRbHrmibZXuy6uq7quiVcj8o9bY3mtdTjKW3DwnytkGBBhsYcBz uurPUDZheKBecYlKZ3hwqF8Cb18i0zHW+C8J92SS+gHJqjOdtmI/z5qi78VIzgn5ovdf OctW/J68R2IVHrpLo6q3sEsBevRAWmSywJ3Vw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=3SS4fekyQGzGk+bEcZn0ToSHy9OyXafStxdmWHS/l5c=; b=ob0SBCgs5Q2SiGeMFF4kLQDVnxs5nfr4dIrQ8ZlWj2Gl2NNAV2lvMA+UHIMV7J2z74 DasW16iNv+mt711d1hOYPdN/xuUwtsmAPmiaKULDSrQHSms79UyWOuYn/cowYMebOXzY HjUJ6Bhc+taBxadEtIkLK0I0oL7Uuwy1SEf3x1WhyzxRovof9IL54BZI5zVci3hTiBah Hn3EpC7yKk3mv5AIFAxyclX82DXd65cxYSV4kdm6tmCc9kGWPPl+5Xylav1eipJwJHD1 4iB1xrluwvskDWsWoOnCAdK7hUuLn5f2eC3VcmWa25fQuPm9GH+OMba3s+2UkY7hY14z dxxw== X-Gm-Message-State: ANhLgQ0zH7RX8MxshiiS604NEq5AgAZsxE0LX5IhTG4vSGzUs1Wvtqf+ Z/QBIBrd/IaQD6ObRZ6fHJutSQ== X-Received: by 2002:adf:b189:: with SMTP id q9mr4679569wra.169.1583348937825; Wed, 04 Mar 2020 11:08:57 -0800 (PST) Received: from chromium.org (77-56-209-237.dclient.hispeed.ch. [77.56.209.237]) by smtp.gmail.com with ESMTPSA id j14sm41257638wrn.32.2020.03.04.11.08.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Mar 2020 11:08:57 -0800 (PST) From: KP Singh X-Google-Original-From: KP Singh Date: Wed, 4 Mar 2020 20:08:55 +0100 To: Andrii Nakryiko Cc: Daniel Borkmann , linux-security-module@vger.kernel.org, open list , bpf , Andrii Nakryiko , Alexei Starovoitov , Paul Turner , Jann Horn , Florent Revest , Brendan Jackman Subject: Re: [PATCH bpf-next v3 1/7] bpf: Refactor trampoline update code Message-ID: <20200304190855.GA31073@chromium.org> References: <20200304154747.23506-1-kpsingh@chromium.org> <20200304154747.23506-2-kpsingh@chromium.org> <20200304184441.GA25392@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 04-M?r 10:47, Andrii Nakryiko wrote: > On Wed, Mar 4, 2020 at 10:44 AM KP Singh wrote: > > > > On 04-M?r 19:37, Daniel Borkmann wrote: > > > On 3/4/20 4:47 PM, KP Singh wrote: > > > > From: KP Singh > > > > > > > > As we need to introduce a third type of attachment for trampolines, the > > > > flattened signature of arch_prepare_bpf_trampoline gets even more > > > > complicated. > > > > > > > > Refactor the prog and count argument to arch_prepare_bpf_trampoline to > > > > use bpf_tramp_progs to simplify the addition and accounting for new > > > > attachment types. > > > > > > > > Signed-off-by: KP Singh > > > > Acked-by: Andrii Nakryiko > > > > > > [...] > > > > diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c > > > > index c498f0fffb40..9f7e0328a644 100644 > > > > --- a/kernel/bpf/bpf_struct_ops.c > > > > +++ b/kernel/bpf/bpf_struct_ops.c > > > > @@ -320,6 +320,7 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, > > > > struct bpf_struct_ops_value *uvalue, *kvalue; > > > > const struct btf_member *member; > > > > const struct btf_type *t = st_ops->type; > > > > + struct bpf_tramp_progs *tprogs = NULL; > > > > void *udata, *kdata; > > > > int prog_fd, err = 0; > > > > void *image; > > > > @@ -425,10 +426,18 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, > > > > goto reset_unlock; > > > > } > > > > + tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL); > > > > + if (!tprogs) { > > > > + err = -ENOMEM; > > > > + goto reset_unlock; > > > > + } > > > > + > > > > > > Looking over the code again, I'm quite certain that here's a memleak > > > since the kcalloc() is done in the for_each_member() loop in the ops > > > update but then going out of scope and in the exit path we only kfree > > > the last tprogs. > > > > You're right, nice catch. Fixing it. > > There is probably no need to do many allocations as well, just one > outside of the loop and reuse? Yeah moved it out of the loop and before we grab the mutex, returning an -ENOMEM directly. Thanks for noticing this. Sending v4 now. - KP > > > > > - KP > > > > > > > > > + tprogs[BPF_TRAMP_FENTRY].progs[0] = prog; > > > > + tprogs[BPF_TRAMP_FENTRY].nr_progs = 1; > > > > err = arch_prepare_bpf_trampoline(image, > > > > st_map->image + PAGE_SIZE, > > > > &st_ops->func_models[i], 0, > > > > - &prog, 1, NULL, 0, NULL); > > > > + tprogs, NULL); > > > > if (err < 0) > > > > goto reset_unlock; > > > > @@ -469,6 +478,7 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, > > > > memset(uvalue, 0, map->value_size); > > > > memset(kvalue, 0, map->value_size); > > > > unlock: > > > > + kfree(tprogs); > > > > mutex_unlock(&st_map->lock); > > > > return err; > > > > }