Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5284466imu; Tue, 29 Jan 2019 16:32:44 -0800 (PST) X-Google-Smtp-Source: ALg8bN56k6+grCGMqvm7k/6XyeQK561WVkw3vVJv2cKsTaQe0sjDnAT6zH112R16DyalKfOqSM1B X-Received: by 2002:a17:902:292b:: with SMTP id g40mr28460827plb.82.1548808364331; Tue, 29 Jan 2019 16:32:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548808364; cv=none; d=google.com; s=arc-20160816; b=UW/vCfTbc5sxBZnw1aNJ1nrxVYD8/GRVP9gkhwT0HjuC4ExdU3S+VaoNJqL7WjIXH8 w642XpOOz/34TYynxfhi26hqd+gKnq5k7jLsRQh9ozKyRZUSpwX/FZe8KAkmIeFyHimF hmaoPJTPHuLUbKyuGrjmmw3gz3AKFnF8tY0FJVAJCD8WqnSnVho/Rzi/8mYD+qJnwKfc FKWnhHwT6radzii3YpOwHCG/ZEY2AGZLT2oe3uxIKRwHhBXgX1Uwu7WiRwE+xK9N2D17 9ld57mTqRATx0H6pQBEe/yBFC5o17ZFZVHrC28JhEoSO+DcDRhccy85Hx86RQQbQQXqc 5UqA== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=QJdxBl0FXEBbBQkxJGgEZNdwVSWPPhCpq4GcSMBRZrs=; b=bhRYXpwQE1utH2O0qCTfAjqNORkSTXPxydw7Dij8uLVwvgY4XdMRRCGylWXd+MFJab A/SoIhD1/g2aQpMDKfVi1BOO58WQIvjv/6UShZlLntT/nOBIpGGIZy1ZCSYCqyw9E8mq exNj6Njjgu/P4dGmHLpj6CpziZGYgz9m6T0STPaSfSy7s3AGJlbFOQ+P1ave5wzBkTW4 aoMWmPUxXpZB59jteMx5fkUPHXyZmGKihuzqzmyWlmwBWsvlfM040anZozfKWo01n0Cf W2sCe/OWY3JUg2HHfboxDXuFM8vBmHQrA8zo8lpeUZx+J0yxmauJZ1dplUpOHZEAqXDb w9fQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Eqt65Lkb; 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 n3si1424795plk.328.2019.01.29.16.32.26; Tue, 29 Jan 2019 16:32:44 -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=Eqt65Lkb; 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 S1727542AbfA3AcX (ORCPT + 99 others); Tue, 29 Jan 2019 19:32:23 -0500 Received: from mail-lf1-f65.google.com ([209.85.167.65]:36574 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727228AbfA3AcW (ORCPT ); Tue, 29 Jan 2019 19:32:22 -0500 Received: by mail-lf1-f65.google.com with SMTP id a16so16075022lfg.3 for ; Tue, 29 Jan 2019 16:32:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=QJdxBl0FXEBbBQkxJGgEZNdwVSWPPhCpq4GcSMBRZrs=; b=Eqt65LkbQ/EShVVCRdk80YW6QFI2o+gu55YswsK80D+rWsBpGPGqHsmEWi4y+LbjpM pADmpMLKe9UX7V8kmL7B+1+FOt8rlyBFtGQBEuiJeq8ABV26u0r06fTcoSoElr4p0c9w tlYSI4ZCh04oAokkAY/fSac75IFjHOek92gU3cRYw5jo7YHP5rpga2UnXfo3BFP3hO0l FQZR42eJUgaS5cj89vaUpovSPAym8meY0T1W5HI5gCkdnFur1JWgiAeV2lHrNRLbFTeR aHL0n0tbaorTuCmhAivPZJMUw+n+jLAv7Uaz4mUlVkg4C6Mo3d2NB7xLJqaleoPb0hGJ +kRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=QJdxBl0FXEBbBQkxJGgEZNdwVSWPPhCpq4GcSMBRZrs=; b=P+ol3F/8NuozR+So6dnQVXNJpDt5RkPQOwAyl2vNFKALgh2geG6oPX9mYnO/QjhfKB 61BxMwkzJfEAKxVoLuTR3kZO+OWDp5V7rYiJWK73XhgJAlRaQAPEZfR5tdUYrUYthxpf kPY70rVfJtPoPPCw+c45zfeaPS2y3GLUM4N9ewVEiGUj8BztXVbZWBSEyIIaO0gN/MRH X3eGhze5IPJXQZTt19yKAsKPYUkeZHnDxTx6n5yDMuKP/ekvl5PuDm3AWdISK+MRLyLZ NkKxKiet2/pzv3BFk9xrg5To9el4AAi6LmRY8tloRPMXhKfYwvxGcZFDV0kOAsXMVZMl vKWQ== X-Gm-Message-State: AJcUukdEjseF1sbfA4d2c3Z29GyFgMJqaIKzPcdDjBY5mN5GEfG5J75L ZH+F9PjEa9HtxUvYuoGckm3oX0+ox/ND7ouvObuHYw== X-Received: by 2002:a19:2106:: with SMTP id h6mr21509066lfh.29.1548808340395; Tue, 29 Jan 2019 16:32:20 -0800 (PST) MIME-Version: 1.0 References: <20190129004934.85885-1-tkjos@google.com> <20190129004934.85885-2-tkjos@google.com> <20190129081218.GL1795@kadam> In-Reply-To: <20190129081218.GL1795@kadam> From: Todd Kjos Date: Tue, 29 Jan 2019 16:32:09 -0800 Message-ID: Subject: Re: [PATCH 1/7] binder: create userspace-to-binder-buffer copy function To: Dan Carpenter Cc: Todd Kjos , Greg Kroah-Hartman , =?UTF-8?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , "open list:ANDROID DRIVERS" , LKML , Martijn Coenen , joel@joelfernandes.org, Android Kernel Team Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 29, 2019 at 12:12 AM Dan Carpenter w= rote: > > On Mon, Jan 28, 2019 at 04:49:28PM -0800, Todd Kjos wrote: > > +/** > > + * binder_alloc_copy_user_to_buffer() - copy src user to tgt user > > + * @alloc: binder_alloc for this proc > > + * @buffer: binder buffer to be accessed > > + * @buffer_offset: offset into @buffer data > > + * @from: userspace pointer to source buffer > > + * @bytes: bytes to copy > > + * > > + * Copy bytes from source userspace to target buffer. > > + * > > + * Return: bytes remaining to be copied > > + */ > > +unsigned long > > +binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc, > > + struct binder_buffer *buffer, > > + binder_size_t buffer_offset, > > + const void __user *from, > > + size_t bytes) > > +{ > > + if (!check_buffer(alloc, buffer, buffer_offset, bytes)) > > + return bytes; > > + > > + while (bytes) { > > + unsigned long size; > > + unsigned long ret; > > + struct page *page; > > + pgoff_t pgoff; > > + void *kptr; > > + > > + page =3D binder_alloc_get_page(alloc, buffer, > > + buffer_offset, &pgoff); > > + size =3D min(bytes, (size_t)(PAGE_SIZE - pgoff)); > > This code has so much more casting than necessary. To me casting says > that we haven't got the types correct or something. I've just pulled > this function out as an example really, but none of the casts here are > required... This could just be: > > size =3D min(bytes, PAGE_SIZE - pgoff); Dan, Thanks for the feedback. The one above: "size =3D min(bytes, PAGE_SIZE - pgoff);", results in 32-bit compile warnings: ./include/linux/kernel.h:846:29: warning: comparison of distinct pointer types lacks a cast (!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) [...] drivers/android/binder_alloc.c:1157:10: note: in expansion of macro =E2=80= =98min=E2=80=99 size =3D min(bytes, PAGE_SIZE - pgoff); so, I took your suggestion: size =3D min_t(size_t, bytes, PAGE_SIZE - pgoff); > > Btw, if you really need to do a cast inside a min() (you don't in this > cast) then use min_t(). > > > + kptr =3D (void *)((uintptr_t)kmap(page) + pgoff); > > This would be a lot cleaner as: > > kptr =3D kmap(page) + pgoff; It definitely is cleaner, but the clean version is doing arithmetic on a void pointer which is generally not considered good practice (but is supported by gcc). Is this acceptable in the kernel? I don't see any statement on it in Documentation/process/coding-style.rst. If so, I agree a bunch of these casts can go as you suggest. -Todd [...] > > regards, > dan carpenter >