From patchwork Fri Aug 23 10:06:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Kandagatla X-Patchwork-Id: 172101 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp427573ily; Fri, 23 Aug 2019 03:07:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHt3OmtlmvfIHdHIljjSokseNBMH4jIsQTiTIUSqeMd1V8cIYJvL2U7h0uUAc5W19xToWN X-Received: by 2002:a63:2a87:: with SMTP id q129mr3322546pgq.101.1566554822347; Fri, 23 Aug 2019 03:07:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566554822; cv=none; d=google.com; s=arc-20160816; b=VHkeHoGSPPA9/xliPKLKi4jx+L/2Yn5uzODfYTQJ2dduT97RL9CNyiWo5vqMk9ILlz HMsTmLZ1sDovEFfk0za/di7qzR2kaI9pFMO537IO9Su/74a8XkKKXYLCeVLUtIy/J6f1 iu2ok7qr1xKRaAjQ4itSZSKlwIpqEcmt19aR8q8MBHpByIhW99kpY2ys8MzkdjMzK4xZ 0h2f9WeRv0u7jj+ipVn+o2uPd/izq5yu8Qo+R7wUbTTD3LkjS82zu66YEydXhuQo9dtt ybMXy5mUAb20RpVeh5bPcBJTueoW3StGPJsp7bpjQerx/Tys43u2fQStpoyJG7/rfwTE DoaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LXLbAisSuv8cfBADSYAMgpxpCBkUKvb89lk53GDeSHM=; b=ZrooF5js2gn3qj5wgHoL6J0Um+UFNd855ICiBCMoa1FmZgUOdAo/r31GoMyfCSiwTF tWCpU9Edj6eTHZ5VeihSEYDHB1FiimhRwKkam6AYd3JehpJzzBC9jmTvaRcKk1pojrhb moGElDxzL2BAgpJQ5QitlcpXLmDEr6l/u1U/B+7QytLxAQgiwSo9CQY2Dk17TX+RaLnY dgqnB+yH4e/wiQFm+cSiwRJ/96hG0bZwsX9n6oV+UOk8wsbzNSgnVSDL/+3xI+F78Nl8 QKCXKcD0c54rsxt+Sxp1/l+pgj2JwJ86tkSmhy85cbYpDaJy0lRggAXdgVnmtnvhLJRL pKlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YE1lvaSm; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i14si2275697pfq.287.2019.08.23.03.07.02; Fri, 23 Aug 2019 03:07:02 -0700 (PDT) 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=@linaro.org header.s=google header.b=YE1lvaSm; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391991AbfHWKHA (ORCPT + 28 others); Fri, 23 Aug 2019 06:07:00 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:55843 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391937AbfHWKG6 (ORCPT ); Fri, 23 Aug 2019 06:06:58 -0400 Received: by mail-wm1-f66.google.com with SMTP id f72so8374018wmf.5 for ; Fri, 23 Aug 2019 03:06:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LXLbAisSuv8cfBADSYAMgpxpCBkUKvb89lk53GDeSHM=; b=YE1lvaSmCvpowekWEv5ZkcGZGjniKsDjbFAgxCvTNMLOjCkcZiqDnw5AD2FpOTvbt+ fP3ti8v1OUVAXUCH8LEm9d2KVcejeSrgo73EuJWGlAzoLUAsYHpMgf/Nar/pMpVm1OQB M/2dqC7eVrXYgEEnzCEHJ2hrLy71PA4amL14cHmrCBZw0bCNyw2fkle3Ml9/RS3DvIqJ WWTOniYrAhPoEfnp6x4Y23NRA8VQIWrVRTIRSUZxj+rfJD0DZ6vbPPMdv+MYkH5ZBRLr j8iZTWwiVTU/vYzVTQJauCtPCF/29yFaYNTwNcg5gRwa4wy7GAdY8csXB3kIgFJWNDzn gX8w== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=LXLbAisSuv8cfBADSYAMgpxpCBkUKvb89lk53GDeSHM=; b=r6ZBj2mQwmCbMf71vQTygTfxqbNyXJTfGjg4hg0nYIjzdNC93WjU98QfO1SMzy2R2W yU83TLB9ppG45fMc2P+QRKKVGM8OeMPNcnUgmPTuUML14majAN7wzKJBK6cPQR1sTeZa nKoDHj+ikARAeoOa1PxhoXroN2FMcBhDJKab2tt26tPpZcjZlO5QT/+uIyarPnORs9xg 4YU/ped/ODl23ukuLrWbL4JcE5qFD4MzPQlUVWY1r2gYaC5asXdS2QsgH2SdSFcQvDYH UvUOiRqPYUbXdgSFBU22NXmDjWp71VCK9jfzPSQ4UT0i+07hpbBX5VKJXP8xBkfcn2jt vfdw== X-Gm-Message-State: APjAAAUZQOBIT82dvdlP8I9MXk4tQnyZhZDt+UJunnb7yVNui3LrStwq YU0JyhoUwAXcpgXIuNGfOa3M/w== X-Received: by 2002:a1c:7619:: with SMTP id r25mr4289673wmc.153.1566554816559; Fri, 23 Aug 2019 03:06:56 -0700 (PDT) Received: from srini-hackbox.lan (cpc89974-aztw32-2-0-cust43.18-1.cable.virginm.net. [86.30.250.44]) by smtp.gmail.com with ESMTPSA id q124sm2058048wma.33.2019.08.23.03.06.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2019 03:06:55 -0700 (PDT) From: Srinivas Kandagatla To: gregkh@linuxfoundation.org Cc: arnd@arndb.de, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Bjorn Andersson , Mayank Chopra , Abhinav Asati , Vamsi Singamsetty , Srinivas Kandagatla Subject: [PATCH 1/5] misc: fastrpc: Reference count channel context Date: Fri, 23 Aug 2019 11:06:18 +0100 Message-Id: <20190823100622.3892-2-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190823100622.3892-1-srinivas.kandagatla@linaro.org> References: <20190823100622.3892-1-srinivas.kandagatla@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bjorn Andersson The channel context is referenced from the fastrpc user and might as user space holds the file descriptor open outlive the fastrpc device, which is removed when the remote processor is shutting down. Reference count the channel context in order to retain this object until all references has been relinquished. TEST=stop and start remote proc1 using sysfs Signed-off-by: Bjorn Andersson Signed-off-by: Mayank Chopra Signed-off-by: Abhinav Asati Signed-off-by: Vamsi Singamsetty Signed-off-by: Srinivas Kandagatla --- drivers/misc/fastrpc.c | 43 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) -- 2.21.0 diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index c790585da14c..c019e867e7fa 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -186,6 +186,7 @@ struct fastrpc_channel_ctx { struct idr ctx_idr; struct list_head users; struct miscdevice miscdev; + struct kref refcount; }; struct fastrpc_user { @@ -293,6 +294,25 @@ static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, return 0; } +static void fastrpc_channel_ctx_free(struct kref *ref) +{ + struct fastrpc_channel_ctx *cctx; + + cctx = container_of(ref, struct fastrpc_channel_ctx, refcount); + + kfree(cctx); +} + +static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx) +{ + kref_get(&cctx->refcount); +} + +static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) +{ + kref_put(&cctx->refcount, fastrpc_channel_ctx_free); +} + static void fastrpc_context_free(struct kref *ref) { struct fastrpc_invoke_ctx *ctx; @@ -316,6 +336,8 @@ static void fastrpc_context_free(struct kref *ref) kfree(ctx->maps); kfree(ctx->olaps); kfree(ctx); + + fastrpc_channel_ctx_put(cctx); } static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx) @@ -422,6 +444,9 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc( fastrpc_get_buff_overlaps(ctx); } + /* Released in fastrpc_context_put() */ + fastrpc_channel_ctx_get(cctx); + ctx->sc = sc; ctx->retval = -1; ctx->pid = current->pid; @@ -451,6 +476,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc( spin_lock(&user->lock); list_del(&ctx->node); spin_unlock(&user->lock); + fastrpc_channel_ctx_put(cctx); kfree(ctx->maps); kfree(ctx->olaps); kfree(ctx); @@ -1123,6 +1149,7 @@ static int fastrpc_device_release(struct inode *inode, struct file *file) } fastrpc_session_free(cctx, fl->sctx); + fastrpc_channel_ctx_put(cctx); mutex_destroy(&fl->mutex); kfree(fl); @@ -1141,6 +1168,9 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp) if (!fl) return -ENOMEM; + /* Released in fastrpc_device_release() */ + fastrpc_channel_ctx_get(cctx); + filp->private_data = fl; spin_lock_init(&fl->lock); mutex_init(&fl->mutex); @@ -1398,10 +1428,6 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) int i, err, domain_id = -1; const char *domain; - data = devm_kzalloc(rdev, sizeof(*data), GFP_KERNEL); - if (!data) - return -ENOMEM; - err = of_property_read_string(rdev->of_node, "label", &domain); if (err) { dev_info(rdev, "FastRPC Domain not specified in DT\n"); @@ -1420,6 +1446,10 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) return -EINVAL; } + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + data->miscdev.minor = MISC_DYNAMIC_MINOR; data->miscdev.name = kasprintf(GFP_KERNEL, "fastrpc-%s", domains[domain_id]); @@ -1428,6 +1458,8 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) if (err) return err; + kref_init(&data->refcount); + dev_set_drvdata(&rpdev->dev, data); dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32)); INIT_LIST_HEAD(&data->users); @@ -1462,7 +1494,8 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) misc_deregister(&cctx->miscdev); of_platform_depopulate(&rpdev->dev); - kfree(cctx); + + fastrpc_channel_ctx_put(cctx); } static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data, From patchwork Fri Aug 23 10:06:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Kandagatla X-Patchwork-Id: 172105 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp428060ily; Fri, 23 Aug 2019 03:07:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqwGV231t3Ebjbo8BTqs5tZAf6ImQBeo7ASd45b4Q7Eo4Tkv5CI7FEWQJwPV1AS5oSZ1vFAP X-Received: by 2002:a63:b904:: with SMTP id z4mr3174922pge.388.1566554843586; Fri, 23 Aug 2019 03:07:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566554843; cv=none; d=google.com; s=arc-20160816; b=CfOx8g+lENGfhyl9MnRr0L4ByULMpbIUbOzrw/F1GetxZIJmpohGNG/K4dnowNKwhV OucS8th+tN4XCjkxy5/DLekJk7BPFZOb+nI14C3IwJOoDqtcAvxx5XLObF6LW3JRSOtk OBHg0kAaj3+H263xcGuYgkLLNqWNM8RoIO5KRGnm3LBiSjIYDqhfBXPAXvXhJNAle4sS Q+KUuMScvyL5+SdvjeyiW1QPZfON6bBnFaEP87zkheGlReSmrYP2up7KOjYN7YRZh9QZ wOQzIkwzgbB28TfUJiEE6TdU1kz46aqSTrendwVsFj258tE9U2Wn4Bg4PCVl/epg7+Tg CthA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eaF1NgI3/tCvQBGNuV2Ozkr5swOUz9Q1YNoph3dPfCA=; b=zkaD0WdES8bu8rR15uIS/O4TYwPCjM+fNl/hC8U9N7BUCrx3CssJOegGH+T/6H1EUU worm4mkY7lNtMJw3p5/7MRxKx3zu2h6VCnzPOzanIUoitafaNL+f2meUEguJ7na1TLAv ia4tkQPFlygxeyGMmIg3E24JgTpMFrI5QTOSyCoYAN9tYcXUP9KqIioiFHwnleiBC2sx pxhb6PfxsBxtDPhb8LCVNhS63MZT5WNtnxgWbHahrOYHqdIc4ONuEuyywRFoOktH2r8Q LXVEPKMxc0u4o23H+/rNTG4NdCrmOwMSOoHHitXdpZBWvWN7FJT6RZ2fN1zk6ENFW5DQ /fCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G1xW6LLi; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t17si1552282pgv.54.2019.08.23.03.07.23; Fri, 23 Aug 2019 03:07:23 -0700 (PDT) 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=@linaro.org header.s=google header.b=G1xW6LLi; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392175AbfHWKHW (ORCPT + 28 others); Fri, 23 Aug 2019 06:07:22 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:53353 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391917AbfHWKG7 (ORCPT ); Fri, 23 Aug 2019 06:06:59 -0400 Received: by mail-wm1-f65.google.com with SMTP id 10so8363000wmp.3 for ; Fri, 23 Aug 2019 03:06:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eaF1NgI3/tCvQBGNuV2Ozkr5swOUz9Q1YNoph3dPfCA=; b=G1xW6LLifBzrZM98pMoqJppw5kCp9ljlnOSzFRZtPclDF6fw8Cd2RUQbL0e+HEm3Vh XX47d4h6nOFaDu8xJVs81qT+pZ+mIy7lRkoybJ7YhLjSgksA+EHlJsTjucE5uYSVAZvo +maJ1dWGYioVbSCriunnxWS+VkjLwTXWXByvsLbX0bDs9OJaHQFzEhMuBh8ecp61O0fp jZVCvAk73YWOGRf70F/tbiI2HeEnsfYcO/0xZjti62IB6C9kIYLIC/r+QNPhKKbJ21zv TM0sAUn4q+jETQpBFdyuPj7eFjqls67JbdbpT3rW6taBVOyeCNlkgILM5x1fMUnJlyaO dEBQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=eaF1NgI3/tCvQBGNuV2Ozkr5swOUz9Q1YNoph3dPfCA=; b=A5rkSuV61Epzf9c7YQ+W7zkk6gFFLqXxKvDGemSXKkrL+lM2GXhnmsgMlPBsqenf6c eeoO5VZKp4RgPqwNas67Qaljeim4/HcFEGydZ5qezD8BuGZxhpznjkM1In6lsQPojJrE cvu+F5S3pfHJ4+58+J4OQqDuYgnroaN2aoTywJBbZ846M084E28/3OZOSTinXEOfs3jX 43lKAib6Bct32so4L92GFJg1nEsriHSbRiIxhFj+8k6qVE5EFDMRLhMPgYzKtyYcT6Ex riHPi6FHHbWQ/6Cpg+HaaIlmx2qeHhlcqVKgSQ/8vlI0CVEwgMWJs8LJ4Un0Z/KVJwyv UXqQ== X-Gm-Message-State: APjAAAVDgN3/Cr0ADy1XCdcedZglBeEzqlpdbetX7SaNyc8J4obx4fJS 2+pFWTg82MnilGAxs9Lb6Pvd6A== X-Received: by 2002:a1c:2015:: with SMTP id g21mr4100755wmg.33.1566554817994; Fri, 23 Aug 2019 03:06:57 -0700 (PDT) Received: from srini-hackbox.lan (cpc89974-aztw32-2-0-cust43.18-1.cable.virginm.net. [86.30.250.44]) by smtp.gmail.com with ESMTPSA id q124sm2058048wma.33.2019.08.23.03.06.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2019 03:06:57 -0700 (PDT) From: Srinivas Kandagatla To: gregkh@linuxfoundation.org Cc: arnd@arndb.de, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Bjorn Andersson , Mayank Chopra , Abhinav Asati , Vamsi Singamsetty , Srinivas Kandagatla Subject: [PATCH 2/5] misc: fastrpc: Don't reference rpmsg_device after remove Date: Fri, 23 Aug 2019 11:06:19 +0100 Message-Id: <20190823100622.3892-3-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190823100622.3892-1-srinivas.kandagatla@linaro.org> References: <20190823100622.3892-1-srinivas.kandagatla@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bjorn Andersson As fastrpc_rpmsg_remove() returns the rpdev of the channel context is no longer a valid object, so ensure to update the channel context to no longer reference the old object and guard in the invoke code path against dereferencing it. TEST=stop and start remote proc1 using sysfs Signed-off-by: Bjorn Andersson Signed-off-by: Mayank Chopra Signed-off-by: Abhinav Asati Signed-off-by: Vamsi Singamsetty Signed-off-by: Srinivas Kandagatla --- drivers/misc/fastrpc.c | 4 ++++ 1 file changed, 4 insertions(+) -- 2.21.0 diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index c019e867e7fa..59ee6de26229 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -913,6 +913,9 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, if (!fl->sctx) return -EINVAL; + if (!fl->cctx->rpdev) + return -EPIPE; + ctx = fastrpc_context_alloc(fl, kernel, sc, args); if (IS_ERR(ctx)) return PTR_ERR(ctx); @@ -1495,6 +1498,7 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) misc_deregister(&cctx->miscdev); of_platform_depopulate(&rpdev->dev); + cctx->rpdev = NULL; fastrpc_channel_ctx_put(cctx); } From patchwork Fri Aug 23 10:06:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Kandagatla X-Patchwork-Id: 172103 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp427655ily; Fri, 23 Aug 2019 03:07:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqw5DlYh5Yoni+PxLBNas6NME8/DHO+tlVyQkCpv92vTXHuhOO1xJsXVFMtdj+Bk4nWUgiwN X-Received: by 2002:a17:902:b604:: with SMTP id b4mr3734832pls.94.1566554826163; Fri, 23 Aug 2019 03:07:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566554826; cv=none; d=google.com; s=arc-20160816; b=coz7Uh6MKKaofNx6jC7NNati34PWNFB9hfL+pTrHZn2CAvbkHKB4Ngvt0cB1j/c5uV 46pam8xw6ZsxODzqrytaxNNbA+6/7pkw+tBoSCz2Aa7PHkNlSOAx/zzBmf8Zc+9Te5hw Iuo/ytb09UPJAHVUctg7wkXUFvfS0WWT819QalZt1GBz1+2JEfMw4OWi4x9kEJPu4SKl j8jjaHgPf12xEfUwlGOoVabb29R6eiWBuA1S9xSIej9llBeeCzzTbKfrDiHRpaIQRoj7 hflLVhJS5HKSM3qqTikWWsaqzXBfuZ1u71SZWGE2GQWXZ+lZun1prp21OexqWUJbuXSV GUMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4bn9fvz5KyheuCTcqtCUPe7LWgeQB+vBp0zLz7BbKyg=; b=QivbO5p28KHK5i1+m5GHxk9iff+cHeIHJ66UhRMa+Wm1LAlCH4IwE54WbEKFr1r5GX 1RjHacRFB7y2LP5pT6lZuvUVqqGafDXEtCUVKekl7EjfipGUiJNxtvtwfG86kY5ZmgZj aT4ncI485VajTO/13mka1mttfvXtTKNZOrihrQzVdSzmJiwh/ln5W/4vqbNoMTb+VLPQ 2Lvpo0DYA/lQrKRW/ifEL3iZGpKnTF9GlBeuLRNa9yWPc4PX5cy4wAerSxuh6cTq6b/b Wjq76LOBmhaT6UzeLEE5tV/EY5B6MclbQ7xFo30vIAcRluYFiFJrTZWN5oX6mJV11zQz 8Kig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=i8kWpNq1; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v11si2092269plg.31.2019.08.23.03.07.05; Fri, 23 Aug 2019 03:07:06 -0700 (PDT) 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=@linaro.org header.s=google header.b=i8kWpNq1; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392036AbfHWKHE (ORCPT + 28 others); Fri, 23 Aug 2019 06:07:04 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:55854 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391992AbfHWKHC (ORCPT ); Fri, 23 Aug 2019 06:07:02 -0400 Received: by mail-wm1-f66.google.com with SMTP id f72so8374227wmf.5 for ; Fri, 23 Aug 2019 03:07:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4bn9fvz5KyheuCTcqtCUPe7LWgeQB+vBp0zLz7BbKyg=; b=i8kWpNq1BAp6nGlQ2U0mFfWA7MVPLqzBGVt+JqsdDMretmxZn7KIYxP6lIRF9g+BX8 2z2PyxyNrujPgUGIezcz3O+35GsB8jeSkw0a5mL+nF/2oxHBrr1VELcwRt6FePii4ISq pwlxw7UrQQiAyhOEo7cZwQTW3fKOcfvrv+EgnCPDQKt3Ddiu2mH1GHcfGHULfqgJz5qZ BVnkMhj5zHRVnvE7VhbXLFzYTHk33AtBoUO8c61xx3Y5J2dz/9RTbjKZKg+/YjP5epzx RV9QnA3mHRhAXuV2ZgfxVOoAovEnXYCGGREzg1MUnZ7LsK4KnGTi58ER4obOcFH6S4XE jsVA== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=4bn9fvz5KyheuCTcqtCUPe7LWgeQB+vBp0zLz7BbKyg=; b=HbLXo7usCWyUfHP7DFTJJUJHBfVGsNsTmUofHIZCUr5+8hy+HXyDVixHD2UsDiFGO1 qwMymIp/IabGsvRSg4RFBi0ajqFyg2F+zRJAzMMq//7v0SzK0ld0PfYLO8IUC0ugwmH4 JrddGeHmtupJLV/k8yVhqDH0ckMxlYwNExKiaf2Wynyox6fn+baiz+hlNWCLJ/Epm4aV 9kJWZRxv10MHUPvjAaIWi07xYckyUBWSfUjGzBN18xn2Kgflwuv5up0cRUTk0vyALXbG MsUlAwTjBJ8dhszU/SnKmV9G4wGCOdF+K5hmyb25McglW5ECKJD5QV0chBbpxavTu5m7 3s/w== X-Gm-Message-State: APjAAAVYVmqkm6rDpEaehp5x2PKepXV+TaxCslIxbHRD62PeWVME0pzi 9L3vr1LXwvcG5q2ZrVggN58yLg== X-Received: by 2002:a1c:7611:: with SMTP id r17mr4181366wmc.117.1566554820260; Fri, 23 Aug 2019 03:07:00 -0700 (PDT) Received: from srini-hackbox.lan (cpc89974-aztw32-2-0-cust43.18-1.cable.virginm.net. [86.30.250.44]) by smtp.gmail.com with ESMTPSA id q124sm2058048wma.33.2019.08.23.03.06.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2019 03:06:59 -0700 (PDT) From: Srinivas Kandagatla To: gregkh@linuxfoundation.org Cc: arnd@arndb.de, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Srinivas Kandagatla , Mayank Chopra , Jorge Ramirez-Ortiz Subject: [PATCH 4/5] misc: fastrpc: fix double refcounting on dmabuf Date: Fri, 23 Aug 2019 11:06:21 +0100 Message-Id: <20190823100622.3892-5-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190823100622.3892-1-srinivas.kandagatla@linaro.org> References: <20190823100622.3892-1-srinivas.kandagatla@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org dma buf refcount has to be done by the driver which is going to use the fd. This driver already does refcount on the dmabuf fd if its actively using it but also does an additional refcounting via extra ioctl. This additional refcount can lead to memory leak in cases where the applications fail to call the ioctl to decrement the refcount. So remove this extra refcount in the ioctl More info of dma buf usage at drivers/dma-buf/dma-buf.c Reported-by: Mayank Chopra Reported-by: Jorge Ramirez-Ortiz Signed-off-by: Srinivas Kandagatla --- drivers/misc/fastrpc.c | 25 ------------------------- 1 file changed, 25 deletions(-) -- 2.21.0 diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index 38829fa74f28..eee2bb398947 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -1198,26 +1198,6 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp) return 0; } -static int fastrpc_dmabuf_free(struct fastrpc_user *fl, char __user *argp) -{ - struct dma_buf *buf; - int info; - - if (copy_from_user(&info, argp, sizeof(info))) - return -EFAULT; - - buf = dma_buf_get(info); - if (IS_ERR_OR_NULL(buf)) - return -EINVAL; - /* - * one for the last get and other for the ALLOC_DMA_BUFF ioctl - */ - dma_buf_put(buf); - dma_buf_put(buf); - - return 0; -} - static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) { struct fastrpc_alloc_dma_buf bp; @@ -1253,8 +1233,6 @@ static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) return -EFAULT; } - get_dma_buf(buf->dmabuf); - return 0; } @@ -1322,9 +1300,6 @@ static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, case FASTRPC_IOCTL_INIT_CREATE: err = fastrpc_init_create_process(fl, argp); break; - case FASTRPC_IOCTL_FREE_DMA_BUFF: - err = fastrpc_dmabuf_free(fl, argp); - break; case FASTRPC_IOCTL_ALLOC_DMA_BUFF: err = fastrpc_dmabuf_alloc(fl, argp); break; From patchwork Fri Aug 23 10:06:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Kandagatla X-Patchwork-Id: 172104 Delivered-To: patch@linaro.org Received: by 2002:a92:d204:0:0:0:0:0 with SMTP id y4csp427923ily; Fri, 23 Aug 2019 03:07:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqyK6Hm0WZt3QpQQfxkI13Ub8JZv2Ihpgp3ls5wQ1D6coN/7+eJ5x/8WGvx+o7WT/WBkrGva X-Received: by 2002:a17:90a:be07:: with SMTP id a7mr4342018pjs.88.1566554838716; Fri, 23 Aug 2019 03:07:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566554838; cv=none; d=google.com; s=arc-20160816; b=h1AOA4t8Z/Qd/2/Fn6TIHk3InGcOdjeKPR5kQpIl9aQTooCIp05/3kcZLITymBRsSQ krriuhUX86F2ST2tjE8KjshPvQIfCBWdx2fkotJzmbBjCrklE3S1Oe/YhnyxGKvrfIpo g1Sn2pxVj3YcJ0/H4Pxvy7Dl+NtaOJYpZbtWLkp66dYiqWH6bYf+yWCCTCZKpiqig1or Kq02Ur9q2nRRJa9Ng0V/NWaeytVtCkipBkXBIxVaq7Oqx8k7PoVXQe+bPV5y0DOwcDfv cSfLgTtEmP9TIt+Mad3Ym6pZqrpaRU/gJOHmFeROmJQcOP6wvRDKwaxb8JnSkXT4yS7r ZbhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=txsHPtmH/4H2/WnX6EiFrXzeHkL+qrKcHt+2DoU5kzQ=; b=RT4dcpv/SyHVhn3E/KPjxw/y90HO2rqJQ3gkQlROIbJ4par4VvNUZKIRoaxxn1u3ik bZHFDsbqpFEm5FP981YootQtq25un4/0QRDzp4+BtdzuX0xB8jWLLHjQp5zOgbCdZQus GsI6NGCoCEN6TeLI3sVxFJo44XgNIXpNxpHZL5QrZZbXXqN2vdXP7q21VXng6ZYhXVEP 1bt6oM/SI9UBGawOCeM8E7refRKBpv+b6Cr5KeQonfWITElP+TKMs48zyaNG32oyxqI8 zd+Vp/N94V9dLl583k6p3jstp4QI1Bs0YiaNUv1IIE+5YKqqBJqULZgsB9jEbKIvC0VS Evhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zsLTQ6Oc; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x18si1972544plm.292.2019.08.23.03.07.18; Fri, 23 Aug 2019 03:07:18 -0700 (PDT) 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=@linaro.org header.s=google header.b=zsLTQ6Oc; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392123AbfHWKHR (ORCPT + 28 others); Fri, 23 Aug 2019 06:07:17 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:54331 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391998AbfHWKHD (ORCPT ); Fri, 23 Aug 2019 06:07:03 -0400 Received: by mail-wm1-f65.google.com with SMTP id p74so8365892wme.4 for ; Fri, 23 Aug 2019 03:07:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=txsHPtmH/4H2/WnX6EiFrXzeHkL+qrKcHt+2DoU5kzQ=; b=zsLTQ6OcR9Dk0Qa39F2DDnmWFcluUGtQg+KpRb+bwLN1VIK3mhupCJbjkodp3dI4Fe iqWh9+beI2fEZPtXQqBILn5x5It/sxsv7zun4Zt7NVFJsPhAfH/gGhkWLaF7nzV25Hc0 Y6hQwhCfGnfnrHTNZljsbuzWLq9L02poSqq/DJOxzCk4+RvvBqiV2Z/5YZzof/Ut4PgM rSlB1fo+5JK7gqGVKFIa3UWXy2A+xNE3hLhLeLjzdhmTLmP4MXsdi7osPx/D/EFFnDwM ztznERXTlzizlp25hDlJ4CRX4E7X4+QrQsYOYPrtn6xK68tfMik0P0I788C2hxA69/YK MX0g== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=txsHPtmH/4H2/WnX6EiFrXzeHkL+qrKcHt+2DoU5kzQ=; b=eZxAw5J23RpPuP8joEyXEyTHYs9orshBXU7YAm5Va+3HWlNJvlziw9kvBAbexFuhRe bao2UuTwcX8l8/glbiMKQQHBe+1WxADSGgv+KS4hRPTKipaZwJuChLWWxl9j++3nbK7N dHjfAWuTrK3UxdH+QxriGSR5ns0MbNQYjU+uAA/LofGX3g7pnxENoTPEwt1kkrUhZiJY n3wf+BW3hwvNpAwh8YTOStW+6yCtMALg8/zfYzmhnqMDRbd5y3Sv4/m6kFCkVmsbQrRF BApniqLk7lIRAbAoV4E/czJw6QhAwhEiVjVkiXG/8B0XRVZZvnWia7M4nLMPM8if5Svs wlOw== X-Gm-Message-State: APjAAAWgJAsrH2OvIijSaGaYoeVJp/ZXGAGpgz2hsK9wcFrSylM3xo0/ lPSDpDfpL9hhrY1+poNANCzI/DNKwBg= X-Received: by 2002:a1c:a80a:: with SMTP id r10mr4148894wme.103.1566554821626; Fri, 23 Aug 2019 03:07:01 -0700 (PDT) Received: from srini-hackbox.lan (cpc89974-aztw32-2-0-cust43.18-1.cable.virginm.net. [86.30.250.44]) by smtp.gmail.com with ESMTPSA id q124sm2058048wma.33.2019.08.23.03.07.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2019 03:07:00 -0700 (PDT) From: Srinivas Kandagatla To: gregkh@linuxfoundation.org Cc: arnd@arndb.de, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Srinivas Kandagatla , Mayank Chopra Subject: [PATCH 5/5] misc: fastrpc: free dma buf scatter list Date: Fri, 23 Aug 2019 11:06:22 +0100 Message-Id: <20190823100622.3892-6-srinivas.kandagatla@linaro.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190823100622.3892-1-srinivas.kandagatla@linaro.org> References: <20190823100622.3892-1-srinivas.kandagatla@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org dma buf scatter list is never freed, free it! Orignally detected by kmemleak: backtrace: [] kmemleak_alloc+0x50/0x84 [] sg_kmalloc+0x38/0x60 [] __sg_alloc_table+0x60/0x110 [] sg_alloc_table+0x28/0x58 [] __sg_alloc_table_from_pages+0xc0/0x1ac [] sg_alloc_table_from_pages+0x14/0x1c [] __iommu_get_sgtable+0x5c/0x8c [] fastrpc_dma_buf_attach+0x84/0xf8 [] dma_buf_attach+0x70/0xc8 [] fastrpc_map_create+0xf8/0x1e8 [] fastrpc_device_ioctl+0x508/0x900 [] compat_SyS_ioctl+0x128/0x200 [] el0_svc_naked+0x34/0x38 [] 0xffffffffffffffff Reported-by: Mayank Chopra Signed-off-by: Srinivas Kandagatla --- drivers/misc/fastrpc.c | 1 + 1 file changed, 1 insertion(+) -- 2.21.0 diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index eee2bb398947..47ae84afac2e 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -550,6 +550,7 @@ static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf, mutex_lock(&buffer->lock); list_del(&a->node); mutex_unlock(&buffer->lock); + sg_free_table(&a->sgt); kfree(a); }