Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5483567imu; Tue, 29 Jan 2019 21:13:36 -0800 (PST) X-Google-Smtp-Source: ALg8bN4Bjp1x4zxy6r8VogpupnNoBf++kT2h/iFrdcugQnBfJ21jJTGMwHwk0+h4D9NHKc2Jrvqd X-Received: by 2002:a62:d005:: with SMTP id p5mr29198301pfg.175.1548825216879; Tue, 29 Jan 2019 21:13:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548825216; cv=none; d=google.com; s=arc-20160816; b=0lsTlRL8Ep+DXZ1cm0RVJie0QuZJrNhnqg3M9s8uNrVGnk3taBQs+VVjpgLz9NZCFU hKd6rbVthyTDSiNcb0h+PzVwYjTuwysLK5F1OmSGKCra1GT7xk9c4jOQ0CLTaKFQAHQF vdwUTxnzM/QAZWH+F/EY0EhuqGIyTeHRVtupn19QdtXfHWIoE3Jz0z1QgEF4f3nrmlD4 iQ7A7I5qXY/v4YVYPhoAJd+ZgppQwO9P8brm1gKl/lhEhDDmaGnHL36vKlgAq7qyoWK4 JWhLMpaKdri5owHysNhvyfKiL4Pmqn02QhcC5UfZ5rH/0ZMNJcRq4FsPJfyWEG06V8/l ggGw== 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:from:date:dkim-signature; bh=WItGHf9nOAdYHChIFTXl5rw6TmM3xVQC+SnZeHiHN4E=; b=ehW5ONKhDwXPuuEwKI2R9GkRiPAfYAAw/3OwX2qCloMf3TjfhmDwKhBKwExpn8znkk aD8zdlfuZ3Rvzr3l9SCYRSAhek+9iP6GHCbJRq9x5UlV+mRhJemyMeePfVDDeYiqrySX U+yldQa/hhYl3xK4uqJ2Y5OhPDmYsUyvKR7z+8H/ZgfXkC0tGNhmsXQ/4i2owDcih61d UflkkjnN7kPA8OA8D7jx66LRddghflm/cJmP2m6Xb6XfunVovuyejy/tAxqwXd0/yIxV crJNvfC9vNOSssCpFI58a+zztM5jlzypfwu/cI2bcbxHd91t29CqTuTM2dV0UqoECnlp 57XA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=WvdhWaER; 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=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v200si474485pgb.15.2019.01.29.21.13.20; Tue, 29 Jan 2019 21:13:36 -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=@oracle.com header.s=corp-2018-07-02 header.b=WvdhWaER; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725843AbfA3FNP (ORCPT + 99 others); Wed, 30 Jan 2019 00:13:15 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:53302 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725791AbfA3FNP (ORCPT ); Wed, 30 Jan 2019 00:13:15 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id x0U58v4a001581; Wed, 30 Jan 2019 05:13:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : content-transfer-encoding : in-reply-to; s=corp-2018-07-02; bh=WItGHf9nOAdYHChIFTXl5rw6TmM3xVQC+SnZeHiHN4E=; b=WvdhWaER5z8dajm6zAc1/cm2TElGmyQxXLkyM4rFE1yCDW/K8gdRH+hSCpa5oAc8bJtM NyTf6JNjhJHrduy/N8k2kuchgJ6Eg3wnJ/HEBh8N8FsB/5NWSy3YfDH8olBMGRSfTvIH S65foOGebUjCbTIxCamv3MnLAwbkUe/bUwcL/inJ262NyB4pLdQtARoiom9EbnxrY92A fxWFOLeKTOFoCrt6gg7EDaMrmU6O3UAc5Fxwo1wgAq2F2bDUWUM1TeOSZCS7YekJe4l7 47lZ509l36G3rKB0Q4FFgT+yvFF4QjRGi6AtBynPUjL35NvaY5xLCtBDBvHlIKWXRUiT dQ== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by userp2120.oracle.com with ESMTP id 2q8g6r8a0b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 30 Jan 2019 05:13:09 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id x0U5D9hD025126 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 30 Jan 2019 05:13:09 GMT Received: from abhmp0003.oracle.com (abhmp0003.oracle.com [141.146.116.9]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x0U5D82w019150; Wed, 30 Jan 2019 05:13:08 GMT Received: from kadam (/197.157.0.43) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 29 Jan 2019 21:13:07 -0800 Date: Wed, 30 Jan 2019 08:12:59 +0300 From: Dan Carpenter To: Todd Kjos Cc: "open list:ANDROID DRIVERS" , Todd Kjos , Greg Kroah-Hartman , LKML , Arve =?iso-8859-1?B?SGr4bm5lduVn?= , Martijn Coenen , joel@joelfernandes.org, Android Kernel Team Subject: Re: [PATCH 1/7] binder: create userspace-to-binder-buffer copy function Message-ID: <20190130051259.GO1795@kadam> References: <20190129004934.85885-1-tkjos@google.com> <20190129004934.85885-2-tkjos@google.com> <20190129081218.GL1795@kadam> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9151 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1901300038 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 04:32:09PM -0800, Todd Kjos wrote: > On Tue, Jan 29, 2019 at 12:12 AM Dan Carpenter wrote: > > > > 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 = binder_alloc_get_page(alloc, buffer, > > > + buffer_offset, &pgoff); > > > + size = 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 = min(bytes, PAGE_SIZE - pgoff); > > Dan, > > Thanks for the feedback. > > The one above: "size = 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 == (typeof(y) *)1))) > [...] > drivers/android/binder_alloc.c:1157:10: note: in expansion of macro ‘min’ > size = min(bytes, PAGE_SIZE - pgoff); > > so, I took your suggestion: > > size = 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 = (void *)((uintptr_t)kmap(page) + pgoff); > > > > This would be a lot cleaner as: > > > > kptr = 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. Yeah, it's a GCCism but to me it seems so nice. We do it all the time. We'd like to get Clang to compile the kernel, but we've done very little work to make that happen. Removing variable length arrays was really done for other reasons... regards, dan carpenter