From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CBEEC4332F for ; Sat, 8 Oct 2022 18:16:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230515AbiJHSQe (ORCPT ); Sat, 8 Oct 2022 14:16:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229916AbiJHSQc (ORCPT ); Sat, 8 Oct 2022 14:16:32 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 487403A177; Sat, 8 Oct 2022 11:16:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665252991; x=1696788991; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=Q7YlJwn22ePcF+UsrTEOpg8IcVNJxX9fgtN3uSim9/E=; b=Cy4RrU9t92ztTMzry6jaPaWlZaLIB+yMDteFQq7drWgDpR+tajmRMi64 ykD3t070bB43GQAQqvKyud+dBzKSeDr8V9s1IEBmkV0dAcLzqehFOyr7D un7mKVNaVlaMrC5a6GwPw82JHRxcdMaLG+ubeZmMsq+l6ls5ltIZoYNjZ 53DwZoCUDNZvtTqD+V3ypiLvmlGZwi67J28TZLHt2ZD7uotZLAc3Az7tf yDSD16lHUMpqwIRCGkVQz2qnC+0rEx1ZaMbO71IBwW04LXG1zegKMQoHN 5nZHsTBnhmJHREjKkJp2rp/DM/3J4jmEGia2mk3bRlY0E6RcO+5EIrz0B Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="390269218" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="390269218" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2022 11:16:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="750899918" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="750899918" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga004.jf.intel.com with ESMTP; 08 Oct 2022 11:16:23 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1ohEN3-0048ft-0Q; Sat, 08 Oct 2022 21:16:21 +0300 Date: Sat, 8 Oct 2022 21:16:20 +0300 From: Andy Shevchenko To: Kees Cook Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, patches@lists.linux.dev, dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, kernel-janitors@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nvme@lists.infradead.org, linux-parisc@vger.kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, linux-um@lists.infradead.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org, Jan Kara Subject: Re: [PATCH v4 2/6] treewide: use prandom_u32_max() when possible Message-ID: References: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Precedence: bulk List-ID: X-Mailing-List: kernel-janitors@vger.kernel.org On Fri, Oct 07, 2022 at 08:50:43PM -0700, Kees Cook wrote: > On October 7, 2022 7:21:28 PM PDT, "Jason A. Donenfeld" wrote: > >On Fri, Oct 07, 2022 at 03:47:44PM -0700, Kees Cook wrote: > >> On Fri, Oct 07, 2022 at 12:01:03PM -0600, Jason A. Donenfeld wrote: ... > >> These are more fun, but Coccinelle can still do them with a little > >> Pythonic help: > >> > >> // Find a potential literal > >> @literal_mask@ > >> expression LITERAL; > >> identifier randfunc =~ "get_random_int|prandom_u32|get_random_u32"; > >> position p; > >> @@ > >> > >> (randfunc()@p & (LITERAL)) > >> > >> // Add one to the literal. > >> @script:python add_one@ > >> literal << literal_mask.LITERAL; > >> RESULT; > >> @@ > >> > >> if literal.startswith('0x'): > >> value = int(literal, 16) + 1 > >> coccinelle.RESULT = cocci.make_expr("0x%x" % (value)) > >> elif literal[0] in '123456789': > >> value = int(literal, 10) + 1 > >> coccinelle.RESULT = cocci.make_expr("%d" % (value)) > >> else: > >> print("I don't know how to handle: %s" % (literal)) Wouldn't Python take care about (known) prefixes itself? try: x = int(literal) except ValueError as ex: print(..., ex.error) > >> // Replace the literal mask with the calculated result. > >> @plus_one@ > >> expression literal_mask.LITERAL; > >> position literal_mask.p; > >> expression add_one.RESULT; > >> identifier FUNC; > >> @@ > >> > >> - (FUNC()@p & (LITERAL)) > >> + prandom_u32_max(RESULT) > > > >Oh that's pretty cool. I can do the saturation check in python, since > >`value` holds the parsed result. Neat. > > It is (at least how I have it here) just the string, so YMMV. ... > >Thanks a bunch for the guidance. > > Sure thing! I was pleased to figure out how to do the python bit. I believe it can be optimized -- With Best Regards, Andy Shevchenko From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2C155C433FE for ; Sat, 8 Oct 2022 18:17:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=MVc+gaJkkc025xSWPHTGFf5TeoGseH4X1F6LeBBhhPU=; b=lEVHX/K+r4PYqP 0u9D3eZXzatXN5Oo88lGuzEu50WQHoecpkU+GZQGlfcXs6jSsSe4O5YyLmwRvTTP2BsFIVsLwUrdX SFtsmL/HK4n2Kvc1H41C3C8GAmLA08xdH2GRnzvVJl9Pz20sAUX5w/pwUi9xTln/xyr16v9b5wwGF sEkHLjMxz4FCe1z/iITS0hCpAtGSPNY9ZZ70xUJBHgNBpT8/SBTZYw4+UrqM+LRlqc/LeVuk8q2mL BhHAAdOFqp/Diuoq/PF5tg71XT5VSx/o5kI+WFOJO2+uiLnrSKB4FFutk3HaDhH4Xv7rupwKEfA5f HgsuQJ8aZUZ7IYXd42JA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ohENS-00DxD7-RN; Sat, 08 Oct 2022 18:16:46 +0000 Received: from mga06b.intel.com ([134.134.136.31] helo=mga06.intel.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ohENH-00Dx8Q-6w; Sat, 08 Oct 2022 18:16:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665252995; x=1696788995; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=Q7YlJwn22ePcF+UsrTEOpg8IcVNJxX9fgtN3uSim9/E=; b=nmrnNvC4+8vyRyLwigolCMDVOR2rxzFuISY7b9WRlf5FEqVTJh/3Ziun 3MEFBherXCMisBNOFfLZ0yWE+zPyzGWbeUnEkDK+tXqnAHnr9hhKH0OZC 4jZt1zklk6DOXF3sPx8JngVisI+jSb9W81obJik1sSnU1YDBRABxCtQ3m UKSu2ICX1W4EGmqwvZv1SKDNMxm/y/iz0ETqnqq5a+3zk55yPmTzygGP7 FbZmmbA0Jr0KuP3v1079WwVexN+mYwuStqwp7LIVnABst8XY0Tr/LikMF 2TeXEeF1g/8YEdKwz9CtMlJ8JcJG9NvdWp4nxzHgTGv7vCGiW0V6ERZbs g==; X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="365920528" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="365920528" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2022 11:16:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="750899918" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="750899918" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga004.jf.intel.com with ESMTP; 08 Oct 2022 11:16:23 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1ohEN3-0048ft-0Q; Sat, 08 Oct 2022 21:16:21 +0300 Date: Sat, 8 Oct 2022 21:16:20 +0300 From: Andy Shevchenko To: Kees Cook Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, patches@lists.linux.dev, dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, kernel-janitors@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nvme@lists.infradead.org, linux-parisc@vger.kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, linux-um@lists.infradead.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org, Jan Kara Subject: Re: [PATCH v4 2/6] treewide: use prandom_u32_max() when possible Message-ID: References: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221008_111635_338741_739F85AF X-CRM114-Status: GOOD ( 16.41 ) X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" Errors-To: linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org On Fri, Oct 07, 2022 at 08:50:43PM -0700, Kees Cook wrote: > On October 7, 2022 7:21:28 PM PDT, "Jason A. Donenfeld" wrote: > >On Fri, Oct 07, 2022 at 03:47:44PM -0700, Kees Cook wrote: > >> On Fri, Oct 07, 2022 at 12:01:03PM -0600, Jason A. Donenfeld wrote: ... > >> These are more fun, but Coccinelle can still do them with a little > >> Pythonic help: > >> > >> // Find a potential literal > >> @literal_mask@ > >> expression LITERAL; > >> identifier randfunc =~ "get_random_int|prandom_u32|get_random_u32"; > >> position p; > >> @@ > >> > >> (randfunc()@p & (LITERAL)) > >> > >> // Add one to the literal. > >> @script:python add_one@ > >> literal << literal_mask.LITERAL; > >> RESULT; > >> @@ > >> > >> if literal.startswith('0x'): > >> value = int(literal, 16) + 1 > >> coccinelle.RESULT = cocci.make_expr("0x%x" % (value)) > >> elif literal[0] in '123456789': > >> value = int(literal, 10) + 1 > >> coccinelle.RESULT = cocci.make_expr("%d" % (value)) > >> else: > >> print("I don't know how to handle: %s" % (literal)) Wouldn't Python take care about (known) prefixes itself? try: x = int(literal) except ValueError as ex: print(..., ex.error) > >> // Replace the literal mask with the calculated result. > >> @plus_one@ > >> expression literal_mask.LITERAL; > >> position literal_mask.p; > >> expression add_one.RESULT; > >> identifier FUNC; > >> @@ > >> > >> - (FUNC()@p & (LITERAL)) > >> + prandom_u32_max(RESULT) > > > >Oh that's pretty cool. I can do the saturation check in python, since > >`value` holds the parsed result. Neat. > > It is (at least how I have it here) just the string, so YMMV. ... > >Thanks a bunch for the guidance. > > Sure thing! I was pleased to figure out how to do the python bit. I believe it can be optimized -- With Best Regards, Andy Shevchenko ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Sat, 8 Oct 2022 21:16:20 +0300 From: Andy Shevchenko Subject: Re: [PATCH v4 2/6] treewide: use prandom_u32_max() when possible Message-ID: References: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-um" Errors-To: linux-um-bounces+geert=linux-m68k.org@lists.infradead.org To: Kees Cook Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, patches@lists.linux.dev, dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, kernel-janitors@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nvme@lists.infradead.org, linux-parisc@vger.kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, linux-um@lists.infradead.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org, Jan Kara On Fri, Oct 07, 2022 at 08:50:43PM -0700, Kees Cook wrote: > On October 7, 2022 7:21:28 PM PDT, "Jason A. Donenfeld" wrote: > >On Fri, Oct 07, 2022 at 03:47:44PM -0700, Kees Cook wrote: > >> On Fri, Oct 07, 2022 at 12:01:03PM -0600, Jason A. Donenfeld wrote: ... > >> These are more fun, but Coccinelle can still do them with a little > >> Pythonic help: > >> > >> // Find a potential literal > >> @literal_mask@ > >> expression LITERAL; > >> identifier randfunc =~ "get_random_int|prandom_u32|get_random_u32"; > >> position p; > >> @@ > >> > >> (randfunc()@p & (LITERAL)) > >> > >> // Add one to the literal. > >> @script:python add_one@ > >> literal << literal_mask.LITERAL; > >> RESULT; > >> @@ > >> > >> if literal.startswith('0x'): > >> value = int(literal, 16) + 1 > >> coccinelle.RESULT = cocci.make_expr("0x%x" % (value)) > >> elif literal[0] in '123456789': > >> value = int(literal, 10) + 1 > >> coccinelle.RESULT = cocci.make_expr("%d" % (value)) > >> else: > >> print("I don't know how to handle: %s" % (literal)) Wouldn't Python take care about (known) prefixes itself? try: x = int(literal) except ValueError as ex: print(..., ex.error) > >> // Replace the literal mask with the calculated result. > >> @plus_one@ > >> expression literal_mask.LITERAL; > >> position literal_mask.p; > >> expression add_one.RESULT; > >> identifier FUNC; > >> @@ > >> > >> - (FUNC()@p & (LITERAL)) > >> + prandom_u32_max(RESULT) > > > >Oh that's pretty cool. I can do the saturation check in python, since > >`value` holds the parsed result. Neat. > > It is (at least how I have it here) just the string, so YMMV. ... > >Thanks a bunch for the guidance. > > Sure thing! I was pleased to figure out how to do the python bit. I believe it can be optimized -- With Best Regards, Andy Shevchenko _______________________________________________ linux-um mailing list linux-um@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-um From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 691B7C433F5 for ; Sat, 8 Oct 2022 18:17:40 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4MlD1G55Dhz3dsS for ; Sun, 9 Oct 2022 05:17:38 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=J5FfrJnd; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=linux.intel.com (client-ip=134.134.136.24; helo=mga09.intel.com; envelope-from=andriy.shevchenko@linux.intel.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=J5FfrJnd; dkim-atps=neutral Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4MlD093YsXz2yQl for ; Sun, 9 Oct 2022 05:16:40 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665253001; x=1696789001; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=Q7YlJwn22ePcF+UsrTEOpg8IcVNJxX9fgtN3uSim9/E=; b=J5FfrJndrdnaETov1xAa8KX8GUdv7A7Gjw/mm7BYhXZysKcSsj4oN3dX S5/TlwFblPxnFkO/fLNfr6N0jMc4wOihZG/lElhk1rC+vvjoJKUC66Mlo XTyLa/jchBTD943/8nG+y/IN6/6GLOrZH/VDatZ7IqN6vhlIhp7L5x3v7 /24r9jZImWaDclnIcTNEU1qToIQFam6ySl8olX9yl8Dv4OgqRu7cY+oW1 Wp+KuKeW8RDIEpRPPK8EfKYMjWd3WPvGHyb7+SeNoQaE/NBEyWcNR1JLs jNkZ50CsRTPt9Us0p+R2yfn5eC0RXKBdUSmvD0Et2MqxiqCOgnG9Txz9Q Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="304985434" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="304985434" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2022 11:16:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="750899918" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="750899918" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga004.jf.intel.com with ESMTP; 08 Oct 2022 11:16:23 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1ohEN3-0048ft-0Q; Sat, 08 Oct 2022 21:16:21 +0300 Date: Sat, 8 Oct 2022 21:16:20 +0300 From: Andy Shevchenko To: Kees Cook Subject: Re: [PATCH v4 2/6] treewide: use prandom_u32_max() when possible Message-ID: References: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Jason A. Donenfeld" , Jan Kara , linux-doc@vger.kernel.org, linux-wireless@vger.kernel.org, kernel-janitors@vger.kernel.org, linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-mtd@lists.infradead.org, sparclinux@vger.kernel.org, linux-s390@vger.kernel.org, linux-rdma@vger.kernel.org, x86@kernel.org, kasan-dev@googlegroups.com, linux-media@vger.kernel.org, linux-um@lists.infradead.org, linux-block@vger.kernel.org, dri-devel@lists.freedesktop.org, loongarch@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-parisc@vger.kernel.org, netdev@vger.kernel.org, linux-usb@vger.kernel.org, linux-mmc@vger.kernel.org, linux-mips@vger.kernel.org, linux-crypto@vger.kernel.org, patches@lists.linux.dev, linux-fsdevel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" On Fri, Oct 07, 2022 at 08:50:43PM -0700, Kees Cook wrote: > On October 7, 2022 7:21:28 PM PDT, "Jason A. Donenfeld" wrote: > >On Fri, Oct 07, 2022 at 03:47:44PM -0700, Kees Cook wrote: > >> On Fri, Oct 07, 2022 at 12:01:03PM -0600, Jason A. Donenfeld wrote: ... > >> These are more fun, but Coccinelle can still do them with a little > >> Pythonic help: > >> > >> // Find a potential literal > >> @literal_mask@ > >> expression LITERAL; > >> identifier randfunc =~ "get_random_int|prandom_u32|get_random_u32"; > >> position p; > >> @@ > >> > >> (randfunc()@p & (LITERAL)) > >> > >> // Add one to the literal. > >> @script:python add_one@ > >> literal << literal_mask.LITERAL; > >> RESULT; > >> @@ > >> > >> if literal.startswith('0x'): > >> value = int(literal, 16) + 1 > >> coccinelle.RESULT = cocci.make_expr("0x%x" % (value)) > >> elif literal[0] in '123456789': > >> value = int(literal, 10) + 1 > >> coccinelle.RESULT = cocci.make_expr("%d" % (value)) > >> else: > >> print("I don't know how to handle: %s" % (literal)) Wouldn't Python take care about (known) prefixes itself? try: x = int(literal) except ValueError as ex: print(..., ex.error) > >> // Replace the literal mask with the calculated result. > >> @plus_one@ > >> expression literal_mask.LITERAL; > >> position literal_mask.p; > >> expression add_one.RESULT; > >> identifier FUNC; > >> @@ > >> > >> - (FUNC()@p & (LITERAL)) > >> + prandom_u32_max(RESULT) > > > >Oh that's pretty cool. I can do the saturation check in python, since > >`value` holds the parsed result. Neat. > > It is (at least how I have it here) just the string, so YMMV. ... > >Thanks a bunch for the guidance. > > Sure thing! I was pleased to figure out how to do the python bit. I believe it can be optimized -- With Best Regards, Andy Shevchenko From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 76B19C433FE for ; Sat, 8 Oct 2022 18:17:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Q5QVMmK3rue+C6bZUl8q5cz/jpmF4RhLmF0zlxE1wqQ=; b=q6wfkYKJi/DDgJ /f4BY9uqtbbVHAmNml+47kTBenx+U4HM9T/Sbc4FxKGa9CznsK9VHuYCnV/PqhUxLLAa67YQ5sN90 WGQkLbOZf91lmFl+gFwPw9exe3gOsQKY7Zij1J1rVT7oMXsJpxYgkYOacBodfUfRNgX0uVc1rQg5V tuA1+OUnqn41YOsvhmOAZnRszuW8SMi1EdgKdL3bG0a4bR6xIZHDT6rhf/YxK8ETwo0F64B9JBla/ fGcbWqGn6BojYKwQaBYEVctN6euBgnbuSrKEsGDdLHCg0e3ubxZpTyBaTBTT/iUfmWmJaMO0ByDhP bmEdQmtKYUFRU8jAqRKg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ohENK-00DxAI-Lf; Sat, 08 Oct 2022 18:16:38 +0000 Received: from mga06b.intel.com ([134.134.136.31] helo=mga06.intel.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ohENH-00Dx8Q-6w; Sat, 08 Oct 2022 18:16:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665252995; x=1696788995; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=Q7YlJwn22ePcF+UsrTEOpg8IcVNJxX9fgtN3uSim9/E=; b=nmrnNvC4+8vyRyLwigolCMDVOR2rxzFuISY7b9WRlf5FEqVTJh/3Ziun 3MEFBherXCMisBNOFfLZ0yWE+zPyzGWbeUnEkDK+tXqnAHnr9hhKH0OZC 4jZt1zklk6DOXF3sPx8JngVisI+jSb9W81obJik1sSnU1YDBRABxCtQ3m UKSu2ICX1W4EGmqwvZv1SKDNMxm/y/iz0ETqnqq5a+3zk55yPmTzygGP7 FbZmmbA0Jr0KuP3v1079WwVexN+mYwuStqwp7LIVnABst8XY0Tr/LikMF 2TeXEeF1g/8YEdKwz9CtMlJ8JcJG9NvdWp4nxzHgTGv7vCGiW0V6ERZbs g==; X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="365920528" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="365920528" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Oct 2022 11:16:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10494"; a="750899918" X-IronPort-AV: E=Sophos;i="5.95,170,1661842800"; d="scan'208";a="750899918" Received: from smile.fi.intel.com ([10.237.72.54]) by orsmga004.jf.intel.com with ESMTP; 08 Oct 2022 11:16:23 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.96) (envelope-from ) id 1ohEN3-0048ft-0Q; Sat, 08 Oct 2022 21:16:21 +0300 Date: Sat, 8 Oct 2022 21:16:20 +0300 From: Andy Shevchenko To: Kees Cook Cc: "Jason A. Donenfeld" , linux-kernel@vger.kernel.org, patches@lists.linux.dev, dri-devel@lists.freedesktop.org, kasan-dev@googlegroups.com, kernel-janitors@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-block@vger.kernel.org, linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-media@vger.kernel.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nvme@lists.infradead.org, linux-parisc@vger.kernel.org, linux-rdma@vger.kernel.org, linux-s390@vger.kernel.org, linux-um@lists.infradead.org, linux-usb@vger.kernel.org, linux-wireless@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, netdev@vger.kernel.org, sparclinux@vger.kernel.org, x86@kernel.org, Jan Kara Subject: Re: [PATCH v4 2/6] treewide: use prandom_u32_max() when possible Message-ID: References: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <53DD0148-ED15-4294-8496-9E4B4C7AD061@chromium.org> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221008_111635_338741_739F85AF X-CRM114-Status: GOOD ( 16.41 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Oct 07, 2022 at 08:50:43PM -0700, Kees Cook wrote: > On October 7, 2022 7:21:28 PM PDT, "Jason A. Donenfeld" wrote: > >On Fri, Oct 07, 2022 at 03:47:44PM -0700, Kees Cook wrote: > >> On Fri, Oct 07, 2022 at 12:01:03PM -0600, Jason A. Donenfeld wrote: ... > >> These are more fun, but Coccinelle can still do them with a little > >> Pythonic help: > >> > >> // Find a potential literal > >> @literal_mask@ > >> expression LITERAL; > >> identifier randfunc =~ "get_random_int|prandom_u32|get_random_u32"; > >> position p; > >> @@ > >> > >> (randfunc()@p & (LITERAL)) > >> > >> // Add one to the literal. > >> @script:python add_one@ > >> literal << literal_mask.LITERAL; > >> RESULT; > >> @@ > >> > >> if literal.startswith('0x'): > >> value = int(literal, 16) + 1 > >> coccinelle.RESULT = cocci.make_expr("0x%x" % (value)) > >> elif literal[0] in '123456789': > >> value = int(literal, 10) + 1 > >> coccinelle.RESULT = cocci.make_expr("%d" % (value)) > >> else: > >> print("I don't know how to handle: %s" % (literal)) Wouldn't Python take care about (known) prefixes itself? try: x = int(literal) except ValueError as ex: print(..., ex.error) > >> // Replace the literal mask with the calculated result. > >> @plus_one@ > >> expression literal_mask.LITERAL; > >> position literal_mask.p; > >> expression add_one.RESULT; > >> identifier FUNC; > >> @@ > >> > >> - (FUNC()@p & (LITERAL)) > >> + prandom_u32_max(RESULT) > > > >Oh that's pretty cool. I can do the saturation check in python, since > >`value` holds the parsed result. Neat. > > It is (at least how I have it here) just the string, so YMMV. ... > >Thanks a bunch for the guidance. > > Sure thing! I was pleased to figure out how to do the python bit. I believe it can be optimized -- With Best Regards, Andy Shevchenko _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel